Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added p2wpkh-p2sh addresses support (segwit) #302

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 20 additions & 7 deletions btcrecover/btcrseed.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@

ADDRESSDB_DEF_FILENAME = "addresses.db"

P2PKH_VERSION_BYTE = "\x00"
P2SH_VERSION_BYTE = "\x05"

def full_version():
return "seedrecover {}, {}".format(
__version__,
Expand Down Expand Up @@ -206,9 +209,9 @@ def _addresses_to_hash160s(addresses):
hash160s = set()
for address in addresses:
hash160, version_byte = base58check_to_hash160(address)
if ord(version_byte) != 0:
raise ValueError("not a Bitcoin P2PKH address; version byte is {:#04x}".format(ord(version_byte)))
hash160s.add(hash160)
if version_byte != P2PKH_VERSION_BYTE and version_byte != P2SH_VERSION_BYTE:
raise ValueError("not a Bitcoin P2PKH or P2SH address; version byte is {:#04x}".format(ord(version_byte)))
hash160s.add( (hash160, version_byte) )
return hash160s

@staticmethod
Expand Down Expand Up @@ -448,7 +451,7 @@ def return_verified_password_or_false(self, mnemonic_ids_list):

d_pubkey = coincurve.PublicKey.from_valid_secret(d_privkey).format(compressed=False)
# Compute the hash160 of the *uncompressed* public key, and check for a match
if hashlib_new("ripemd160", l_sha256(d_pubkey).digest()).digest() in self._known_hash160s:
if hashlib_new("ripemd160", l_sha256(d_pubkey).digest()).digest() in [ hash160 for hash160,v in self._known_hash160s ]:
return mnemonic_ids, count # found it

return False, count
Expand Down Expand Up @@ -754,8 +757,18 @@ def _verify_seed(self, seed_bytes):
privkey_int) % GENERATOR_ORDER, 32)

d_pubkey = coincurve.PublicKey.from_valid_secret(d_privkey_bytes).format(compressed=False)
if self.pubkey_to_hash160(d_pubkey) in self._known_hash160s:
return True

pubkey_hash160 = self.pubkey_to_hash160(d_pubkey)
for hash160, version_byte in self._known_hash160s:
if version_byte == P2SH_VERSION_BYTE: # assuming P2SH(P2WPKH) BIP141
WITNESS_VERSION = "\x00\x14"
witness_program = WITNESS_VERSION + pubkey_hash160
witness_program_hash160 = hashlib.new("ripemd160", hashlib.sha256(witness_program).digest()).digest()
if witness_program_hash160 == hash160:
return True
else: # defaults to P2PKH
if pubkey_hash160 == hash160:
return True

return False

Expand Down Expand Up @@ -1338,7 +1351,7 @@ def _addresses_to_hash160s(addresses):
if c.isalpha() and \
c.isupper() != bool(ord(checksum[nibble // 2]) & (0b1000 if nibble&1 else 0b10000000)):
raise ValueError("invalid EIP55 checksum")
hash160s.add(cur_hash160)
hash160s.add( (cur_hash160, P2PKH_VERSION_BYTE) )
madacol marked this conversation as resolved.
Show resolved Hide resolved
return hash160s

@staticmethod
Expand Down