@@ -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
@@ -75,60 +75,60 @@ def base58_to_byte(s):
75
75
return res [1 :- 4 ], int (res [0 ])
76
76
77
77
78
- def keyhash_to_p2pkh (hash , main = False ):
78
+ def keyhash_to_p2pkh (hash , main = False ):
79
79
assert len (hash ) == 20
80
80
version = 0 if main else 111
81
81
return byte_to_base58 (hash , version )
82
82
83
- def scripthash_to_p2sh (hash , main = False ):
83
+ def scripthash_to_p2sh (hash , main = False ):
84
84
assert len (hash ) == 20
85
85
version = 5 if main else 196
86
86
return byte_to_base58 (hash , version )
87
87
88
- def key_to_p2pkh (key , main = False ):
88
+ def key_to_p2pkh (key , main = False ):
89
89
key = check_key (key )
90
90
return keyhash_to_p2pkh (hash160 (key ), main )
91
91
92
- def script_to_p2sh (script , main = False ):
92
+ def script_to_p2sh (script , main = False ):
93
93
script = check_script (script )
94
94
return scripthash_to_p2sh (hash160 (script ), main )
95
95
96
- def key_to_p2sh_p2wpkh (key , main = False ):
96
+ def key_to_p2sh_p2wpkh (key , main = False ):
97
97
key = check_key (key )
98
98
p2shscript = CScript ([OP_0 , hash160 (key )])
99
99
return script_to_p2sh (p2shscript , main )
100
100
101
- def program_to_witness (version , program , main = False ):
101
+ def program_to_witness (version , program , main = False ):
102
102
if (type (program ) is str ):
103
103
program = hex_str_to_bytes (program )
104
104
assert 0 <= version <= 16
105
105
assert 2 <= len (program ) <= 40
106
106
assert version > 0 or len (program ) in [20 , 32 ]
107
107
return encode_segwit_address ("bc" if main else "bcrt" , version , program )
108
108
109
- def script_to_p2wsh (script , main = False ):
109
+ def script_to_p2wsh (script , main = False ):
110
110
script = check_script (script )
111
111
return program_to_witness (0 , sha256 (script ), main )
112
112
113
- def key_to_p2wpkh (key , main = False ):
113
+ def key_to_p2wpkh (key , main = False ):
114
114
key = check_key (key )
115
115
return program_to_witness (0 , hash160 (key ), main )
116
116
117
- def script_to_p2sh_p2wsh (script , main = False ):
117
+ def script_to_p2sh_p2wsh (script , main = False ):
118
118
script = check_script (script )
119
119
p2shscript = CScript ([OP_0 , sha256 (script )])
120
120
return script_to_p2sh (p2shscript , main )
121
121
122
122
def check_key (key ):
123
123
if (type (key ) is str ):
124
- key = hex_str_to_bytes (key ) # Assuming this is hex string
124
+ key = hex_str_to_bytes (key ) # Assuming this is hex string
125
125
if (type (key ) is bytes and (len (key ) == 33 or len (key ) == 65 )):
126
126
return key
127
127
assert False
128
128
129
129
def check_script (script ):
130
130
if (type (script ) is str ):
131
- script = hex_str_to_bytes (script ) # Assuming this is hex string
131
+ script = hex_str_to_bytes (script ) # Assuming this is hex string
132
132
if (type (script ) is bytes or type (script ) is CScript ):
133
133
return script
134
134
assert False
0 commit comments