Skip to content

Commit 5610d45

Browse files
committed
Add validation for redeem in scriptTree
1 parent 4dd4a51 commit 5610d45

File tree

3 files changed

+38
-6
lines changed

3 files changed

+38
-6
lines changed

src/payments/p2tr.js

+11-3
Original file line numberDiff line numberDiff line change
@@ -207,9 +207,17 @@ function p2tr(a, opts) {
207207
if (!_ecc().isXOnlyPoint(pubkey))
208208
throw new TypeError('Invalid pubkey for p2tr');
209209
}
210-
if (a.hash && a.scriptTree) {
211-
const hash = _hashTree().hash;
212-
if (!a.hash.equals(hash)) throw new TypeError('Hash mismatch');
210+
const hashTree = _hashTree();
211+
if (a.hash && hashTree) {
212+
if (!a.hash.equals(hashTree.hash)) throw new TypeError('Hash mismatch');
213+
}
214+
if (a.redeem && a.redeem.output && hashTree) {
215+
const leafHash = (0, taprootutils_1.tapleafHash)({
216+
output: a.redeem.output,
217+
version: o.redeemVersion,
218+
});
219+
if (!(0, taprootutils_1.findScriptPath)(hashTree, leafHash))
220+
throw new TypeError('Redeem script not in tree');
213221
}
214222
const witness = _witness();
215223
// compare the provided redeem data with the one computed from witness

test/fixtures/p2tr.json

+14
Original file line numberDiff line numberDiff line change
@@ -1175,6 +1175,20 @@
11751175
]
11761176
}
11771177
}
1178+
},
1179+
{
1180+
"description": "Redeem script not in tree",
1181+
"exception": "Redeem script not in tree",
1182+
"options": {},
1183+
"arguments": {
1184+
"internalPubkey": "9fa5ffb68821cf559001caa0577eeea4978b29416def328a707b15e91701a2f7",
1185+
"scriptTree": {
1186+
"output": "83d8ee77a0f3a32a5cea96fd1624d623b836c1e5d1ac2dcde46814b619320c18 OP_CHECKSIG"
1187+
},
1188+
"redeem": {
1189+
"output": "83d8ee77a0f3a32a5cea96fd1624d623b836c1e5d1ac2dcde46814b619320c19 OP_CHECKSIG"
1190+
}
1191+
}
11781192
}
11791193
],
11801194
"dynamic": {

ts_src/payments/p2tr.ts

+13-3
Original file line numberDiff line numberDiff line change
@@ -231,9 +231,19 @@ export function p2tr(a: Payment, opts?: PaymentOpts): Payment {
231231
throw new TypeError('Invalid pubkey for p2tr');
232232
}
233233

234-
if (a.hash && a.scriptTree) {
235-
const hash = _hashTree()!.hash;
236-
if (!a.hash.equals(hash)) throw new TypeError('Hash mismatch');
234+
const hashTree = _hashTree();
235+
236+
if (a.hash && hashTree) {
237+
if (!a.hash.equals(hashTree.hash)) throw new TypeError('Hash mismatch');
238+
}
239+
240+
if (a.redeem && a.redeem.output && hashTree) {
241+
const leafHash = tapleafHash({
242+
output: a.redeem.output,
243+
version: o.redeemVersion,
244+
});
245+
if (!findScriptPath(hashTree, leafHash))
246+
throw new TypeError('Redeem script not in tree');
237247
}
238248

239249
const witness = _witness();

0 commit comments

Comments
 (0)