def test_p2wsh_p2sh() -> None: # leading/trailing spaces should be tolerated pub = " 02 79BE667EF9DCBBAC55A06295CE870B07029BFCDB2DCE28D959F2815B16F81798" script_pub_key: List[Command] = [pub, "OP_CHECKSIG"] witness_script_bytes = serialize(script_pub_key) b58.p2wsh_p2sh(witness_script_bytes) b58.p2wsh_p2sh(witness_script_bytes, "testnet")
def test_p2wsh() -> None: # self-consistency pub_key = "02 cc71eb30d653c0c3163990c47b976f3fb3f37cccdcbedb169a1dfef58bbfbfaf" redeem_script = ScriptPubKey.p2pkh(pub_key).script payload = sha256(redeem_script) script_pub_key = serialize(["OP_0", payload]) assert_p2wsh(script_pub_key) assert script_pub_key == ScriptPubKey.p2wsh(redeem_script).script assert ("p2wsh", payload) == type_and_payload(script_pub_key) # bech32 address network = "mainnet" addr = b32.p2wsh(redeem_script, network) assert addr == address(script_pub_key, network) assert addr == b32.address_from_witness(0, payload, network) # back from the address to the script_pub_key assert script_pub_key == ScriptPubKey.from_address(addr).script assert network == ScriptPubKey.from_address(addr).network # p2sh-wrapped base58 address addr = b58.p2wsh_p2sh(redeem_script, network) assert addr == "39GUePMSQ4mADpihVLd8cFQ2tih9Fy4qkz" err_msg = "invalid witness version: " with pytest.raises(BTClibValueError, match=err_msg): assert_p2wsh(b"\x33" + script_pub_key[1:]) err_msg = "invalid redeem script hash length marker: " with pytest.raises(BTClibValueError, match=err_msg): assert_p2wsh(script_pub_key[:1] + b"\x00" + script_pub_key[2:])
def test_p2w_p2sh() -> None: pub_key_str = "03 a1af804ac108a8a51782198c2d034b28bf90c8803f5a53f76276fa69a4eae77f" pub_key, network = pub_keyinfo_from_key(pub_key_str, compressed=True) witness_program = hash160(pub_key) b58addr = b58.p2wpkh_p2sh(pub_key, network) assert b58addr == "36NvZTcMsMowbt78wPzJaHHWaNiyR73Y4g" script_pub_key = serialize([ "OP_DUP", "OP_HASH160", witness_program, "OP_EQUALVERIFY", "OP_CHECKSIG" ]) b58addr = b58.p2wsh_p2sh(script_pub_key, network) assert b58addr == "3QHRam4Hvp1GZVkgjoKWUC1GEd8ck8e4WX"
def test_p2w_p2sh() -> None: pub_key = "03 a1af804ac108a8a51782198c2d034b28bf90c8803f5a53f76276fa69a4eae77f" witness_program, network = hash160_from_key(pub_key) b58addr = b58.p2wpkh_p2sh(pub_key, network) assert b58addr == b58.address_from_v0_witness(witness_program, network) script_pub_key = serialize([ "OP_DUP", "OP_HASH160", witness_program, "OP_EQUALVERIFY", "OP_CHECKSIG" ]) witness_program = sha256(script_pub_key) b58addr = b58.p2wsh_p2sh(script_pub_key, network) assert b58addr == b58.address_from_v0_witness(witness_program, network) err_msg = "invalid size: " with pytest.raises(BTClibValueError, match=err_msg): b58.address_from_v0_witness(witness_program[:-1], network)