Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def _codestring_to_array(codestring, base):
codestring = to_bytes(codestring)
codebase = code_strings[base]
array = []
for s in codestring:
try:
array.append(codebase.index(s))
except ValueError:
raise EncodingError("Character '%s' not found in codebase" % s)
return array
def int_to_varbyteint(inp):
"""
Convert integer to CompactSize Variable length integer in byte format.
See https://en.bitcoin.it/wiki/Protocol_documentation#Variable_length_integer for specification
:param inp: Integer to convert
:type inp: int
:return: byteint: 1-9 byte representation as integer
"""
if not isinstance(inp, numbers.Number):
raise EncodingError("Input must be a number type")
if inp < 0xfd:
return struct.pack('B', inp)
elif inp < 0xffff:
return struct.pack('
def varbyteint_to_int(byteint):
"""
Convert CompactSize Variable length integer in byte format to integer.
See https://en.bitcoin.it/wiki/Protocol_documentation#Variable_length_integer for specification
:param byteint: 1-9 byte representation
:type byteint: bytes, list, bytearray
:return (int, int): tuple wit converted integer and size
"""
if not isinstance(byteint, (bytes, list, bytearray)):
raise EncodingError("Byteint be a list or defined as bytes")
if PY3 or isinstance(byteint, (list, bytearray)):
ni = byteint[0]
else:
ni = ord(byteint[0])
if ni < 253:
return ni, 1
if ni == 253: # integer of 2 bytes
size = 2
elif ni == 254: # integer of 4 bytes
size = 4
else: # integer of 8 bytes
size = 8
return change_base(byteint[1:1+size][::-1], 256, 10), size + 1
:param var: input variable in any format
:type var: str, byte, bytearray, unicode
:param base: specify variable format, i.e. 10 for decimal, 16 for hex
:type base: int
:return: Normalized var in string for Python 2, bytes for Python 3, decimal for base10
"""
try:
if PY3 and isinstance(var, str):
var = var.encode('ISO-8859-1')
except ValueError:
try:
var = var.encode('utf-8')
except ValueError:
raise EncodingError("Unknown character '%s' in input format" % var)
if not PY3 and isinstance(var, unicode):
try:
var = str(var)
except UnicodeEncodeError:
try:
var = var.encode('utf-8')
except ValueError:
raise EncodingError("Cannot convert this unicode to string format")
if base == 10:
return int(var)
elif isinstance(var, list):
return deepcopy(var)
else:
return var
"""
if base_from == 10 and not min_length:
raise EncodingError("For a decimal input a minimum output length is required!")
code_str = _get_code_string(base_to)
if not isinstance(base_to, int):
base_to = len(code_str)
elif int(base_to) not in code_strings:
output_as_list = True
code_str_from = _get_code_string(base_from)
if not isinstance(base_from, int):
base_from = len(code_str)
if not isinstance(code_str_from, (bytes, list)):
raise EncodingError("Code strings must be a list or defined as bytes")
output = []
input_dec = 0
addzeros = 0
inp = normalize_var(chars, base_from)
# Use binascii and int for standard conversions to speedup things
if not min_length:
if base_from == 256 and base_to == 16:
return to_hexstring(inp)
elif base_from == 16 and base_to == 256:
return binascii.unhexlify(inp)
if base_from == 16 and base_to == 10:
return int(inp, 16)
if output_even is None and base_to == 16:
output_even = True
if PY3 and isinstance(var, str):
var = var.encode('ISO-8859-1')
except ValueError:
try:
var = var.encode('utf-8')
except ValueError:
raise EncodingError("Unknown character '%s' in input format" % var)
if not PY3 and isinstance(var, unicode):
try:
var = str(var)
except UnicodeEncodeError:
try:
var = var.encode('utf-8')
except ValueError:
raise EncodingError("Cannot convert this unicode to string format")
if base == 10:
return int(var)
elif isinstance(var, list):
return deepcopy(var)
else:
return var
:param include_witver: Include witness version in output? Default is False
:type include_witver: bool
:param as_hex: Output public key hash as hex or bytes. Default is False
:type as_hex: bool
:return str: Public Key Hash
"""
if (any(ord(x) < 33 or ord(x) > 126 for x in bech)) or (bech.lower() != bech and bech.upper() != bech):
return False
bech = bech.lower()
pos = bech.rfind('1')
if pos < 1 or pos + 7 > len(bech) or len(bech) > 90:
return False
if prefix and prefix != bech[:pos]:
raise EncodingError("Invalid address. Prefix '%s', prefix expected is '%s'" % (bech[:pos], prefix))
else:
hrp = bech[:pos]
data = _codestring_to_array(bech[pos + 1:], 'bech32')
hrp_expanded = [ord(x) >> 5 for x in hrp] + [0] + [ord(x) & 31 for x in hrp]
if not _bech32_polymod(hrp_expanded + data) == 1:
return False
data = data[:-6]
decoded = bytearray(convertbits(data[1:], 5, 8, pad=False))
if decoded is None or len(decoded) < 2 or len(decoded) > 40:
return False
if data[0] > 16:
return False
if data[0] == 0 and len(decoded) not in [20, 32]:
return False
prefix = b''
if include_witver:
:param encoding: Encoding of address to calculate: base58 or bech32. Default is base58
:type encoding: str
:return str: Base58 or bech32 encoded address
"""
if encoding == 'base58':
if prefix is None:
prefix = b'\x00'
return pubkeyhash_to_addr_base58(pubkeyhash, prefix)
elif encoding == 'bech32':
if prefix is None:
prefix = 'bc'
return pubkeyhash_to_addr_bech32(pubkeyhash, prefix)
else:
raise EncodingError("Encoding %s not supported" % encoding)