2014-06-16 08:05:31 +02:00
|
|
|
var assert = require('assert')
|
|
|
|
var scripts = require('../src/scripts')
|
|
|
|
|
|
|
|
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')
|
|
|
|
|
2015-01-28 07:31:06 +01:00
|
|
|
function construct(txb, f, sign) {
|
|
|
|
f.inputs.forEach(function(input) {
|
|
|
|
var prevTxScript
|
|
|
|
|
|
|
|
if (input.prevTxScript) {
|
|
|
|
prevTxScript = Script.fromASM(input.prevTxScript)
|
|
|
|
}
|
|
|
|
|
|
|
|
txb.addInput(input.txId, input.vout, input.sequence, prevTxScript)
|
|
|
|
})
|
|
|
|
|
|
|
|
f.outputs.forEach(function(output) {
|
|
|
|
var script = Script.fromASM(output.script)
|
|
|
|
|
|
|
|
txb.addOutput(script, output.value)
|
|
|
|
})
|
|
|
|
|
|
|
|
if (sign === undefined || sign) {
|
|
|
|
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)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// FIXME: add support for locktime/version in TransactionBuilder API
|
|
|
|
if (f.version !== undefined) txb.tx.version = f.version
|
|
|
|
if (f.locktime !== undefined) txb.tx.locktime = f.locktime
|
|
|
|
}
|
|
|
|
|
2014-06-16 08:05:31 +02:00
|
|
|
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()
|
|
|
|
|
|
|
|
privKey = new ECKey(BigInteger.ONE, false)
|
|
|
|
privAddress = privKey.pub.getAddress()
|
|
|
|
privScript = privAddress.toOutputScript()
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('addInput', function() {
|
2014-07-28 06:28:44 +02:00
|
|
|
it('accepts a txHash, index [and sequence number]', function() {
|
|
|
|
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
|
|
|
})
|
|
|
|
|
2014-07-28 06:28:44 +02:00
|
|
|
it('accepts a txHash, index [, sequence number and scriptPubKey]', function() {
|
|
|
|
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
|
|
|
})
|
|
|
|
|
2014-07-28 06:28:44 +02:00
|
|
|
it('accepts a prevTx, index [and sequence number]', function() {
|
|
|
|
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
|
|
|
})
|
|
|
|
|
|
|
|
it('returns the input index', function() {
|
|
|
|
assert.equal(txb.addInput(prevTxHash, 0), 0)
|
|
|
|
assert.equal(txb.addInput(prevTxHash, 1), 1)
|
|
|
|
})
|
|
|
|
|
|
|
|
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)
|
2014-12-12 06:31:47 +01:00
|
|
|
txb.addOutput(privScript, 2000)
|
2014-06-16 08:05:31 +02:00
|
|
|
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)
|
|
|
|
|
2015-01-06 02:33:49 +01:00
|
|
|
assert.equal(txb.inputs[0].scriptType, 'pubkeyhash')
|
|
|
|
assert.equal(txb.inputs[0].redeemScript, undefined)
|
2014-06-16 08:05:31 +02:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('when redeemScript is defined', function() {
|
|
|
|
it('assumes scriptHash', function() {
|
|
|
|
txb.addInput(prevTxHash, 0)
|
|
|
|
txb.sign(0, privKey, privScript)
|
|
|
|
|
2015-01-06 02:33:49 +01:00
|
|
|
assert.equal(txb.inputs[0].prevOutType, 'scripthash')
|
|
|
|
assert.equal(txb.inputs[0].redeemScript, privScript)
|
2014-06-16 08:05:31 +02:00
|
|
|
})
|
|
|
|
|
2014-12-12 04:48:31 +01:00
|
|
|
it('throws if hashType is inconsistent', function() {
|
|
|
|
var redeemScript = scripts.multisigOutput(1, [privKey.pub])
|
|
|
|
|
|
|
|
txb.addInput(prevTxHash, 0)
|
|
|
|
txb.sign(0, privKey, redeemScript, 83)
|
|
|
|
|
|
|
|
assert.throws(function() {
|
|
|
|
txb.sign(0, privKey, redeemScript, 82)
|
|
|
|
}, /Inconsistent hashType/)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('throws if redeemScript is inconsistent', function() {
|
|
|
|
var firstScript = scripts.multisigOutput(1, [privKey.pub])
|
|
|
|
var otherScript = scripts.multisigOutput(2, [privKey.pub, privKey.pub])
|
|
|
|
|
|
|
|
txb.addInput(prevTxHash, 0)
|
|
|
|
txb.sign(0, privKey, firstScript)
|
|
|
|
|
|
|
|
assert.throws(function() {
|
|
|
|
txb.sign(0, privKey, otherScript)
|
|
|
|
}, /Inconsistent redeemScript/)
|
|
|
|
})
|
2014-12-12 05:19:03 +01:00
|
|
|
})
|
2015-01-06 01:00:49 +01:00
|
|
|
|
|
|
|
fixtures.invalid.sign.forEach(function(f) {
|
2015-01-06 02:33:49 +01:00
|
|
|
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
|
|
|
|
|
|
|
f.inputs.forEach(function(input, index) {
|
|
|
|
var redeemScript
|
|
|
|
|
|
|
|
if (input.redeemScript) {
|
|
|
|
redeemScript = Script.fromASM(input.redeemScript)
|
|
|
|
}
|
|
|
|
|
2015-01-06 01:18:04 +01:00
|
|
|
input.privKeys.forEach(function(wif, i) {
|
2015-01-06 01:00:49 +01:00
|
|
|
var privKey = ECKey.fromWIF(wif)
|
|
|
|
|
2015-01-06 01:18:04 +01:00
|
|
|
if (input.throws !== i) {
|
2015-01-06 01:00:49 +01:00
|
|
|
txb.sign(index, privKey, redeemScript)
|
|
|
|
|
|
|
|
} else {
|
|
|
|
assert.throws(function() {
|
|
|
|
txb.sign(index, privKey, redeemScript)
|
|
|
|
}, new RegExp(f.exception))
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
2014-06-16 08:05:31 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
describe('build', function() {
|
|
|
|
fixtures.valid.build.forEach(function(f) {
|
2015-01-06 06:13:15 +01:00
|
|
|
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
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2014-07-16 14:24:10 +02:00
|
|
|
fixtures.invalid.build.forEach(function(f) {
|
2015-01-28 07:31:06 +01:00
|
|
|
describe('for ' + f.description, function() {
|
|
|
|
beforeEach(function() {
|
|
|
|
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-01-28 07:31:06 +01:00
|
|
|
it('throws on ' + f.exception, function() {
|
|
|
|
assert.throws(function() {
|
|
|
|
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
|
|
|
|
it('doesn\'t throw if building incomplete', function() {
|
|
|
|
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
|
|
|
|
|
|
|
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
|
|
|
|
2014-12-12 06:31:47 +01:00
|
|
|
fixtures.invalid.fromTransaction.forEach(function(f) {
|
2014-08-30 06:35:46 +02:00
|
|
|
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
|
|
|
|
|
|
|
assert.throws(function() {
|
|
|
|
TransactionBuilder.fromTransaction(tx)
|
|
|
|
}, new RegExp(f.exception))
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2015-01-06 05:50:13 +01:00
|
|
|
it('works for the out-of-order P2SH multisig case', function() {
|
2014-07-28 07:40:07 +02:00
|
|
|
var privKeys = [
|
2015-01-06 05:50:13 +01:00
|
|
|
"91avARGdfge8E4tZfYLoxeJ5sGBdNJQH4kvjJoQFacbgww7vXtT",
|
|
|
|
"91avARGdfge8E4tZfYLoxeJ5sGBdNJQH4kvjJoQFacbgwmaKkrx"
|
2014-12-12 06:41:36 +01:00
|
|
|
].map(ECKey.fromWIF)
|
2014-07-28 07:40:07 +02:00
|
|
|
var redeemScript = Script.fromASM("OP_2 0479be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b8 04c6047f9441ed7d6d3045406e95c07cd85c778e4b8cef3ca7abac09b95c709ee51ae168fea63dc339a3c58419466ceaeef7f632653266d0e1236431a950cfe52a OP_2 OP_CHECKMULTISIG")
|
|
|
|
|
|
|
|
txb.addInput("4971f016798a167331bcbc67248313fbc444c6e92e4416efd06964425588f5cf", 0)
|
|
|
|
txb.addOutput("1BgGZ9tcN4rm9KBzDn7KprQz87SZ26SAMH", 10000)
|
|
|
|
txb.sign(0, privKeys[0], redeemScript)
|
|
|
|
|
|
|
|
var tx = txb.buildIncomplete()
|
|
|
|
|
|
|
|
// in another galaxy...
|
|
|
|
// ... far, far away
|
|
|
|
var txb2 = TransactionBuilder.fromTransaction(tx)
|
|
|
|
|
|
|
|
// [you should] verify that Transaction is what you want...
|
|
|
|
// ... then sign it
|
|
|
|
txb2.sign(0, privKeys[1], redeemScript)
|
|
|
|
var tx2 = txb2.build()
|
|
|
|
|
|
|
|
assert.equal(tx2.toHex(), '0100000001cff58855426469d0ef16442ee9c644c4fb13832467bcbc3173168a7916f0714900000000fd1c01004830450221009c92c1ae1767ac04e424da7f6db045d979b08cde86b1ddba48621d59a109d818022004f5bb21ad72255177270abaeb2d7940ac18f1e5ca1f53db4f3fd1045647a8a8014830450221009418caa5bc18da87b188a180125c0cf06dce6092f75b2d3c01a29493466800fd02206ead65e7ca6e0f17eefe6f78457c084eab59af7c9882be1437de2e7116358eb9014c8752410479be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b84104c6047f9441ed7d6d3045406e95c07cd85c778e4b8cef3ca7abac09b95c709ee51ae168fea63dc339a3c58419466ceaeef7f632653266d0e1236431a950cfe52a52aeffffffff0110270000000000001976a914751e76e8199196d454941c45d1b3a323f1433bd688ac00000000')
|
|
|
|
})
|
2014-07-28 06:28:44 +02:00
|
|
|
})
|
2014-06-16 08:05:31 +02:00
|
|
|
})
|