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

Fixed P2WSH and P2WPKH calculations and tests #27

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
15 changes: 10 additions & 5 deletions bitcoinaddress/address.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ def generate_publicaddress3(self):
self.pubaddr3 = self.instance._generate_publicaddress3(Address.MainNet.SEGWIT_PREFIX)

def generate_publicaddress_bc1_P2WPKH(self):
self.pubaddrbc1_P2WPKH = self.instance._generate_publicaddress_bech32_P2WPKH(Address.MainNet.BECH32_PREFIX)
self.pubaddrbc1_P2WPKH = self.instance._generate_publicaddress_bech32_P2WPKH(Address.MainNet.BECH32_PREFIX,
Address.MainNet.PREFIX_B)

def generate_publicaddress_bc1_P2WSH(self):
self.pubaddrbc1_P2WSH = self.instance._generate_publicaddress_bech32_P2WSH(Address.MainNet.BECH32_PREFIX)
Expand All @@ -70,7 +71,8 @@ def generate_publicaddress3(self):
self.pubaddr3 = self.instance._generate_publicaddress3(Address.TestNet.SEGWIT_PREFIX)

def generate_publicaddress_tb1_P2WPKH(self):
self.pubaddrtb1_P2WPKH = self.instance._generate_publicaddress_bech32_P2WPKH(Address.TestNet.BECH32_PREFIX)
self.pubaddrtb1_P2WPKH = self.instance._generate_publicaddress_bech32_P2WPKH(Address.TestNet.BECH32_PREFIX,
Address.TestNet.PREFIX_B)

def generate_publicaddress_tb1_P2WSH(self):
self.pubaddrtb1_P2WSH = self.instance._generate_publicaddress_bech32_P2WSH(Address.TestNet.BECH32_PREFIX)
Expand Down Expand Up @@ -122,14 +124,17 @@ def _generate_publicaddress3(self, prefix):
c = checksum(m)
return base58.b58encode(m + c).decode('utf-8')

def _generate_publicaddress_bech32_P2WPKH(self, bech32_prefix):
p = self._generate_compressed_pubkey()
def _generate_publicaddress_bech32_P2WPKH(self, bech32_prefix, prefix_b):
p = self._generate_uncompressed_pubkey(prefix_b)
redeem_script_P2WPKH = hash160(p).digest() # 20 bytes
return str(segwit_addr.encode(bech32_prefix, Address.WITNESS_VERSION, redeem_script_P2WPKH))

def _generate_publicaddress_bech32_P2WSH(self, bech32_prefix):
p = self._generate_compressed_pubkey()
redeem_script_P2WSH = hashlib.sha256(p).digest()
pr1 = bytes.fromhex('21')
po1 = bytes.fromhex('ac')
p_red = pr1 + p + po1
redeem_script_P2WSH = hashlib.sha256(p_red).digest()
return str(segwit_addr.encode(bech32_prefix, Address.WITNESS_VERSION, redeem_script_P2WSH))

def _generate_uncompressed_pubkey(self, prefix):
Expand Down
8 changes: 4 additions & 4 deletions tests/test_address.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@ def testFromWIF(self):
self.assertEqual(address.mainnet.pubaddr1c, "1D7XaU5PbsPxfZeYBcGGyMEqiVvPhtFMUF")
self.assertEqual(address.mainnet.pubaddr3, "34QhdWUjZjv3HLyvNYgb4AR7ikAfcdzfCW")
self.assertEqual(address.mainnet.pubaddrbc1_P2WSH,
"bc1qup6umurcl7s6zw42gcxfzl346psazws74x72ty6gmlvkaxz6kv4sqsth99")
self.assertEqual(address.mainnet.pubaddrbc1_P2WPKH, "bc1qsnwc0y43fpljyl2ep0e2gtsqa55utcj4ntzwlf")
"bc1q6gmqnd9x8q40gusftcxw84sjmdszcp3hv0ur3k7aufvjwzw5y77sl2kknp")
self.assertEqual(address.mainnet.pubaddrbc1_P2WPKH, "bc1qaj3zyc83azvukfr5kyltf3guyzca44lhzgq6la")

self.assertEqual(address.testnet.pubaddr1, "n369zca37LR8Pzc2GBKh5CzitRxxhkHDhK")
self.assertEqual(address.testnet.pubaddr1c, "msdUsXANQtqDSg89uBEeoGTAaVX6bWK3dW")
self.assertEqual(address.testnet.pubaddr3, "2MuxuhFQmBCRPV8cU3gJTg7QNw6NqTuUm2A")
self.assertEqual(address.testnet.pubaddrtb1_P2WSH,
"tb1qup6umurcl7s6zw42gcxfzl346psazws74x72ty6gmlvkaxz6kv4shcacl2")
self.assertEqual(address.testnet.pubaddrtb1_P2WPKH, "tb1qsnwc0y43fpljyl2ep0e2gtsqa55utcj4edeay6")
"tb1q6gmqnd9x8q40gusftcxw84sjmdszcp3hv0ur3k7aufvjwzw5y77sgzqefw")
self.assertEqual(address.testnet.pubaddrtb1_P2WPKH, "tb1qaj3zyc83azvukfr5kyltf3guyzca44lhgwmfyw")

def testFromRandomSeed(self):
# when
Expand Down