Skip to content

Commit e872989

Browse files
committed
add str() tests for P2WPKHBitcoinAddress and P2WSHBitcoinAddress
unfortunately, at the moment, the tests do not work for python2, only for python3
1 parent 7c2b4e1 commit e872989

File tree

2 files changed

+16
-3
lines changed

2 files changed

+16
-3
lines changed

bitcoin/tests/test_wallet.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,14 @@ class Test_CBitcoinAddress(unittest.TestCase):
2222
def test_create_from_string(self):
2323
"""Create CBitcoinAddress's from strings"""
2424

25-
def T(str_addr, expected_bytes, expected_nVersion, expected_class):
25+
def T(str_addr, expected_bytes, expected_version, expected_class):
2626
addr = CBitcoinAddress(str_addr)
2727
self.assertEqual(addr.to_bytes(), expected_bytes)
28-
self.assertEqual(addr.nVersion, expected_nVersion)
2928
self.assertEqual(addr.__class__, expected_class)
29+
if isinstance(addr, CBase58BitcoinAddress):
30+
self.assertEqual(addr.nVersion, expected_version)
31+
elif isinstance(addr, CBech32BitcoinAddress):
32+
self.assertEqual(addr.witver, expected_version)
3033

3134
T('1A1zP1eP5QGefi2DMPTfTL5SLmv7DivfNa',
3235
x('62e907b15cbf27d5425399ebf6f0fb50ebb88f18'), 0,
@@ -36,6 +39,14 @@ def T(str_addr, expected_bytes, expected_nVersion, expected_class):
3639
x('4266fc6f2c2861d7fe229b279a79803afca7ba34'), 5,
3740
P2SHBitcoinAddress)
3841

42+
T('BC1QW508D6QEJXTDG4Y5R3ZARVARY0C5XW7KV8F3T4',
43+
x('751e76e8199196d454941c45d1b3a323f1433bd6'), 0,
44+
P2WPKHBitcoinAddress)
45+
46+
T('bc1qc7slrfxkknqcq2jevvvkdgvrt8080852dfjewde450xdlk4ugp7szw5tk9',
47+
x('c7a1f1a4d6b4c1802a59631966a18359de779e8a6a65973735a3ccdfdabc407d'), 0,
48+
P2WSHBitcoinAddress)
49+
3950
def test_wrong_nVersion(self):
4051
"""Creating a CBitcoinAddress from a unknown nVersion fails"""
4152

bitcoin/wallet.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ def from_bytes(cls, witver, witprog):
8181
elif len(self) == 20:
8282
self.__class__ = P2WPKHBitcoinAddress
8383
else:
84-
raise CBitcoinAddressError('witness program does not match any known segwit address format')
84+
raise CBitcoinAddressError('witness program does not match any known segwit address format')
8585

8686
return self
8787

@@ -384,6 +384,8 @@ def __init__(self, s):
384384
'CBech32BitcoinAddress',
385385
'P2SHBitcoinAddress',
386386
'P2PKHBitcoinAddress',
387+
'P2WSHBitcoinAddress',
388+
'P2WPKHBitcoinAddress',
387389
'CKey',
388390
'CBitcoinSecretError',
389391
'CBitcoinSecret',

0 commit comments

Comments
 (0)