2015-02-23 10:36:57 +11:00
|
|
|
/* global describe, it, beforeEach */
|
|
|
|
|
2014-04-17 23:31:45 +10:00
|
|
|
var assert = require('assert')
|
2015-08-20 13:31:29 +10:00
|
|
|
var bscript = require('../src/script')
|
2014-05-13 16:35:07 +10:00
|
|
|
|
2014-05-16 17:12:39 +10:00
|
|
|
var Transaction = require('../src/transaction')
|
2014-03-23 21:34:52 +08:00
|
|
|
|
2014-06-17 00:17:08 +10:00
|
|
|
var fixtures = require('./fixtures/transaction')
|
2014-03-09 13:46:20 +08:00
|
|
|
|
2015-02-23 10:36:57 +11:00
|
|
|
describe('Transaction', function () {
|
|
|
|
function fromRaw (raw) {
|
2014-12-13 09:20:19 +11:00
|
|
|
var tx = new Transaction()
|
|
|
|
tx.version = raw.version
|
|
|
|
tx.locktime = raw.locktime
|
2014-05-21 11:48:30 +10:00
|
|
|
|
2015-02-23 10:36:57 +11:00
|
|
|
raw.ins.forEach(function (txIn) {
|
2014-12-13 09:20:19 +11:00
|
|
|
var txHash = new Buffer(txIn.hash, 'hex')
|
2015-08-20 13:31:29 +10:00
|
|
|
var scriptSig
|
2015-02-19 12:04:17 +11:00
|
|
|
|
|
|
|
if (txIn.data) {
|
2015-08-20 13:31:29 +10:00
|
|
|
scriptSig = new Buffer(txIn.data, 'hex')
|
2015-02-19 12:04:17 +11:00
|
|
|
} else if (txIn.script) {
|
2015-08-20 13:31:29 +10:00
|
|
|
scriptSig = bscript.fromASM(txIn.script)
|
2015-02-19 12:04:17 +11:00
|
|
|
}
|
2014-05-21 11:48:30 +10:00
|
|
|
|
2015-08-20 13:31:29 +10:00
|
|
|
tx.addInput(txHash, txIn.index, txIn.sequence, scriptSig)
|
2014-12-13 09:20:19 +11:00
|
|
|
})
|
|
|
|
|
2015-02-23 10:36:57 +11:00
|
|
|
raw.outs.forEach(function (txOut) {
|
2015-03-04 20:32:08 +11:00
|
|
|
var script
|
|
|
|
|
|
|
|
if (txOut.data) {
|
2015-09-05 14:10:25 +10:00
|
|
|
script = new Buffer(txOut.data, 'hex')
|
2015-03-04 20:32:08 +11:00
|
|
|
} else if (txOut.script) {
|
2015-08-20 13:31:29 +10:00
|
|
|
script = bscript.fromASM(txOut.script)
|
2015-03-04 20:32:08 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
tx.addOutput(script, txOut.value)
|
2014-12-13 09:20:19 +11:00
|
|
|
})
|
|
|
|
|
|
|
|
return tx
|
|
|
|
}
|
2014-03-09 13:46:20 +08:00
|
|
|
|
2015-02-23 10:36:57 +11:00
|
|
|
describe('fromBuffer/fromHex', function () {
|
|
|
|
fixtures.valid.forEach(function (f) {
|
2015-03-04 20:32:08 +11:00
|
|
|
it('imports ' + f.description + ' (' + f.id + ')', function () {
|
2014-06-17 00:17:08 +10:00
|
|
|
var actual = Transaction.fromHex(f.hex)
|
2014-05-05 08:14:29 +10:00
|
|
|
|
2015-05-07 11:29:20 +10:00
|
|
|
assert.strictEqual(actual.toHex(), f.hex, actual.toHex())
|
2014-06-17 00:17:08 +10:00
|
|
|
})
|
2014-05-05 08:14:29 +10:00
|
|
|
})
|
|
|
|
|
2015-02-23 10:36:57 +11:00
|
|
|
fixtures.invalid.fromBuffer.forEach(function (f) {
|
|
|
|
it('throws on ' + f.exception, function () {
|
|
|
|
assert.throws(function () {
|
2014-06-17 00:17:08 +10:00
|
|
|
Transaction.fromHex(f.hex)
|
2014-06-17 00:16:48 +10:00
|
|
|
}, new RegExp(f.exception))
|
2014-06-17 00:17:08 +10:00
|
|
|
})
|
2014-03-17 16:31:35 +08:00
|
|
|
})
|
2014-06-17 00:17:08 +10:00
|
|
|
})
|
2014-03-17 16:31:35 +08:00
|
|
|
|
2015-02-23 10:36:57 +11:00
|
|
|
describe('toBuffer/toHex', function () {
|
|
|
|
fixtures.valid.forEach(function (f) {
|
2015-03-04 20:32:08 +11:00
|
|
|
it('exports ' + f.description + ' (' + f.id + ')', function () {
|
2014-12-13 09:20:19 +11:00
|
|
|
var actual = fromRaw(f.raw)
|
2014-06-17 00:17:08 +10:00
|
|
|
|
2015-05-07 11:29:20 +10:00
|
|
|
assert.strictEqual(actual.toHex(), f.hex, actual.toHex())
|
2014-06-17 00:17:08 +10:00
|
|
|
})
|
2014-03-17 16:31:35 +08:00
|
|
|
})
|
2014-06-17 00:17:08 +10:00
|
|
|
})
|
2014-03-17 16:31:35 +08:00
|
|
|
|
2015-02-23 10:36:57 +11:00
|
|
|
describe('addInput', function () {
|
2015-03-02 17:18:56 +11:00
|
|
|
var prevTxHash
|
2015-02-23 10:36:57 +11:00
|
|
|
beforeEach(function () {
|
2014-06-17 00:17:08 +10:00
|
|
|
var f = fixtures.valid[0]
|
2015-03-02 17:18:56 +11:00
|
|
|
prevTxHash = new Buffer(f.hash, 'hex')
|
2014-03-17 16:31:35 +08:00
|
|
|
})
|
|
|
|
|
2015-02-23 10:36:57 +11:00
|
|
|
it('accepts a transaction hash', function () {
|
2014-06-17 00:16:19 +10:00
|
|
|
var tx = new Transaction()
|
|
|
|
tx.addInput(prevTxHash, 0)
|
|
|
|
|
|
|
|
assert.deepEqual(tx.ins[0].hash, prevTxHash)
|
|
|
|
})
|
|
|
|
|
2015-02-23 10:36:57 +11:00
|
|
|
it('returns an index', function () {
|
2014-03-26 19:11:10 +11:00
|
|
|
var tx = new Transaction()
|
2015-05-07 11:29:20 +10:00
|
|
|
assert.strictEqual(tx.addInput(prevTxHash, 0), 0)
|
|
|
|
assert.strictEqual(tx.addInput(prevTxHash, 0), 1)
|
2014-06-17 00:17:08 +10:00
|
|
|
})
|
2014-05-02 06:25:57 +10:00
|
|
|
|
2015-02-23 10:36:57 +11:00
|
|
|
it('defaults to DEFAULT_SEQUENCE', function () {
|
2014-06-17 00:17:08 +10:00
|
|
|
var tx = new Transaction()
|
2014-06-17 00:16:19 +10:00
|
|
|
tx.addInput(prevTxHash, 0)
|
2014-05-02 06:25:57 +10:00
|
|
|
|
2015-05-07 11:29:20 +10:00
|
|
|
assert.strictEqual(tx.ins[0].sequence, Transaction.DEFAULT_SEQUENCE)
|
2014-03-31 11:47:47 +08:00
|
|
|
})
|
2014-03-09 13:46:20 +08:00
|
|
|
|
2015-02-23 10:36:57 +11:00
|
|
|
it('defaults to empty script', function () {
|
2014-10-16 15:25:43 +11:00
|
|
|
var tx = new Transaction()
|
|
|
|
tx.addInput(prevTxHash, 0)
|
|
|
|
|
2015-08-07 16:30:24 +10:00
|
|
|
assert.strictEqual(tx.ins[0].script.length, 0)
|
2014-10-16 15:25:43 +11:00
|
|
|
})
|
|
|
|
|
2015-02-23 10:36:57 +11:00
|
|
|
fixtures.invalid.addInput.forEach(function (f) {
|
|
|
|
it('throws on ' + f.exception, function () {
|
2014-06-17 20:18:39 +10:00
|
|
|
var tx = new Transaction()
|
|
|
|
var hash = new Buffer(f.hash, 'hex')
|
|
|
|
|
2015-02-23 10:36:57 +11:00
|
|
|
assert.throws(function () {
|
2014-06-17 20:18:39 +10:00
|
|
|
tx.addInput(hash, f.index)
|
|
|
|
}, new RegExp(f.exception))
|
|
|
|
})
|
|
|
|
})
|
2014-06-17 00:17:08 +10:00
|
|
|
})
|
2014-03-17 17:49:37 +08:00
|
|
|
|
2015-02-23 10:36:57 +11:00
|
|
|
describe('addOutput', function () {
|
2015-03-04 20:32:08 +11:00
|
|
|
it('returns an index', function () {
|
|
|
|
var tx = new Transaction()
|
2015-08-07 16:30:24 +10:00
|
|
|
assert.strictEqual(tx.addOutput(new Buffer(0), 0), 0)
|
|
|
|
assert.strictEqual(tx.addOutput(new Buffer(0), 0), 1)
|
2014-06-17 00:17:08 +10:00
|
|
|
})
|
|
|
|
})
|
2014-03-18 01:32:31 +08:00
|
|
|
|
2015-02-23 10:36:57 +11:00
|
|
|
describe('clone', function () {
|
|
|
|
fixtures.valid.forEach(function (f) {
|
2015-02-19 08:59:10 +11:00
|
|
|
var actual, expected
|
|
|
|
|
2015-02-23 10:36:57 +11:00
|
|
|
beforeEach(function () {
|
2015-02-19 08:59:10 +11:00
|
|
|
expected = Transaction.fromHex(f.hex)
|
|
|
|
actual = expected.clone()
|
|
|
|
})
|
2014-03-18 09:31:17 +08:00
|
|
|
|
2015-02-23 10:36:57 +11:00
|
|
|
it('should have value equality', function () {
|
2014-06-17 00:17:08 +10:00
|
|
|
assert.deepEqual(actual, expected)
|
2014-03-18 09:31:17 +08:00
|
|
|
})
|
|
|
|
|
2015-02-23 10:36:57 +11:00
|
|
|
it('should not have reference equality', function () {
|
2014-06-17 00:17:08 +10:00
|
|
|
assert.notEqual(actual, expected)
|
2014-03-18 09:31:17 +08:00
|
|
|
})
|
2014-06-17 00:17:08 +10:00
|
|
|
})
|
|
|
|
})
|
2014-03-18 09:31:17 +08:00
|
|
|
|
2015-02-23 10:36:57 +11:00
|
|
|
describe('getId', function () {
|
|
|
|
fixtures.valid.forEach(function (f) {
|
|
|
|
it('should return the id for ' + f.id, function () {
|
2014-06-17 00:17:08 +10:00
|
|
|
var tx = Transaction.fromHex(f.hex)
|
2014-03-18 09:31:17 +08:00
|
|
|
|
2015-05-07 11:29:20 +10:00
|
|
|
assert.strictEqual(tx.getId(), f.id)
|
2014-03-18 01:32:31 +08:00
|
|
|
})
|
|
|
|
})
|
2014-03-17 17:49:37 +08:00
|
|
|
})
|
|
|
|
|
2015-02-23 10:36:57 +11:00
|
|
|
describe('getHash', function () {
|
|
|
|
fixtures.valid.forEach(function (f) {
|
|
|
|
it('should return the hash for ' + f.id, function () {
|
2014-06-16 18:52:06 +10:00
|
|
|
var tx = Transaction.fromHex(f.hex)
|
|
|
|
|
2015-05-07 11:29:20 +10:00
|
|
|
assert.strictEqual(tx.getHash().toString('hex'), f.hash)
|
2014-06-16 18:52:06 +10:00
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2016-05-03 21:51:30 +10:00
|
|
|
describe('isCoinbase', function () {
|
|
|
|
fixtures.valid.forEach(function (f) {
|
|
|
|
it('should return ' + f.coinbase + ' for ' + f.id, function () {
|
|
|
|
var tx = Transaction.fromHex(f.hex)
|
|
|
|
|
|
|
|
assert.strictEqual(tx.isCoinbase(), f.coinbase)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2016-06-22 14:45:36 +10:00
|
|
|
describe('hashForSignature', function () {
|
|
|
|
fixtures.hashForSignature.forEach(function (f) {
|
|
|
|
it('should return ' + f.hash + ' for ' + (f.description ? ('case "' + f.description + '"') : f.script), function () {
|
|
|
|
var tx = Transaction.fromHex(f.txHex)
|
|
|
|
var script = bscript.fromASM(f.script)
|
|
|
|
|
|
|
|
assert.strictEqual(tx.hashForSignature(f.inIndex, script, f.type).toString('hex'), f.hash)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
2014-03-09 13:46:20 +08:00
|
|
|
})
|