Skip to content

Commit 4b5a519

Browse files
committed
Use single func instead of 3
1 parent 22682fc commit 4b5a519

File tree

3 files changed

+78
-78
lines changed

3 files changed

+78
-78
lines changed

src/psbt.js

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -204,33 +204,19 @@ class Psbt {
204204
range(this.data.inputs.length).forEach(idx => this.finalizeInput(idx));
205205
return this;
206206
}
207-
finalizeInput(
208-
inputIndex,
209-
{
210-
classifyScript: classifyScriptF,
211-
canFinalize: canFinalizeF,
212-
getFinalScripts: getFinalScriptsF,
213-
} = {
214-
classifyScript,
215-
canFinalize,
216-
getFinalScripts,
217-
},
218-
) {
207+
finalizeInput(inputIndex, finalScriptsFunc = getFinalScripts) {
219208
const input = utils_1.checkForInput(this.data.inputs, inputIndex);
220209
const { script, isP2SH, isP2WSH, isSegwit } = getScriptFromInput(
221210
inputIndex,
222211
input,
223212
this.__CACHE,
224213
);
225214
if (!script) throw new Error(`No script found for input #${inputIndex}`);
226-
const scriptType = classifyScriptF(script);
227-
if (!canFinalizeF(input, script, scriptType))
228-
throw new Error(`Can not finalize input #${inputIndex}`);
229215
checkPartialSigSighashes(input);
230-
const { finalScriptSig, finalScriptWitness } = getFinalScriptsF(
216+
const { finalScriptSig, finalScriptWitness } = finalScriptsFunc(
217+
inputIndex,
218+
input,
231219
script,
232-
scriptType,
233-
input.partialSig,
234220
isSegwit,
235221
isP2SH,
236222
isP2WSH,
@@ -749,7 +735,20 @@ function getTxCacheValue(key, name, inputs, c) {
749735
if (key === '__FEE_RATE') return c.__FEE_RATE;
750736
else if (key === '__FEE') return c.__FEE;
751737
}
752-
function getFinalScripts(
738+
function getFinalScripts(inputIndex, input, script, isSegwit, isP2SH, isP2WSH) {
739+
const scriptType = classifyScript(script);
740+
if (!canFinalize(input, script, scriptType))
741+
throw new Error(`Can not finalize input #${inputIndex}`);
742+
return prepareFinalScripts(
743+
script,
744+
scriptType,
745+
input.partialSig,
746+
isSegwit,
747+
isP2SH,
748+
isP2WSH,
749+
);
750+
}
751+
function prepareFinalScripts(
753752
script,
754753
scriptType,
755754
partialSig,

ts_src/psbt.ts

Lines changed: 46 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -253,15 +253,7 @@ export class Psbt {
253253

254254
finalizeInput(
255255
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,
265257
): this {
266258
const input = checkForInput(this.data.inputs, inputIndex);
267259
const { script, isP2SH, isP2WSH, isSegwit } = getScriptFromInput(
@@ -271,16 +263,12 @@ export class Psbt {
271263
);
272264
if (!script) throw new Error(`No script found for input #${inputIndex}`);
273265

274-
const scriptType = classifyScriptF(script);
275-
if (!canFinalizeF(input, script, scriptType))
276-
throw new Error(`Can not finalize input #${inputIndex}`);
277-
278266
checkPartialSigSighashes(input);
279267

280-
const { finalScriptSig, finalScriptWitness } = getFinalScriptsF(
268+
const { finalScriptSig, finalScriptWitness } = finalScriptsFunc(
269+
inputIndex,
270+
input,
281271
script,
282-
scriptType,
283-
input.partialSig!,
284272
isSegwit,
285273
isP2SH,
286274
isP2WSH,
@@ -746,39 +734,6 @@ class PsbtTransaction implements ITransaction {
746734
}
747735
}
748736

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-
782737
function canFinalize(
783738
input: PsbtInput,
784739
script: Buffer,
@@ -996,7 +951,49 @@ function getTxCacheValue(
996951
else if (key === '__FEE') return c.__FEE!;
997952
}
998953

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+
999972
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(
1000997
script: Buffer,
1001998
scriptType: string,
1002999
partialSig: PartialSig[],

types/psbt.d.ts

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Psbt as PsbtBase } from 'bip174';
2-
import { KeyValue, PartialSig, PsbtGlobalUpdate, PsbtInput, PsbtInputUpdate, PsbtOutput, PsbtOutputUpdate, TransactionInput } from 'bip174/src/lib/interfaces';
2+
import { KeyValue, PsbtGlobalUpdate, PsbtInput, PsbtInputUpdate, PsbtOutput, PsbtOutputUpdate, TransactionInput } from 'bip174/src/lib/interfaces';
33
import { Signer, SignerAsync } from './ecpair';
44
import { Network } from './networks';
55
import { Transaction } from './transaction';
@@ -58,7 +58,7 @@ export declare class Psbt {
5858
getFeeRate(): number;
5959
getFee(): number;
6060
finalizeAllInputs(): this;
61-
finalizeInput(inputIndex: number, { classifyScript: classifyScriptF, canFinalize: canFinalizeF, getFinalScripts: getFinalScriptsF, }?: IFinalizeFuncs): this;
61+
finalizeInput(inputIndex: number, finalScriptsFunc?: FinalScriptsFunc): this;
6262
validateSignaturesOfAllInputs(): boolean;
6363
validateSignaturesOfInput(inputIndex: number, pubkey?: Buffer): boolean;
6464
signAllInputsHD(hdKeyPair: HDSigner, sighashTypes?: number[]): this;
@@ -124,14 +124,18 @@ interface HDSignerAsync extends HDSignerBase {
124124
derivePath(path: string): HDSignerAsync;
125125
sign(hash: Buffer): Promise<Buffer>;
126126
}
127-
interface IFinalizeFuncs {
128-
classifyScript: FinalizeFuncClassifyScript;
129-
canFinalize: FinalizeFuncCanFinalize;
130-
getFinalScripts: FinalizeFuncGetFinalScripts;
131-
}
132-
declare type FinalizeFuncClassifyScript = (script: Buffer) => string;
133-
declare type FinalizeFuncCanFinalize = (input: PsbtInput, script: Buffer, scriptType: string) => boolean;
134-
declare type FinalizeFuncGetFinalScripts = (script: Buffer, scriptType: string, partialSig: PartialSig[], isSegwit: boolean, isP2SH: boolean, isP2WSH: boolean) => {
127+
/**
128+
* This function must do two things:
129+
* 1. Check if the `input` can be finalized. If it can not be finalized, throw.
130+
* ie. `Can not finalize input #${inputIndex}`
131+
* 2. Create the finalScriptSig and finalScriptWitness Buffers.
132+
*/
133+
declare type FinalScriptsFunc = (inputIndex: number, // Which input is it?
134+
input: PsbtInput, // The PSBT input contents
135+
script: Buffer, // The "meaningful" locking script Buffer (redeemScript for P2SH etc.)
136+
isSegwit: boolean, // Is it segwit?
137+
isP2SH: boolean, // Is it P2SH?
138+
isP2WSH: boolean) => {
135139
finalScriptSig: Buffer | undefined;
136140
finalScriptWitness: Buffer | undefined;
137141
};

0 commit comments

Comments
 (0)