Skip to content

Commit eb28539

Browse files
committed
Added example case for taproot key path spending with identical signature
1 parent d482ad8 commit eb28539

1 file changed

Lines changed: 102 additions & 0 deletions

File tree

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
# Copyright (C) 2018-2024 The python-bitcoin-utils developers
2+
#
3+
# This file is part of python-bitcoin-utils
4+
#
5+
# It is subject to the license terms in the LICENSE file found in the top-level
6+
# directory of this distribution.
7+
#
8+
# No part of python-bitcoin-utils, including this file, may be copied,
9+
# modified, propagated, or distributed except according to the terms contained
10+
# in the LICENSE file.
11+
12+
from binascii import unhexlify
13+
from bitcoinutils.setup import setup
14+
from bitcoinutils.utils import to_satoshis
15+
from bitcoinutils.transactions import Transaction, TxInput, TxOutput, TxWitnessInput
16+
from bitcoinutils.keys import P2pkhAddress, PrivateKey, P2trAddress
17+
from bitcoinutils.script import Script
18+
19+
20+
def main():
21+
try:
22+
# always remember to setup the network
23+
setup("testnet")
24+
25+
# the key that corresponds to the P2WPKH address
26+
#priv = PrivateKey("cPiTDzdpEfCwotToidTcNjh4WsdTZRLkYYScs1vHXT4Dzncwasdo")
27+
priv = PrivateKey(b=unhexlify("3fa9909fad4c01625adc47639626877073d94ead84e9630a66b731776cb66fcf"))
28+
print(f"internal priv key: {priv.to_bytes().hex()}")
29+
30+
pub = priv.get_public_key()
31+
32+
fromAddress = pub.get_taproot_address()
33+
print(fromAddress.to_string())
34+
35+
# UTXO of fromAddress
36+
txid = "bf0dc482d03fd2b57fb97cefd84de42f0bf370930856785e8ec9c019f1fb3aca"
37+
vout = 0
38+
39+
# all amounts are needed to sign a taproot input
40+
# (depending on sighash)
41+
first_amount = to_satoshis(0.00385004)
42+
amounts = [first_amount]
43+
44+
# all scriptPubKeys are needed to sign a taproot input
45+
# (depending on sighash) but always of the spend input
46+
first_script_pubkey = fromAddress.to_script_pub_key()
47+
48+
script_pubkey02 = Script(["OP_1", pub.to_taproot_hex()[0]])
49+
print(f"first_script_pubkey: {first_script_pubkey.to_hex()}")
50+
print(f"script_pubkey02: {script_pubkey02.to_hex()}")
51+
52+
# alternatively:
53+
# first_script_pubkey = Script(['OP_1', pub.to_taproot_hex()])
54+
55+
utxos_script_pubkeys = [first_script_pubkey]
56+
57+
# create transaction input from tx id of UTXO
58+
seq = "fdffffff"
59+
txin = TxInput(txid, vout, sequence=seq)
60+
61+
toAddress1 = P2trAddress("tb1pz38c8e54f6fkfpetmw8j7ft0ft54yeqy7cvzyckwxgjany8vv7vssddyfa")
62+
toAddress2 = P2trAddress("tb1pdtpkgjvkc77nuzssqh420ulgu3xm88m5vy59wrmgg5ctklkandgqmyh9ah")
63+
64+
# create transaction output
65+
txOut1 = TxOutput(to_satoshis(0.00364261), toAddress1.to_script_pub_key())
66+
txOut2 = TxOutput(to_satoshis(0.00005000 ), toAddress2.to_script_pub_key())
67+
# create transaction without change output - if at least a single input is
68+
# segwit we need to set has_segwit=True
69+
70+
locktime = "a8643400"
71+
tx = Transaction([txin], [txOut1, txOut2], has_segwit=True, locktime=locktime)
72+
73+
print("\nRaw transaction:\n" + tx.serialize())
74+
75+
print("\ntxid: " + tx.get_txid())
76+
print("\ntxwid: " + tx.get_wtxid())
77+
78+
# sign taproot input
79+
# to create the digest message to sign in taproot we need to
80+
# pass all the utxos' scriptPubKeys and their amounts
81+
sig = priv.sign_taproot_input(tx, 0, utxos_script_pubkeys, amounts, rand_aux=bytes(32))
82+
# print(sig)
83+
84+
tx.witnesses.append(TxWitnessInput([sig]))
85+
86+
# print raw signed transaction ready to be broadcasted
87+
print("\nRaw signed transaction:\n" + tx.serialize())
88+
89+
print("\nTxId:", tx.get_txid())
90+
print("\nTxwId:", tx.get_wtxid())
91+
92+
print("\nSize:", tx.get_size())
93+
print("\nvSize:", tx.get_vsize())
94+
except Exception as e:
95+
print('Something went wrong', e)
96+
finally:
97+
print('The try except is finished')
98+
99+
100+
101+
if __name__ == "__main__":
102+
main()

0 commit comments

Comments
 (0)