obelisk

Electrum server using libbitcoin as its backend
git clone https://git.parazyd.org/obelisk
Log | Files | Refs | README | LICENSE

util.py (2846B)


      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):
     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     # TODO: check endianness
     85     block_header = block[:80]
     86     # version = block_header[:4]
     87     # prev_merkle_root = block_header[4:36]
     88     # merkle_root = block_header[36:68]
     89     # timestamp = block_header[68:72]
     90     # bits = block_header[72:76]
     91     # nonce = block_header[76:80]
     92     return block_header
     93 
     94 
     95 def sha256(inp):
     96     """ Simple wrapper of hashlib sha256. """
     97     return _sha256(inp).digest()
     98 
     99 
    100 def double_sha256(inp):
    101     """ sha256 of sha256, as used extensively in bitcoin """
    102     return sha256(sha256(inp))
    103 
    104 
    105 def hash_to_hex_str(inp):
    106     """Convert a big-endian binary hash to displayed hex string.
    107     Display form of a binary hash is reversed and converted to hex.
    108     """
    109     return bytes(reversed(inp)).hex()