2
2
# Copyright (c) 2016-2020 The Bitcoin Core developers
3
3
# Distributed under the MIT software license, see the accompanying
4
4
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
5
- """Encode and decode BASE58, P2PKH and P2SH addresses."""
5
+ """Encode and decode Bitcoin addresses.
6
+
7
+ - base58 P2PKH and P2SH addresses.
8
+ - bech32 segwit v0 P2WPKH and P2WSH addresses."""
6
9
7
10
import enum
8
11
import unittest
9
12
10
13
from .script import hash256 , hash160 , sha256 , CScript , OP_0
11
- from .util import hex_str_to_bytes
12
-
13
- from . import segwit_addr
14
-
15
- from test_framework .util import assert_equal
14
+ from .segwit_addr import encode_segwit_address
15
+ from .util import assert_equal , hex_str_to_bytes
16
16
17
17
ADDRESS_BCRT1_UNSPENDABLE = 'bcrt1qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq3xueyj'
18
18
ADDRESS_BCRT1_UNSPENDABLE_DESCRIPTOR = 'addr(bcrt1qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq3xueyj)#juyq9d97'
@@ -35,7 +35,7 @@ def byte_to_base58(b, version):
35
35
str = chr (version ).encode ('latin-1' ).hex () + str
36
36
checksum = hash256 (hex_str_to_bytes (str )).hex ()
37
37
str += checksum [:8 ]
38
- value = int ('0x' + str ,0 )
38
+ value = int ('0x' + str , 0 )
39
39
while value > 0 :
40
40
result = chars [value % 58 ] + result
41
41
value //= 58
@@ -45,7 +45,10 @@ def byte_to_base58(b, version):
45
45
return result
46
46
47
47
48
- def base58_to_byte (s , verify_checksum = True ):
48
+ def base58_to_byte (s ):
49
+ """Converts a base58-encoded string to its data and version.
50
+
51
+ Throws if the base58 checksum is invalid."""
49
52
if not s :
50
53
return b''
51
54
n = 0
@@ -65,66 +68,67 @@ def base58_to_byte(s, verify_checksum=True):
65
68
else :
66
69
break
67
70
res = b'\x00 ' * pad + res
68
- if verify_checksum :
69
- assert_equal (hash256 (res [:- 4 ])[:4 ], res [- 4 :])
71
+
72
+ # Assert if the checksum is invalid
73
+ assert_equal (hash256 (res [:- 4 ])[:4 ], res [- 4 :])
70
74
71
75
return res [1 :- 4 ], int (res [0 ])
72
76
73
77
74
- def keyhash_to_p2pkh (hash , main = False ):
78
+ def keyhash_to_p2pkh (hash , main = False ):
75
79
assert len (hash ) == 20
76
80
version = 0 if main else 111
77
81
return byte_to_base58 (hash , version )
78
82
79
- def scripthash_to_p2sh (hash , main = False ):
83
+ def scripthash_to_p2sh (hash , main = False ):
80
84
assert len (hash ) == 20
81
85
version = 5 if main else 196
82
86
return byte_to_base58 (hash , version )
83
87
84
- def key_to_p2pkh (key , main = False ):
88
+ def key_to_p2pkh (key , main = False ):
85
89
key = check_key (key )
86
90
return keyhash_to_p2pkh (hash160 (key ), main )
87
91
88
- def script_to_p2sh (script , main = False ):
92
+ def script_to_p2sh (script , main = False ):
89
93
script = check_script (script )
90
94
return scripthash_to_p2sh (hash160 (script ), main )
91
95
92
- def key_to_p2sh_p2wpkh (key , main = False ):
96
+ def key_to_p2sh_p2wpkh (key , main = False ):
93
97
key = check_key (key )
94
98
p2shscript = CScript ([OP_0 , hash160 (key )])
95
99
return script_to_p2sh (p2shscript , main )
96
100
97
- def program_to_witness (version , program , main = False ):
101
+ def program_to_witness (version , program , main = False ):
98
102
if (type (program ) is str ):
99
103
program = hex_str_to_bytes (program )
100
104
assert 0 <= version <= 16
101
105
assert 2 <= len (program ) <= 40
102
106
assert version > 0 or len (program ) in [20 , 32 ]
103
- return segwit_addr . encode ("bc" if main else "bcrt" , version , program )
107
+ return encode_segwit_address ("bc" if main else "bcrt" , version , program )
104
108
105
- def script_to_p2wsh (script , main = False ):
109
+ def script_to_p2wsh (script , main = False ):
106
110
script = check_script (script )
107
111
return program_to_witness (0 , sha256 (script ), main )
108
112
109
- def key_to_p2wpkh (key , main = False ):
113
+ def key_to_p2wpkh (key , main = False ):
110
114
key = check_key (key )
111
115
return program_to_witness (0 , hash160 (key ), main )
112
116
113
- def script_to_p2sh_p2wsh (script , main = False ):
117
+ def script_to_p2sh_p2wsh (script , main = False ):
114
118
script = check_script (script )
115
119
p2shscript = CScript ([OP_0 , sha256 (script )])
116
120
return script_to_p2sh (p2shscript , main )
117
121
118
122
def check_key (key ):
119
123
if (type (key ) is str ):
120
- key = hex_str_to_bytes (key ) # Assuming this is hex string
124
+ key = hex_str_to_bytes (key ) # Assuming this is hex string
121
125
if (type (key ) is bytes and (len (key ) == 33 or len (key ) == 65 )):
122
126
return key
123
127
assert False
124
128
125
129
def check_script (script ):
126
130
if (type (script ) is str ):
127
- script = hex_str_to_bytes (script ) # Assuming this is hex string
131
+ script = hex_str_to_bytes (script ) # Assuming this is hex string
128
132
if (type (script ) is bytes or type (script ) is CScript ):
129
133
return script
130
134
assert False
@@ -135,15 +139,15 @@ def test_base58encodedecode(self):
135
139
def check_base58 (data , version ):
136
140
self .assertEqual (base58_to_byte (byte_to_base58 (data , version )), (data , version ))
137
141
138
- check_base58 (b' \x1f \x8e \xa1 p*{ \xd4 \x94 \x1b \xca \t A \xb8 R \xc4 \xbb \xfe \xdb . \x05 ' , 111 )
139
- check_base58 (b': \x0b \x05 \xf4 \xd7 \xf6 l; \xa7 \x00 \x9f E50)l \x84 \\ \xc9 \xcf ' , 111 )
140
- check_base58 (b'A \xc1 \xea \xf1 \x11 \x80 %Y \xba \xd6 \x1b ` \xd6 + \x1f \x89 |c \x92 \x8a ' , 111 )
141
- check_base58 (b' \0 A \xc1 \xea \xf1 \x11 \x80 %Y \xba \xd6 \x1b ` \xd6 + \x1f \x89 |c \x92 \x8a ' , 111 )
142
- check_base58 (b' \0 \0 A \xc1 \xea \xf1 \x11 \x80 %Y \xba \xd6 \x1b ` \xd6 + \x1f \x89 |c \x92 \x8a ' , 111 )
143
- check_base58 (b' \0 \0 \0 A \xc1 \xea \xf1 \x11 \x80 %Y \xba \xd6 \x1b ` \xd6 + \x1f \x89 |c \x92 \x8a ' , 111 )
144
- check_base58 (b' \x1f \x8e \xa1 p*{ \xd4 \x94 \x1b \xca \t A \xb8 R \xc4 \xbb \xfe \xdb . \x05 ' , 0 )
145
- check_base58 (b': \x0b \x05 \xf4 \xd7 \xf6 l; \xa7 \x00 \x9f E50)l \x84 \\ \xc9 \xcf ' , 0 )
146
- check_base58 (b'A \xc1 \xea \xf1 \x11 \x80 %Y \xba \xd6 \x1b ` \xd6 + \x1f \x89 |c \x92 \x8a ' , 0 )
147
- check_base58 (b' \0 A \xc1 \xea \xf1 \x11 \x80 %Y \xba \xd6 \x1b ` \xd6 + \x1f \x89 |c \x92 \x8a ' , 0 )
148
- check_base58 (b' \0 \0 A \xc1 \xea \xf1 \x11 \x80 %Y \xba \xd6 \x1b ` \xd6 + \x1f \x89 |c \x92 \x8a ' , 0 )
149
- check_base58 (b' \0 \0 \0 A \xc1 \xea \xf1 \x11 \x80 %Y \xba \xd6 \x1b ` \xd6 + \x1f \x89 |c \x92 \x8a ' , 0 )
142
+ check_base58 (bytes . fromhex ( '1f8ea1702a7bd4941bca0941b852c4bbfedb2e05' ) , 111 )
143
+ check_base58 (bytes . fromhex ( '3a0b05f4d7f66c3ba7009f453530296c845cc9cf' ) , 111 )
144
+ check_base58 (bytes . fromhex ( '41c1eaf111802559bad61b60d62b1f897c63928a' ) , 111 )
145
+ check_base58 (bytes . fromhex ( '0041c1eaf111802559bad61b60d62b1f897c63928a' ) , 111 )
146
+ check_base58 (bytes . fromhex ( '000041c1eaf111802559bad61b60d62b1f897c63928a' ) , 111 )
147
+ check_base58 (bytes . fromhex ( '00000041c1eaf111802559bad61b60d62b1f897c63928a' ) , 111 )
148
+ check_base58 (bytes . fromhex ( '1f8ea1702a7bd4941bca0941b852c4bbfedb2e05' ) , 0 )
149
+ check_base58 (bytes . fromhex ( '3a0b05f4d7f66c3ba7009f453530296c845cc9cf' ) , 0 )
150
+ check_base58 (bytes . fromhex ( '41c1eaf111802559bad61b60d62b1f897c63928a' ) , 0 )
151
+ check_base58 (bytes . fromhex ( '0041c1eaf111802559bad61b60d62b1f897c63928a' ) , 0 )
152
+ check_base58 (bytes . fromhex ( '000041c1eaf111802559bad61b60d62b1f897c63928a' ) , 0 )
153
+ check_base58 (bytes . fromhex ( '00000041c1eaf111802559bad61b60d62b1f897c63928a' ) , 0 )
0 commit comments