TxBuilder: simplify addInput

This commit is contained in:
Daniel Cousens 2015-03-02 18:06:49 +11:00
parent 7f3b4c93ba
commit 4668cb1f95

View file

@ -121,24 +121,17 @@ TransactionBuilder.fromTransaction = function (transaction) {
return txb return txb
} }
TransactionBuilder.prototype.addInput = function (prevTx, index, sequence, prevOutScript) { TransactionBuilder.prototype.addInput = function (txHash, vout, sequence, prevOutScript) {
var prevOutHash // is it a txId?
if (typeof txHash === 'string') {
// a txId is big-endian hex, we want a little-endian Buffer
txHash = new Buffer(txHash, 'hex')
Array.prototype.reverse.call(txHash)
// txId // is it a Transaction?
if (typeof prevTx === 'string') { } else if (txHash instanceof Transaction) {
prevOutHash = new Buffer(prevTx, 'hex') prevOutScript = txHash.outs[vout].script
txHash = txHash.getHash()
// TxId hex is big-endian, we want little-endian hash
Array.prototype.reverse.call(prevOutHash)
// Transaction
} else if (prevTx instanceof Transaction) {
prevOutHash = prevTx.getHash()
prevOutScript = prevTx.outs[index].script
// txHash
} else {
prevOutHash = prevTx
} }
var input = {} var input = {}
@ -172,10 +165,10 @@ TransactionBuilder.prototype.addInput = function (prevTx, index, sequence, prevO
return input2.hashType & Transaction.SIGHASH_ANYONECANPAY return input2.hashType & Transaction.SIGHASH_ANYONECANPAY
}), 'No, this would invalidate signatures') }), 'No, this would invalidate signatures')
var prevOut = prevOutHash.toString('hex') + ':' + index var prevOut = txHash.toString('hex') + ':' + vout
assert(!(prevOut in this.prevTxMap), 'Transaction is already an input') assert(!(prevOut in this.prevTxMap), 'Transaction is already an input')
var vin = this.tx.addInput(prevOutHash, index, sequence) var vin = this.tx.addInput(txHash, vout, sequence)
this.inputs[vin] = input this.inputs[vin] = input
this.prevTxMap[prevOut] = vin this.prevTxMap[prevOut] = vin