bitcoinjs-lib/test/transaction_builder.js

321 lines
12 KiB
JavaScript
Raw Normal View History

2015-02-23 00:36:57 +01:00
/* global describe, it, beforeEach */
2014-06-16 08:05:31 +02:00
var assert = require('assert')
var ops = require('../src/opcodes')
var scripts = require('../src/scripts')
var Address = require('../src/address')
2014-06-16 08:05:31 +02:00
var BigInteger = require('bigi')
2015-03-02 06:48:36 +01:00
var ECPair = require('../src/ecpair')
2014-06-16 08:05:31 +02:00
var Transaction = require('../src/transaction')
var TransactionBuilder = require('../src/transaction_builder')
2015-07-28 08:42:57 +02:00
var NETWORKS = require('../src/networks')
2014-06-16 08:05:31 +02:00
var fixtures = require('./fixtures/transaction_builder')
function construct (f, sign) {
2015-07-28 08:42:57 +02:00
var network = NETWORKS[f.network]
var txb = new TransactionBuilder(network)
2015-07-28 08:42:57 +02:00
2015-02-23 00:36:57 +01:00
f.inputs.forEach(function (input) {
2015-01-28 07:31:06 +01:00
var prevTxScript
if (input.prevTxScript) {
2015-08-14 03:16:17 +02:00
prevTxScript = scripts.fromASM(input.prevTxScript)
2015-01-28 07:31:06 +01:00
}
txb.addInput(input.txId, input.vout, input.sequence, prevTxScript)
})
2015-02-23 00:36:57 +01:00
f.outputs.forEach(function (output) {
2015-08-14 03:16:17 +02:00
var script = scripts.fromASM(output.script)
2015-01-28 07:31:06 +01:00
txb.addOutput(script, output.value)
})
if (sign === undefined || sign) {
2015-02-23 00:36:57 +01:00
f.inputs.forEach(function (input, index) {
input.signs.forEach(function (sign) {
2015-07-28 08:42:57 +02:00
var keyPair = ECPair.fromWIF(sign.keyPair, network)
var redeemScript
2015-01-28 07:31:06 +01:00
if (sign.redeemScript) {
2015-08-14 03:16:17 +02:00
redeemScript = scripts.fromASM(sign.redeemScript)
}
2015-01-28 07:31:06 +01:00
2015-03-02 06:48:36 +01:00
txb.sign(index, keyPair, redeemScript, sign.hashType)
2015-01-28 07:31:06 +01:00
})
})
}
// FIXME: add support for locktime/version in TransactionBuilder API
if (f.version !== undefined) {
2015-02-23 00:36:57 +01:00
txb.tx.version = f.version
}
if (f.locktime !== undefined) {
2015-02-23 00:36:57 +01:00
txb.tx.locktime = f.locktime
}
return txb
2015-01-28 07:31:06 +01:00
}
2015-02-23 00:36:57 +01:00
describe('TransactionBuilder', function () {
2014-06-16 08:05:31 +02:00
var privAddress, privScript
var prevTx, prevTxHash
2015-03-02 06:48:36 +01:00
var keyPair
2014-06-16 08:05:31 +02:00
var txb
2015-02-23 00:36:57 +01:00
beforeEach(function () {
2014-06-16 08:05:31 +02:00
txb = new TransactionBuilder()
prevTx = new Transaction()
2015-07-21 02:55:47 +02:00
prevTx.addOutput(Address.toOutputScript('1BgGZ9tcN4rm9KBzDn7KprQz87SZ26SAMH'), 0)
prevTx.addOutput(Address.toOutputScript('1cMh228HTCiwS8ZsaakH8A8wze1JR5ZsP'), 1)
2014-06-16 08:05:31 +02:00
prevTxHash = prevTx.getHash()
2015-03-02 06:48:36 +01:00
keyPair = new ECPair(BigInteger.ONE)
privAddress = keyPair.getAddress()
2015-07-21 02:55:47 +02:00
privScript = Address.toOutputScript(privAddress)
2014-06-16 08:05:31 +02:00
})
describe('fromTransaction', function () {
fixtures.valid.build.forEach(function (f) {
it('builds the correct TransactionBuilder for ' + f.description, function () {
var network = NETWORKS[f.network || 'bitcoin']
var tx = Transaction.fromHex(f.txHex)
var txb = TransactionBuilder.fromTransaction(tx, network)
assert.strictEqual(txb.build().toHex(), f.txHex)
assert.strictEqual(txb.network, network)
})
})
fixtures.invalid.fromTransaction.forEach(function (f) {
it('throws on ' + f.exception, function () {
var tx = Transaction.fromHex(f.txHex)
assert.throws(function () {
TransactionBuilder.fromTransaction(tx)
}, new RegExp(f.exception))
})
})
})
2015-02-23 00:36:57 +01:00
describe('addInput', function () {
it('accepts a txHash, index [and sequence number]', function () {
var vin = txb.addInput(prevTxHash, 1, 54)
2015-05-07 03:29:20 +02:00
assert.strictEqual(vin, 0)
2014-06-16 08:05:31 +02:00
2015-01-06 06:13:15 +01:00
var txIn = txb.tx.ins[0]
2015-05-07 03:29:20 +02:00
assert.strictEqual(txIn.hash, prevTxHash)
assert.strictEqual(txIn.index, 1)
assert.strictEqual(txIn.sequence, 54)
assert.strictEqual(txb.inputs[0].prevOutScript, undefined)
2014-06-16 08:05:31 +02:00
})
2015-02-23 00:36:57 +01:00
it('accepts a txHash, index [, sequence number and scriptPubKey]', function () {
var vin = txb.addInput(prevTxHash, 1, 54, prevTx.outs[1].script)
2015-05-07 03:29:20 +02:00
assert.strictEqual(vin, 0)
2014-06-16 08:05:31 +02:00
2015-01-06 06:13:15 +01:00
var txIn = txb.tx.ins[0]
2015-05-07 03:29:20 +02:00
assert.strictEqual(txIn.hash, prevTxHash)
assert.strictEqual(txIn.index, 1)
assert.strictEqual(txIn.sequence, 54)
assert.strictEqual(txb.inputs[0].prevOutScript, prevTx.outs[1].script)
2014-06-16 08:05:31 +02:00
})
2015-02-23 00:36:57 +01:00
it('accepts a prevTx, index [and sequence number]', function () {
var vin = txb.addInput(prevTx, 1, 54)
2015-05-07 03:29:20 +02:00
assert.strictEqual(vin, 0)
2014-06-16 08:05:31 +02:00
2015-01-06 06:13:15 +01:00
var txIn = txb.tx.ins[0]
assert.deepEqual(txIn.hash, prevTxHash)
2015-05-07 03:29:20 +02:00
assert.strictEqual(txIn.index, 1)
assert.strictEqual(txIn.sequence, 54)
assert.strictEqual(txb.inputs[0].prevOutScript, prevTx.outs[1].script)
2014-06-16 08:05:31 +02:00
})
2015-02-23 00:36:57 +01:00
it('returns the input index', function () {
2015-05-07 03:29:20 +02:00
assert.strictEqual(txb.addInput(prevTxHash, 0), 0)
assert.strictEqual(txb.addInput(prevTxHash, 1), 1)
2014-06-16 08:05:31 +02:00
})
2015-02-23 00:36:57 +01:00
it('throws if SIGHASH_ALL has been used to sign any existing scriptSigs', function () {
2014-06-16 08:05:31 +02:00
txb.addInput(prevTxHash, 0)
2015-03-02 06:48:36 +01:00
txb.sign(0, keyPair)
2014-06-16 08:05:31 +02:00
2015-02-23 00:36:57 +01:00
assert.throws(function () {
2014-06-16 08:05:31 +02:00
txb.addInput(prevTxHash, 0)
}, /No, this would invalidate signatures/)
})
})
2015-02-23 00:36:57 +01:00
describe('addOutput', function () {
it('accepts an address string and value', function () {
var vout = txb.addOutput(privAddress, 1000)
2015-05-07 03:29:20 +02:00
assert.strictEqual(vout, 0)
var txout = txb.tx.outs[0]
assert.deepEqual(txout.script, privScript)
2015-05-07 03:29:20 +02:00
assert.strictEqual(txout.value, 1000)
})
it('accepts a ScriptPubKey and value', function () {
var vout = txb.addOutput(privScript, 1000)
2015-05-07 03:29:20 +02:00
assert.strictEqual(vout, 0)
var txout = txb.tx.outs[0]
assert.deepEqual(txout.script, privScript)
2015-05-07 03:29:20 +02:00
assert.strictEqual(txout.value, 1000)
})
it('throws if address is of the wrong network', function () {
assert.throws(function () {
txb.addOutput('2NGHjvjw83pcVFgMcA7QvSMh2c246rxLVz9', 1000)
}, /2NGHjvjw83pcVFgMcA7QvSMh2c246rxLVz9 has no matching Script/)
})
2015-02-23 00:36:57 +01:00
it('throws if SIGHASH_ALL has been used to sign any existing scriptSigs', function () {
2014-06-16 08:05:31 +02:00
txb.addInput(prevTxHash, 0)
2014-12-12 06:31:47 +01:00
txb.addOutput(privScript, 2000)
2015-03-02 06:48:36 +01:00
txb.sign(0, keyPair)
2014-06-16 08:05:31 +02:00
2015-02-23 00:36:57 +01:00
assert.throws(function () {
2014-06-16 08:05:31 +02:00
txb.addOutput(privScript, 9000)
}, /No, this would invalidate signatures/)
})
})
2015-02-23 00:36:57 +01:00
describe('sign', function () {
fixtures.invalid.sign.forEach(function (f) {
2015-07-28 08:42:57 +02:00
it('throws on ' + f.exception + (f.description ? ' (' + f.description + ')' : ''), function () {
txb = construct(f, false)
2015-02-23 00:36:57 +01:00
f.inputs.forEach(function (input, index) {
input.signs.forEach(function (sign) {
var keyPairNetwork = NETWORKS[sign.network || f.network]
var keyPair = ECPair.fromWIF(sign.keyPair, keyPairNetwork)
var redeemScript
if (sign.redeemScript) {
2015-08-14 03:16:17 +02:00
redeemScript = scripts.fromASM(sign.redeemScript)
}
if (!sign.throws) {
2015-03-02 06:48:36 +01:00
txb.sign(index, keyPair, redeemScript, sign.hashType)
} else {
2015-02-23 00:36:57 +01:00
assert.throws(function () {
2015-03-02 06:48:36 +01:00
txb.sign(index, keyPair, redeemScript, sign.hashType)
}, new RegExp(f.exception))
}
})
})
})
})
2014-06-16 08:05:31 +02:00
})
2015-02-23 00:36:57 +01:00
describe('build', function () {
fixtures.valid.build.forEach(function (f) {
it('builds "' + f.description + '"', function () {
txb = construct(f)
2014-06-16 08:05:31 +02:00
var tx = txb.build()
2015-05-07 03:29:20 +02:00
assert.strictEqual(tx.toHex(), f.txHex)
2014-06-16 08:05:31 +02:00
})
})
2015-02-23 00:36:57 +01:00
fixtures.invalid.build.forEach(function (f) {
describe('for ' + (f.description || f.exception), function () {
beforeEach(function () {
2015-01-28 07:31:06 +01:00
if (f.txHex) {
var tx = Transaction.fromHex(f.txHex)
txb = TransactionBuilder.fromTransaction(tx)
2015-01-28 07:31:06 +01:00
} else {
txb = construct(f)
}
})
2014-06-16 08:05:31 +02:00
2015-02-23 00:36:57 +01:00
it('throws', function () {
assert.throws(function () {
2015-01-28 07:31:06 +01:00
txb.build()
}, new RegExp(f.exception))
})
2014-06-16 08:05:31 +02:00
2015-01-28 07:31:06 +01:00
if (f.alwaysThrows) return
2015-02-23 00:36:57 +01:00
it("doesn't throw if building incomplete", function () {
2015-01-28 07:31:06 +01:00
txb.buildIncomplete()
})
})
2014-06-16 08:05:31 +02:00
})
})
describe('multisig', function () {
fixtures.valid.multisig.forEach(function (f) {
it(f.description, function () {
txb = construct(f, false)
var tx
var network = NETWORKS[f.network]
f.inputs.forEach(function (input, i) {
2015-08-14 03:16:17 +02:00
var redeemScript = scripts.fromASM(input.redeemScript)
input.signs.forEach(function (sign) {
// rebuild the transaction each-time after the first
if (tx) {
// do we filter OP_0's beforehand?
if (sign.filterOP_0) {
var scriptSig = tx.ins[i].script
// ignore OP_0 on the front, ignore redeemScript
2015-08-14 03:16:17 +02:00
var signatures = scripts.decompile(scriptSig).slice(1, -1).filter(function (x) { return x !== ops.OP_0 })
// rebuild/replace the scriptSig without them
var replacement = scripts.scriptHashInput(scripts.multisigInput(signatures), redeemScript)
2015-08-14 03:16:17 +02:00
assert.strictEqual(scripts.toASM(replacement), sign.scriptSigFiltered)
tx.ins[i].script = replacement
}
// now import it
txb = TransactionBuilder.fromTransaction(tx, network)
}
var keyPair = ECPair.fromWIF(sign.keyPair, network)
2015-03-02 06:48:36 +01:00
txb.sign(i, keyPair, redeemScript, sign.hashType)
// update the tx
tx = txb.buildIncomplete()
// now verify the serialized scriptSig is as expected
2015-08-14 03:16:17 +02:00
assert.strictEqual(scripts.toASM(tx.ins[i].script), sign.scriptSig)
})
})
tx = txb.build()
2015-05-07 03:29:20 +02:00
assert.strictEqual(tx.toHex(), f.txHex)
})
})
})
describe('multisig edge case', function () {
it('should handle badly pre-filled OP_0s', function () {
var lameTx = Transaction.fromHex('0100000001cff58855426469d0ef16442ee9c644c4fb13832467bcbc3173168a7916f0714900000000fd16010000483045022100daf0f4f3339d9fbab42b098045c1e4958ee3b308f4ae17be80b63808558d0adb02202f07e3d1f79dc8da285ae0d7f68083d769c11f5621ebd9691d6b48c0d4283d7d014cc952410479be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b84104c6047f9441ed7d6d3045406e95c07cd85c778e4b8cef3ca7abac09b95c709ee51ae168fea63dc339a3c58419466ceaeef7f632653266d0e1236431a950cfe52a4104f9308a019258c31049344f85f89d5229b531c845836f99b08601f113bce036f9388f7b0f632de8140fe337e62a37f3566500a99934c2231b6cb9fd7584b8e67253aeffffffff01e8030000000000001976a914aa4d7985c57e011a8b3dd8e0e5a73aaef41629c588ac00000000')
var network = NETWORKS.testnet
txb = TransactionBuilder.fromTransaction(lameTx, network)
2015-08-14 03:16:17 +02:00
var redeemScript = scripts.fromASM('OP_2 0479be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b8 04c6047f9441ed7d6d3045406e95c07cd85c778e4b8cef3ca7abac09b95c709ee51ae168fea63dc339a3c58419466ceaeef7f632653266d0e1236431a950cfe52a 04f9308a019258c31049344f85f89d5229b531c845836f99b08601f113bce036f9388f7b0f632de8140fe337e62a37f3566500a99934c2231b6cb9fd7584b8e672 OP_3 OP_CHECKMULTISIG')
var keyPair = ECPair.fromWIF('91avARGdfge8E4tZfYLoxeJ5sGBdNJQH4kvjJoQFacbgx3cTMqe', network)
txb.sign(0, keyPair, redeemScript)
var tx = txb.build()
assert.equal(tx.toHex(), '0100000001cff58855426469d0ef16442ee9c644c4fb13832467bcbc3173168a7916f0714900000000fd5e0100483045022100daf0f4f3339d9fbab42b098045c1e4958ee3b308f4ae17be80b63808558d0adb02202f07e3d1f79dc8da285ae0d7f68083d769c11f5621ebd9691d6b48c0d4283d7d01483045022100a346c61738304eac5e7702188764d19cdf68f4466196729db096d6c87ce18cdd022018c0e8ad03054b0e7e235cda6bedecf35881d7aa7d94ff425a8ace7220f38af0014cc952410479be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b84104c6047f9441ed7d6d3045406e95c07cd85c778e4b8cef3ca7abac09b95c709ee51ae168fea63dc339a3c58419466ceaeef7f632653266d0e1236431a950cfe52a4104f9308a019258c31049344f85f89d5229b531c845836f99b08601f113bce036f9388f7b0f632de8140fe337e62a37f3566500a99934c2231b6cb9fd7584b8e67253aeffffffff01e8030000000000001976a914aa4d7985c57e011a8b3dd8e0e5a73aaef41629c588ac00000000')
})
})
2014-06-16 08:05:31 +02:00
})