@@ -253,15 +253,7 @@ export class Psbt {
253
253
254
254
finalizeInput (
255
255
inputIndex : number ,
256
- {
257
- classifyScript : classifyScriptF ,
258
- canFinalize : canFinalizeF ,
259
- getFinalScripts : getFinalScriptsF ,
260
- } : IFinalizeFuncs = {
261
- classifyScript,
262
- canFinalize,
263
- getFinalScripts,
264
- } ,
256
+ finalScriptsFunc : FinalScriptsFunc = getFinalScripts ,
265
257
) : this {
266
258
const input = checkForInput ( this . data . inputs , inputIndex ) ;
267
259
const { script, isP2SH, isP2WSH, isSegwit } = getScriptFromInput (
@@ -271,16 +263,12 @@ export class Psbt {
271
263
) ;
272
264
if ( ! script ) throw new Error ( `No script found for input #${ inputIndex } ` ) ;
273
265
274
- const scriptType = classifyScriptF ( script ) ;
275
- if ( ! canFinalizeF ( input , script , scriptType ) )
276
- throw new Error ( `Can not finalize input #${ inputIndex } ` ) ;
277
-
278
266
checkPartialSigSighashes ( input ) ;
279
267
280
- const { finalScriptSig, finalScriptWitness } = getFinalScriptsF (
268
+ const { finalScriptSig, finalScriptWitness } = finalScriptsFunc (
269
+ inputIndex ,
270
+ input ,
281
271
script ,
282
- scriptType ,
283
- input . partialSig ! ,
284
272
isSegwit ,
285
273
isP2SH ,
286
274
isP2WSH ,
@@ -746,39 +734,6 @@ class PsbtTransaction implements ITransaction {
746
734
}
747
735
}
748
736
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
-
782
737
function canFinalize (
783
738
input : PsbtInput ,
784
739
script : Buffer ,
@@ -996,7 +951,49 @@ function getTxCacheValue(
996
951
else if ( key === '__FEE' ) return c . __FEE ! ;
997
952
}
998
953
954
+ /**
955
+ * This function must do two things:
956
+ * 1. Check if the `input` can be finalized. If it can not be finalized, throw.
957
+ * ie. `Can not finalize input #${inputIndex}`
958
+ * 2. Create the finalScriptSig and finalScriptWitness Buffers.
959
+ */
960
+ type FinalScriptsFunc = (
961
+ inputIndex : number , // Which input is it?
962
+ input : PsbtInput , // The PSBT input contents
963
+ script : Buffer , // The "meaningful" locking script Buffer (redeemScript for P2SH etc.)
964
+ isSegwit : boolean , // Is it segwit?
965
+ isP2SH : boolean , // Is it P2SH?
966
+ isP2WSH : boolean , // Is it P2WSH?
967
+ ) => {
968
+ finalScriptSig : Buffer | undefined ;
969
+ finalScriptWitness : Buffer | undefined ;
970
+ } ;
971
+
999
972
function getFinalScripts (
973
+ inputIndex : number ,
974
+ input : PsbtInput ,
975
+ script : Buffer ,
976
+ isSegwit : boolean ,
977
+ isP2SH : boolean ,
978
+ isP2WSH : boolean ,
979
+ ) : {
980
+ finalScriptSig : Buffer | undefined ;
981
+ finalScriptWitness : Buffer | undefined ;
982
+ } {
983
+ const scriptType = classifyScript ( script ) ;
984
+ if ( ! canFinalize ( input , script , scriptType ) )
985
+ throw new Error ( `Can not finalize input #${ inputIndex } ` ) ;
986
+ return prepareFinalScripts (
987
+ script ,
988
+ scriptType ,
989
+ input . partialSig ! ,
990
+ isSegwit ,
991
+ isP2SH ,
992
+ isP2WSH ,
993
+ ) ;
994
+ }
995
+
996
+ function prepareFinalScripts (
1000
997
script : Buffer ,
1001
998
scriptType : string ,
1002
999
partialSig : PartialSig [ ] ,
0 commit comments