From 06b38a7d834cf2b07e81bd3375f5f29028f0bf45 Mon Sep 17 00:00:00 2001 From: junderw Date: Tue, 23 Jul 2019 15:51:12 +0900 Subject: [PATCH] Refactor: add function getPsigsFromInputFinalScripts --- src/psbt.js | 29 +++++++++++++++-------------- ts_src/psbt.ts | 30 ++++++++++++++++-------------- 2 files changed, 31 insertions(+), 28 deletions(-) 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;