def frombtcbytes(inp: bytes) -> bytes: # Handle genesis block hashes, e.g. from Lightning messages bitcoin_mainnet_rev_genesis = bfh(bitcoin.rev_hex("000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f")) if bitcoin_mainnet_rev_genesis in inp: return inp.replace(bitcoin_mainnet_rev_genesis, BitcoinMainnet.rev_genesis_bytes()) bitcoin_testnet_rev_genesis = bfh(bitcoin.rev_hex("000000000933ea01ad0ee984209779baaec3ced90fa3f408719526f8d77f4943")) if bitcoin_testnet_rev_genesis in inp: return inp.replace(bitcoin_testnet_rev_genesis, BitcoinTestnet.rev_genesis_bytes()) raise AssertionError(f"Invalid input for format conversion: {inp}")
def test_scrypt(self): #0200000011f1fe21e0b66dc214be46366465cb95d29830e31ddd225a11349a836a993bf7b5db36b3e5593d039779bff204d132b65ee029a2e499ebeb5a4b19cbe862eee2b623cc5276676c1c000e1c60 header = { 'block_height': 12095, 'nonce': 1612451328, 'timestamp': 1389110198, 'version': 2, 'prev_block_hash': 'f73b996a839a34115a22dd1de33098d295cb65643646be14c26db6e021fef111', 'merkle_root': 'e2ee62e8cb194b5aebeb99e4a229e05eb632d104f2bf7997033d59e5b336dbb5', 'bits': 476866422 } powhash = rev_hex(bh2u(scryptGetHash(bfh(serialize_header(header))))) self.assertEqual( powhash, '00000000335c88172421df73a1c1f22f4d7c23d8ef34c78d728c4eff3ba24a34')
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 rev_genesis_bytes(cls) -> bytes: return bytes.fromhex(bitcoin.rev_hex(cls.GENESIS))