Skip to content

Commit 3460286

Browse files
authored
Merge pull request #2 from genme/bech32-address-tests
Bech32 address tests
2 parents e872989 + e8d2037 commit 3460286

File tree

2 files changed

+40
-5
lines changed

2 files changed

+40
-5
lines changed

bitcoin/tests/test_wallet.py

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,12 @@ def T(hex_scriptpubkey, expected_str_address, expected_class):
6868
P2SHBitcoinAddress)
6969
T('76a914000000000000000000000000000000000000000088ac', '1111111111111111111114oLvT2',
7070
P2PKHBitcoinAddress)
71+
T('0014751e76e8199196d454941c45d1b3a323f1433bd6',
72+
'bc1qw508d6qejxtdg4y5r3zarvary0c5xw7kv8f3t4',
73+
P2WPKHBitcoinAddress)
74+
T('0020c7a1f1a4d6b4c1802a59631966a18359de779e8a6a65973735a3ccdfdabc407d',
75+
'bc1qc7slrfxkknqcq2jevvvkdgvrt8080852dfjewde450xdlk4ugp7szw5tk9',
76+
P2WSHBitcoinAddress)
7177

7278
def test_from_nonstd_scriptPubKey(self):
7379
"""CBitcoinAddress.from_scriptPubKey() with non-standard scriptPubKeys"""
@@ -111,6 +117,23 @@ def test_from_invalid_scriptPubKey(self):
111117
with self.assertRaises(CBitcoinAddressError):
112118
CBitcoinAddress.from_scriptPubKey(scriptPubKey)
113119

120+
def test_to_redeemScript(self):
121+
def T(str_addr, expected_scriptPubKey_hexbytes):
122+
addr = CBitcoinAddress(str_addr)
123+
124+
actual_scriptPubKey = addr.to_redeemScript()
125+
self.assertEqual(b2x(actual_scriptPubKey),
126+
expected_scriptPubKey_hexbytes)
127+
128+
T('31h1vYVSYuKP6AhS86fbRdMw9XHieotbST',
129+
'a914000000000000000000000000000000000000000087')
130+
131+
T('1111111111111111111114oLvT2',
132+
'76a914000000000000000000000000000000000000000088ac')
133+
134+
T('bc1qw508d6qejxtdg4y5r3zarvary0c5xw7kv8f3t4',
135+
'76a914751e76e8199196d454941c45d1b3a323f1433bd688ac')
136+
114137
def test_to_scriptPubKey(self):
115138
"""CBitcoinAddress.to_scriptPubKey() works"""
116139
def T(str_addr, expected_scriptPubKey_hexbytes):
@@ -125,10 +148,17 @@ def T(str_addr, expected_scriptPubKey_hexbytes):
125148
T('1111111111111111111114oLvT2',
126149
'76a914000000000000000000000000000000000000000088ac')
127150

151+
128152
class Test_P2SHBitcoinAddress(unittest.TestCase):
129153
def test_from_redeemScript(self):
130-
addr = P2SHBitcoinAddress.from_redeemScript(CScript())
131-
self.assertEqual(str(addr), '3J98t1WpEZ73CNmQviecrnyiWrnqRhWNLy')
154+
def T(script, expected_str_address):
155+
addr = P2SHBitcoinAddress.from_redeemScript(script)
156+
self.assertEqual(str(addr), expected_str_address)
157+
158+
T(CScript(), '3J98t1WpEZ73CNmQviecrnyiWrnqRhWNLy')
159+
T(CScript(x('76a914751e76e8199196d454941c45d1b3a323f1433bd688ac')),
160+
'3LRW7jeCvQCRdPF8S3yUCfRAx4eqXFmdcr')
161+
132162

133163
class Test_P2PKHBitcoinAddress(unittest.TestCase):
134164
def test_from_non_canonical_scriptPubKey(self):

bitcoin/wallet.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,14 @@
1717

1818
from __future__ import absolute_import, division, print_function, unicode_literals
1919

20+
import array
2021
import sys
2122

2223
_bord = ord
24+
_tobytes = lambda x: array.array('B', x).tostring()
2325
if sys.version > '3':
2426
_bord = lambda x: x
27+
_tobytes = bytes
2528

2629
import bitcoin
2730
import bitcoin.base58
@@ -72,9 +75,11 @@ class CBech32BitcoinAddress(bitcoin.bech32.CBech32Data, CBitcoinAddress):
7275
@classmethod
7376
def from_bytes(cls, witver, witprog):
7477

75-
assert(witver == 0)
76-
77-
self = super(CBech32BitcoinAddress, cls).from_bytes(witver, witprog)
78+
assert witver == 0
79+
self = super(CBech32BitcoinAddress, cls).from_bytes(
80+
witver,
81+
_tobytes(witprog)
82+
)
7883

7984
if len(self) == 32:
8085
self.__class__ = P2WSHBitcoinAddress

0 commit comments

Comments
 (0)