def test_non_standard_script_in_p2wsh() -> None: network = "mainnet" fed_pub_keys: List[Command] = ["00" * 33, "11" * 33, "22" * 33] rec_pub_keys: List[Command] = ["77" * 33, "88" * 33, "99" * 33] # fmt: off redeem_script_cmds: List[Command] = [ "OP_IF", "OP_2", *fed_pub_keys, "OP_3", "OP_CHECKMULTISIG", # noqa E131 "OP_ELSE", 500, "OP_CHECKLOCKTIMEVERIFY", "OP_DROP", # noqa E131 "OP_2", *rec_pub_keys, "OP_3", "OP_CHECKMULTISIG", # noqa E131 "OP_ENDIF", ] # fmt: on redeem_script = serialize(redeem_script_cmds) assert redeem_script_cmds == parse(redeem_script) payload = sha256(redeem_script) script_pub_key = ( "00207b5310339c6001f75614daa5083839fa54d46165f6c56025cc54d397a85a5708") assert script_pub_key == ScriptPubKey.p2wsh(redeem_script).script.hex() addr = "bc1q0df3qvuuvqqlw4s5m2jsswpelf2dgct97mzkqfwv2nfe02z62uyq7n4zjj" assert addr == address(script_pub_key, network) assert addr == b32.address_from_witness(0, payload, network)
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:])