diff --git a/src/transaction_builder.js b/src/transaction_builder.js index 45d8022..1c79de7 100644 --- a/src/transaction_builder.js +++ b/src/transaction_builder.js @@ -14,6 +14,14 @@ var ECPair = require('./ecpair') var ECSignature = require('./ecsignature') var Transaction = require('./transaction') +function supportedType (type) { + return SIGNABLE.indexOf(type) !== -1 +} + +function supportedP2SHType (type) { + return P2SH.indexOf(type) !== -1 +} + function extractChunks (type, chunks, script) { var pubKeys = [] var signatures = [] @@ -82,7 +90,7 @@ function expandInput (scriptSig, witnessStack) { if (scriptSig.length === 0) { prevOutScript = bscript.witnessScriptHash.output.encode(bcrypto.sha256(witnessScript)) prevOutType = scriptTypes.P2WSH - if (typeof redeemScript !== 'undefined') { + if (redeemScript !== undefined) { throw new Error('Redeem script given when unnecessary') } // bare witness @@ -96,9 +104,10 @@ function expandInput (scriptSig, witnessStack) { } } - if (SIGNABLE.indexOf(bscript.classifyOutput(witnessScript)) === -1) { + if (!supportedType(bscript.classifyOutput(witnessScript))) { throw new Error('unsupported witness script') } + script = witnessScript scriptType = witnessScriptType chunks = witnessStack.slice(0, -1) @@ -124,7 +133,7 @@ function expandInput (scriptSig, witnessStack) { scriptType = scriptTypes.P2PKH chunks = witnessStack } else if (redeemScript) { - if (P2SH.indexOf(redeemScriptType) === -1) { + if (!supportedP2SHType(redeemScriptType)) { throw new Error('Bad redeemscript!') } @@ -398,7 +407,8 @@ function buildInput (input, allowIncomplete) { var scriptType = input.prevOutType var sig = [] var witness = [] - if (SIGNABLE.indexOf(scriptType) !== -1) { + + if (supportedType(scriptType)) { sig = buildStack(scriptType, input.signatures, input.pubKeys, allowIncomplete) } @@ -406,13 +416,14 @@ function buildInput (input, allowIncomplete) { if (scriptType === bscript.types.P2SH) { // We can remove this error later when we have a guarantee prepareInput // rejects unsignable scripts - it MUST be signable at this point. - if (P2SH.indexOf(input.redeemScriptType) === -1 && !allowIncomplete) { + if (!allowIncomplete && !supportedP2SHType(input.redeemScriptType)) { throw new Error('Impossible to sign this type') } - if (SIGNABLE.indexOf(input.redeemScriptType) !== -1) { + if (supportedType(input.redeemScriptType)) { sig = buildStack(input.redeemScriptType, input.signatures, input.pubKeys, allowIncomplete) } + // If it wasn't SIGNABLE, it's witness, defer to that if (input.redeemScriptType) { p2sh = true @@ -420,18 +431,25 @@ function buildInput (input, allowIncomplete) { } } - if (scriptType === bscript.types.P2WPKH) { + switch (scriptType) { // P2WPKH is a special case of P2PKH - witness = buildStack(bscript.types.P2PKH, input.signatures, input.pubKeys, allowIncomplete) - } else if (scriptType === bscript.types.P2WSH) { - // We can remove this check later - if (SIGNABLE.indexOf(input.witnessScriptType) === -1 && !allowIncomplete) { - throw new Error('Impossible to sign this type') - } else if (SIGNABLE.indexOf(input.witnessScriptType) !== -1) { - witness = buildStack(input.witnessScriptType, input.signatures, input.pubKeys, allowIncomplete) - witness.push(input.witnessScript) - scriptType = input.witnessScriptType - } + case bscript.types.P2WPKH: + witness = buildStack(bscript.types.P2PKH, input.signatures, input.pubKeys, allowIncomplete) + break + + case bscript.types.P2WSH: + // We can remove this check later + if (!allowIncomplete && !supportedType(input.witnessScriptType)) { + throw new Error('Impossible to sign this type') + } + + if (supportedType(input.witnessScriptType)) { + witness = buildStack(input.witnessScriptType, input.signatures, input.pubKeys, allowIncomplete) + witness.push(input.witnessScript) + scriptType = input.witnessScriptType + } + + break } // append redeemScript if necessary @@ -616,7 +634,7 @@ TransactionBuilder.prototype.__build = function (allowIncomplete) { // skip if no result if (!allowIncomplete) { - if (SIGNABLE.indexOf(result.type) === -1 && result.type !== bscript.types.P2WPKH) { + if (!supportedType(result.type) && result.type !== bscript.types.P2WPKH) { throw new Error(result.type + ' not supported') } }