diff --git a/src/transaction_builder.js b/src/transaction_builder.js new file mode 100644 index 0000000..e284d63 --- /dev/null +++ b/src/transaction_builder.js @@ -0,0 +1,159 @@ +var assert = require('assert') +var scripts = require('./scripts') + +var ECKey = require('./eckey') +var Transaction = require('./transaction') +var Script = require('./script') + +function TransactionBuilder() { + this.prevOutMap = {} + this.prevOutScripts = {} + this.prevOutTypes = {} + + this.signatures = [] + this.tx = new Transaction() +} + +TransactionBuilder.prototype.addInput = function(prevTx, index, prevOutScript) { + 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) { + assert(prevOutScript === undefined, 'Unnecessary Script provided') + + 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') + + var vout = this.tx.addInput(prevOutHash, index) + 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) +} + +TransactionBuilder.prototype.sign = function(index, privKey, redeemScript, hashType) { + assert(this.tx.ins.length >= index, 'No input at index: ' + index) + hashType = hashType || Transaction.SIGHASH_ALL + + var prevOutScript = this.prevOutScripts[index] + var prevOutType = this.prevOutTypes[index] + + var scriptType, hash + if (redeemScript) { + prevOutScript = prevOutScript || scripts.scriptHashOutput(redeemScript.getHash()) + prevOutType = prevOutType || 'scripthash' + + assert.equal(prevOutType, 'scripthash', 'PrevOutScript must be P2SH') + + scriptType = scripts.classifyOutput(redeemScript) + + assert.notEqual(scriptType, 'scripthash', 'RedeemScript can\'t be P2SH') + assert.notEqual(scriptType, 'nonstandard', 'RedeemScript not supported (nonstandard)') + + hash = this.tx.hashForSignature(index, redeemScript, hashType) + + } else { + prevOutScript = prevOutScript || privKey.pub.getAddress().toOutputScript() + scriptType = prevOutType || 'pubkeyhash' + + assert.notEqual(scriptType, 'scripthash', 'PrevOutScript requires redeemScript') + + hash = this.tx.hashForSignature(index, prevOutScript, hashType) + } + + var signature = privKey.sign(hash) + + if (!(index in this.signatures)) { + this.signatures[index] = { + hashType: hashType, + pubKeys: [], + redeemScript: redeemScript, + scriptType: scriptType, + signatures: [] + } + } + + var input = this.signatures[index] + input.pubKeys.push(privKey.pub) + input.signatures.push(signature) + + assert.equal(input.hashType, hashType, 'Inconsistent hashType') + assert.deepEqual(input.redeemScript, redeemScript, 'Inconsistent redeemScript') +} + +TransactionBuilder.prototype.build = function(allowIncomplete) { + if (!allowIncomplete) { + assert(this.tx.ins.length > 0, 'Transaction has no inputs') + assert(this.tx.outs.length > 0, 'Transaction has no outputs') + assert.equal(this.signatures.length, this.tx.ins.length, 'Transaction is missing signatures') + } + + var tx = this.tx.clone() + + this.signatures.forEach(function(input, index) { + var scriptSig + + if (input.scriptType === 'pubkeyhash') { + var signature = input.signatures[0].toScriptSignature(input.hashType) + var publicKey = input.pubKeys[0] + scriptSig = scripts.pubKeyHashInput(signature, publicKey) + + } else if (input.scriptType === 'multisig') { + var redeemScript = allowIncomplete ? undefined : input.redeemScript + var signatures = input.signatures.map(function(signature) { + return signature.toScriptSignature(input.hashType) + }) + scriptSig = scripts.multisigInput(signatures, redeemScript) + + } else if (input.scriptType === 'pubkey') { + var signature = input.signatures[0] + scriptSig = scripts.pubKeyInput(signature) + + } else { + assert(false, input.scriptType + ' not supported') + } + + if (input.redeemScript) { + scriptSig = scripts.scriptHashInput(scriptSig, input.redeemScript) + } + + tx.setInputScript(index, scriptSig) + }) + + return tx +} + +module.exports = TransactionBuilder diff --git a/test/fixtures/transaction_builder.json b/test/fixtures/transaction_builder.json new file mode 100644 index 0000000..69c1fea --- /dev/null +++ b/test/fixtures/transaction_builder.json @@ -0,0 +1,113 @@ +{ + "valid": { + "build": [ + { + "description": "pubKeyHash 1:1 transaction", + "txid": "bd641f4b0aa8bd70189ab45e935c4762f0e1c49f294b4779d79887937b7cf42e", + "inputs": [ + { + "index": 0, + "prevTx": "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", + "privKeys": ["KwDiBf89QgGbjEhKnhXJuH7LrciVrZi3qYjgd9M7rFU73sVHnoWn"] + } + ], + "outputs": [ + { + "script": "OP_DUP OP_HASH160 aa4d7985c57e011a8b3dd8e0e5a73aaef41629c5 OP_EQUALVERIFY OP_CHECKSIG", + "value": 10000 + } + ] + }, + { + "description": "2-of-2 P2SH multisig Transaction", + "txid": "8c500ce6eef6c78a10de923b68394cf31120151bdc4600e4b12de865defa9d24", + "inputs": [ + { + "index": 0, + "prevTx": "4971f016798a167331bcbc67248313fbc444c6e92e4416efd06964425588f5cf", + "privKeys": ["91avARGdfge8E4tZfYLoxeJ5sGBdNJQH4kvjJoQFacbgwmaKkrx", "91avARGdfge8E4tZfYLoxeJ5sGBdNJQH4kvjJoQFacbgww7vXtT"], + "redeemScript": "OP_2 0479be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b8 04c6047f9441ed7d6d3045406e95c07cd85c778e4b8cef3ca7abac09b95c709ee51ae168fea63dc339a3c58419466ceaeef7f632653266d0e1236431a950cfe52a OP_2 OP_CHECKMULTISIG" + } + ], + "outputs": [ + { + "script": "OP_DUP OP_HASH160 faf1d99bf040ea9c7f8cc9f14ac6733ad75ce246 OP_EQUALVERIFY OP_CHECKSIG", + "value": 10000 + } + ] + } + ] + }, + "invalid": { + "build": [ + { + "exception": "Transaction has no inputs", + "hex": "", + "outputs": [ + { + "script": "", + "value": 1000 + } + ] + }, + { + "exception": "Transaction has no outputs", + "hex": "", + "inputs": [ + { + "hash": "", + "index": 0 + } + ] + }, + { + "exception": "Transaction has no signatures", + "hex": "", + "inputs": [ + { + "hash": "", + "index": 0 + } + ], + "outputs": [ + { + "script": "", + "value": 1000 + } + ] + }, + { + "exception": "Transaction is missing signatures", + "hex": "", + "inputs": [ + { + "hash": "", + "index": 0 + }, + { + "hash": "", + "index": 1 + } + ], + "outputs": [ + { + "script": "", + "value": 1000 + } + ], + "signatures": [ + { + "wif": "", + "index": 0 + } + ] + } + ], + "fromTransaction": [ + { + "exception": "Transaction contains unsupported script types", + "hex": "" + } + ] + } +} diff --git a/test/transaction_builder.js b/test/transaction_builder.js new file mode 100644 index 0000000..4a7f0ea --- /dev/null +++ b/test/transaction_builder.js @@ -0,0 +1,228 @@ +var assert = require('assert') +var ecdsa = require('../src/ecdsa') +var scripts = require('../src/scripts') + +var Address = require('../src/address') +var BigInteger = require('bigi') +var ECKey = require('../src/eckey') +var Script = require('../src/script') +var Transaction = require('../src/transaction') +var TransactionBuilder = require('../src/transaction_builder') + +var fixtures = require('./fixtures/transaction_builder') + +describe('TransactionBuilder', function() { + var privAddress, privScript + var prevTx, prevTxHash + var privKey + var txb + + beforeEach(function() { + txb = new TransactionBuilder() + + prevTx = new Transaction() + prevTx.addOutput('1BgGZ9tcN4rm9KBzDn7KprQz87SZ26SAMH', 0) + prevTx.addOutput('1cMh228HTCiwS8ZsaakH8A8wze1JR5ZsP', 1) + prevTxHash = prevTx.getHash() + prevTxId = prevTx.getId() + + privKey = new ECKey(BigInteger.ONE, false) + privAddress = privKey.pub.getAddress() + privScript = privAddress.toOutputScript() + value = 10000 + }) + + describe('addInput', function() { + it('accepts a txHash and index', function() { + var vin = txb.addInput(prevTxHash, 1) + assert.equal(vin, 0) + + var txin = txb.tx.ins[0] + assert.equal(txin.hash, prevTxHash) + assert.equal(txin.index, 1) + assert.equal(txb.prevOutScripts[0], undefined) + }) + + it('accepts a txHash, index and scriptPubKey', function() { + var vin = txb.addInput(prevTxHash, 1, prevTx.outs[1].script) + assert.equal(vin, 0) + + var txin = txb.tx.ins[0] + assert.equal(txin.hash, prevTxHash) + assert.equal(txin.index, 1) + assert.equal(txb.prevOutScripts[0], prevTx.outs[1].script) + }) + + it('accepts a prevTx and index', function() { + var vin = txb.addInput(prevTx, 1) + assert.equal(vin, 0) + + var txin = txb.tx.ins[0] + assert.deepEqual(txin.hash, prevTxHash) + assert.equal(txin.index, 1) + assert.equal(txb.prevOutScripts[0], prevTx.outs[1].script) + }) + + it('returns the input index', function() { + assert.equal(txb.addInput(prevTxHash, 0), 0) + assert.equal(txb.addInput(prevTxHash, 1), 1) + }) + + it('throws if a Tx and prevOutScript is given', function() { + assert.throws(function() { + txb.addInput(prevTx, 0, privScript) + }, /Unnecessary Script provided/) + }) + + it('throws if prevOutScript is not supported', function() { + assert.throws(function() { + txb.addInput(prevTxHash, 0, Script.EMPTY) + }, /PrevOutScript not supported \(nonstandard\)/) + }) + + it('throws if SIGHASH_ALL has been used to sign any existing scriptSigs', function() { + txb.addInput(prevTxHash, 0) + txb.sign(0, privKey) + + assert.throws(function() { + txb.addInput(prevTxHash, 0) + }, /No, this would invalidate signatures/) + }) + }) + + describe('addOutput', function() { + it('throws if SIGHASH_ALL has been used to sign any existing scriptSigs', function() { + txb.addInput(prevTxHash, 0) + txb.addOutput(privScript, value) + txb.sign(0, privKey) + + assert.throws(function() { + txb.addOutput(privScript, 9000) + }, /No, this would invalidate signatures/) + }) + }) + + describe('sign', function() { + describe('when prevOutScript is undefined', function() { + it('assumes pubKeyHash', function() { + txb.addInput(prevTxHash, 0) + txb.sign(0, privKey) + + assert.strictEqual(txb.signatures[0].redeemScript, undefined) + assert.equal(txb.signatures[0].scriptType, 'pubkeyhash') + }) + }) + + describe('when redeemScript is defined', function() { + it('assumes scriptHash', function() { + txb.addInput(prevTxHash, 0) + txb.sign(0, privKey, privScript) + + assert.equal(txb.signatures[0].redeemScript, privScript) + }) + + it('throws if prevOutScript is not P2SH', function() { + txb.addInput(prevTx, 0) + + assert.throws(function() { + txb.sign(0, privKey, privScript) + }, /PrevOutScript must be P2SH/) + }) + + it('throws if redeemScript is P2SH', function() { + txb.addInput(prevTxHash, 0) + + var privScriptP2SH = scripts.scriptHashOutput(privScript.getHash()) + + assert.throws(function() { + txb.sign(0, privKey, privScriptP2SH) + }, /RedeemScript can\'t be P2SH/) + }) + + it('throws if redeemScript not supported', function() { + txb.addInput(prevTxHash, 0) + + assert.throws(function() { + txb.sign(0, privKey, Script.EMPTY) + }, /RedeemScript not supported \(nonstandard\)/) + }) + }) + }) + + describe('build', function() { + fixtures.valid.build.forEach(function(f) { + it('builds the correct transaction', function() { + f.inputs.forEach(function(input) { + var prevTx + if (input.prevTx.length === 64) { + prevTx = input.prevTx + } else { + prevTx = Transaction.fromHex(input.prevTx) + } + + txb.addInput(prevTx, input.index) + }) + + f.outputs.forEach(function(output) { + var script = Script.fromASM(output.script) + + txb.addOutput(script, output.value) + }) + + f.inputs.forEach(function(input, index) { + var redeemScript + + if (input.redeemScript) { + redeemScript = Script.fromASM(input.redeemScript) + } + + input.privKeys.forEach(function(wif) { + var privKey = ECKey.fromWIF(wif) + + txb.sign(index, privKey, redeemScript) + }) + }) + + var tx = txb.build() + + assert.equal(tx.getId(), f.txid) + }) + }) + + it('throws if Transaction has no inputs', function() { + txb.addOutput(privScript, value) + + assert.throws(function() { + txb.build() + }, /Transaction has no inputs/) + }) + + it('throws if Transaction has no outputs', function() { + txb.addInput(prevTxHash, 0) + + assert.throws(function() { + txb.build() + }, /Transaction has no outputs/) + }) + + it('throws if Transaction has no signatures', function() { + txb.addInput(prevTxHash, 0) + txb.addOutput(privScript, value) + + assert.throws(function() { + txb.build() + }, /Transaction is missing signatures/) + }) + + it('throws if Transaction has not enough signatures', function() { + txb.addInput(prevTxHash, 0) + txb.addInput(prevTxHash, 1) + txb.addOutput(privScript, value) + txb.sign(0, privKey) + + assert.throws(function() { + txb.build() + }, /Transaction is missing signatures/) + }) + }) +})