2015-02-23 00:36:57 +01:00
|
|
|
/* global describe, it, beforeEach */
|
|
|
|
|
2014-06-16 08:05:31 +02:00
|
|
|
var assert = require('assert')
|
2015-03-11 01:51:01 +01:00
|
|
|
var ops = require('../src/opcodes')
|
|
|
|
var scripts = require('../src/scripts')
|
|
|
|
|
2015-03-02 07:18:56 +01:00
|
|
|
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 Script = require('../src/script')
|
|
|
|
var Transaction = require('../src/transaction')
|
|
|
|
var TransactionBuilder = require('../src/transaction_builder')
|
|
|
|
|
|
|
|
var fixtures = require('./fixtures/transaction_builder')
|
|
|
|
|
2015-02-23 00:36:57 +01:00
|
|
|
function construct (txb, f, sign) {
|
|
|
|
f.inputs.forEach(function (input) {
|
2015-01-28 07:31:06 +01:00
|
|
|
var prevTxScript
|
|
|
|
|
|
|
|
if (input.prevTxScript) {
|
|
|
|
prevTxScript = Script.fromASM(input.prevTxScript)
|
|
|
|
}
|
|
|
|
|
|
|
|
txb.addInput(input.txId, input.vout, input.sequence, prevTxScript)
|
|
|
|
})
|
|
|
|
|
2015-02-23 00:36:57 +01:00
|
|
|
f.outputs.forEach(function (output) {
|
2015-01-28 07:31:06 +01:00
|
|
|
var script = Script.fromASM(output.script)
|
|
|
|
|
|
|
|
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-03-02 06:48:36 +01:00
|
|
|
var keyPair = ECPair.fromWIF(sign.keyPair)
|
2015-02-04 10:22:11 +01:00
|
|
|
var redeemScript
|
2015-01-28 07:31:06 +01:00
|
|
|
|
2015-02-04 10:22:11 +01:00
|
|
|
if (sign.redeemScript) {
|
|
|
|
redeemScript = Script.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
|
2015-03-02 03:31:03 +01:00
|
|
|
if (f.version !== undefined) {
|
2015-02-23 00:36:57 +01:00
|
|
|
txb.tx.version = f.version
|
2015-03-02 03:31:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (f.locktime !== undefined) {
|
2015-02-23 00:36:57 +01:00
|
|
|
txb.tx.locktime = f.locktime
|
2015-03-02 03:31:03 +01:00
|
|
|
}
|
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-03-02 07:18:56 +01:00
|
|
|
prevTx.addOutput(Address.fromBase58Check('1BgGZ9tcN4rm9KBzDn7KprQz87SZ26SAMH').toOutputScript(), 0)
|
|
|
|
prevTx.addOutput(Address.fromBase58Check('1cMh228HTCiwS8ZsaakH8A8wze1JR5ZsP').toOutputScript(), 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()
|
2014-06-16 08:05:31 +02:00
|
|
|
privScript = privAddress.toOutputScript()
|
|
|
|
})
|
|
|
|
|
2015-02-23 00:36:57 +01:00
|
|
|
describe('addInput', function () {
|
|
|
|
it('accepts a txHash, index [and sequence number]', function () {
|
2014-07-28 06:28:44 +02:00
|
|
|
var vin = txb.addInput(prevTxHash, 1, 54)
|
2014-06-16 08:05:31 +02:00
|
|
|
assert.equal(vin, 0)
|
|
|
|
|
2015-01-06 06:13:15 +01:00
|
|
|
var txIn = txb.tx.ins[0]
|
|
|
|
assert.equal(txIn.hash, prevTxHash)
|
|
|
|
assert.equal(txIn.index, 1)
|
|
|
|
assert.equal(txIn.sequence, 54)
|
2015-01-06 02:33:49 +01:00
|
|
|
assert.equal(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 () {
|
2014-07-28 06:28:44 +02:00
|
|
|
var vin = txb.addInput(prevTxHash, 1, 54, prevTx.outs[1].script)
|
2014-06-16 08:05:31 +02:00
|
|
|
assert.equal(vin, 0)
|
|
|
|
|
2015-01-06 06:13:15 +01:00
|
|
|
var txIn = txb.tx.ins[0]
|
|
|
|
assert.equal(txIn.hash, prevTxHash)
|
|
|
|
assert.equal(txIn.index, 1)
|
|
|
|
assert.equal(txIn.sequence, 54)
|
2015-01-06 02:33:49 +01:00
|
|
|
assert.equal(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 () {
|
2014-07-28 06:28:44 +02:00
|
|
|
var vin = txb.addInput(prevTx, 1, 54)
|
2014-06-16 08:05:31 +02:00
|
|
|
assert.equal(vin, 0)
|
|
|
|
|
2015-01-06 06:13:15 +01:00
|
|
|
var txIn = txb.tx.ins[0]
|
|
|
|
assert.deepEqual(txIn.hash, prevTxHash)
|
|
|
|
assert.equal(txIn.index, 1)
|
|
|
|
assert.equal(txIn.sequence, 54)
|
2015-01-06 02:33:49 +01:00
|
|
|
assert.equal(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 () {
|
2014-06-16 08:05:31 +02:00
|
|
|
assert.equal(txb.addInput(prevTxHash, 0), 0)
|
|
|
|
assert.equal(txb.addInput(prevTxHash, 1), 1)
|
|
|
|
})
|
|
|
|
|
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 () {
|
2015-03-02 07:18:56 +01:00
|
|
|
it('accepts an address string and value', function () {
|
|
|
|
var vout = txb.addOutput(privAddress.toBase58Check(), 1000)
|
|
|
|
assert.equal(vout, 0)
|
|
|
|
|
|
|
|
var txout = txb.tx.outs[0]
|
|
|
|
assert.deepEqual(txout.script, privScript)
|
|
|
|
assert.equal(txout.value, 1000)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('accepts an Address object and value', function () {
|
|
|
|
var vout = txb.addOutput(privAddress, 1000)
|
|
|
|
assert.equal(vout, 0)
|
|
|
|
|
|
|
|
var txout = txb.tx.outs[0]
|
|
|
|
assert.deepEqual(txout.script, privScript)
|
|
|
|
assert.equal(txout.value, 1000)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('accepts a ScriptPubKey and value', function () {
|
|
|
|
var vout = txb.addOutput(privScript, 1000)
|
|
|
|
assert.equal(vout, 0)
|
|
|
|
|
|
|
|
var txout = txb.tx.outs[0]
|
|
|
|
assert.deepEqual(txout.script, privScript)
|
|
|
|
assert.equal(txout.value, 1000)
|
|
|
|
})
|
|
|
|
|
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) {
|
|
|
|
it('throws on ' + f.exception + ' (' + f.description + ')', function () {
|
2015-01-28 07:31:06 +01:00
|
|
|
construct(txb, f, false)
|
2015-01-06 01:00:49 +01:00
|
|
|
|
2015-02-23 00:36:57 +01:00
|
|
|
f.inputs.forEach(function (input, index) {
|
|
|
|
input.signs.forEach(function (sign) {
|
2015-03-02 06:48:36 +01:00
|
|
|
var keyPair = ECPair.fromWIF(sign.keyPair)
|
2015-02-04 10:22:11 +01:00
|
|
|
var redeemScript
|
2015-01-06 01:00:49 +01:00
|
|
|
|
2015-02-04 10:22:11 +01:00
|
|
|
if (sign.redeemScript) {
|
|
|
|
redeemScript = Script.fromASM(sign.redeemScript)
|
|
|
|
}
|
2015-01-06 01:00:49 +01:00
|
|
|
|
2015-02-04 10:22:11 +01:00
|
|
|
if (!sign.throws) {
|
2015-03-02 06:48:36 +01:00
|
|
|
txb.sign(index, keyPair, redeemScript, sign.hashType)
|
|
|
|
|
2015-01-06 01:00:49 +01:00
|
|
|
} 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)
|
2015-01-06 01:00:49 +01:00
|
|
|
}, 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 () {
|
2015-01-28 07:31:06 +01:00
|
|
|
construct(txb, f)
|
2015-01-23 06:35:09 +01:00
|
|
|
|
2014-06-16 08:05:31 +02:00
|
|
|
var tx = txb.build()
|
2015-01-06 06:13:15 +01:00
|
|
|
assert.equal(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)
|
|
|
|
} else {
|
|
|
|
construct(txb, f)
|
2014-07-16 14:24:10 +02:00
|
|
|
}
|
|
|
|
})
|
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-07-16 14:24:10 +02:00
|
|
|
})
|
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-07-16 14:24:10 +02:00
|
|
|
})
|
|
|
|
})
|
2014-06-16 08:05:31 +02:00
|
|
|
})
|
|
|
|
})
|
2014-07-28 06:28:44 +02:00
|
|
|
|
2015-03-03 11:50:46 +01:00
|
|
|
describe('multisig', function () {
|
|
|
|
fixtures.valid.multisig.forEach(function (f) {
|
|
|
|
it(f.description, function () {
|
2015-03-11 01:51:01 +01:00
|
|
|
construct(txb, f, false)
|
2015-03-03 11:50:46 +01:00
|
|
|
|
|
|
|
var tx
|
|
|
|
|
2015-03-11 01:51:01 +01:00
|
|
|
f.inputs.forEach(function (input, i) {
|
|
|
|
var redeemScript = Script.fromASM(input.redeemScript)
|
2015-03-03 11:50:46 +01:00
|
|
|
|
2015-03-11 01:51:01 +01:00
|
|
|
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-03-11 02:42:56 +01:00
|
|
|
var signatures = scriptSig.chunks.slice(1, -1).filter(function (x) { return x !== ops.OP_0 })
|
2015-03-11 01:51:01 +01:00
|
|
|
|
|
|
|
// rebuild/replace the scriptSig without them
|
|
|
|
var replacement = scripts.scriptHashInput(scripts.multisigInput(signatures), redeemScript)
|
2015-03-11 01:32:27 +01:00
|
|
|
assert.equal(replacement.toASM(), sign.scriptSigFiltered)
|
|
|
|
sign.scriptSigFiltered = replacement.toASM()
|
|
|
|
|
2015-03-11 01:51:01 +01:00
|
|
|
tx.ins[i].script = replacement
|
|
|
|
}
|
|
|
|
|
|
|
|
// now import it
|
|
|
|
txb = TransactionBuilder.fromTransaction(tx)
|
|
|
|
}
|
2015-03-03 11:50:46 +01:00
|
|
|
|
2015-03-02 06:48:36 +01:00
|
|
|
var keyPair = ECPair.fromWIF(sign.keyPair)
|
|
|
|
txb.sign(i, keyPair, redeemScript, sign.hashType)
|
2015-03-03 11:50:46 +01:00
|
|
|
|
2015-03-11 01:51:01 +01:00
|
|
|
// update the tx
|
|
|
|
tx = txb.buildIncomplete()
|
2015-03-03 11:50:46 +01:00
|
|
|
|
2015-03-11 01:32:27 +01:00
|
|
|
// now verify the serialized scriptSig is as expected
|
|
|
|
assert.equal(tx.ins[i].script.toASM(), sign.scriptSig)
|
2015-03-03 11:50:46 +01:00
|
|
|
})
|
2015-03-11 01:51:01 +01:00
|
|
|
})
|
2015-03-03 11:50:46 +01:00
|
|
|
|
|
|
|
tx = txb.build()
|
2015-03-11 01:32:27 +01:00
|
|
|
assert.equal(tx.toHex(), f.txHex)
|
2015-03-03 11:50:46 +01:00
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2015-02-23 00:36:57 +01:00
|
|
|
describe('fromTransaction', function () {
|
|
|
|
fixtures.valid.build.forEach(function (f) {
|
|
|
|
it('builds the correct TransactionBuilder for ' + f.description, function () {
|
2015-01-06 06:13:15 +01:00
|
|
|
var tx = Transaction.fromHex(f.txHex)
|
2014-07-28 06:28:44 +02:00
|
|
|
var txb = TransactionBuilder.fromTransaction(tx)
|
|
|
|
|
2015-01-06 06:13:15 +01:00
|
|
|
assert.equal(txb.build().toHex(), f.txHex)
|
2014-07-28 06:28:44 +02:00
|
|
|
})
|
|
|
|
})
|
2014-07-28 07:40:07 +02:00
|
|
|
|
2015-02-23 00:36:57 +01:00
|
|
|
fixtures.invalid.fromTransaction.forEach(function (f) {
|
|
|
|
it('throws on ' + f.exception, function () {
|
2015-01-06 02:33:49 +01:00
|
|
|
var tx = Transaction.fromHex(f.txHex)
|
2014-08-30 06:35:46 +02:00
|
|
|
|
2015-02-23 00:36:57 +01:00
|
|
|
assert.throws(function () {
|
2014-08-30 06:35:46 +02:00
|
|
|
TransactionBuilder.fromTransaction(tx)
|
|
|
|
}, new RegExp(f.exception))
|
|
|
|
})
|
|
|
|
})
|
2014-07-28 06:28:44 +02:00
|
|
|
})
|
2014-06-16 08:05:31 +02:00
|
|
|
})
|