Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_autofix_not_applicable_check_str():
charset = BITCOIN_ALPHABET.replace(b'x', b'l')
msg = b'hello world'
enc = b58encode_check(msg).replace(b'x', b'l').replace(b'o', b'0')
back = b58decode_check(enc, alphabet=charset, autofix=True)
assert_that(back, equal_to(msg))
def to_hex(address):
"""Helper function that will convert a generic value to hex
Args:
address (str): address
"""
return base58.b58decode_check(address).hex().upper()
def _base58_decode(encoded_str):
"""decode a base58 with checksum string to bytes"""
return base58.b58decode_check(encoded_str)
def wif_to_private(self, wif):
dec = base58.b58decode_check(wif)
if dec[0] != 0x80:
raise ValueError("Invalid network (expected mainnet)")
return dec[1:]
def neo_get_scripthash_from_address(address):
"""
Convert a Public Address String to a ScriptHash (Address) String.
:param address: The Public address to convert.
:type address: str
:return: String containing the converted ScriptHash.
"""
hash_bytes = binascii.hexlify(base58.b58decode_check(address))
return reverse_hex(hash_bytes[2:].decode('utf-8'))
def guess_currency_from_address(address):
"""
Given a crypto address, find which currency it likely belongs to.
Raises an exception if it can't find a match. Raises exception if address
is invalid.
"""
if is_py2:
fixer = lambda x: int(x.encode('hex'), 16)
else:
fixer = lambda x: x # does nothing
first_byte = fixer(b58decode_check(address)[0])
double_first_byte = fixer(b58decode_check(address)[:2])
hits = []
for currency, data in crypto_data.items():
if hasattr(data, 'get'): # skip incomplete data listings
version = data.get('address_version_byte', None)
if version is not None and version in [double_first_byte, first_byte]:
hits.append([currency, data['name']])
if hits:
return hits
raise ValueError("Unknown Currency with first byte: %s" % first_byte)
def b58_decode(str58):
return base58.b58decode_check(str58)
def from_b58check(key):
""" Decodes a Base58Check encoded key.
The encoding must conform to the description in:
https://github.com/bitcoin/bips/blob/master/bip-0032.mediawiki#serialization-format
Args:
key (str): A Base58Check encoded key.
Returns:
HDPrivateKey or HDPublicKey:
Either an HD private or
public key object, depending on what was serialized.
"""
return HDKey.from_bytes(base58.b58decode_check(key))
bytes: The private key
Raises:
ValueError:
if the input `nep2_key` length != 58
if the input `nep2_key` is invalid
if the input `passphrase` is wrong
"""
if not nep2_key or len(nep2_key) != 58:
raise ValueError('Please provide a nep2_key with a length of 58 bytes (LEN: {0:d})'.format(len(nep2_key)))
ADDRESS_HASH_SIZE = 4
ADDRESS_HASH_OFFSET = len(NEP_FLAG) + len(NEP_HEADER)
try:
decoded_key = base58.b58decode_check(nep2_key)
except Exception:
raise ValueError("Invalid nep2_key")
address_hash = decoded_key[ADDRESS_HASH_OFFSET:ADDRESS_HASH_OFFSET + ADDRESS_HASH_SIZE]
encrypted = decoded_key[-32:]
pwd_normalized = bytes(unicodedata.normalize('NFC', passphrase), 'utf-8')
derived = scrypt.hash(pwd_normalized, address_hash,
N=SCRYPT_ITERATIONS,
r=SCRYPT_BLOCKSIZE,
p=SCRYPT_PARALLEL_FACTOR,
buflen=SCRYPT_KEY_LEN_BYTES)
derived1 = derived[:32]
derived2 = derived[32:]