comparison src/puppy/helpers.py @ 8:4d259e84160d

fix OverFlow bug
author Dennis Concepcion Martin <dennisconcepcionmartin@gmail.com>
date Sun, 24 Oct 2021 17:38:23 +0200
parents
children e218f70e19e9
comparison
equal deleted inserted replaced
7:e4afde8d5a7e 8:4d259e84160d
1 import hashlib
2
3 """
4 Helper methods
5 """
6
7
8 def get_var_int(f):
9 """
10 A VarInt (variable integer) is a field used in transaction data to indicate the number of upcoming fields,
11 or the length of an upcoming field.
12 More info: https://learnmeabitcoin.com/technical/varint
13 :param f: buffer, required
14 :return: string
15 """
16
17 prefix = f.read(1).hex()
18
19 if int(prefix, 16) == 253:
20 number_of_transactions = f.read(2)[::-1].hex()
21 elif int(prefix, 16) == 254:
22 number_of_transactions = f.read(4)[::-1].hex()
23 elif int(prefix, 16) == 255:
24 number_of_transactions = f.read(8)[::-1].hex()
25 else:
26 number_of_transactions = prefix
27
28 return number_of_transactions
29
30
31 def compute_hash(data):
32 """
33 Get hash
34 :param data: bytes, required
35 :return: string
36 """
37
38 h = hashlib.sha256(data).digest()
39 h = hashlib.sha256(h).digest()
40
41 return h[::-1].hex()