def test_control_block() -> None: script_tree = [[(0xC0, ["OP_2"])], [(0xC0, ["OP_3"])]] pubkey = output_pubkey(None, script_tree)[0] script, control = input_script_sig(None, script_tree, 0) assert check_output_pubkey(pubkey, serialize(script), control) prvkey = 123456 internal_pubkey = mult(prvkey) script_tree = [[(0xC0, ["OP_2"])], [(0xC0, ["OP_3"])]] pubkey = output_pubkey(internal_pubkey, script_tree)[0] script, control = input_script_sig(internal_pubkey, script_tree, 0) assert check_output_pubkey(pubkey, serialize(script), control)
def test_p2tr() -> None: key = 1 pubkey = output_pubkey(key)[0] addr = b32.p2tr(key) _, wit_prg, _ = b32.witness_from_address(addr) assert wit_prg == pubkey
def p2tr( internal_key: Optional[Key] = None, script_path: Optional[TaprootScriptTree] = None, network: str = "mainnet", ): "Return the p2tr bech32 address corresponding to a taproot output key." pub_key = output_pubkey(internal_key, script_path)[0] return address_from_witness(1, pub_key, network)
def p2tr( cls: Type["ScriptPubKey"], internal_key: Optional[Key] = None, script_path: Optional[TaprootScriptTree] = None, network: str = "mainnet", check_validity: bool = True, ) -> "ScriptPubKey": "Return the p2tr ScriptPubKey of the provided script tree." pub_key = output_pubkey(internal_key, script_path)[0] script = serialize(["OP_1", pub_key]) return cls(script, network, check_validity)
def test_taproot_key_tweaking() -> None: prvkey = 123456 pubkey = mult(prvkey) script_trees = [ None, [(0xC0, ["OP_1"])], [[(0xC0, ["OP_2"])], [(0xC0, ["OP_3"])]], ] for script_tree in script_trees: tweaked_prvkey = output_prvkey(prvkey, script_tree) tweaked_pubkey = output_pubkey(pubkey, script_tree)[0] assert tweaked_pubkey == mult(tweaked_prvkey)[0].to_bytes(32, "big")
def test_bip_test_vector(): fname = "taproot_test_vector.json" filename = path.join(path.dirname(__file__), "_data", fname) with open(filename, "r", encoding="ascii") as file_: data = json.load(file_)["scriptPubKey"] for test in data: pubkey = test["given"]["internalPubkey"] script_tree = convert_script_tree(test["given"]["scriptTree"]) tweaked_pubkey = output_pubkey("02" + pubkey, script_tree)[0] address = b32.p2tr("02" + pubkey, script_tree) assert tweaked_pubkey.hex() == test["intermediary"]["tweakedPubkey"] assert address == test["expected"]["bip350Address"]
def test_p2tr() -> None: pub_key = "cc71eb30d653c0c3163990c47b976f3fb3f37cccdcbedb169a1dfef58bbfbfaf" payload = output_pubkey(pub_key)[0] script_pub_key = serialize(["OP_1", payload]) assert_p2tr(script_pub_key) assert ("p2tr", payload) == type_and_payload(script_pub_key) network = "mainnet" addr = b32.p2tr(pub_key, network=network) assert addr == address(script_pub_key, network) assert script_pub_key == ScriptPubKey.from_address(addr).script assert script_pub_key == ScriptPubKey.p2tr(pub_key).script err_msg = "invalid redeem script hash length marker: " with pytest.raises(BTClibValueError, match=err_msg): assert_p2tr(script_pub_key[:1] + b"\x00" + script_pub_key[2:])
def test_unspendable_script() -> None: err_msg = "Missing data" with pytest.raises(BTClibValueError, match=err_msg): output_pubkey()