Skip to content

Commit f95cecf

Browse files
authored
Update btcrseed.py
1 parent 774e530 commit f95cecf

File tree

1 file changed

+43
-30
lines changed

1 file changed

+43
-30
lines changed

btcrecover/btcrseed.py

Lines changed: 43 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -350,6 +350,46 @@ def convert_to_xpub(input_mpk: str) -> str:
350350

351351
return output_mpk
352352

353+
354+
def address_to_hash160(address: str) -> bytes:
355+
if address.startswith("r"): # Convert XRP addresses to standard Bitcoin Legacy Addresses
356+
address = base58_tools.b58encode(
357+
base58_tools.b58decode(address, alphabet=base58_tools.XRP_ALPHABET)).decode()
358+
try:
359+
# Check if we are getting BCH Cashaddresses and if so, convert them to standard legacy addresses
360+
if address.lower().startswith("bitcoincash:"):
361+
address = convert.to_legacy_address(address)
362+
else:
363+
try:
364+
address = convert.to_legacy_address(f"bitcoincash:{address}")
365+
except convert.InvalidAddress:
366+
pass
367+
hash160 = binascii.unhexlify(
368+
encoding.addr_base58_to_pubkeyhash(address, True)) # assume we have a P2PKH (Legacy) or Segwit (P2SH) so try a Base58 conversion
369+
except (encoding.EncodingError, AssertionError) as e:
370+
try:
371+
hash160 = binascii.unhexlify(encoding.grs_addr_base58_to_pubkeyhash(address,
372+
True)) # assume we have a P2PKH (Legacy) or Segwit (P2SH) so try a Base58 conversion
373+
except Exception as e:
374+
try:
375+
hash160 = binascii.unhexlify(encoding.addr_bech32_to_pubkeyhash(address, prefix=None,
376+
include_witver=False,
377+
as_hex=True)) # Base58 conversion above will give a keyError if attempted with a Bech32 address for things like BTC
378+
except Exception as e:
379+
if bundled_bitcoinlib_mod_available:
380+
# Try for some obscure altcoins which require modified versions of Bitcoinlib
381+
try:
382+
hash160 = binascii.unhexlify(encoding_mod.grs_addr_base58_to_pubkeyhash(address, True))
383+
except Exception as e:
384+
hash160 = binascii.unhexlify(
385+
encoding_mod.addr_bech32_to_pubkeyhash(address, prefix=None, include_witver=False,
386+
as_hex=True))
387+
else:
388+
print("Address not valid and unable to load modified bitcoinlib on this platform...")
389+
return b''
390+
return hash160
391+
392+
353393
############### WalletBase ###############
354394

355395
# Methods common to most wallets, but overridden by WalletEthereum
@@ -375,36 +415,9 @@ def set_securityWarningsFlag(setflag: bool) -> None:
375415
def _addresses_to_hash160s(addresses: List[str]) -> set:
376416
hash160s = set()
377417
for address in addresses:
378-
if address.startswith("r"): # Convert XRP addresses to standard Bitcoin Legacy Addresses
379-
address = base58_tools.b58encode(
380-
base58_tools.b58decode(address, alphabet=base58_tools.XRP_ALPHABET)).decode()
381-
try:
382-
# Check if we are getting BCH Cashaddresses and if so, convert them to standard legacy addresses
383-
if address.lower().startswith("bitcoincash:"):
384-
address = convert.to_legacy_address(address)
385-
else:
386-
try:
387-
address = convert.to_legacy_address(f"bitcoincash:{address}")
388-
except convert.InvalidAddress:
389-
pass
390-
hash160 = binascii.unhexlify(encoding.addr_base58_to_pubkeyhash(address, True)) #assume we have a P2PKH (Legacy) or Segwit (P2SH) so try a Base58 conversion
391-
except (encoding.EncodingError, AssertionError) as e:
392-
try:
393-
hash160 = binascii.unhexlify(encoding.grs_addr_base58_to_pubkeyhash(address, True)) #assume we have a P2PKH (Legacy) or Segwit (P2SH) so try a Base58 conversion
394-
except Exception as e:
395-
try:
396-
hash160 = binascii.unhexlify(encoding.addr_bech32_to_pubkeyhash(address, prefix=None, include_witver=False, as_hex=True)) #Base58 conversion above will give a keyError if attempted with a Bech32 address for things like BTC
397-
except Exception as e:
398-
if bundled_bitcoinlib_mod_available:
399-
# Try for some obscure altcoins which require modified versions of Bitcoinlib
400-
try:
401-
hash160 = binascii.unhexlify(encoding_mod.grs_addr_base58_to_pubkeyhash(address, True))
402-
except Exception as e:
403-
hash160 = binascii.unhexlify(encoding_mod.addr_bech32_to_pubkeyhash(address, prefix=None, include_witver=False, as_hex=True))
404-
else:
405-
print("Address not valid and unable to load modified bitcoinlib on this platform...")
406-
407-
hash160s.add(hash160)
418+
hash160 = address_to_hash160(address)
419+
if hash160:
420+
hash160s.add(hash160)
408421
return hash160s
409422

410423
@staticmethod

0 commit comments

Comments
 (0)