bitcoinjs-lib/test/transaction.js

168 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')
2015-08-20 05:31:29 +02:00
var bscript = require('../src/script')
2014-05-13 08:35:07 +02:00
2014-05-16 09:12:39 +02:00
var Transaction = require('../src/transaction')
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')
2015-08-20 05:31:29 +02:00
var scriptSig
if (txIn.data) {
2015-08-20 05:31:29 +02:00
scriptSig = new Buffer(txIn.data, 'hex')
} else if (txIn.script) {
2015-08-20 05:31:29 +02:00
scriptSig = bscript.fromASM(txIn.script)
}
2015-08-20 05:31:29 +02:00
tx.addInput(txHash, txIn.index, txIn.sequence, scriptSig)
})
2015-02-23 00:36:57 +01:00
raw.outs.forEach(function (txOut) {
var script
if (txOut.data) {
2015-09-05 06:10:25 +02:00
script = new Buffer(txOut.data, 'hex')
} else if (txOut.script) {
2015-08-20 05:31:29 +02:00
script = bscript.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)
2015-05-07 03:29:20 +02:00
assert.strictEqual(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
2015-05-07 03:29:20 +02:00
assert.strictEqual(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()
2015-05-07 03:29:20 +02:00
assert.strictEqual(tx.addInput(prevTxHash, 0), 0)
assert.strictEqual(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
2015-05-07 03:29:20 +02:00
assert.strictEqual(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)
2015-08-07 08:30:24 +02:00
assert.strictEqual(tx.ins[0].script.length, 0)
})
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()
2015-08-07 08:30:24 +02:00
assert.strictEqual(tx.addOutput(new Buffer(0), 0), 0)
assert.strictEqual(tx.addOutput(new Buffer(0), 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)
2015-05-07 03:29:20 +02:00
assert.strictEqual(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)
2015-05-07 03:29:20 +02:00
assert.strictEqual(tx.getHash().toString('hex'), f.hash)
})
})
})
2014-06-16 16:17:08 +02:00
// TODO:
// hashForSignature: [Function],
})