TxBuilder: remove impossible/untestable assertions
This commit is contained in:
parent
986e9d4710
commit
fd2311bda4
1 changed files with 14 additions and 21 deletions
|
@ -205,6 +205,9 @@ TransactionBuilder.prototype.addOutput = function(scriptPubKey, value) {
|
||||||
|
|
||||||
TransactionBuilder.prototype.build = function() { return this.__build(false) }
|
TransactionBuilder.prototype.build = function() { return this.__build(false) }
|
||||||
TransactionBuilder.prototype.buildIncomplete = function() { return this.__build(true) }
|
TransactionBuilder.prototype.buildIncomplete = function() { return this.__build(true) }
|
||||||
|
|
||||||
|
var canSignTypes = { 'pubkeyhash': true, 'multisig': true, 'pubkey': true }
|
||||||
|
|
||||||
TransactionBuilder.prototype.__build = function(allowIncomplete) {
|
TransactionBuilder.prototype.__build = function(allowIncomplete) {
|
||||||
if (!allowIncomplete) {
|
if (!allowIncomplete) {
|
||||||
assert(this.tx.ins.length > 0, 'Transaction has no inputs')
|
assert(this.tx.ins.length > 0, 'Transaction has no inputs')
|
||||||
|
@ -215,24 +218,22 @@ TransactionBuilder.prototype.__build = function(allowIncomplete) {
|
||||||
|
|
||||||
// Create script signatures from signature meta-data
|
// Create script signatures from signature meta-data
|
||||||
this.inputs.forEach(function(input, index) {
|
this.inputs.forEach(function(input, index) {
|
||||||
if (!allowIncomplete) {
|
var scriptType = input.scriptType
|
||||||
assert(input.initialized, 'Transaction is not complete')
|
|
||||||
}
|
|
||||||
|
|
||||||
var scriptSig
|
var scriptSig
|
||||||
|
|
||||||
switch (input.scriptType) {
|
if (!allowIncomplete) {
|
||||||
case 'pubkeyhash':
|
assert(input.initialized, 'Transaction is not complete')
|
||||||
assert(input.signatures, 'Transaction is missing signatures')
|
assert(scriptType in canSignTypes, scriptType + ' not supported')
|
||||||
assert.equal(input.signatures.length, 1, 'Transaction is missing signatures')
|
assert(input.signatures, 'Transaction is missing signatures')
|
||||||
|
}
|
||||||
|
|
||||||
|
switch (scriptType) {
|
||||||
|
case 'pubkeyhash':
|
||||||
var pkhSignature = input.signatures[0].toScriptSignature(input.hashType)
|
var pkhSignature = input.signatures[0].toScriptSignature(input.hashType)
|
||||||
scriptSig = scripts.pubKeyHashInput(pkhSignature, input.pubKeys[0])
|
scriptSig = scripts.pubKeyHashInput(pkhSignature, input.pubKeys[0])
|
||||||
break
|
break
|
||||||
|
|
||||||
case 'multisig':
|
case 'multisig':
|
||||||
assert(input.signatures, 'Transaction is missing signatures')
|
|
||||||
|
|
||||||
// Array.prototype.map is sparse-compatible
|
// Array.prototype.map is sparse-compatible
|
||||||
var msSignatures = input.signatures.map(function(signature) {
|
var msSignatures = input.signatures.map(function(signature) {
|
||||||
return signature.toScriptSignature(input.hashType)
|
return signature.toScriptSignature(input.hashType)
|
||||||
|
@ -250,20 +251,13 @@ TransactionBuilder.prototype.__build = function(allowIncomplete) {
|
||||||
break
|
break
|
||||||
|
|
||||||
case 'pubkey':
|
case 'pubkey':
|
||||||
assert(input.signatures, 'Transaction is missing signatures')
|
|
||||||
assert.equal(input.signatures.length, 1, 'Transaction is missing signatures')
|
|
||||||
|
|
||||||
var pkSignature = input.signatures[0].toScriptSignature(input.hashType)
|
var pkSignature = input.signatures[0].toScriptSignature(input.hashType)
|
||||||
scriptSig = scripts.pubKeyInput(pkSignature)
|
scriptSig = scripts.pubKeyInput(pkSignature)
|
||||||
break
|
break
|
||||||
|
|
||||||
default:
|
|
||||||
if (allowIncomplete) return
|
|
||||||
|
|
||||||
assert(false, input.scriptType + ' not supported')
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (input.redeemScript) {
|
// if we built a scriptSig, wrap as scriptHash if necessary
|
||||||
|
if (scriptSig && input.prevOutType === 'scripthash') {
|
||||||
scriptSig = scripts.scriptHashInput(scriptSig, input.redeemScript)
|
scriptSig = scripts.scriptHashInput(scriptSig, input.redeemScript)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -273,8 +267,6 @@ TransactionBuilder.prototype.__build = function(allowIncomplete) {
|
||||||
return tx
|
return tx
|
||||||
}
|
}
|
||||||
|
|
||||||
var canSignTypes = { 'pubkeyhash': true, 'multisig': true, 'pubkey': true }
|
|
||||||
|
|
||||||
TransactionBuilder.prototype.sign = function(index, privKey, redeemScript, hashType) {
|
TransactionBuilder.prototype.sign = function(index, privKey, redeemScript, hashType) {
|
||||||
assert(index in this.inputs, 'No input at index: ' + index)
|
assert(index in this.inputs, 'No input at index: ' + index)
|
||||||
hashType = hashType || Transaction.SIGHASH_ALL
|
hashType = hashType || Transaction.SIGHASH_ALL
|
||||||
|
@ -342,6 +334,7 @@ TransactionBuilder.prototype.sign = function(index, privKey, redeemScript, hashT
|
||||||
} else {
|
} else {
|
||||||
assert.notEqual(input.prevOutType, 'scripthash', 'PrevOutScript is P2SH, missing redeemScript')
|
assert.notEqual(input.prevOutType, 'scripthash', 'PrevOutScript is P2SH, missing redeemScript')
|
||||||
|
|
||||||
|
// if nothing known, assume pubKeyHash
|
||||||
if (!input.scriptType) {
|
if (!input.scriptType) {
|
||||||
input.prevOutScript = privKey.pub.getAddress().toOutputScript()
|
input.prevOutScript = privKey.pub.getAddress().toOutputScript()
|
||||||
input.prevOutType = 'pubkeyhash'
|
input.prevOutType = 'pubkeyhash'
|
||||||
|
|
Loading…
Add table
Reference in a new issue