@@ -251,7 +251,18 @@ export class Psbt {
251
251
return this ;
252
252
}
253
253
254
- finalizeInput ( inputIndex : number ) : this {
254
+ finalizeInput (
255
+ inputIndex : number ,
256
+ {
257
+ classifyScript : classifyScriptF ,
258
+ canFinalize : canFinalizeF ,
259
+ getFinalScripts : getFinalScriptsF ,
260
+ } : IFinalizeFuncs = {
261
+ classifyScript,
262
+ canFinalize,
263
+ getFinalScripts,
264
+ } ,
265
+ ) : this {
255
266
const input = checkForInput ( this . data . inputs , inputIndex ) ;
256
267
const { script, isP2SH, isP2WSH, isSegwit } = getScriptFromInput (
257
268
inputIndex ,
@@ -260,13 +271,13 @@ export class Psbt {
260
271
) ;
261
272
if ( ! script ) throw new Error ( `No script found for input #${ inputIndex } ` ) ;
262
273
263
- const scriptType = classifyScript ( script ) ;
264
- if ( ! canFinalize ( input , script , scriptType ) )
274
+ const scriptType = classifyScriptF ( script ) ;
275
+ if ( ! canFinalizeF ( input , script , scriptType ) )
265
276
throw new Error ( `Can not finalize input #${ inputIndex } ` ) ;
266
277
267
278
checkPartialSigSighashes ( input ) ;
268
279
269
- const { finalScriptSig, finalScriptWitness } = getFinalScripts (
280
+ const { finalScriptSig, finalScriptWitness } = getFinalScriptsF (
270
281
script ,
271
282
scriptType ,
272
283
input . partialSig ! ,
@@ -735,6 +746,39 @@ class PsbtTransaction implements ITransaction {
735
746
}
736
747
}
737
748
749
+ // This interface is added to allow for custom scripts to be finalized with PSBT.
750
+ interface IFinalizeFuncs {
751
+ classifyScript : FinalizeFuncClassifyScript ;
752
+ canFinalize : FinalizeFuncCanFinalize ;
753
+ getFinalScripts : FinalizeFuncGetFinalScripts ;
754
+ }
755
+
756
+ // Takes the meaningful script (redeemScript for P2SH and witnessScript for P2WSH)
757
+ // and returns a string to classify the script.
758
+ type FinalizeFuncClassifyScript = ( script : Buffer ) => string ;
759
+ // Takes the Psbt data for the input and the meaningful script and its type name.
760
+ // returns true if we can finalize the input
761
+ type FinalizeFuncCanFinalize = (
762
+ input : PsbtInput ,
763
+ script : Buffer ,
764
+ scriptType : string ,
765
+ ) => boolean ;
766
+ // Takes the meaningful script, its type name, all the signatures from this input,
767
+ // and 3 booleans to tell you if it is segwit, P2SH, and P2WSH.
768
+ // it returns finalScriptSig and finalScriptWitness to be placed in the Psbt.
769
+ // if one is not needed, it should be undefined. (In TypeScript, it must be declared but undefined.)
770
+ type FinalizeFuncGetFinalScripts = (
771
+ script : Buffer ,
772
+ scriptType : string ,
773
+ partialSig : PartialSig [ ] ,
774
+ isSegwit : boolean ,
775
+ isP2SH : boolean ,
776
+ isP2WSH : boolean ,
777
+ ) => {
778
+ finalScriptSig : Buffer | undefined ;
779
+ finalScriptWitness : Buffer | undefined ;
780
+ } ;
781
+
738
782
function canFinalize (
739
783
input : PsbtInput ,
740
784
script : Buffer ,
0 commit comments