Skip to content

Commit 0157f18

Browse files
committed
txbuilder: refactor branches for readability
1 parent 9bae30d commit 0157f18

File tree

1 file changed

+36
-18
lines changed

1 file changed

+36
-18
lines changed

src/transaction_builder.js

Lines changed: 36 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,14 @@ var ECPair = require('./ecpair')
1414
var ECSignature = require('./ecsignature')
1515
var Transaction = require('./transaction')
1616

17+
function supportedType (type) {
18+
return SIGNABLE.indexOf(type) !== -1
19+
}
20+
21+
function supportedP2SHType (type) {
22+
return P2SH.indexOf(type) !== -1
23+
}
24+
1725
function extractChunks (type, chunks, script) {
1826
var pubKeys = []
1927
var signatures = []
@@ -82,7 +90,7 @@ function expandInput (scriptSig, witnessStack) {
8290
if (scriptSig.length === 0) {
8391
prevOutScript = bscript.witnessScriptHash.output.encode(bcrypto.sha256(witnessScript))
8492
prevOutType = scriptTypes.P2WSH
85-
if (typeof redeemScript !== 'undefined') {
93+
if (redeemScript !== undefined) {
8694
throw new Error('Redeem script given when unnecessary')
8795
}
8896
// bare witness
@@ -96,9 +104,10 @@ function expandInput (scriptSig, witnessStack) {
96104
}
97105
}
98106

99-
if (SIGNABLE.indexOf(bscript.classifyOutput(witnessScript)) === -1) {
107+
if (!supportedType(bscript.classifyOutput(witnessScript))) {
100108
throw new Error('unsupported witness script')
101109
}
110+
102111
script = witnessScript
103112
scriptType = witnessScriptType
104113
chunks = witnessStack.slice(0, -1)
@@ -124,7 +133,7 @@ function expandInput (scriptSig, witnessStack) {
124133
scriptType = scriptTypes.P2PKH
125134
chunks = witnessStack
126135
} else if (redeemScript) {
127-
if (P2SH.indexOf(redeemScriptType) === -1) {
136+
if (!supportedP2SHType(redeemScriptType)) {
128137
throw new Error('Bad redeemscript!')
129138
}
130139

@@ -398,40 +407,49 @@ function buildInput (input, allowIncomplete) {
398407
var scriptType = input.prevOutType
399408
var sig = []
400409
var witness = []
401-
if (SIGNABLE.indexOf(scriptType) !== -1) {
410+
411+
if (supportedType(scriptType)) {
402412
sig = buildStack(scriptType, input.signatures, input.pubKeys, allowIncomplete)
403413
}
404414

405415
var p2sh = false
406416
if (scriptType === bscript.types.P2SH) {
407417
// We can remove this error later when we have a guarantee prepareInput
408418
// rejects unsignable scripts - it MUST be signable at this point.
409-
if (P2SH.indexOf(input.redeemScriptType) === -1 && !allowIncomplete) {
419+
if (!allowIncomplete && !supportedP2SHType(input.redeemScriptType)) {
410420
throw new Error('Impossible to sign this type')
411421
}
412422

413-
if (SIGNABLE.indexOf(input.redeemScriptType) !== -1) {
423+
if (supportedType(input.redeemScriptType)) {
414424
sig = buildStack(input.redeemScriptType, input.signatures, input.pubKeys, allowIncomplete)
415425
}
426+
416427
// If it wasn't SIGNABLE, it's witness, defer to that
417428
if (input.redeemScriptType) {
418429
p2sh = true
419430
scriptType = input.redeemScriptType
420431
}
421432
}
422433

423-
if (scriptType === bscript.types.P2WPKH) {
434+
switch (scriptType) {
424435
// P2WPKH is a special case of P2PKH
425-
witness = buildStack(bscript.types.P2PKH, input.signatures, input.pubKeys, allowIncomplete)
426-
} else if (scriptType === bscript.types.P2WSH) {
427-
// We can remove this check later
428-
if (SIGNABLE.indexOf(input.witnessScriptType) === -1 && !allowIncomplete) {
429-
throw new Error('Impossible to sign this type')
430-
} else if (SIGNABLE.indexOf(input.witnessScriptType) !== -1) {
431-
witness = buildStack(input.witnessScriptType, input.signatures, input.pubKeys, allowIncomplete)
432-
witness.push(input.witnessScript)
433-
scriptType = input.witnessScriptType
434-
}
436+
case bscript.types.P2WPKH:
437+
witness = buildStack(bscript.types.P2PKH, input.signatures, input.pubKeys, allowIncomplete)
438+
break
439+
440+
case bscript.types.P2WSH:
441+
// We can remove this check later
442+
if (!allowIncomplete && !supportedType(input.witnessScriptType)) {
443+
throw new Error('Impossible to sign this type')
444+
}
445+
446+
if (supportedType(input.witnessScriptType)) {
447+
witness = buildStack(input.witnessScriptType, input.signatures, input.pubKeys, allowIncomplete)
448+
witness.push(input.witnessScript)
449+
scriptType = input.witnessScriptType
450+
}
451+
452+
break
435453
}
436454

437455
// append redeemScript if necessary
@@ -616,7 +634,7 @@ TransactionBuilder.prototype.__build = function (allowIncomplete) {
616634

617635
// skip if no result
618636
if (!allowIncomplete) {
619-
if (SIGNABLE.indexOf(result.type) === -1 && result.type !== bscript.types.P2WPKH) {
637+
if (!supportedType(result.type) && result.type !== bscript.types.P2WPKH) {
620638
throw new Error(result.type + ' not supported')
621639
}
622640
}

0 commit comments

Comments
 (0)