Skip to content

Commit fb7df4a

Browse files
committed
feat: check for hash mismatch between the input hash and the computed hash from the taproot tree
1 parent d840ab6 commit fb7df4a

File tree

3 files changed

+24
-0
lines changed

3 files changed

+24
-0
lines changed

src/payments/p2tr.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,10 @@ function p2tr(a, opts) {
122122
if ((0, types_1.liftX)(pubkey) === null)
123123
throw new TypeError('Invalid pubkey for p2tr');
124124
}
125+
if (a.hash && a.scriptsTree) {
126+
const hash = (0, merkle_1.computeMastRoot)(a.scriptsTree);
127+
if (!a.hash.equals(hash)) throw new TypeError('Hash mismatch');
128+
}
125129
if (a.witness) {
126130
if (a.witness.length !== 1) throw new TypeError('Witness is invalid');
127131
// todo: recheck

test/fixtures/p2tr.json

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -375,6 +375,20 @@
375375
"pubkey": "ab610d22c801def8a1e02368d1b92018970eb52a729919705e8a1a2f60c750f5"
376376
}
377377
},
378+
{
379+
"description": "Hash mismatch between scriptsTree and hash",
380+
"exception": "Hash mismatch",
381+
"options": {},
382+
"arguments": {
383+
"internalPubkey": "9fa5ffb68821cf559001caa0577eeea4978b29416def328a707b15e91701a2f7",
384+
"scriptsTree": [
385+
{
386+
"output": "83d8ee77a0f3a32a5cea96fd1624d623b836c1e5d1ac2dcde46814b619320c18 OP_CHECKSIG"
387+
}
388+
],
389+
"hash": "b76077013c8e303085e300000000000000000000000000000000000000000000"
390+
}
391+
},
378392
{
379393
"exception": "Invalid internalPubkey for p2t",
380394
"options": {},

ts_src/payments/p2tr.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,12 @@ export function p2tr(a: Payment, opts?: PaymentOpts): Payment {
133133
throw new TypeError('Invalid pubkey for p2tr');
134134
}
135135

136+
if (a.hash && a.scriptsTree) {
137+
const hash = computeMastRoot(a.scriptsTree)
138+
if (!a.hash.equals(hash))
139+
throw new TypeError('Hash mismatch');
140+
}
141+
136142
if (a.witness) {
137143
if (a.witness.length !== 1) throw new TypeError('Witness is invalid');
138144

0 commit comments

Comments
 (0)