def b58check_to_bin(addr, version=False): """optionally include the version byte for get_version_byte. """ if not version: return ebt.DecodeBase58Check(addr)[1:] else: return ebt.DecodeBase58Check(addr)
def b58check_to_bin(addr, version=False): """optionally include the version byte for get_version_byte. Note that jmbitcoin does not need this flag, because it uses the changebase() function, which here has been restricted. """ if not version: return ebt.DecodeBase58Check(addr)[1:] else: return ebt.DecodeBase58Check(addr)
def get_version_byte(inp): #Null input not allowed if not inp: return None leadingzbytes = len(re.match('^1*', inp).group(0)) try: decoded = ebt.DecodeBase58Check(inp) except: return None #Checksum failure will return None if not decoded: return None data = b'\x00' * leadingzbytes + ebt.DecodeBase58Check(inp) return ord(data[0])
def tobtc(inp: str) -> str: """Given a Namecoin address or key, converts it to Bitcoin format""" # Handle bech32 segwit data first. if inp[:3] == "nc1": return convert_bech32(inp, "bc") if inp[:3] == "tn1": return convert_bech32(inp, "tb") # Otherwise, try to base58-decode it and then look at the version to # determine what it could have been. try: vch = bitcoin.DecodeBase58Check(inp) old_version = vch[0] if vch[0] == 52: # P2PKH address new_version = 0 elif vch[0] == 13: # P2SH address new_version = 5 else: raise AssertionError( f"Unknown Bitcoin base58 version: {old_version}") new_vch = bytes([new_version]) + vch[1:] outp = bitcoin.EncodeBase58Check(new_vch) return outp except bitcoin.InvalidChecksum: # This is not base58 data, maybe try something else. pass raise AssertionError(f"Invalid input for format conversion: {inp}")
def frombtc(inp: str) -> str: """Given a Bitcoin address or key, converts it to Namecoin format""" # If there is a trailing suffix on an URI with an address, remove it # and add it back after conversion. qm = inp.find("?") if qm != -1: suffix = inp[qm:] stripped = inp[:qm] return frombtc(stripped) + suffix # If there is a prefix separated by colon, strip it off and add it # back later. For bitcoin: URI's, the prefix is rebranded as well. colon = inp.find(":") if colon != -1: prefix = inp[:colon] stripped = inp[colon + 1:] if prefix == "bitcoin": prefix = "xaya" return prefix + ":" + frombtc(stripped) # Handle bech32 segwit data first. if inp[:3].lower() == "bc1": return convert_bech32(inp, BitcoinMainnet.SEGWIT_HRP) if inp[:3].lower() == "tb1": return convert_bech32(inp, BitcoinTestnet.SEGWIT_HRP) # Otherwise, try to base58-decode it and then look at the version to # determine what it could have been. try: vch = bitcoin.DecodeBase58Check(inp) old_version = vch[0] if vch[0] == 0: # P2PKH address new_version = BitcoinMainnet.ADDRTYPE_P2PKH elif vch[0] == 111: new_version = BitcoinTestnet.ADDRTYPE_P2PKH elif vch[0] == 5: # P2SH address new_version = BitcoinMainnet.ADDRTYPE_P2SH elif vch[0] == 196: new_version = BitcoinTestnet.ADDRTYPE_P2SH elif vch[0] in range(128, 136): # Privkey with optional script type offset = vch[0] - 128 new_version = BitcoinMainnet.WIF_PREFIX + offset elif vch[0] == 239: new_version = BitcoinTestnet.WIF_PREFIX else: raise AssertionError( f"Unknown Bitcoin base58 version: {old_version}") new_vch = bytes([new_version]) + vch[1:] outp = bitcoin.EncodeBase58Check(new_vch) return outp except bitcoin.InvalidChecksum: # This is not base58 data, maybe try something else. pass raise AssertionError(f"Invalid input for format conversion: {inp}")
def get_xpubkey(keystore: BIP32_KeyStore, c, i) -> str: def encode_path_int(path_int) -> str: if path_int < 0xffff: hex = bitcoin.int_to_hex(path_int, 2) else: hex = 'ffff' + bitcoin.int_to_hex(path_int, 4) return hex s = ''.join(map(encode_path_int, (c, i))) return 'ff' + bitcoin.DecodeBase58Check(keystore.xpub).hex() + s
def frombtc(inp: str) -> str: """Given a Bitcoin address or key, converts it to Namecoin format""" # If there is a trailing suffix on an URI with an address, remove it # and add it back after conversion. qm = inp.find("?") if qm != -1: suffix = inp[qm:] stripped = inp[:qm] return frombtc(stripped) + suffix # If there is a prefix separated by colon, strip it off and add it # back later. For bitcoin: URI's, the prefix is rebranded as well. colon = inp.find(":") if colon != -1: prefix = inp[:colon] stripped = inp[colon + 1:] if prefix == "bitcoin": prefix = "xaya" return prefix + ":" + frombtc(stripped) # Handle bech32 segwit data first. if inp[:3].lower() == "bc1": return convert_bech32(inp, BitcoinMainnet.SEGWIT_HRP) if inp[:3].lower() in ["tb1", "tn1"]: return convert_bech32(inp, BitcoinTestnet.SEGWIT_HRP) # Handle bech32 lightning addresses. if inp[:4].lower() == "lnbc": return convert_ln_bech32(inp, BitcoinMainnet.SEGWIT_HRP) # Handle genesis block hashes, e.g. from Lightning messages bitcoin_mainnet_rev_genesis = bitcoin.rev_hex( "000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f") if bitcoin_mainnet_rev_genesis in inp: return inp.replace(bitcoin_mainnet_rev_genesis, bitcoin.rev_hex(BitcoinMainnet.GENESIS)) bitcoin_testnet_rev_genesis = bitcoin.rev_hex( "000000000933ea01ad0ee984209779baaec3ced90fa3f408719526f8d77f4943") if bitcoin_testnet_rev_genesis in inp: return inp.replace(bitcoin_testnet_rev_genesis, bitcoin.rev_hex(BitcoinTestnet.GENESIS)) # Otherwise, try to base58-decode it and then look at the version to # determine what it could have been. try: vch = bitcoin.DecodeBase58Check(inp) old_version = vch[0] if vch[0] == 0: # P2PKH address new_version = BitcoinMainnet.ADDRTYPE_P2PKH elif vch[0] == 111: new_version = BitcoinTestnet.ADDRTYPE_P2PKH elif vch[0] == 5: # P2SH address new_version = BitcoinMainnet.ADDRTYPE_P2SH elif vch[0] == 196: new_version = BitcoinTestnet.ADDRTYPE_P2SH elif vch[0] in range(128, 136): # Privkey with optional script type offset = vch[0] - 128 new_version = BitcoinMainnet.WIF_PREFIX + offset elif vch[0] == 239: new_version = BitcoinTestnet.WIF_PREFIX else: raise AssertionError( f"Unknown Bitcoin base58 version: {old_version}") new_vch = bytes([new_version]) + vch[1:] outp = bitcoin.EncodeBase58Check(new_vch) return outp except bitcoin.InvalidChecksum: # This is not base58 data, maybe try something else. pass raise AssertionError(f"Invalid input for format conversion: {inp}")
def b58check_to_bin(addr): #drop the leading version byte returned by electrum's decode58 return ebt.DecodeBase58Check(addr)[1:]