tutil.py - obelisk - Electrum server using libbitcoin as its backend
HTML git clone https://git.parazyd.org/obelisk
DIR Log
DIR Files
DIR Refs
DIR README
DIR LICENSE
---
tutil.py (2837B)
---
1 #!/usr/bin/env python3
2 # Copyright (C) 2020-2021 Ivan J. <parazyd@dyne.org>
3 #
4 # This file is part of obelisk
5 #
6 # This program is free software: you can redistribute it and/or modify
7 # it under the terms of the GNU Affero General Public License version 3
8 # as published by the Free Software Foundation.
9 #
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU Affero General Public License for more details.
14 #
15 # You should have received a copy of the GNU Affero General Public License
16 # along with this program. If not, see <http://www.gnu.org/licenses/>.
17 """Utility functions"""
18 import hashlib
19 from binascii import hexlify
20
21 _sha256 = hashlib.sha256
22
23
24 def is_integer(val):
25 """Check if val is of type int"""
26 return isinstance(val, int)
27
28
29 def is_non_negative_integer(val):
30 """Check if val is of type int and non-negative"""
31 if is_integer(val):
32 return val >= 0
33 return False
34
35
36 def is_boolean(val):
37 """Check if val is of type bool"""
38 return isinstance(val, bool)
39
40
41 def is_hex_str(text):
42 """Check if text is a hex string"""
43 if not isinstance(text, str):
44 return False
45 try:
46 b = bytes.fromhex(text)
47 except: # pylint: disable=W0702
48 return False
49 # Forbid whitespaces in text:
50 if len(text) != 2 * len(b):
51 return False
52 return True
53
54
55 def is_hash256_str(text):
56 """Check if text is a sha256 hash"""
57 if not isinstance(text, str):
58 return False
59 if len(text) != 64:
60 return False
61 return is_hex_str(text)
62
63
64 def safe_hexlify(val):
65 """hexlify and return a string"""
66 return str(hexlify(val), "utf-8")
67
68
69 def bh2u(val):
70 """
71 str with hex representation of a bytes-like object
72
73 >>> x = bytes((1, 2, 10))
74 >>> bh2u(x)
75 '01020A'
76 """
77 return val.hex()
78
79
80 def block_to_header(block): # pragma: no cover
81 """Return block header from raw block"""
82 if not isinstance(block, (bytes, bytearray)):
83 raise ValueError("block is not of type bytes/bytearray")
84 block_header = block[:80]
85 # version = block_header[:4]
86 # prev_merkle_root = block_header[4:36]
87 # merkle_root = block_header[36:68]
88 # timestamp = block_header[68:72]
89 # bits = block_header[72:76]
90 # nonce = block_header[76:80]
91 return block_header
92
93
94 def sha256(inp):
95 """ Simple wrapper of hashlib sha256. """
96 return _sha256(inp).digest()
97
98
99 def double_sha256(inp):
100 """ sha256 of sha256, as used extensively in bitcoin """
101 return sha256(sha256(inp))
102
103
104 def hash_to_hex_str(inp):
105 """Convert a big-endian binary hash to displayed hex string.
106 Display form of a binary hash is reversed and converted to hex.
107 """
108 return bytes(reversed(inp)).hex()