electrum-personal-server

Maximally lightweight electrum server for a single user
git clone https://git.parazyd.org/electrum-personal-server
Log | Files | Refs | README

py3specials.py (3394B)


      1 import sys, os
      2 import binascii
      3 import hashlib
      4 
      5 if sys.version_info.major == 3:
      6     string_types = (str)
      7     string_or_bytes_types = (str, bytes)
      8     int_types = (int, float)
      9     # Base switching
     10     code_strings = {
     11         2: '01',
     12         10: '0123456789',
     13         16: '0123456789abcdef',
     14         32: 'abcdefghijklmnopqrstuvwxyz234567',
     15         58: '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz',
     16         256: ''.join([chr(x) for x in range(256)])
     17     }
     18 
     19     def bin_dbl_sha256(s):
     20         bytes_to_hash = from_string_to_bytes(s)
     21         return hashlib.sha256(hashlib.sha256(bytes_to_hash).digest()).digest()
     22 
     23     def lpad(msg, symbol, length):
     24         if len(msg) >= length:
     25             return msg
     26         return symbol * (length - len(msg)) + msg
     27 
     28     def get_code_string(base):
     29         if base in code_strings:
     30             return code_strings[base]
     31         else:
     32             raise ValueError("Invalid base!")
     33 
     34     def changebase(string, frm, to, minlen=0):
     35         if frm == to:
     36             return lpad(string, get_code_string(frm)[0], minlen)
     37         return encode(decode(string, frm), to, minlen)
     38 
     39     def bin_to_b58check(inp, magicbyte=0):
     40         inp_fmtd = from_int_to_byte(int(magicbyte)) + inp
     41 
     42         leadingzbytes = 0
     43         for x in inp_fmtd:
     44             if x != 0:
     45                 break
     46             leadingzbytes += 1
     47 
     48         checksum = bin_dbl_sha256(inp_fmtd)[:4]
     49         return '1' * leadingzbytes + changebase(inp_fmtd + checksum, 256, 58)
     50 
     51     def bytes_to_hex_string(b):
     52         if isinstance(b, str):
     53             return b
     54 
     55         return ''.join('{:02x}'.format(y) for y in b)
     56 
     57     def safe_from_hex(s):
     58         return bytes.fromhex(s)
     59 
     60     def from_int_to_byte(a):
     61         return bytes([a])
     62 
     63     def from_byte_to_int(a):
     64         return a
     65 
     66     def from_string_to_bytes(a):
     67         return a if isinstance(a, bytes) else bytes(a, 'utf-8')
     68 
     69     def safe_hexlify(a):
     70         return str(binascii.hexlify(a), 'utf-8')
     71 
     72     def encode(val, base, minlen=0):
     73         base, minlen = int(base), int(minlen)
     74         code_string = get_code_string(base)
     75         result_bytes = bytes()
     76         while val > 0:
     77             curcode = code_string[val % base]
     78             result_bytes = bytes([ord(curcode)]) + result_bytes
     79             val //= base
     80 
     81         pad_size = minlen - len(result_bytes)
     82 
     83         padding_element = b'\x00' if base == 256 else b'1' \
     84             if base == 58 else b'0'
     85         if (pad_size > 0):
     86             result_bytes = padding_element * pad_size + result_bytes
     87 
     88         result_string = ''.join([chr(y) for y in result_bytes])
     89         result = result_bytes if base == 256 else result_string
     90 
     91         return result
     92 
     93     def decode(string, base):
     94         if base == 256 and isinstance(string, str):
     95             string = bytes(bytearray.fromhex(string))
     96         base = int(base)
     97         code_string = get_code_string(base)
     98         result = 0
     99         if base == 256:
    100 
    101             def extract(d, cs):
    102                 return d
    103         else:
    104 
    105             def extract(d, cs):
    106                 return cs.find(d if isinstance(d, str) else chr(d))
    107 
    108         if base == 16:
    109             string = string.lower()
    110         while len(string) > 0:
    111             result *= base
    112             result += extract(string[0], code_string)
    113             string = string[1:]
    114         return result
    115 
    116     def from_int_representation_to_bytes(a):
    117         return bytes(str(a), 'utf-8')