def test_p2wsh() -> None: # self-consistency pubkey = "02 cc71eb30d653c0c3163990c47b976f3fb3f37cccdcbedb169a1dfef58bbfbfaf" pubkey_hash = hash160(pubkey) redeem_script = scriptPubKey_from_payload("p2pkh", pubkey_hash) payload = sha256(redeem_script) scriptPubKey = script.encode([0, payload]) assert scriptPubKey == p2wsh(script.decode(redeem_script)) # to the scriptPubKey in two steps (through payload) script_type = "p2wsh" assert scriptPubKey == scriptPubKey_from_payload(script_type, payload) # back from the scriptPubKey to the payload assert (script_type, payload, 0) == payload_from_scriptPubKey(scriptPubKey) # bech32 address network = "mainnet" address = bech32address.p2wsh(redeem_script, network) assert address == address_from_scriptPubKey(scriptPubKey, network) wit_ver = 0 assert address == b32address_from_witness(wit_ver, payload, network) # back from the address to the scriptPubKey assert (scriptPubKey, network) == scriptPubKey_from_address(address) # p2sh-wrapped base58 address address = base58address.p2wsh_p2sh(redeem_script, network) assert address == b58address_from_witness(payload, network)
def test_CLT(self): network = 'mainnet' vault_pubkeys = [b'\x00' * 33, b'\x11' * 33, b'\x22' * 33] recovery_pubkeys = [b'\x77' * 33, b'\x88' * 33, b'\x99' * 33] redeem_script = encode([ 'OP_IF', 2, *vault_pubkeys, 3, 'OP_CHECKMULTISIG', 'OP_ELSE', 500, 'OP_CHECKLOCKTIMEVERIFY', 'OP_DROP', 2, *recovery_pubkeys, 3, 'OP_CHECKMULTISIG', 'OP_ENDIF' ]) payload = sha256(redeem_script) script = "00207b5310339c6001f75614daa5083839fa54d46165f6c56025cc54d397a85a5708" scriptPubKey = p2wsh(redeem_script) self.assertEqual(scriptPubKey.hex(), script) scriptPubKey = scriptPubKey_from_payload('p2wsh', payload) self.assertEqual(scriptPubKey.hex(), script) address = b"bc1q0df3qvuuvqqlw4s5m2jsswpelf2dgct97mzkqfwv2nfe02z62uyq7n4zjj" address2 = address_from_scriptPubKey(scriptPubKey, network) self.assertEqual(address, address2) address2 = bech32address.p2wsh(redeem_script, network) self.assertEqual(address, address2) address2 = b32address_from_witness(0, payload, network) self.assertEqual(address, address2)
def test_p2wsh(): # https://github.com/bitcoin/bips/blob/master/bip-0173.mediawiki pub = "02 79BE667EF9DCBBAC55A06295CE870B07029BFCDB2DCE28D" "959F2815B16F81798" witness_script = [pub, "OP_CHECKSIG"] witness_script_bytes = encode(witness_script) addr = b"tb1qrp33g0q5c5txsp9arysrx4k6zdkfs4nce4xj0gdcccefvpysxf3q0sl5k7" assert addr == p2wsh(witness_script_bytes, "testnet") _, wp, _, _ = witness_from_b32address(addr) assert bytes(wp) == sha256(witness_script_bytes) addr = b"bc1qrp33g0q5c5txsp9arysrx4k6zdkfs4nce4xj0gdcccefvpysxf3qccfmv3" assert addr == p2wsh(witness_script_bytes) _, wp, _, _ = witness_from_b32address(addr) assert bytes(wp) == sha256(witness_script_bytes) assert witness_from_b32address(addr)[1] == sha256(witness_script_bytes) errMsg = r"witness program length \(35\) is not 20 or 32" with pytest.raises(ValueError, match=errMsg): b32address_from_witness(0, witness_script_bytes)
def test_p2wsh() -> None: # https://github.com/bitcoin/bips/blob/master/bip-0173.mediawiki pub = "02 79BE667EF9DCBBAC55A06295CE870B07029BFCDB2DCE28D959F2815B16F81798" script_pubkey: List[ScriptToken] = [pub, "OP_CHECKSIG"] witness_script_bytes = script.serialize(script_pubkey) addr = b"tb1qrp33g0q5c5txsp9arysrx4k6zdkfs4nce4xj0gdcccefvpysxf3q0sl5k7" assert addr == p2wsh(witness_script_bytes, "testnet") _, wp, _, _ = witness_from_b32address(addr) assert bytes(wp) == sha256(witness_script_bytes) addr = b"bc1qrp33g0q5c5txsp9arysrx4k6zdkfs4nce4xj0gdcccefvpysxf3qccfmv3" assert addr == p2wsh(witness_script_bytes) _, wp, _, _ = witness_from_b32address(addr) assert bytes(wp) == sha256(witness_script_bytes) assert witness_from_b32address(addr)[1] == sha256(witness_script_bytes) err_msg = "invalid witness program length for witness v0: " with pytest.raises(BTClibValueError, match=err_msg): b32address_from_witness(0, witness_script_bytes)
def test_p2wsh(self): # https://github.com/bitcoin/bips/blob/master/bip-0173.mediawiki pub = "02 79BE667EF9DCBBAC55A06295CE870B07029BFCDB2DCE28D959F2815B16F81798" witness_script = [pub, 'OP_CHECKSIG'] witness_script_bytes = encode(witness_script) addr = b'tb1qrp33g0q5c5txsp9arysrx4k6zdkfs4nce4xj0gdcccefvpysxf3q0sl5k7' self.assertEqual(addr, p2wsh(witness_script_bytes, 'testnet')) _, wp, _, _ = witness_from_b32address(addr) self.assertEqual(bytes(wp), sha256(witness_script_bytes)) addr = b'bc1qrp33g0q5c5txsp9arysrx4k6zdkfs4nce4xj0gdcccefvpysxf3qccfmv3' self.assertEqual(addr, p2wsh(witness_script_bytes)) _, wp, _, _ = witness_from_b32address(addr) self.assertEqual(bytes(wp), sha256(witness_script_bytes)) self.assertEqual( witness_from_b32address(addr)[1], sha256(witness_script_bytes)) # witness program length (35) is not 32 self.assertRaises(ValueError, b32address_from_witness, 0, witness_script_bytes[1:])
def test_p2wsh(self): script_type = "p2wsh" # self-consistency pubkey = "02" "cc71eb30d653c0c3163990c47b976f3fb3f37cccdcbedb169a1dfef58bbfbfaf" pubkey_hash = hash160(pubkey) redeem_script = scriptPubKey_from_payload("p2pkh", pubkey_hash) payload = sha256(redeem_script) script = encode([0, payload]) # straight to the scriptPubKey scriptPubKey = p2wsh(decode(redeem_script)) self.assertEqual(scriptPubKey.hex(), script.hex()) # to the scriptPubKey in two steps (through payload) scriptPubKey = scriptPubKey_from_payload(script_type, payload) self.assertEqual(scriptPubKey.hex(), script.hex()) # back from the scriptPubKey to the payload script_type2, payload2, m2 = payload_from_scriptPubKey(scriptPubKey) self.assertEqual(script_type, script_type2) self.assertEqual(0, m2) self.assertEqual(payload.hex(), payload2.hex()) script_type2, payload2, m2 = payload_from_scriptPubKey(script) self.assertEqual(script_type, script_type2) self.assertEqual(0, m2) self.assertEqual(payload.hex(), payload2.hex()) # data -> payload is not invertible (hash functions) # bech32 address network = "mainnet" address = bech32address.p2wsh(redeem_script, network) address2 = address_from_scriptPubKey(scriptPubKey, network) self.assertEqual(address, address2) address2 = b32address_from_witness(0, payload, network) self.assertEqual(address, address2) scriptPubKey2, network2 = scriptPubKey_from_address(address) self.assertEqual(scriptPubKey2, scriptPubKey) self.assertEqual(network2, network) # p2sh-wrapped base58 address address = base58address.p2wsh_p2sh(redeem_script, network) address2 = b58address_from_witness(payload, network) self.assertEqual(address, address2)