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-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) {
|
2015-03-04 10:32:08 +01:00
|
|
|
var data = new Buffer(txIn.data, 'hex')
|
|
|
|
script = new Script(data, [])
|
2015-02-19 02:04:17 +01:00
|
|
|
} 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) {
|
2015-03-04 10:32:08 +01:00
|
|
|
var script
|
|
|
|
|
|
|
|
if (txOut.data) {
|
|
|
|
var data = new Buffer(txOut.data, 'hex')
|
|
|
|
script = new Script(data, [])
|
|
|
|
} else if (txOut.script) {
|
|
|
|
script = Script.fromASM(txOut.script)
|
|
|
|
}
|
|
|
|
|
|
|
|
tx.addOutput(script, txOut.value)
|
2014-12-12 23:20:19 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
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) {
|
2015-03-04 10:32:08 +01:00
|
|
|
it('imports ' + f.description + ' (' + f.id + ')', function () {
|
2014-06-16 16:17:08 +02:00
|
|
|
var actual = Transaction.fromHex(f.hex)
|
2014-05-05 00:14:29 +02:00
|
|
|
|
2015-03-04 10:32:08 +01:00
|
|
|
assert.equal(actual.toHex(), f.hex, actual.toHex())
|
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) {
|
2015-03-04 10:32:08 +01:00
|
|
|
it('exports ' + f.description + ' (' + f.id + ')', function () {
|
2014-12-12 23:20:19 +01:00
|
|
|
var actual = fromRaw(f.raw)
|
2014-06-16 16:17:08 +02:00
|
|
|
|
2015-03-04 10:32:08 +01:00
|
|
|
assert.equal(actual.toHex(), f.hex, actual.toHex())
|
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 () {
|
2015-03-02 07:18:56 +01:00
|
|
|
var prevTxHash
|
2015-02-23 00:36:57 +01:00
|
|
|
beforeEach(function () {
|
2014-06-16 16:17:08 +02:00
|
|
|
var f = fixtures.valid[0]
|
2015-03-02 07:18:56 +01:00
|
|
|
prevTxHash = new Buffer(f.hash, 'hex')
|
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('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 () {
|
2015-03-04 10:32:08 +01:00
|
|
|
it('returns an index', function () {
|
|
|
|
var tx = new Transaction()
|
|
|
|
assert.equal(tx.addOutput(Script.EMPTY, 0), 0)
|
|
|
|
assert.equal(tx.addOutput(Script.EMPTY, 0), 1)
|
2014-06-16 16:17:08 +02:00
|
|
|
})
|
|
|
|
})
|
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)
|
2014-03-18 02:31:17 +01:00
|
|
|
|
2015-03-02 07:18:56 +01:00
|
|
|
assert.equal(tx.getId(), 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)
|
|
|
|
|
2015-03-04 10:32:08 +01:00
|
|
|
assert.equal(tx.getHash().toString('hex'), f.hash)
|
2014-06-16 10:52:06 +02:00
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2014-06-16 16:17:08 +02:00
|
|
|
// TODO:
|
|
|
|
// hashForSignature: [Function],
|
2014-03-09 06:46:20 +01:00
|
|
|
})
|