Skip to content

Commit 72a0b8f

Browse files
committed
refactor: move tapscript finalizer check; add unit test
1 parent 719e4a4 commit 72a0b8f

File tree

5 files changed

+18
-11
lines changed

5 files changed

+18
-11
lines changed

src/psbt.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,7 @@ declare type FinalScriptsFunc = (inputIndex: number, // Which input is it?
179179
input: PsbtInput, // The PSBT input contents
180180
script: Buffer, // The "meaningful" locking script Buffer (redeemScript for P2SH etc.)
181181
isSegwit: boolean, // Is it segwit?
182+
isTapscript: boolean, // Is taproot script path?
182183
isP2SH: boolean, // Is it P2SH?
183184
isP2WSH: boolean, // Is it P2WSH?
184185
eccLib?: TinySecp256k1Interface) => {

src/psbt.js

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -289,16 +289,13 @@ class Psbt {
289289
} = getScriptFromInput(inputIndex, input, this.__CACHE);
290290
if (!script) throw new Error(`No script found for input #${inputIndex}`);
291291
checkPartialSigSighashes(input);
292-
if (isTapscript && !finalScriptsFunc)
293-
throw new Error(
294-
`Taproot script-path finalizer required for input #${inputIndex}`,
295-
);
296292
const fn = finalScriptsFunc || getFinalScripts;
297293
const { finalScriptSig, finalScriptWitness } = fn(
298294
inputIndex,
299295
input,
300296
script,
301297
isSegwit,
298+
isTapscript,
302299
isP2SH,
303300
isP2WSH,
304301
this.__CACHE.__EC_LIB,
@@ -936,12 +933,13 @@ function getFinalScripts(
936933
input,
937934
script,
938935
isSegwit,
936+
isTapscript,
939937
isP2SH,
940938
isP2WSH,
941939
eccLib,
942940
) {
943941
const scriptType = classifyScript(script, eccLib);
944-
if (!canFinalize(input, script, scriptType))
942+
if (isTapscript || !canFinalize(input, script, scriptType))
945943
throw new Error(`Can not finalize input #${inputIndex}`);
946944
return prepareFinalScripts(
947945
script,

test/psbt.spec.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1003,6 +1003,15 @@ describe(`Psbt`, () => {
10031003
psbt.finalizeInput(0, tapscriptFinalizer);
10041004
assert.strictEqual(psbt.toBase64(), f.result);
10051005
});
1006+
1007+
it('Failes to finalize a taproot script-path spend when a finalizer is not provided', () => {
1008+
const f = fixtures.finalizeTaprootScriptPathSpendInput;
1009+
const psbt = Psbt.fromBase64(f.psbt, { eccLib: ecc });
1010+
1011+
assert.throws(() => {
1012+
psbt.finalizeInput(0);
1013+
}, new RegExp('Can not finalize input #0'));
1014+
});
10061015
});
10071016

10081017
describe('getFeeRate', () => {

test/psbt.utils.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ const buildTapscriptFinalizer = (
1717
input: PsbtInput,
1818
script: Buffer,
1919
_isSegwit: boolean,
20+
_isTapscript: boolean,
2021
_isP2SH: boolean,
2122
_isP2WSH: boolean,
2223
eccLib?: TinySecp256k1Interface,

ts_src/psbt.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -364,17 +364,13 @@ export class Psbt {
364364

365365
checkPartialSigSighashes(input);
366366

367-
if (isTapscript && !finalScriptsFunc)
368-
throw new Error(
369-
`Taproot script-path finalizer required for input #${inputIndex}`,
370-
);
371-
372367
const fn = finalScriptsFunc || getFinalScripts;
373368
const { finalScriptSig, finalScriptWitness } = fn(
374369
inputIndex,
375370
input,
376371
script,
377372
isSegwit,
373+
isTapscript,
378374
isP2SH,
379375
isP2WSH,
380376
this.__CACHE.__EC_LIB,
@@ -1224,6 +1220,7 @@ type FinalScriptsFunc = (
12241220
input: PsbtInput, // The PSBT input contents
12251221
script: Buffer, // The "meaningful" locking script Buffer (redeemScript for P2SH etc.)
12261222
isSegwit: boolean, // Is it segwit?
1223+
isTapscript: boolean, // Is taproot script path?
12271224
isP2SH: boolean, // Is it P2SH?
12281225
isP2WSH: boolean, // Is it P2WSH?
12291226
eccLib?: TinySecp256k1Interface, // optional lib for checking taproot validity
@@ -1237,6 +1234,7 @@ function getFinalScripts(
12371234
input: PsbtInput,
12381235
script: Buffer,
12391236
isSegwit: boolean,
1237+
isTapscript: boolean,
12401238
isP2SH: boolean,
12411239
isP2WSH: boolean,
12421240
eccLib?: TinySecp256k1Interface,
@@ -1245,7 +1243,7 @@ function getFinalScripts(
12451243
finalScriptWitness: Buffer | undefined;
12461244
} {
12471245
const scriptType = classifyScript(script, eccLib);
1248-
if (!canFinalize(input, script, scriptType))
1246+
if (isTapscript || !canFinalize(input, script, scriptType))
12491247
throw new Error(`Can not finalize input #${inputIndex}`);
12501248
return prepareFinalScripts(
12511249
script,

0 commit comments

Comments
 (0)