Mercurial > public > bitcaviar-plus
comparison src/puppy/helpers.py @ 6:5f6d1a28051a
add python package config files
author | Dennis Concepcion Martin <dennisconcepcionmartin@gmail.com> |
---|---|
date | Sun, 24 Oct 2021 15:18:54 +0200 |
parents | src/helpers.py@1a8d94b500d8 |
children |
comparison
equal
deleted
inserted
replaced
5:1a8d94b500d8 | 6:5f6d1a28051a |
---|---|
1 import hashlib | |
2 | |
3 | |
4 def __get_hash(buffer, bytes_order='backward'): | |
5 """ | |
6 Compute hash from bytes | |
7 More info about bytes order: https://en.wikipedia.org/wiki/Endianness | |
8 :param buffer: bytes, required | |
9 :param bytes_order: string, 'backward' or 'forward', optional | |
10 :return: string | |
11 """ | |
12 | |
13 h = hashlib.sha256(buffer).digest() | |
14 h = hashlib.sha256(h).digest() | |
15 | |
16 if bytes_order == 'backward': | |
17 h = h[::-1] | |
18 | |
19 return h.hex() | |
20 | |
21 | |
22 def __get_variable_int(f): | |
23 """ | |
24 Get variable int from transaction data | |
25 More info: https://learnmeabitcoin.com/technical/varint | |
26 :param f: buffer, required | |
27 :return: int | |
28 """ | |
29 | |
30 first_byte = f.read(1) | |
31 | |
32 if first_byte == b'\xfd': | |
33 variable_int_bytes = f.read(2)[::-1] | |
34 elif first_byte == b'\xfe': | |
35 variable_int_bytes = f.read(4)[::-1] | |
36 elif first_byte == b'\xff': | |
37 variable_int_bytes = f.read(8)[::-1] | |
38 else: | |
39 variable_int_bytes = first_byte | |
40 | |
41 return int.from_bytes(variable_int_bytes, 'little') |