txbuilder: refactor branches for readability

This commit is contained in:
Daniel Cousens 2017-08-23 14:13:17 +10:00 committed by Daniel Cousens
parent 9bae30d112
commit 0157f18510

View file

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