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