diff --git a/src/psbt.js b/src/psbt.js index 0898bc6..051de6e 100644 --- a/src/psbt.js +++ b/src/psbt.js @@ -629,20 +629,7 @@ function checkInputsForPartialSig(inputs, action) { let pSigs = []; if ((input.partialSig || []).length === 0) { if (!input.finalScriptSig && !input.finalScriptWitness) return; - const scriptItems = !input.finalScriptSig - ? [] - : bscript.decompile(input.finalScriptSig) || []; - const witnessItems = !input.finalScriptWitness - ? [] - : bscript.decompile(input.finalScriptWitness) || []; - pSigs = scriptItems - .concat(witnessItems) - .filter(item => { - return ( - Buffer.isBuffer(item) && bscript.isCanonicalScriptSignature(item) - ); - }) - .map(sig => ({ signature: sig })); + pSigs = getPsigsFromInputFinalScripts(input); } else { pSigs = input.partialSig; } @@ -918,6 +905,20 @@ function getPayment(script, scriptType, partialSig) { } return payment; } +function getPsigsFromInputFinalScripts(input) { + const scriptItems = !input.finalScriptSig + ? [] + : bscript.decompile(input.finalScriptSig) || []; + const witnessItems = !input.finalScriptWitness + ? [] + : bscript.decompile(input.finalScriptWitness) || []; + return scriptItems + .concat(witnessItems) + .filter(item => { + return Buffer.isBuffer(item) && bscript.isCanonicalScriptSignature(item); + }) + .map(sig => ({ signature: sig })); +} function getScriptFromInput(inputIndex, input, cache) { const unsignedTx = cache.__TX; const res = { diff --git a/ts_src/psbt.ts b/ts_src/psbt.ts index 707ca21..eb09155 100644 --- a/ts_src/psbt.ts +++ b/ts_src/psbt.ts @@ -798,20 +798,7 @@ function checkInputsForPartialSig(inputs: PsbtInput[], action: string): void { let pSigs: PartialSig[] = []; if ((input.partialSig || []).length === 0) { if (!input.finalScriptSig && !input.finalScriptWitness) return; - const scriptItems = !input.finalScriptSig - ? [] - : bscript.decompile(input.finalScriptSig) || []; - const witnessItems = !input.finalScriptWitness - ? [] - : bscript.decompile(input.finalScriptWitness) || []; - pSigs = scriptItems - .concat(witnessItems) - .filter(item => { - return ( - Buffer.isBuffer(item) && bscript.isCanonicalScriptSignature(item) - ); - }) - .map(sig => ({ signature: sig })) as PartialSig[]; + pSigs = getPsigsFromInputFinalScripts(input); } else { pSigs = input.partialSig!; } @@ -1140,6 +1127,21 @@ function getPayment( return payment!; } +function getPsigsFromInputFinalScripts(input: PsbtInput): PartialSig[] { + const scriptItems = !input.finalScriptSig + ? [] + : bscript.decompile(input.finalScriptSig) || []; + const witnessItems = !input.finalScriptWitness + ? [] + : bscript.decompile(input.finalScriptWitness) || []; + return scriptItems + .concat(witnessItems) + .filter(item => { + return Buffer.isBuffer(item) && bscript.isCanonicalScriptSignature(item); + }) + .map(sig => ({ signature: sig })) as PartialSig[]; +} + interface GetScriptReturn { script: Buffer | null; isSegwit: boolean;