Mercurial > public > bitcaviar-plus
comparison src/bitcaviar_plus/helpers.py @ 12:6a0a8cce058e
refactor code
author | Dennis Concepcion Martin <dennisconcepcionmartin@gmail.com> |
---|---|
date | Tue, 26 Oct 2021 09:38:38 +0200 |
parents | src/puppy/helpers.py@6e9f420c117e |
children | 30535f42d0ff |
comparison
equal
deleted
inserted
replaced
11:4987a219a704 | 12:6a0a8cce058e |
---|---|
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() |