2015-02-23 00:36:57 +01:00
|
|
|
/* global describe, it, beforeEach */
|
|
|
|
|
2014-04-17 15:31:45 +02:00
|
|
|
var assert = require('assert')
|
2014-05-13 08:35:07 +02:00
|
|
|
|
2014-05-13 09:55:53 +02:00
|
|
|
var Address = require('../src/address')
|
2014-05-16 09:12:39 +02:00
|
|
|
var Transaction = require('../src/transaction')
|
2014-10-16 06:25:43 +02:00
|
|
|
var Script = require('../src/script')
|
2014-03-23 14:34:52 +01:00
|
|
|
|
2014-06-16 16:17:08 +02:00
|
|
|
var fixtures = require('./fixtures/transaction')
|
2014-03-09 06:46:20 +01:00
|
|
|
|
2015-02-23 00:36:57 +01:00
|
|
|
describe('Transaction', function () {
|
|
|
|
function fromRaw (raw) {
|
2014-12-12 23:20:19 +01:00
|
|
|
var tx = new Transaction()
|
|
|
|
tx.version = raw.version
|
|
|
|
tx.locktime = raw.locktime
|
2014-05-21 03:48:30 +02:00
|
|
|
|
2015-02-23 00:36:57 +01:00
|
|
|
raw.ins.forEach(function (txIn) {
|
2014-12-12 23:20:19 +01:00
|
|
|
var txHash = new Buffer(txIn.hash, 'hex')
|
2015-02-19 02:04:17 +01:00
|
|
|
var script
|
|
|
|
|
|
|
|
if (txIn.data) {
|
|
|
|
script = new Script(new Buffer(txIn.data, 'hex'), [])
|
|
|
|
} else if (txIn.script) {
|
|
|
|
script = Script.fromASM(txIn.script)
|
|
|
|
}
|
2014-05-21 03:48:30 +02:00
|
|
|
|
2014-12-12 23:20:19 +01:00
|
|
|
tx.addInput(txHash, txIn.index, txIn.sequence, script)
|
|
|
|
})
|
|
|
|
|
2015-02-23 00:36:57 +01:00
|
|
|
raw.outs.forEach(function (txOut) {
|
2014-12-12 23:20:19 +01:00
|
|
|
tx.addOutput(Script.fromASM(txOut.script), txOut.value)
|
|
|
|
})
|
|
|
|
|
|
|
|
return tx
|
|
|
|
}
|
2014-03-09 06:46:20 +01:00
|
|
|
|
2015-02-23 00:36:57 +01:00
|
|
|
describe('fromBuffer/fromHex', function () {
|
|
|
|
fixtures.valid.forEach(function (f) {
|
|
|
|
it('imports ' + f.id + ' correctly', function () {
|
2014-06-16 16:17:08 +02:00
|
|
|
var actual = Transaction.fromHex(f.hex)
|
2014-05-05 00:14:29 +02:00
|
|
|
|
2014-12-12 23:20:19 +01:00
|
|
|
assert.deepEqual(actual.toHex(), f.hex)
|
2014-06-16 16:17:08 +02:00
|
|
|
})
|
2014-05-05 00:14:29 +02:00
|
|
|
})
|
|
|
|
|
2015-02-23 00:36:57 +01:00
|
|
|
fixtures.invalid.fromBuffer.forEach(function (f) {
|
|
|
|
it('throws on ' + f.exception, function () {
|
|
|
|
assert.throws(function () {
|
2014-06-16 16:17:08 +02:00
|
|
|
Transaction.fromHex(f.hex)
|
2014-06-16 16:16:48 +02:00
|
|
|
}, new RegExp(f.exception))
|
2014-06-16 16:17:08 +02:00
|
|
|
})
|
2014-03-17 09:31:35 +01:00
|
|
|
})
|
2014-06-16 16:17:08 +02:00
|
|
|
})
|
2014-03-17 09:31:35 +01:00
|
|
|
|
2015-02-23 00:36:57 +01:00
|
|
|
describe('toBuffer/toHex', function () {
|
|
|
|
fixtures.valid.forEach(function (f) {
|
|
|
|
it('exports ' + f.id + ' correctly', function () {
|
2014-12-12 23:20:19 +01:00
|
|
|
var actual = fromRaw(f.raw)
|
2014-06-16 16:17:08 +02:00
|
|
|
|
2014-12-12 23:20:19 +01:00
|
|
|
assert.deepEqual(actual.toHex(), f.hex)
|
2014-06-16 16:17:08 +02:00
|
|
|
})
|
2014-03-17 09:31:35 +01:00
|
|
|
})
|
2014-06-16 16:17:08 +02:00
|
|
|
})
|
2014-03-17 09:31:35 +01:00
|
|
|
|
2015-02-23 00:36:57 +01:00
|
|
|
describe('addInput', function () {
|
2014-06-16 16:17:08 +02:00
|
|
|
// FIXME: not as pretty as could be
|
|
|
|
// Probably a bit representative of the API
|
|
|
|
var prevTxHash, prevTxId, prevTx
|
2015-02-23 00:36:57 +01:00
|
|
|
beforeEach(function () {
|
2014-06-16 16:17:08 +02:00
|
|
|
var f = fixtures.valid[0]
|
|
|
|
prevTx = Transaction.fromHex(f.hex)
|
2014-06-16 10:52:06 +02:00
|
|
|
prevTxHash = prevTx.getHash()
|
2014-06-16 16:17:08 +02:00
|
|
|
prevTxId = prevTx.getId()
|
|
|
|
})
|
2014-03-17 09:31:35 +01:00
|
|
|
|
2015-02-23 00:36:57 +01:00
|
|
|
it('accepts a transaction id', function () {
|
2014-06-16 16:17:08 +02:00
|
|
|
var tx = new Transaction()
|
|
|
|
tx.addInput(prevTxId, 0)
|
2014-03-17 09:31:35 +01:00
|
|
|
|
2014-06-16 16:17:08 +02:00
|
|
|
assert.deepEqual(tx.ins[0].hash, prevTxHash)
|
2014-03-17 09:31:35 +01:00
|
|
|
})
|
|
|
|
|
2015-02-23 00:36:57 +01:00
|
|
|
it('accepts a transaction hash', function () {
|
2014-06-16 16:16:19 +02:00
|
|
|
var tx = new Transaction()
|
|
|
|
tx.addInput(prevTxHash, 0)
|
|
|
|
|
|
|
|
assert.deepEqual(tx.ins[0].hash, prevTxHash)
|
|
|
|
})
|
|
|
|
|
2015-02-23 00:36:57 +01:00
|
|
|
it('accepts a Transaction object', function () {
|
2014-06-16 16:17:08 +02:00
|
|
|
var tx = new Transaction()
|
|
|
|
tx.addInput(prevTx, 0)
|
2014-03-17 09:31:35 +01:00
|
|
|
|
2014-06-16 16:17:08 +02:00
|
|
|
assert.deepEqual(tx.ins[0].hash, prevTxHash)
|
2014-03-17 09:31:35 +01:00
|
|
|
})
|
|
|
|
|
2015-02-23 00:36:57 +01:00
|
|
|
it('returns an index', function () {
|
2014-03-26 09:11:10 +01:00
|
|
|
var tx = new Transaction()
|
2014-06-16 16:16:19 +02:00
|
|
|
assert.equal(tx.addInput(prevTxHash, 0), 0)
|
|
|
|
assert.equal(tx.addInput(prevTxHash, 0), 1)
|
2014-06-16 16:17:08 +02:00
|
|
|
})
|
2014-05-01 22:25:57 +02:00
|
|
|
|
2015-02-23 00:36:57 +01:00
|
|
|
it('defaults to DEFAULT_SEQUENCE', function () {
|
2014-06-16 16:17:08 +02:00
|
|
|
var tx = new Transaction()
|
2014-06-16 16:16:19 +02:00
|
|
|
tx.addInput(prevTxHash, 0)
|
2014-05-01 22:25:57 +02:00
|
|
|
|
2014-06-16 09:31:01 +02:00
|
|
|
assert.equal(tx.ins[0].sequence, Transaction.DEFAULT_SEQUENCE)
|
2014-03-31 05:47:47 +02:00
|
|
|
})
|
2014-03-09 06:46:20 +01:00
|
|
|
|
2015-02-23 00:36:57 +01:00
|
|
|
it('defaults to empty script', function () {
|
2014-10-16 06:25:43 +02:00
|
|
|
var tx = new Transaction()
|
|
|
|
tx.addInput(prevTxHash, 0)
|
|
|
|
|
|
|
|
assert.equal(tx.ins[0].script, Script.EMPTY)
|
|
|
|
})
|
|
|
|
|
2015-02-23 00:36:57 +01:00
|
|
|
fixtures.invalid.addInput.forEach(function (f) {
|
|
|
|
it('throws on ' + f.exception, function () {
|
2014-06-17 12:18:39 +02:00
|
|
|
var tx = new Transaction()
|
|
|
|
var hash = new Buffer(f.hash, 'hex')
|
|
|
|
|
2015-02-23 00:36:57 +01:00
|
|
|
assert.throws(function () {
|
2014-06-17 12:18:39 +02:00
|
|
|
tx.addInput(hash, f.index)
|
|
|
|
}, new RegExp(f.exception))
|
|
|
|
})
|
|
|
|
})
|
2014-06-16 16:17:08 +02:00
|
|
|
})
|
2014-03-17 10:49:37 +01:00
|
|
|
|
2015-02-23 00:36:57 +01:00
|
|
|
describe('addOutput', function () {
|
2014-06-16 16:17:08 +02:00
|
|
|
// FIXME: not as pretty as could be
|
|
|
|
// Probably a bit representative of the API
|
|
|
|
var destAddressB58, destAddress, destScript
|
2015-02-23 00:36:57 +01:00
|
|
|
beforeEach(function () {
|
2014-06-16 16:17:08 +02:00
|
|
|
destAddressB58 = '15mMHKL96tWAUtqF3tbVf99Z8arcmnJrr3'
|
|
|
|
destAddress = Address.fromBase58Check(destAddressB58)
|
|
|
|
destScript = destAddress.toOutputScript()
|
|
|
|
})
|
2014-06-13 08:44:02 +02:00
|
|
|
|
2015-02-23 00:36:57 +01:00
|
|
|
it('accepts an address string', function () {
|
2014-06-16 16:17:08 +02:00
|
|
|
var tx = new Transaction()
|
|
|
|
tx.addOutput(destAddressB58, 40000)
|
2014-03-17 10:49:37 +01:00
|
|
|
|
2014-06-16 16:17:08 +02:00
|
|
|
assert.deepEqual(tx.outs[0].script, destScript)
|
|
|
|
assert.equal(tx.outs[0].value, 40000)
|
|
|
|
})
|
2014-03-17 10:49:37 +01:00
|
|
|
|
2015-02-23 00:36:57 +01:00
|
|
|
it('accepts an Address', function () {
|
2014-06-16 16:17:08 +02:00
|
|
|
var tx = new Transaction()
|
|
|
|
tx.addOutput(destAddress, 40000)
|
2014-03-17 10:49:37 +01:00
|
|
|
|
2014-06-16 16:17:08 +02:00
|
|
|
assert.deepEqual(tx.outs[0].script, destScript)
|
|
|
|
assert.equal(tx.outs[0].value, 40000)
|
2014-03-17 11:01:36 +01:00
|
|
|
})
|
|
|
|
|
2015-02-23 00:36:57 +01:00
|
|
|
it('accepts a scriptPubKey', function () {
|
2014-06-16 16:17:08 +02:00
|
|
|
var tx = new Transaction()
|
|
|
|
tx.addOutput(destScript, 40000)
|
2014-05-05 07:48:44 +02:00
|
|
|
|
2014-06-16 16:17:08 +02:00
|
|
|
assert.deepEqual(tx.outs[0].script, destScript)
|
|
|
|
assert.equal(tx.outs[0].value, 40000)
|
|
|
|
})
|
2014-06-13 08:29:57 +02:00
|
|
|
|
2015-02-23 00:36:57 +01:00
|
|
|
it('returns an index', function () {
|
2014-06-16 16:17:08 +02:00
|
|
|
var tx = new Transaction()
|
|
|
|
assert.equal(tx.addOutput(destScript, 40000), 0)
|
|
|
|
assert.equal(tx.addOutput(destScript, 40000), 1)
|
|
|
|
})
|
|
|
|
})
|
2014-03-17 18:32:31 +01:00
|
|
|
|
2015-02-23 00:36:57 +01:00
|
|
|
describe('clone', function () {
|
|
|
|
fixtures.valid.forEach(function (f) {
|
2015-02-18 22:59:10 +01:00
|
|
|
var actual, expected
|
|
|
|
|
2015-02-23 00:36:57 +01:00
|
|
|
beforeEach(function () {
|
2015-02-18 22:59:10 +01:00
|
|
|
expected = Transaction.fromHex(f.hex)
|
|
|
|
actual = expected.clone()
|
|
|
|
})
|
2014-03-18 02:31:17 +01:00
|
|
|
|
2015-02-23 00:36:57 +01:00
|
|
|
it('should have value equality', function () {
|
2014-06-16 16:17:08 +02:00
|
|
|
assert.deepEqual(actual, expected)
|
2014-03-18 02:31:17 +01:00
|
|
|
})
|
|
|
|
|
2015-02-23 00:36:57 +01:00
|
|
|
it('should not have reference equality', function () {
|
2014-06-16 16:17:08 +02:00
|
|
|
assert.notEqual(actual, expected)
|
2014-03-18 02:31:17 +01:00
|
|
|
})
|
2014-06-16 16:17:08 +02:00
|
|
|
})
|
|
|
|
})
|
2014-03-18 02:31:17 +01:00
|
|
|
|
2015-02-23 00:36:57 +01:00
|
|
|
describe('getId', function () {
|
|
|
|
fixtures.valid.forEach(function (f) {
|
|
|
|
it('should return the id for ' + f.id, function () {
|
2014-06-16 16:17:08 +02:00
|
|
|
var tx = Transaction.fromHex(f.hex)
|
|
|
|
var actual = tx.getId()
|
2014-03-18 02:31:17 +01:00
|
|
|
|
2014-12-12 23:48:31 +01:00
|
|
|
assert.equal(actual, f.id)
|
2014-03-17 18:32:31 +01:00
|
|
|
})
|
|
|
|
})
|
2014-03-17 10:49:37 +01:00
|
|
|
})
|
|
|
|
|
2015-02-23 00:36:57 +01:00
|
|
|
describe('getHash', function () {
|
|
|
|
fixtures.valid.forEach(function (f) {
|
|
|
|
it('should return the hash for ' + f.id, function () {
|
2014-06-16 10:52:06 +02:00
|
|
|
var tx = Transaction.fromHex(f.hex)
|
|
|
|
var actual = tx.getHash().toString('hex')
|
|
|
|
|
|
|
|
assert.equal(actual, f.hash)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2014-06-16 16:17:08 +02:00
|
|
|
// TODO:
|
|
|
|
// hashForSignature: [Function],
|
2014-03-09 06:46:20 +01:00
|
|
|
})
|