bitcoinjs-lib/test/transaction.js

170 lines
4.2 KiB
JavaScript
Raw Normal View History

2015-02-23 00:36:57 +01:00
/* global describe, it, beforeEach */
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')
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')
2015-02-23 00:36:57 +01:00
describe('Transaction', function () {
function fromRaw (raw) {
var tx = new Transaction()
tx.version = raw.version
tx.locktime = raw.locktime
2015-02-23 00:36:57 +01:00
raw.ins.forEach(function (txIn) {
var txHash = new Buffer(txIn.hash, 'hex')
var script
if (txIn.data) {
var data = new Buffer(txIn.data, 'hex')
script = new Script(data, [])
} else if (txIn.script) {
script = Script.fromASM(txIn.script)
}
tx.addInput(txHash, txIn.index, txIn.sequence, script)
})
2015-02-23 00:36:57 +01:00
raw.outs.forEach(function (txOut) {
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)
})
return tx
}
2015-02-23 00:36:57 +01:00
describe('fromBuffer/fromHex', function () {
fixtures.valid.forEach(function (f) {
it('imports ' + f.description + ' (' + f.id + ')', function () {
2014-06-16 16:17:08 +02:00
var actual = Transaction.fromHex(f.hex)
assert.equal(actual.toHex(), f.hex, actual.toHex())
2014-06-16 16:17:08 +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)
}, new RegExp(f.exception))
2014-06-16 16:17:08 +02:00
})
})
2014-06-16 16:17:08 +02:00
})
2015-02-23 00:36:57 +01:00
describe('toBuffer/toHex', function () {
fixtures.valid.forEach(function (f) {
it('exports ' + f.description + ' (' + f.id + ')', function () {
var actual = fromRaw(f.raw)
2014-06-16 16:17:08 +02:00
assert.equal(actual.toHex(), f.hex, actual.toHex())
2014-06-16 16:17:08 +02:00
})
})
2014-06-16 16:17:08 +02:00
})
2015-02-23 00:36:57 +01:00
describe('addInput', function () {
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]
prevTxHash = new Buffer(f.hash, 'hex')
})
2015-02-23 00:36:57 +01:00
it('accepts a transaction hash', function () {
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 () {
var tx = new Transaction()
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()
tx.addInput(prevTxHash, 0)
2014-05-01 22:25:57 +02:00
assert.equal(tx.ins[0].sequence, Transaction.DEFAULT_SEQUENCE)
2014-03-31 05:47:47 +02:00
})
2015-02-23 00:36:57 +01:00
it('defaults to empty script', function () {
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 () {
var tx = new Transaction()
var hash = new Buffer(f.hash, 'hex')
2015-02-23 00:36:57 +01:00
assert.throws(function () {
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 () {
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) {
var actual, expected
2015-02-23 00:36:57 +01:00
beforeEach(function () {
expected = Transaction.fromHex(f.hex)
actual = expected.clone()
})
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)
})
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-06-16 16:17:08 +02: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)
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 () {
var tx = Transaction.fromHex(f.hex)
assert.equal(tx.getHash().toString('hex'), f.hash)
})
})
})
2014-06-16 16:17:08 +02:00
// TODO:
// hashForSignature: [Function],
})