bitcoinjs-lib/test/transaction.js

214 lines
5.5 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
var Address = require('../src/address')
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) {
script = new Script(new Buffer(txIn.data, 'hex'), [])
} 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) {
tx.addOutput(Script.fromASM(txOut.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.id + ' correctly', function () {
2014-06-16 16:17:08 +02:00
var actual = Transaction.fromHex(f.hex)
assert.deepEqual(actual.toHex(), f.hex)
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.id + ' correctly', function () {
var actual = fromRaw(f.raw)
2014-06-16 16:17:08 +02:00
assert.deepEqual(actual.toHex(), f.hex)
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 () {
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)
prevTxHash = prevTx.getHash()
2014-06-16 16:17:08 +02:00
prevTxId = prevTx.getId()
})
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-06-16 16:17:08 +02:00
assert.deepEqual(tx.ins[0].hash, prevTxHash)
})
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('accepts a Transaction object', function () {
2014-06-16 16:17:08 +02:00
var tx = new Transaction()
tx.addInput(prevTx, 0)
2014-06-16 16:17:08 +02:00
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 () {
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()
})
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-06-16 16:17:08 +02:00
assert.deepEqual(tx.outs[0].script, destScript)
assert.equal(tx.outs[0].value, 40000)
})
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) {
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)
var actual = tx.getId()
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 () {
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],
})