-
Notifications
You must be signed in to change notification settings - Fork 148
Expand file tree
/
Copy pathtest_taproot_annex.py
More file actions
159 lines (134 loc) · 5.21 KB
/
test_taproot_annex.py
File metadata and controls
159 lines (134 loc) · 5.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
import unittest
import hashlib
import os
import binascii
from bitcoinutils.transactions import Transaction, TxInput, TxOutput
from bitcoinutils.script import Script
from bitcoinutils.constants import LEAF_VERSION_TAPSCRIPT, TAPROOT_SIGHASH_ALL
from bitcoinutils.utils import h_to_b, b_to_h
class TestSignatureHashAnnex(unittest.TestCase):
"""Test cases for signature hash annex functionality."""
def setUp(self):
# Create a simple transaction for testing
self.txin = TxInput(
"0" * 64, # Dummy txid
0, # Dummy index
)
self.txout = TxOutput(
10000, # 0.0001 BTC in satoshis
Script(["OP_1"]) # Dummy script
)
self.tx = Transaction(
[self.txin],
[self.txout],
has_segwit=True
)
# Create some dummy scripts and amounts for the tests
self.script_pubkeys = [Script(["OP_1"])]
self.amounts = [10000]
def test_taproot_digest_with_annex(self):
"""Test that adding an annex changes the signature hash."""
# Get digest without annex
digest_without_annex = self.tx.get_transaction_taproot_digest(
txin_index=0,
script_pubkeys=self.script_pubkeys,
amounts=self.amounts,
sighash=TAPROOT_SIGHASH_ALL
)
# Get digest with annex
test_annex = h_to_b("aabbccdd") # Simple test annex
digest_with_annex = self.tx.get_transaction_taproot_digest(
txin_index=0,
script_pubkeys=self.script_pubkeys,
amounts=self.amounts,
sighash=TAPROOT_SIGHASH_ALL,
annex=test_annex
)
# The digests should be different when an annex is provided
self.assertNotEqual(
digest_without_annex,
digest_with_annex,
"Signature hash should change when annex is provided"
)
def test_taproot_digest_different_annexes(self):
"""Test that different annexes produce different digests."""
# Get digest with first annex
first_annex = h_to_b("aabbccdd")
digest_with_first_annex = self.tx.get_transaction_taproot_digest(
txin_index=0,
script_pubkeys=self.script_pubkeys,
amounts=self.amounts,
sighash=TAPROOT_SIGHASH_ALL,
annex=first_annex
)
# Get digest with second annex
second_annex = h_to_b("11223344")
digest_with_second_annex = self.tx.get_transaction_taproot_digest(
txin_index=0,
script_pubkeys=self.script_pubkeys,
amounts=self.amounts,
sighash=TAPROOT_SIGHASH_ALL,
annex=second_annex
)
# Different annexes should produce different digests
self.assertNotEqual(
digest_with_first_annex,
digest_with_second_annex,
"Different annexes should produce different digests"
)
def test_taproot_digest_script_path_with_annex(self):
"""Test annex support with script path spending."""
# Get digest with script path without annex
digest_without_annex = self.tx.get_transaction_taproot_digest(
txin_index=0,
script_pubkeys=self.script_pubkeys,
amounts=self.amounts,
ext_flag=1, # Script path
script=Script(["OP_TRUE"]),
leaf_ver=LEAF_VERSION_TAPSCRIPT,
sighash=TAPROOT_SIGHASH_ALL
)
# Get digest with script path with annex
test_annex = h_to_b("ffee")
digest_with_annex = self.tx.get_transaction_taproot_digest(
txin_index=0,
script_pubkeys=self.script_pubkeys,
amounts=self.amounts,
ext_flag=1, # Script path
script=Script(["OP_TRUE"]),
leaf_ver=LEAF_VERSION_TAPSCRIPT,
sighash=TAPROOT_SIGHASH_ALL,
annex=test_annex
)
# The digests should be different
self.assertNotEqual(
digest_without_annex,
digest_with_annex,
"Signature hash should change when annex is provided in script path"
)
def test_empty_annex(self):
"""Test that an empty annex is handled properly."""
# Get digest without annex
digest_without_annex = self.tx.get_transaction_taproot_digest(
txin_index=0,
script_pubkeys=self.script_pubkeys,
amounts=self.amounts,
sighash=TAPROOT_SIGHASH_ALL
)
# Get digest with empty annex
empty_annex = b""
digest_with_empty_annex = self.tx.get_transaction_taproot_digest(
txin_index=0,
script_pubkeys=self.script_pubkeys,
amounts=self.amounts,
sighash=TAPROOT_SIGHASH_ALL,
annex=empty_annex
)
# Even an empty annex should change the digest
self.assertNotEqual(
digest_without_annex,
digest_with_empty_annex,
"Signature hash should change even with empty annex"
)
if __name__ == "__main__":
unittest.main()