-
Notifications
You must be signed in to change notification settings - Fork 8
/
util.py
118 lines (92 loc) · 3.13 KB
/
util.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
import hashlib
SIGHASH_ALL = 1
SIGHASH_NONE = 2
SIGHASH_SINGLE = 3
BASE58_ALPHABET = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz'
def hash160(s):
"""sha256 followed by ripemd160"""
return hashlib.new('ripemd160', hashlib.sha256(s).digest()).digest()
def hash256(s):
"""two rounds of sha256"""
return hashlib.sha256(hashlib.sha256(s).digest()).digest()
def sha256(s):
return hashlib.sha256(s).digest()
def encode_base58(s):
count = 0
for c in s:
if c == 0:
count += 1
else:
break
num = int.from_bytes(s, 'big')
prefix = '1' * count
result = ''
while num > 0:
num, mod = divmod(num, 58)
result = BASE58_ALPHABET[mod] + result
return prefix + result
def encode_base58_checksum(b):
return encode_base58(b + hash256(b)[:4])
def decode_base58(s):
num = 0
for c in s:
num *= 58
num += BASE58_ALPHABET.index(c)
combined = num.to_bytes(25, byteorder='big')
checksum = combined[-4:]
if hash256(combined[:-4])[:4] != checksum:
raise ValueError('bad address: {} {}'.format(checksum, hash256(combined[:-4])[:4]))
return combined[1:-4]
def little_endian_to_int(b):
"""little_endian_to_int takes byte sequence as a little-endian number.
Returns an integer"""
# use int.from_bytes()
return int.from_bytes(b, 'little')
def int_to_little_endian(n, length):
"""endian_to_little_endian takes an integer and returns the little-endian
byte sequence of length"""
# use n.to_bytes()
return n.to_bytes(length, 'little')
def read_varint(s):
"""read_varint reads a variable integer from a stream"""
i = s.read(1)[0]
if i == 0xfd:
# 0xfd means the next two bytes are the number
return little_endian_to_int(s.read(2))
elif i == 0xfe:
# 0xfe means the next four bytes are the number
return little_endian_to_int(s.read(4))
elif i == 0xff:
# 0xff means the next eight bytes are the number
return little_endian_to_int(s.read(8))
else:
# anything else is just the integer
return i
def encode_varint(i):
"""encodes an integer as a varint"""
if i < 0xfd:
return bytes([i])
elif i < 0x10000:
return b'\xfd' + int_to_little_endian(i, 2)
elif i < 0x100000000:
return b'\xfe' + int_to_little_endian(i, 4)
elif i < 0x10000000000000000:
return b'\xff' + int_to_little_endian(i, 8)
else:
raise ValueError('integer too large: {}'.format(i))
def h160_to_p2pkh_address(h160, testnet=False):
"""Takes a byte sequence hash160 and returns a p2pkh address string"""
# p2pkh has a prefix of b'\x00' for mainnet, b'\x6f' for testnet
if testnet:
prefix = b'\x6f'
else:
prefix = b'\x00'
return encode_base58_checksum(prefix + h160)
def h160_to_p2sh_address(h160, testnet=False):
"""Takes a byte sequence hash160 and returns a p2sh address string"""
# p2sh has a prefix of b'\x05' for mainnet, b'\xc4' for testnet
if testnet:
prefix = b'\xc4'
else:
prefix = b'\x05'
return encode_base58_checksum(prefix + h160)