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_p2w_p2sh(self): pubkey = "03 a1af804ac108a8a51782198c2d034b28bf90c8803f5a53f76276fa69a4eae77f" h160pubkey, network = hash160_from_pubkey(pubkey) b58addr = p2wpkh_p2sh(pubkey, network) b58addr2 = b58address_from_witness(h160pubkey, network) self.assertEqual(b58addr2, b58addr) script = encode(['OP_DUP', 'OP_HASH160', h160pubkey, 'OP_EQUALVERIFY', 'OP_CHECKSIG']) h256script = hash256_from_script(script) b58addr = p2wsh_p2sh(script, network) b58addr2 = b58address_from_witness(h256script, network) self.assertEqual(b58addr2, b58addr) # Invalid witness program length (19) self.assertRaises(ValueError, b58address_from_witness, h256script[:-1], network)
def test_p2w_p2sh() -> None: pubkey = "03 a1af804ac108a8a51782198c2d034b28bf90c8803f5a53f76276fa69a4eae77f" h160pubkey, network = hash160_from_key(pubkey) b58addr = p2wpkh_p2sh(pubkey, network) b58addr2 = b58address_from_witness(h160pubkey, network) assert b58addr2 == b58addr scriptPubKey: List[ScriptToken] = [ "OP_DUP", "OP_HASH160", h160pubkey, "OP_EQUALVERIFY", "OP_CHECKSIG", ] h256script = hash256_from_script(scriptPubKey) b58addr = p2wsh_p2sh(scriptPubKey, network) b58addr2 = b58address_from_witness(h256script, network) assert b58addr2 == b58addr err_msg = "invalid witness program length for witness version zero: " with pytest.raises(ValueError, match=err_msg): b58address_from_witness(h256script[:-1], network)
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)