def parse_fallback(fallback, currency):
if currency == 'bc' or currency == 'tb':
wver = fallback[0:5].uint
if wver == 17:
addr=base58.b58encode_check(bytes([base58_prefix_map[currency][0]])
+ fallback[5:].tobytes())
elif wver == 18:
addr=base58.b58encode_check(bytes([base58_prefix_map[currency][1]])
+ fallback[5:].tobytes())
elif wver <= 16:
addr=bech32_encode(currency, bitarray_to_u5(fallback))
else:
return None
else:
addr=fallback.tobytes()
return addr
# Map of classical and witness address prefixes
python类b58encode_check()的实例源码
def address(self, compressed=True, testnet=False):
""" Address property that returns the Base58Check
encoded version of the HASH160.
Args:
compressed (bool): Whether or not the compressed key should
be used.
testnet (bool): Whether or not the key is intended for testnet
usage. False indicates mainnet usage.
Returns:
bytes: Base58Check encoded string
"""
# Put the version byte in front, 0x00 for Mainnet, 0x6F for testnet
version = bytes([self.TESTNET_VERSION]) if testnet else bytes([self.MAINNET_VERSION])
return base58.b58encode_check(version + self.hash160(compressed))
def key_hash_to_address(hash160, version=0x0):
"""Convert RIPEMD-160 hash to bitcoin address.
Args:
hash160 (bytes/str): bitcoin hash160 to decode
version (int): The version prefix
Returns:
(bitcoin address): base58 encoded bitcoin address
"""
if isinstance(hash160, str):
# if 0x in string, strip it
if "0x" in hash160:
h160 = hex_str_to_bytes(hash160[2:])
else:
h160 = hex_str_to_bytes(hash160)
elif isinstance(hash160, bytes):
h160 = hash160
address = base58.b58encode_check(bytes([version]) + h160)
return address
def parse_fallback(fallback, currency):
if currency == 'bc' or currency == 'tb':
wver = fallback[0:5].uint
if wver == 17:
addr=base58.b58encode_check(bytes([base58_prefix_map[currency][0]])
+ fallback[5:].tobytes())
elif wver == 18:
addr=base58.b58encode_check(bytes([base58_prefix_map[currency][1]])
+ fallback[5:].tobytes())
elif wver <= 16:
addr=bech32_encode(currency, bitarray_to_u5(fallback))
else:
return None
else:
addr=fallback.tobytes()
return addr
# Map of classical and witness address prefixes
def address(self, compressed=True, testnet=False):
""" Address property that returns the Base58Check
encoded version of the HASH160.
Args:
compressed (bool): Whether or not the compressed key should
be used.
testnet (bool): Whether or not the key is intended for testnet
usage. False indicates mainnet usage.
Returns:
bytes: Base58Check encoded string
"""
version = '0x'
return version + encode_hex(self.keccak[12:])
# Put the version byte in front, 0x00 for Mainnet, 0x6F for testnet
# version = bytes([self.TESTNET_VERSION]) if testnet else bytes([self.MAINNET_VERSION])
# return base58.b58encode_check(version + self.hash160(compressed))
def pubkey_to_address(pubkey: bytes) -> str:
if 'ripemd160' not in hashlib.algorithms_available:
raise RuntimeError('missing ripemd160 hash algorithm')
sha = hashlib.sha256(pubkey).digest()
ripe = hashlib.new('ripemd160', sha).digest()
return b58encode_check(b'\x00' + ripe)
def address(self, testnet=False):
""" Returns the Base58Check encoded version of the HASH160.
Args:
testnet (bool): Whether or not the key is intended for testnet
usage. False indicates mainnet usage.
Returns:
bytes: Base58Check encoded string
"""
rv = ""
prefix = bytes([self.P2SH_TESTNET_VERSION if testnet else self.P2SH_MAINNET_VERSION])
rv = base58.b58encode_check(prefix + self.hash160())
return rv
def to_b58check(self, testnet=False):
""" Generates a Base58Check encoding of this private key.
Returns:
str: A Base58Check encoded string representing the key.
"""
version = self.TESTNET_VERSION if testnet else self.MAINNET_VERSION
return base58.b58encode_check(bytes([version]) + bytes(self))
def to_b58check(self, testnet=False):
""" Generates a Base58Check encoding of this key.
Args:
testnet (bool): True if the key is to be used with
testnet, False otherwise.
Returns:
str: A Base58Check encoded string representing the key.
"""
b = self.testnet_bytes if testnet else bytes(self)
return base58.b58encode_check(b)
def b58xprv(parent_fingerprint, private_key, chain, depth, childnr):
raw = ('\x04\x88\xad\xe4' +
chr(depth) + parent_fingerprint + int_to_string(childnr, 4) +
chain + '\x00' + private_key)
return b58encode_check(raw)
def b58xpub(parent_fingerprint, public_key, chain, depth, childnr):
raw = ('\x04\x88\xb2\x1e' +
chr(depth) + parent_fingerprint + int_to_string(childnr, 4) +
chain + public_key)
return b58encode_check(raw)
def to_b58check(self, testnet=False):
""" Generates a Base58Check encoding of this private key.
Returns:
str: A Base58Check encoded string representing the key.
"""
version = self.TESTNET_VERSION if testnet else self.MAINNET_VERSION
return base58.b58encode_check(bytes([version]) + bytes(self))
def to_b58check(self, testnet=False):
""" Generates a Base58Check encoding of this key.
Args:
testnet (bool): True if the key is to be used with
testnet, False otherwise.
Returns:
str: A Base58Check encoded string representing the key.
"""
b = self.testnet_bytes if testnet else bytes(self)
return base58.b58encode_check(b)
def ExportNEP2(self, passphrase):
"""
Export the encrypted private key in NEP-2 format.
Args:
passphrase (str): The password to encrypt the private key with, as unicode string
Returns:
str: The NEP-2 encrypted private key
"""
if len(passphrase) < 2:
raise ValueError("Passphrase must have a minimum of 2 characters")
# Hash address twice, then only use the first 4 bytes
address_hash_tmp = hashlib.sha256(self.GetAddress().encode('utf-8')).digest()
address_hash_tmp2 = hashlib.sha256(address_hash_tmp).digest()
address_hash = address_hash_tmp2[:4]
# Normalize password and run scrypt over it with the address_hash
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)
# Split the scrypt-result into two parts
derived1 = derived[:32]
derived2 = derived[32:]
# Run XOR and encrypt the derived parts with AES
xor_ed = xor_bytes(bytes(self.PrivateKey), derived1)
cipher = AES.new(derived2, AES.MODE_ECB)
encrypted = cipher.encrypt(xor_ed)
# Assemble the final result
assembled = bytearray()
assembled.extend(NEP_HEADER)
assembled.extend(NEP_FLAG)
assembled.extend(address_hash)
assembled.extend(encrypted)
# Finally, encode with Base58Check
encrypted_key_nep2 = base58.b58encode_check(bytes(assembled))
return encrypted_key_nep2