bitcoinjs-lib/test/transaction.js

289 lines
8.5 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-06-16 16:17:08 +02:00
var fixtures = require('./fixtures/transaction')
var Transaction = require('../src/transaction')
2015-02-23 00:36:57 +01:00
describe('Transaction', function () {
function fromRaw (raw, noWitness) {
var tx = new Transaction()
tx.version = raw.version
tx.locktime = raw.locktime
2016-11-14 03:40:29 +01:00
raw.ins.forEach(function (txIn, i) {
2017-04-19 09:39:16 +02:00
var txHash = Buffer.from(txIn.hash, 'hex')
2015-08-20 05:31:29 +02:00
var scriptSig
if (txIn.data) {
2017-04-19 09:39:16 +02:00
scriptSig = Buffer.from(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)
2016-11-14 03:40:29 +01:00
if (!noWitness && txIn.witness) {
2016-11-14 03:40:29 +01:00
var witness = txIn.witness.map(function (x) {
2017-04-19 09:39:16 +02:00
return Buffer.from(x, 'hex')
2016-11-14 03:40:29 +01:00
})
tx.setWitness(i, witness)
}
})
2015-02-23 00:36:57 +01:00
raw.outs.forEach(function (txOut) {
var script
if (txOut.data) {
2017-04-19 09:39:16 +02:00
script = Buffer.from(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 () {
function importExport (f) {
var id = f.id || f.hash
var txHex = f.hex || f.txHex
it('imports ' + f.description + ' (' + id + ')', function () {
var actual = Transaction.fromHex(txHex)
assert.strictEqual(actual.toHex(), txHex)
})
if (f.whex) {
it('imports ' + f.description + ' (' + id + ') as witness', function () {
var actual = Transaction.fromHex(f.whex)
assert.strictEqual(actual.toHex(), f.whex)
})
}
}
fixtures.valid.forEach(importExport)
fixtures.hashForSignature.forEach(importExport)
fixtures.hashForWitnessV0.forEach(importExport)
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
})
})
it('.version should be interpreted as an int32le', function () {
var txHex = 'ffffffff0000ffffffff'
var tx = Transaction.fromHex(txHex)
assert.equal(-1, tx.version)
assert.equal(0xffffffff, tx.locktime)
})
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, true)
assert.strictEqual(actual.toHex(), f.hex)
2014-06-16 16:17:08 +02:00
})
if (f.whex) {
it('exports ' + f.description + ' (' + f.id + ') as witness', function () {
var wactual = fromRaw(f.raw)
assert.strictEqual(wactual.toHex(), f.whex)
})
}
})
it('accepts target Buffer and offset parameters', function () {
var f = fixtures.valid[0]
var actual = fromRaw(f.raw)
var byteLength = actual.byteLength()
2017-04-19 09:39:16 +02:00
var target = Buffer.alloc(byteLength * 2)
var a = actual.toBuffer(target, 0)
var b = actual.toBuffer(target, byteLength)
assert.strictEqual(a.length, byteLength)
assert.strictEqual(b.length, byteLength)
assert.strictEqual(a.toString('hex'), f.hex)
assert.strictEqual(b.toString('hex'), f.hex)
assert.deepEqual(a, b)
assert.deepEqual(a, target.slice(0, byteLength))
assert.deepEqual(b, target.slice(byteLength))
})
2014-06-16 16:17:08 +02:00
})
describe('hasWitnesses', function () {
fixtures.valid.forEach(function (f) {
it('detects if the transaction has witnesses: ' + (f.whex ? 'true' : 'false'), function () {
assert.strictEqual(Transaction.fromHex(f.whex ? f.whex : f.hex).hasWitnesses(), !!f.whex)
})
})
})
2017-06-20 15:39:47 +02:00
describe('weight/virtualSize', function () {
it('computes virtual size', function () {
fixtures.valid.forEach(function (f) {
var transaction = Transaction.fromHex(f.whex ? f.whex : f.hex)
assert.strictEqual(transaction.virtualSize(), f.virtualSize)
})
})
it('computes weight', function () {
fixtures.valid.forEach(function (f) {
var transaction = Transaction.fromHex(f.whex ? f.whex : f.hex)
assert.strictEqual(transaction.weight(), f.weight)
})
})
})
2015-02-23 00:36:57 +01:00
describe('addInput', function () {
var prevTxHash
2015-02-23 00:36:57 +01:00
beforeEach(function () {
2017-04-19 09:39:16 +02:00
prevTxHash = Buffer.from('ffffffff00ffff000000000000000000000000000000000000000000101010ff', 'hex')
})
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
it('defaults to empty script, witness and 0xffffffff SEQUENCE number', 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)
assert.strictEqual(tx.ins[0].witness.length, 0)
assert.strictEqual(tx.ins[0].sequence, 0xffffffff)
})
2015-02-23 00:36:57 +01:00
fixtures.invalid.addInput.forEach(function (f) {
it('throws on ' + f.exception, function () {
var tx = new Transaction()
2017-04-19 09:39:16 +02:00
var hash = Buffer.from(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()
2017-04-19 09:39:16 +02:00
assert.strictEqual(tx.addOutput(Buffer.alloc(0), 0), 0)
assert.strictEqual(tx.addOutput(Buffer.alloc(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
})
})
describe('getHash/getId', function () {
function verify (f) {
it('should return the id for ' + f.id + '(' + f.description + ')', function () {
var tx = Transaction.fromHex(f.whex || f.hex)
assert.strictEqual(tx.getHash().toString('hex'), f.hash)
2015-05-07 03:29:20 +02:00
assert.strictEqual(tx.getId(), f.id)
2014-03-17 18:32:31 +01:00
})
}
fixtures.valid.forEach(verify)
})
describe('isCoinbase', function () {
function verify (f) {
it('should return ' + f.coinbase + ' for ' + f.id + '(' + f.description + ')', function () {
var tx = Transaction.fromHex(f.hex)
assert.strictEqual(tx.isCoinbase(), f.coinbase)
})
}
fixtures.valid.forEach(verify)
})
describe('hashForSignature', function () {
2016-12-13 23:08:21 +01:00
it('does not use Witness serialization', function () {
2017-04-19 09:39:16 +02:00
var randScript = Buffer.from('6a', 'hex')
var tx = new Transaction()
2017-04-19 09:39:16 +02:00
tx.addInput(Buffer.from('0000000000000000000000000000000000000000000000000000000000000000', 'hex'), 0)
tx.addOutput(randScript, 5000000000)
var original = tx.__toBuffer
tx.__toBuffer = function (a, b, c) {
if (c !== false) throw new Error('hashForSignature MUST pass false')
return original.call(this, a, b, c)
}
assert.throws(function () {
tx.__toBuffer(undefined, undefined, true)
}, /hashForSignature MUST pass false/)
// assert hashForSignature does not pass false
assert.doesNotThrow(function () {
tx.hashForSignature(0, randScript, 1)
})
})
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)
})
})
})
describe('hashForWitnessV0', function () {
fixtures.hashForWitnessV0.forEach(function (f) {
it('should return ' + f.hash + ' for ' + (f.description ? ('case "' + f.description + '"') : ''), function () {
var tx = Transaction.fromHex(f.txHex)
var script = bscript.fromASM(f.script)
assert.strictEqual(tx.hashForWitnessV0(f.inIndex, script, f.value, f.type).toString('hex'), f.hash)
})
})
})
2016-11-14 06:13:21 +01:00
describe('setWitness', function () {
it('only accepts a a witness stack (Array of Buffers)', function () {
assert.throws(function () {
(new Transaction()).setWitness(0, 'foobar')
2016-11-25 04:27:48 +01:00
}, /Expected property "1" of type \[Buffer], got String "foobar"/)
2016-11-14 06:13:21 +01:00
})
})
})