TxBuilder: extract extractSignatures to free function
This commit is contained in:
parent
75ca355d48
commit
906accdc0f
1 changed files with 70 additions and 61 deletions
|
@ -16,29 +16,7 @@ function TransactionBuilder() {
|
|||
this.tx = new Transaction()
|
||||
}
|
||||
|
||||
// Static constructors
|
||||
TransactionBuilder.fromTransaction = function(transaction) {
|
||||
var txb = new TransactionBuilder()
|
||||
|
||||
// Copy other transaction fields
|
||||
txb.tx.version = transaction.version
|
||||
txb.tx.locktime = transaction.locktime
|
||||
|
||||
// Extract/add inputs
|
||||
transaction.ins.forEach(function(txIn) {
|
||||
txb.addInput(txIn.hash, txIn.index, txIn.sequence)
|
||||
})
|
||||
|
||||
// Extract/add outputs
|
||||
transaction.outs.forEach(function(txOut) {
|
||||
txb.addOutput(txOut.script, txOut.value)
|
||||
})
|
||||
|
||||
// Extract/add signatures
|
||||
transaction.ins.forEach(function(txIn, i) {
|
||||
// Ignore empty scripts
|
||||
if (txIn.script.buffer.length === 0) return
|
||||
|
||||
function extractSignature(txIn) {
|
||||
assert(!Array.prototype.every.call(txIn.hash, function(x) {
|
||||
return x === 0
|
||||
}), 'coinbase inputs not supported')
|
||||
|
@ -94,13 +72,44 @@ TransactionBuilder.fromTransaction = function(transaction) {
|
|||
assert(false, scriptType + ' inputs not supported')
|
||||
}
|
||||
|
||||
txb.signatures[i] = {
|
||||
return {
|
||||
hashType: hashType,
|
||||
pubKeys: pubKeys,
|
||||
redeemScript: redeemScript,
|
||||
scriptType: scriptType,
|
||||
signatures: signatures
|
||||
}
|
||||
}
|
||||
|
||||
// Static constructors
|
||||
TransactionBuilder.fromTransaction = function(transaction) {
|
||||
var txb = new TransactionBuilder()
|
||||
|
||||
// Copy other transaction fields
|
||||
txb.tx.version = transaction.version
|
||||
txb.tx.locktime = transaction.locktime
|
||||
|
||||
// Extract/add inputs
|
||||
transaction.ins.forEach(function(txIn) {
|
||||
txb.addInput(txIn.hash, txIn.index, txIn.sequence)
|
||||
})
|
||||
|
||||
// Extract/add outputs
|
||||
transaction.outs.forEach(function(txOut) {
|
||||
txb.addOutput(txOut.script, txOut.value)
|
||||
})
|
||||
|
||||
// Extract/add signatures
|
||||
txb.signatures = transaction.ins.map(function(txIn) {
|
||||
// Coinbase inputs not supported
|
||||
assert(!Array.prototype.every.call(txIn.hash, function(x) {
|
||||
return x === 0
|
||||
}), 'coinbase inputs not supported')
|
||||
|
||||
// Ignore empty scripts
|
||||
if (txIn.script.buffer.length === 0) return
|
||||
|
||||
return extractSignature(txIn)
|
||||
})
|
||||
|
||||
return txb
|
||||
|
|
Loading…
Reference in a new issue