2014-04-17 15:31:45 +02:00
|
|
|
var assert = require('assert')
|
2014-05-13 09:55:53 +02:00
|
|
|
var networks = require('../src/networks')
|
2014-06-13 01:58:52 +02:00
|
|
|
var scripts = require('../src/scripts')
|
2014-05-13 08:35:07 +02:00
|
|
|
|
2014-05-13 09:55:53 +02:00
|
|
|
var Address = require('../src/address')
|
|
|
|
var ECKey = require('../src/eckey')
|
2014-05-16 09:12:39 +02:00
|
|
|
var Transaction = require('../src/transaction')
|
2014-05-13 09:55:53 +02:00
|
|
|
var Script = require('../src/script')
|
2014-03-23 14:34:52 +01:00
|
|
|
|
2014-03-22 16:48:21 +01:00
|
|
|
var fixtureTxes = require('./fixtures/mainnet_tx')
|
|
|
|
var fixtureTx1Hex = fixtureTxes.prevTx
|
|
|
|
var fixtureTx2Hex = fixtureTxes.tx
|
|
|
|
var fixtureTxBigHex = fixtureTxes.bigTx
|
2014-03-09 06:46:20 +01:00
|
|
|
|
2014-04-22 21:34:39 +02:00
|
|
|
function b2h(b) { return new Buffer(b).toString('hex') }
|
|
|
|
|
2014-03-09 06:46:20 +01:00
|
|
|
describe('Transaction', function() {
|
2014-05-21 03:48:30 +02:00
|
|
|
describe('toBuffer', function() {
|
|
|
|
it('matches the expected output', function() {
|
|
|
|
var expected = '010000000189632848f99722915727c5c75da8db2dbf194342a0429828f66ff88fab2af7d600000000fd1b0100483045022100e5be20d440b2bbbc886161f9095fa6d0bca749a4e41d30064f30eb97adc7a1f5022061af132890d8e4e90fedff5e9365aeeb77021afd8ef1d5c114d575512e9a130a0147304402205054e38e9d7b5c10481b6b4991fde5704cd94d49e344406e3c2ce4d18a43bf8e022051d7ba8479865b53a48bee0cce86e89a25633af5b2918aa276859489e232f51c014c8752410479be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b84104c6047f9441ed7d6d3045406e95c07cd85c778e4b8cef3ca7abac09b95c709ee51ae168fea63dc339a3c58419466ceaeef7f632653266d0e1236431a950cfe52a52aeffffffff0101000000000000001976a914751e76e8199196d454941c45d1b3a323f1433bd688ac00000000'
|
|
|
|
|
|
|
|
var actual = Transaction.fromHex(expected).toHex()
|
|
|
|
|
|
|
|
assert.equal(actual, expected)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2014-05-21 03:42:57 +02:00
|
|
|
describe('fromBuffer', function() {
|
2014-03-09 06:46:20 +01:00
|
|
|
var tx, serializedTx
|
|
|
|
beforeEach(function() {
|
2014-05-21 03:48:54 +02:00
|
|
|
serializedTx = '0100000001344630cbff61fbc362f7e1ff2f11a344c29326e4ee96e787dc0d4e5cc02fd069000000004a493046022100ef89701f460e8660c80808a162bbf2d676f40a331a243592c36d6bd1f81d6bdf022100d29c072f1b18e59caba6e1f0b8cadeb373fd33a25feded746832ec179880c23901ffffffff0100f2052a010000001976a914dd40dedd8f7e37466624c4dacc6362d8e7be23dd88ac00000000'
|
2014-05-08 02:44:35 +02:00
|
|
|
tx = Transaction.fromHex(serializedTx)
|
2014-03-09 06:46:20 +01:00
|
|
|
})
|
|
|
|
|
2014-05-05 00:14:29 +02:00
|
|
|
it('does not mutate the input buffer', function() {
|
|
|
|
var buffer = new Buffer(serializedTx, 'hex')
|
2014-05-08 02:44:35 +02:00
|
|
|
Transaction.fromBuffer(buffer)
|
2014-05-05 00:14:29 +02:00
|
|
|
|
|
|
|
assert.equal(buffer.toString('hex'), serializedTx)
|
|
|
|
})
|
|
|
|
|
2014-06-13 08:43:47 +02:00
|
|
|
it('decodes version correctly', function() {
|
2014-03-17 09:31:35 +01:00
|
|
|
assert.equal(tx.version, 1)
|
|
|
|
})
|
|
|
|
|
2014-06-13 08:43:47 +02:00
|
|
|
it('decodes locktime correctly', function() {
|
2014-03-17 09:31:35 +01:00
|
|
|
assert.equal(tx.locktime, 0)
|
|
|
|
})
|
|
|
|
|
2014-06-13 08:43:47 +02:00
|
|
|
it('decodes inputs correctly', function() {
|
2014-03-17 09:31:35 +01:00
|
|
|
assert.equal(tx.ins.length, 1)
|
|
|
|
|
|
|
|
var input = tx.ins[0]
|
2014-05-01 22:08:45 +02:00
|
|
|
assert.equal(input.sequence, 4294967295)
|
2014-03-17 09:31:35 +01:00
|
|
|
|
|
|
|
assert.equal(input.outpoint.index, 0)
|
2014-06-14 16:03:17 +02:00
|
|
|
assert.equal(input.outpoint.hash.toString('hex'), "344630cbff61fbc362f7e1ff2f11a344c29326e4ee96e787dc0d4e5cc02fd069")
|
2014-03-17 09:31:35 +01:00
|
|
|
|
2014-05-21 03:48:54 +02:00
|
|
|
assert.equal(b2h(input.script.buffer), "493046022100ef89701f460e8660c80808a162bbf2d676f40a331a243592c36d6bd1f81d6bdf022100d29c072f1b18e59caba6e1f0b8cadeb373fd33a25feded746832ec179880c23901")
|
2014-03-17 09:31:35 +01:00
|
|
|
})
|
|
|
|
|
2014-06-13 08:43:47 +02:00
|
|
|
it('decodes outputs correctly', function() {
|
2014-03-17 09:31:35 +01:00
|
|
|
assert.equal(tx.outs.length, 1)
|
|
|
|
|
|
|
|
var output = tx.outs[0]
|
|
|
|
|
|
|
|
assert.equal(output.value, 5000000000)
|
2014-06-13 03:30:07 +02:00
|
|
|
assert.deepEqual(output.script, Address.fromBase58Check('n1gqLjZbRH1biT5o4qiVMiNig8wcCPQeB9').toOutputScript())
|
2014-03-17 09:31:35 +01:00
|
|
|
})
|
|
|
|
|
2014-03-26 09:11:10 +01:00
|
|
|
it('decodes large inputs correctly', function() {
|
|
|
|
// transaction has only 1 input
|
|
|
|
var tx = new Transaction()
|
|
|
|
tx.addInput("0cb859105100ebc3344f749c835c7af7d7103ec0d8cbc3d8ccbd5d28c3c36b57", 0)
|
|
|
|
tx.addOutput("15mMHKL96tWAUtqF3tbVf99Z8arcmnJrr3", 100)
|
|
|
|
|
2014-05-08 02:44:35 +02:00
|
|
|
var buffer = tx.toBuffer()
|
2014-05-01 22:25:57 +02:00
|
|
|
|
|
|
|
// we're going to replace the 8bit VarInt for tx.ins.length with a stretched 32bit equivalent
|
|
|
|
var mutated = Buffer.concat([
|
|
|
|
buffer.slice(0, 4),
|
|
|
|
new Buffer([254, 1, 0, 0, 0]),
|
|
|
|
buffer.slice(5)
|
|
|
|
])
|
|
|
|
|
|
|
|
// the deserialized-serialized transaction should return to its non-mutated state (== tx)
|
2014-05-08 02:44:35 +02:00
|
|
|
var buffer2 = Transaction.fromBuffer(mutated).toBuffer()
|
2014-05-01 22:25:57 +02:00
|
|
|
assert.deepEqual(buffer, buffer2)
|
2014-03-31 05:47:47 +02:00
|
|
|
})
|
2014-03-09 06:46:20 +01:00
|
|
|
})
|
|
|
|
|
2014-03-17 10:49:37 +01:00
|
|
|
describe('creating a transaction', function() {
|
2014-03-18 02:31:17 +01:00
|
|
|
var tx, prevTx
|
2014-03-17 10:49:37 +01:00
|
|
|
beforeEach(function() {
|
2014-05-08 02:44:35 +02:00
|
|
|
prevTx = Transaction.fromHex(fixtureTx1Hex)
|
2014-03-17 10:49:37 +01:00
|
|
|
tx = new Transaction()
|
|
|
|
})
|
|
|
|
|
2014-06-13 08:43:47 +02:00
|
|
|
describe('addInput', function() {
|
|
|
|
it('allows a Transaction object to be passed in', function() {
|
2014-03-18 02:31:17 +01:00
|
|
|
tx.addInput(prevTx, 0)
|
2014-03-17 10:49:37 +01:00
|
|
|
verifyTransactionIn()
|
|
|
|
})
|
|
|
|
|
2014-06-13 08:43:47 +02:00
|
|
|
it('allows a Transaction hash to be passed in', function() {
|
2014-03-17 10:49:37 +01:00
|
|
|
tx.addInput("0cb859105100ebc3344f749c835c7af7d7103ec0d8cbc3d8ccbd5d28c3c36b57", 0)
|
|
|
|
verifyTransactionIn()
|
|
|
|
})
|
|
|
|
|
2014-06-12 04:48:04 +02:00
|
|
|
function verifyTransactionIn() {
|
2014-03-17 10:49:37 +01:00
|
|
|
assert.equal(tx.ins.length, 1)
|
|
|
|
|
|
|
|
var input = tx.ins[0]
|
2014-05-01 22:08:45 +02:00
|
|
|
assert.equal(input.sequence, 4294967295)
|
2014-03-17 10:49:37 +01:00
|
|
|
|
|
|
|
assert.equal(input.outpoint.index, 0)
|
2014-06-14 16:03:17 +02:00
|
|
|
assert.equal(input.outpoint.hash.toString('hex'), "576bc3c3285dbdccd8c3cbd8c03e10d7f77a5c839c744f34c3eb00511059b80c")
|
2014-03-17 10:49:37 +01:00
|
|
|
|
2014-06-12 04:48:04 +02:00
|
|
|
assert.equal(input.script, Script.EMPTY)
|
2014-03-17 10:49:37 +01:00
|
|
|
}
|
2014-03-17 11:01:36 +01:00
|
|
|
})
|
|
|
|
|
2014-06-13 08:43:47 +02:00
|
|
|
describe('addOutput', function() {
|
|
|
|
it('allows an address and a value to be passed in', function() {
|
2014-03-17 11:01:36 +01:00
|
|
|
tx.addOutput("15mMHKL96tWAUtqF3tbVf99Z8arcmnJrr3", 40000)
|
|
|
|
verifyTransactionOut()
|
|
|
|
})
|
2014-03-17 10:49:37 +01:00
|
|
|
|
2014-06-13 08:43:47 +02:00
|
|
|
it('allows an Address object and value to be passed in', function() {
|
2014-05-05 07:48:44 +02:00
|
|
|
tx.addOutput(Address.fromBase58Check('15mMHKL96tWAUtqF3tbVf99Z8arcmnJrr3'), 40000)
|
|
|
|
verifyTransactionOut()
|
|
|
|
})
|
|
|
|
|
2014-05-19 06:55:54 +02:00
|
|
|
it('allows a scriptPubKey and a value to be passed in', function() {
|
|
|
|
tx.addOutput(Address.fromBase58Check('15mMHKL96tWAUtqF3tbVf99Z8arcmnJrr3').toOutputScript(), 40000)
|
|
|
|
verifyTransactionOut()
|
|
|
|
})
|
|
|
|
|
2014-06-13 08:43:47 +02:00
|
|
|
it('supports alternative networks', function() {
|
2014-05-21 03:38:03 +02:00
|
|
|
var address = Address.fromBase58Check('mkHJaNR7uuwRG1JrmTZsV4MszaTKjCBvCR')
|
|
|
|
var script = address.toOutputScript()
|
2014-05-06 06:12:37 +02:00
|
|
|
|
2014-05-21 03:38:03 +02:00
|
|
|
tx.addOutput(address, 40000)
|
2014-05-06 06:12:37 +02:00
|
|
|
verifyTransactionOut()
|
|
|
|
|
2014-05-21 03:38:03 +02:00
|
|
|
assert.deepEqual(tx.outs[0].script, script)
|
2014-05-06 06:12:37 +02:00
|
|
|
})
|
|
|
|
|
2014-06-13 08:43:47 +02:00
|
|
|
function verifyTransactionOut() {
|
2014-03-17 11:01:36 +01:00
|
|
|
assert.equal(tx.outs.length, 1)
|
|
|
|
|
|
|
|
var output = tx.outs[0]
|
|
|
|
assert.equal(output.value, 40000)
|
2014-04-22 21:34:39 +02:00
|
|
|
assert.equal(b2h(output.script.buffer), "76a9143443bc45c560866cfeabf1d52f50a6ed358c69f288ac")
|
2014-03-17 11:01:36 +01:00
|
|
|
}
|
2014-03-17 10:49:37 +01:00
|
|
|
})
|
2014-03-17 18:32:31 +01:00
|
|
|
|
2014-06-13 08:43:47 +02:00
|
|
|
describe('sign', function() {
|
|
|
|
it('works', function() {
|
2014-05-19 01:31:16 +02:00
|
|
|
tx.addInput("0cb859105100ebc3344f749c835c7af7d7103ec0d8cbc3d8ccbd5d28c3c36b57", 0)
|
|
|
|
tx.addOutput("15mMHKL96tWAUtqF3tbVf99Z8arcmnJrr3", 40000)
|
|
|
|
tx.addOutput("1Bu3bhwRmevHLAy1JrRB6AfcxfgDG2vXRd", 50000)
|
2014-03-17 18:32:31 +01:00
|
|
|
|
2014-04-17 11:08:16 +02:00
|
|
|
var key = ECKey.fromWIF('L44f7zxJ5Zw4EK9HZtyAnzCYz2vcZ5wiJf9AuwhJakiV4xVkxBeb')
|
2014-03-17 18:32:31 +01:00
|
|
|
tx.sign(0, key)
|
|
|
|
|
2014-04-22 21:33:11 +02:00
|
|
|
var script = prevTx.outs[0].script
|
2014-05-10 14:38:05 +02:00
|
|
|
var sig = new Buffer(tx.ins[0].script.chunks[0])
|
2014-03-18 02:31:17 +01:00
|
|
|
|
2014-06-13 03:30:07 +02:00
|
|
|
assert.equal(tx.validateInput(0, script, key.pub, sig), true)
|
2014-03-18 02:31:17 +01:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2014-06-13 08:43:47 +02:00
|
|
|
describe('validateInput', function() {
|
2014-03-18 02:31:17 +01:00
|
|
|
var validTx
|
|
|
|
|
|
|
|
beforeEach(function() {
|
2014-05-08 02:44:35 +02:00
|
|
|
validTx = Transaction.fromHex(fixtureTx2Hex)
|
2014-03-18 02:31:17 +01:00
|
|
|
})
|
|
|
|
|
2014-06-13 08:43:47 +02:00
|
|
|
it('returns true for valid signature', function() {
|
2014-04-17 11:08:16 +02:00
|
|
|
var key = ECKey.fromWIF('L44f7zxJ5Zw4EK9HZtyAnzCYz2vcZ5wiJf9AuwhJakiV4xVkxBeb')
|
2014-04-22 21:33:11 +02:00
|
|
|
var script = prevTx.outs[0].script
|
2014-05-10 14:38:05 +02:00
|
|
|
var sig = new Buffer(validTx.ins[0].script.chunks[0])
|
2014-03-18 02:31:17 +01:00
|
|
|
|
2014-06-13 03:30:07 +02:00
|
|
|
assert.equal(validTx.validateInput(0, script, key.pub, sig), true)
|
2014-03-17 18:32:31 +01:00
|
|
|
})
|
|
|
|
})
|
2014-03-18 02:31:17 +01:00
|
|
|
|
2014-06-13 08:43:47 +02:00
|
|
|
describe('estimateFee', function() {
|
|
|
|
it('works for fixture tx 1', function() {
|
2014-05-08 02:44:35 +02:00
|
|
|
var tx = Transaction.fromHex(fixtureTx1Hex)
|
2014-03-22 16:48:21 +01:00
|
|
|
assert.equal(tx.estimateFee(), 20000)
|
|
|
|
})
|
|
|
|
|
2014-06-13 08:43:47 +02:00
|
|
|
it('works for fixture big tx', function() {
|
2014-05-08 02:44:35 +02:00
|
|
|
var tx = Transaction.fromHex(fixtureTxBigHex)
|
2014-03-22 16:48:21 +01:00
|
|
|
assert.equal(tx.estimateFee(), 60000)
|
|
|
|
})
|
|
|
|
|
2014-06-13 08:43:47 +02:00
|
|
|
it('allow feePerKb to be passed in as an argument', function() {
|
2014-05-08 02:44:35 +02:00
|
|
|
var tx = Transaction.fromHex(fixtureTx2Hex)
|
2014-03-22 16:48:21 +01:00
|
|
|
assert.equal(tx.estimateFee(10000), 10000)
|
|
|
|
})
|
2014-03-26 01:37:07 +01:00
|
|
|
|
2014-06-13 08:43:47 +02:00
|
|
|
it('allow feePerKb to be set to 0', function() {
|
2014-05-08 02:44:35 +02:00
|
|
|
var tx = Transaction.fromHex(fixtureTx2Hex)
|
2014-03-26 01:37:07 +01:00
|
|
|
assert.equal(tx.estimateFee(0), 0)
|
|
|
|
})
|
2014-03-22 16:48:21 +01:00
|
|
|
})
|
2014-03-17 10:49:37 +01:00
|
|
|
})
|
|
|
|
|
2014-06-13 03:30:07 +02:00
|
|
|
describe('signInput', function() {
|
2014-04-26 07:38:43 +02:00
|
|
|
it('works for multi-sig redeem script', function() {
|
|
|
|
var tx = new Transaction()
|
|
|
|
tx.addInput('d6f72aab8ff86ff6289842a0424319bf2ddba85dc7c52757912297f948286389', 0)
|
2014-05-06 08:52:31 +02:00
|
|
|
tx.addOutput('mrCDrCybB6J1vRfbwM5hemdJz73FwDBC8r', 1)
|
2014-04-26 07:38:43 +02:00
|
|
|
|
|
|
|
var privKeys = [
|
|
|
|
'5HpHagT65TZzG1PH3CSu63k8DbpvD8s5ip4nEB3kEsreAnchuDf',
|
|
|
|
'5HpHagT65TZzG1PH3CSu63k8DbpvD8s5ip4nEB3kEsreAvUcVfH'
|
|
|
|
].map(function(wif) {
|
|
|
|
return ECKey.fromWIF(wif)
|
|
|
|
})
|
|
|
|
var pubKeys = privKeys.map(function(eck) { return eck.pub })
|
2014-06-13 01:58:52 +02:00
|
|
|
var redeemScript = scripts.multisigOutput(2, pubKeys)
|
2014-04-22 21:35:44 +02:00
|
|
|
|
2014-04-26 07:38:43 +02:00
|
|
|
var signatures = privKeys.map(function(privKey) {
|
2014-06-13 03:30:07 +02:00
|
|
|
return tx.signInput(0, redeemScript, privKey)
|
2014-04-26 07:38:43 +02:00
|
|
|
})
|
2014-04-22 21:35:44 +02:00
|
|
|
|
2014-06-13 01:58:52 +02:00
|
|
|
var redeemScriptSig = scripts.multisigInput(signatures)
|
|
|
|
var scriptSig = scripts.scriptHashInput(redeemScriptSig, redeemScript)
|
2014-06-13 03:30:07 +02:00
|
|
|
tx.setInputScript(0, scriptSig)
|
2014-04-22 21:35:44 +02:00
|
|
|
|
2014-04-26 07:38:43 +02:00
|
|
|
signatures.forEach(function(sig, i){
|
2014-06-13 03:30:07 +02:00
|
|
|
assert(tx.validateInput(0, redeemScript, privKeys[i].pub, sig))
|
2014-04-26 07:38:43 +02:00
|
|
|
})
|
2014-04-22 21:35:44 +02:00
|
|
|
|
2014-04-26 07:38:43 +02:00
|
|
|
var expected = '010000000189632848f99722915727c5c75da8db2dbf194342a0429828f66ff88fab2af7d600000000fd1b0100483045022100e5be20d440b2bbbc886161f9095fa6d0bca749a4e41d30064f30eb97adc7a1f5022061af132890d8e4e90fedff5e9365aeeb77021afd8ef1d5c114d575512e9a130a0147304402205054e38e9d7b5c10481b6b4991fde5704cd94d49e344406e3c2ce4d18a43bf8e022051d7ba8479865b53a48bee0cce86e89a25633af5b2918aa276859489e232f51c014c8752410479be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b84104c6047f9441ed7d6d3045406e95c07cd85c778e4b8cef3ca7abac09b95c709ee51ae168fea63dc339a3c58419466ceaeef7f632653266d0e1236431a950cfe52a52aeffffffff0101000000000000001976a914751e76e8199196d454941c45d1b3a323f1433bd688ac00000000'
|
2014-05-08 02:44:35 +02:00
|
|
|
assert.equal(tx.toHex(), expected)
|
2014-04-26 07:38:43 +02:00
|
|
|
})
|
2014-04-22 21:35:44 +02:00
|
|
|
})
|
2014-05-20 06:07:22 +02:00
|
|
|
|
|
|
|
describe('getId', function() {
|
|
|
|
it('returns the expected txid', function() {
|
|
|
|
var tx = new Transaction()
|
|
|
|
tx.addInput('d6f72aab8ff86ff6289842a0424319bf2ddba85dc7c52757912297f948286389', 0)
|
|
|
|
tx.addOutput('mrCDrCybB6J1vRfbwM5hemdJz73FwDBC8r', 1)
|
|
|
|
|
|
|
|
assert.equal(tx.getId(), '7c3275f1212fd1a2add614f47a1f1f7b6d9570a97cb88e0e2664ab1752976e9f')
|
|
|
|
})
|
|
|
|
})
|
2014-03-09 06:46:20 +01:00
|
|
|
})
|
2014-03-17 09:31:35 +01:00
|
|
|
|