2014-06-16 08:05:31 +02:00
|
|
|
var assert = require('assert')
|
|
|
|
var scripts = require('./scripts')
|
|
|
|
|
2014-07-28 06:28:44 +02:00
|
|
|
var ECPubKey = require('./ecpubkey')
|
|
|
|
var ECSignature = require('./ecsignature')
|
2014-06-16 08:05:31 +02:00
|
|
|
var Script = require('./script')
|
2014-07-28 06:28:44 +02:00
|
|
|
var Transaction = require('./transaction')
|
2014-06-16 08:05:31 +02:00
|
|
|
|
|
|
|
function TransactionBuilder() {
|
|
|
|
this.prevOutMap = {}
|
|
|
|
this.prevOutScripts = {}
|
|
|
|
this.prevOutTypes = {}
|
|
|
|
|
|
|
|
this.signatures = []
|
|
|
|
this.tx = new Transaction()
|
|
|
|
}
|
|
|
|
|
2014-08-18 00:59:26 +02:00
|
|
|
// Static constructors
|
|
|
|
TransactionBuilder.fromTransaction = function(transaction) {
|
|
|
|
var txb = new TransactionBuilder()
|
|
|
|
|
|
|
|
// 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
|
2014-08-30 04:10:02 +02:00
|
|
|
transaction.ins.forEach(function(txin, i) {
|
2014-08-18 00:59:26 +02:00
|
|
|
// Ignore empty scripts
|
|
|
|
if (txin.script.buffer.length === 0) return
|
|
|
|
|
|
|
|
var redeemScript
|
|
|
|
var scriptSig = txin.script
|
|
|
|
var scriptType = scripts.classifyInput(scriptSig)
|
|
|
|
|
|
|
|
// Re-classify if P2SH
|
|
|
|
if (scriptType === 'scripthash') {
|
|
|
|
redeemScript = Script.fromBuffer(scriptSig.chunks.slice(-1)[0])
|
|
|
|
scriptSig = Script.fromChunks(scriptSig.chunks.slice(0, -1))
|
|
|
|
|
|
|
|
scriptType = scripts.classifyInput(scriptSig)
|
|
|
|
assert.equal(scripts.classifyOutput(redeemScript), scriptType, 'Non-matching scriptSig and scriptPubKey in input')
|
|
|
|
}
|
|
|
|
|
|
|
|
// Extract hashType, pubKeys and signatures
|
|
|
|
var hashType, pubKeys, signatures
|
|
|
|
|
|
|
|
switch (scriptType) {
|
|
|
|
case 'pubkeyhash':
|
|
|
|
var parsed = ECSignature.parseScriptSignature(scriptSig.chunks[0])
|
|
|
|
var pubKey = ECPubKey.fromBuffer(scriptSig.chunks[1])
|
|
|
|
|
|
|
|
hashType = parsed.hashType
|
|
|
|
pubKeys = [pubKey]
|
|
|
|
signatures = [parsed.signature]
|
|
|
|
|
|
|
|
break
|
|
|
|
|
|
|
|
case 'multisig':
|
|
|
|
var scriptSigs = scriptSig.chunks.slice(1) // ignore OP_0
|
|
|
|
var parsed = scriptSigs.map(function(scriptSig) {
|
|
|
|
return ECSignature.parseScriptSignature(scriptSig)
|
|
|
|
})
|
|
|
|
|
|
|
|
hashType = parsed[0].hashType
|
|
|
|
pubKeys = []
|
|
|
|
signatures = parsed.map(function(p) { return p.signature })
|
|
|
|
|
|
|
|
break
|
|
|
|
|
|
|
|
case 'pubkey':
|
|
|
|
var parsed = ECSignature.parseScriptSignature(scriptSig.chunks[0])
|
|
|
|
|
|
|
|
hashType = parsed.hashType
|
|
|
|
pubKeys = []
|
|
|
|
signatures = [parsed.signature]
|
|
|
|
|
|
|
|
break
|
|
|
|
|
|
|
|
default:
|
|
|
|
assert(false, scriptType + ' not supported')
|
|
|
|
}
|
|
|
|
|
2014-08-30 04:10:02 +02:00
|
|
|
txb.signatures[i] = {
|
2014-08-18 00:59:26 +02:00
|
|
|
hashType: hashType,
|
|
|
|
pubKeys: pubKeys,
|
|
|
|
redeemScript: redeemScript,
|
|
|
|
scriptType: scriptType,
|
|
|
|
signatures: signatures
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
return txb
|
|
|
|
}
|
|
|
|
|
|
|
|
// Operations
|
2014-07-28 06:28:44 +02:00
|
|
|
TransactionBuilder.prototype.addInput = function(prevTx, index, sequence, prevOutScript) {
|
2014-06-16 08:05:31 +02:00
|
|
|
var prevOutHash
|
|
|
|
|
|
|
|
if (typeof prevTx === 'string') {
|
|
|
|
prevOutHash = new Buffer(prevTx, 'hex')
|
|
|
|
|
|
|
|
// TxId hex is big-endian, we want little-endian hash
|
|
|
|
Array.prototype.reverse.call(prevOutHash)
|
|
|
|
|
|
|
|
} else if (prevTx instanceof Transaction) {
|
|
|
|
prevOutHash = prevTx.getHash()
|
|
|
|
prevOutScript = prevTx.outs[index].script
|
|
|
|
|
|
|
|
} else {
|
|
|
|
prevOutHash = prevTx
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
var prevOutType
|
|
|
|
if (prevOutScript !== undefined) {
|
|
|
|
prevOutType = scripts.classifyOutput(prevOutScript)
|
|
|
|
|
|
|
|
assert.notEqual(prevOutType, 'nonstandard', 'PrevOutScript not supported (nonstandard)')
|
|
|
|
}
|
|
|
|
|
|
|
|
assert(this.signatures.every(function(input) {
|
|
|
|
return input.hashType & Transaction.SIGHASH_ANYONECANPAY
|
|
|
|
}), 'No, this would invalidate signatures')
|
|
|
|
|
|
|
|
var prevOut = prevOutHash.toString('hex') + ':' + index
|
|
|
|
assert(!(prevOut in this.prevOutMap), 'Transaction is already an input')
|
|
|
|
|
2014-07-25 10:30:39 +02:00
|
|
|
var vout = this.tx.addInput(prevOutHash, index, sequence)
|
2014-06-16 08:05:31 +02:00
|
|
|
this.prevOutMap[prevOut] = true
|
|
|
|
this.prevOutScripts[vout] = prevOutScript
|
|
|
|
this.prevOutTypes[vout] = prevOutType
|
|
|
|
|
|
|
|
return vout
|
|
|
|
}
|
|
|
|
|
|
|
|
TransactionBuilder.prototype.addOutput = function(scriptPubKey, value) {
|
|
|
|
assert(this.signatures.every(function(signature) {
|
|
|
|
return (signature.hashType & 0x1f) === Transaction.SIGHASH_SINGLE
|
|
|
|
}), 'No, this would invalidate signatures')
|
|
|
|
|
|
|
|
return this.tx.addOutput(scriptPubKey, value)
|
|
|
|
}
|
|
|
|
|
2014-08-15 05:13:36 +02:00
|
|
|
TransactionBuilder.prototype.build = function() {
|
|
|
|
return this.__build(false)
|
|
|
|
}
|
|
|
|
|
|
|
|
TransactionBuilder.prototype.buildIncomplete = function() {
|
|
|
|
return this.__build(true)
|
|
|
|
}
|
|
|
|
|
|
|
|
TransactionBuilder.prototype.__build = function(allowIncomplete) {
|
2014-06-16 08:05:31 +02:00
|
|
|
if (!allowIncomplete) {
|
|
|
|
assert(this.tx.ins.length > 0, 'Transaction has no inputs')
|
|
|
|
assert(this.tx.outs.length > 0, 'Transaction has no outputs')
|
2014-07-16 14:24:10 +02:00
|
|
|
assert(this.signatures.length > 0, 'Transaction has no signatures')
|
2014-06-16 08:05:31 +02:00
|
|
|
assert.equal(this.signatures.length, this.tx.ins.length, 'Transaction is missing signatures')
|
|
|
|
}
|
|
|
|
|
|
|
|
var tx = this.tx.clone()
|
|
|
|
|
2014-07-28 06:28:44 +02:00
|
|
|
// Create script signatures from signature meta-data
|
2014-06-16 08:05:31 +02:00
|
|
|
this.signatures.forEach(function(input, index) {
|
|
|
|
var scriptSig
|
2014-07-28 06:28:44 +02:00
|
|
|
var scriptType = input.scriptType
|
2014-06-16 08:05:31 +02:00
|
|
|
|
2014-07-16 14:37:28 +02:00
|
|
|
var signatures = input.signatures.map(function(signature) {
|
|
|
|
return signature.toScriptSignature(input.hashType)
|
|
|
|
})
|
|
|
|
|
2014-07-28 06:28:44 +02:00
|
|
|
switch (scriptType) {
|
|
|
|
case 'pubkeyhash':
|
|
|
|
var signature = signatures[0]
|
|
|
|
var pubKey = input.pubKeys[0]
|
|
|
|
scriptSig = scripts.pubKeyHashInput(signature, pubKey)
|
|
|
|
|
|
|
|
break
|
2014-07-28 07:40:07 +02:00
|
|
|
|
2014-07-28 06:28:44 +02:00
|
|
|
case 'multisig':
|
|
|
|
var redeemScript = allowIncomplete ? undefined : input.redeemScript
|
|
|
|
scriptSig = scripts.multisigInput(signatures, redeemScript)
|
|
|
|
|
|
|
|
break
|
2014-07-28 07:40:07 +02:00
|
|
|
|
2014-07-28 06:28:44 +02:00
|
|
|
case 'pubkey':
|
|
|
|
var signature = signatures[0]
|
|
|
|
scriptSig = scripts.pubKeyInput(signature)
|
|
|
|
|
|
|
|
break
|
2014-07-28 07:40:07 +02:00
|
|
|
|
2014-07-28 06:28:44 +02:00
|
|
|
default:
|
|
|
|
assert(false, scriptType + ' not supported')
|
2014-06-16 08:05:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (input.redeemScript) {
|
|
|
|
scriptSig = scripts.scriptHashInput(scriptSig, input.redeemScript)
|
|
|
|
}
|
|
|
|
|
|
|
|
tx.setInputScript(index, scriptSig)
|
|
|
|
})
|
|
|
|
|
|
|
|
return tx
|
|
|
|
}
|
|
|
|
|
2014-08-18 00:59:26 +02:00
|
|
|
TransactionBuilder.prototype.sign = function(index, privKey, redeemScript, hashType) {
|
|
|
|
assert(this.tx.ins.length >= index, 'No input at index: ' + index)
|
|
|
|
hashType = hashType || Transaction.SIGHASH_ALL
|
2014-07-28 06:28:44 +02:00
|
|
|
|
2014-08-18 00:59:26 +02:00
|
|
|
var prevOutScript = this.prevOutScripts[index]
|
|
|
|
var prevOutType = this.prevOutTypes[index]
|
2014-07-28 06:28:44 +02:00
|
|
|
|
2014-08-18 00:59:26 +02:00
|
|
|
var scriptType, hash
|
|
|
|
if (redeemScript) {
|
|
|
|
prevOutScript = prevOutScript || scripts.scriptHashOutput(redeemScript.getHash())
|
|
|
|
prevOutType = prevOutType || 'scripthash'
|
2014-07-28 07:40:07 +02:00
|
|
|
|
2014-08-18 00:59:26 +02:00
|
|
|
assert.equal(prevOutType, 'scripthash', 'PrevOutScript must be P2SH')
|
2014-07-28 06:28:44 +02:00
|
|
|
|
2014-08-18 00:59:26 +02:00
|
|
|
scriptType = scripts.classifyOutput(redeemScript)
|
2014-07-28 06:28:44 +02:00
|
|
|
|
2014-08-18 00:59:26 +02:00
|
|
|
assert.notEqual(scriptType, 'scripthash', 'RedeemScript can\'t be P2SH')
|
|
|
|
assert.notEqual(scriptType, 'nonstandard', 'RedeemScript not supported (nonstandard)')
|
2014-07-28 07:40:07 +02:00
|
|
|
|
2014-08-18 00:59:26 +02:00
|
|
|
hash = this.tx.hashForSignature(index, redeemScript, hashType)
|
2014-07-28 06:28:44 +02:00
|
|
|
|
2014-08-18 00:59:26 +02:00
|
|
|
} else {
|
|
|
|
prevOutScript = prevOutScript || privKey.pub.getAddress().toOutputScript()
|
2014-08-30 04:22:55 +02:00
|
|
|
prevOutType = prevOutType || 'pubkeyhash'
|
2014-07-28 06:28:44 +02:00
|
|
|
|
2014-08-30 04:22:55 +02:00
|
|
|
assert.notEqual(prevOutType, 'scripthash', 'PrevOutScript is P2SH, missing redeemScript')
|
|
|
|
|
|
|
|
scriptType = prevOutType
|
2014-07-28 07:40:07 +02:00
|
|
|
|
2014-08-18 00:59:26 +02:00
|
|
|
hash = this.tx.hashForSignature(index, prevOutScript, hashType)
|
|
|
|
}
|
2014-07-28 06:28:44 +02:00
|
|
|
|
2014-08-30 04:22:55 +02:00
|
|
|
this.prevOutScripts[index] = prevOutScript
|
|
|
|
this.prevOutTypes[index] = prevOutType
|
|
|
|
|
2014-08-18 00:59:26 +02:00
|
|
|
if (!(index in this.signatures)) {
|
|
|
|
this.signatures[index] = {
|
2014-07-28 06:28:44 +02:00
|
|
|
hashType: hashType,
|
2014-08-18 00:59:26 +02:00
|
|
|
pubKeys: [],
|
2014-07-28 06:28:44 +02:00
|
|
|
redeemScript: redeemScript,
|
|
|
|
scriptType: scriptType,
|
2014-08-18 00:59:26 +02:00
|
|
|
signatures: []
|
2014-07-28 06:28:44 +02:00
|
|
|
}
|
2014-08-18 00:59:26 +02:00
|
|
|
}
|
2014-07-28 06:28:44 +02:00
|
|
|
|
2014-08-18 00:59:26 +02:00
|
|
|
var input = this.signatures[index]
|
|
|
|
assert.equal(input.hashType, hashType, 'Inconsistent hashType')
|
|
|
|
assert.deepEqual(input.redeemScript, redeemScript, 'Inconsistent redeemScript')
|
|
|
|
|
|
|
|
var signature = privKey.sign(hash)
|
|
|
|
input.pubKeys.push(privKey.pub)
|
|
|
|
input.signatures.push(signature)
|
2014-07-28 06:28:44 +02:00
|
|
|
}
|
|
|
|
|
2014-06-16 08:05:31 +02:00
|
|
|
module.exports = TransactionBuilder
|