-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathusefulfunctions.py
176 lines (139 loc) · 4.44 KB
/
usefulfunctions.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
import hashlib
SIGHASH_ALL = 1
SIGHASH_NONE = 2
SIGHASH_SINGLE = 3
BASE58_ALPHABET = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"
def little_endian_to_int(b):
return int.from_bytes(b, "little")
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 read_varint(s):
i = s.read(1)[0]
if i == 0xFD:
return little_endian_to_int(s.read(2))
elif i == 0xFE:
return little_endian_to_int(s.read(4))
elif i == 0xFF:
return little_endian_to_int(s.read(8))
else:
# anything else is just the integer
return i
def int_to_little_endian(n, length): # converting the int into the little edian format
return n.to_bytes(length, "little")
# encode a variant which can represent the number over their bytes represntation
def encode_varint(i):
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)
def encode_num(num):
if num == 0:
return b""
abs_num = abs(num)
negative = num < 0
result = bytearray()
while abs_num:
result.append(abs_num & 0xFF)
abs_num >>= 8
if result[-1] & 0x80:
if negative:
result.append(0x80)
else:
result.append(0)
elif negative:
result[-1] |= 0x80
return bytes(result)
def decode_num(element):
if element == b"":
return 0
big_endian = element[::-1]
if big_endian[0] & 0x80:
negative = True
result = big_endian[0] & 0x7F
else:
negative = False
result = big_endian[0]
for c in big_endian[1:]:
result <<= 8
result += c
if negative:
return -result
else:
return result
# sha256 operation followed by the ripemd160 hash operation, the combination of which is called a hash160 operation.
def hash160(s):
return hashlib.new("ripemd160", hashlib.sha256(s).digest()).digest()
# double SHA256 operation
def hash256(s):
return hashlib.sha256(hashlib.sha256(s).digest()).digest()
def sha256(s):
return hashlib.sha256(s).digest()
# BIP 0173 Bech32 uses a 32-character alphabet that’s just numbers and lowercase letters, except 1, b, i, and o. Thus far, it’s only used for Segwit
def encode_base58(s):
# determine how many 0 bytes (b'\x00') s starts with
count = 0
for c in s:
if c == 0:
count += 1
else:
break
# convert to big endian integer
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
# getting the first 4 bytes as the checksum and adding it to the end of the base58 encoded string
def encode_base58_checksum(s):
return encode_base58(s + hash256(s)[: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 h160_to_p2pkh_address(h160):
prefix = b"\x00"
return encode_base58_checksum(prefix + h160)
def h160_to_p2sh_address(h160):
prefix = b"\x05"
return encode_base58_checksum(prefix + h160)
def merkle_parent(hash1, hash2):
return hash256(hash1 + hash2)
def merkle_parent_level(hashes):
if len(hashes) == 1:
raise RuntimeError("Cannot take a parent level with only 1 item")
if len(hashes) % 2 == 1:
hashes.append(hashes[-1])
parent_level = []
for i in range(0, len(hashes), 2):
parent = merkle_parent(hashes[i], hashes[i + 1])
parent_level.append(parent)
return parent_level
def merkle_root(hashes):
current_level = hashes
while len(current_level) > 1:
current_level = merkle_parent_level(current_level)
return current_level[0]