Browse Source

Add Transaction test types

addTSTests
junderw 6 years ago
parent
commit
c0cf5e2076
No known key found for this signature in database GPG Key ID: B256185D3A971908
  1. 49
      ts_test/fixtureTypes.ts
  2. 30
      ts_test/transaction.ts

49
ts_test/fixtureTypes.ts

@ -437,5 +437,54 @@ export interface FixtureTransactionBuilder {
}>; }>;
}; };
} }
export interface FixtureTransactionRaw {
version: number;
ins: Array<{
hash: string;
index: number;
data: string;
script: string;
sequence: number;
witness: string[];
}>;
outs: Array<{
script: string;
data: string;
value: number;
}>;
locktime: number;
}
export interface FixtureTransactionValid {
description: string;
id: string;
hash: string;
txHex: string;
script: string;
inIndex: number;
type: number;
value: number;
hex: string;
whex: string;
raw: FixtureTransactionRaw;
coinbase: boolean;
virtualSize: number;
weight: number;
}
export interface FixtureTransaction {
valid: FixtureTransactionValid[];
hashForSignature: FixtureTransactionValid[];
hashForWitnessV0: FixtureTransactionValid[];
invalid: {
addInput: Array<{
exception: string;
hash: string;
index: number;
}>;
fromBuffer: Array<{
exception: string;
hex: string;
}>;
};
}
// aa // aa

30
ts_test/transaction.ts

@ -1,18 +1,26 @@
import * as assert from 'assert'; import * as assert from 'assert';
import { beforeEach, describe, it } from 'mocha'; import { beforeEach, describe, it } from 'mocha';
import { script as bscript } from '..'; import { script as bscript } from '..';
const fixtures = require('../ts_test/fixtures/transaction'); import {
FixtureTransaction,
FixtureTransactionRaw,
FixtureTransactionValid,
} from './fixtureTypes';
const fixtures: FixtureTransaction = require('../ts_test/fixtures/transaction');
import { Transaction } from '..'; import { Transaction } from '..';
describe('Transaction', () => { describe('Transaction', () => {
function fromRaw(raw, noWitness?): Transaction { function fromRaw(
raw: FixtureTransactionRaw,
noWitness?: boolean,
): Transaction {
const tx = new Transaction(); const tx = new Transaction();
tx.version = raw.version; tx.version = raw.version;
tx.locktime = raw.locktime; tx.locktime = raw.locktime;
raw.ins.forEach((txIn, i) => { raw.ins.forEach((txIn, i) => {
const txHash = Buffer.from(txIn.hash, 'hex'); const txHash = Buffer.from(txIn.hash, 'hex');
let scriptSig; let scriptSig: Buffer;
if (txIn.data) { if (txIn.data) {
scriptSig = Buffer.from(txIn.data, 'hex'); scriptSig = Buffer.from(txIn.data, 'hex');
@ -32,7 +40,7 @@ describe('Transaction', () => {
}); });
raw.outs.forEach(txOut => { raw.outs.forEach(txOut => {
let script; let script: Buffer;
if (txOut.data) { if (txOut.data) {
script = Buffer.from(txOut.data, 'hex'); script = Buffer.from(txOut.data, 'hex');
@ -47,7 +55,7 @@ describe('Transaction', () => {
} }
describe('fromBuffer/fromHex', () => { describe('fromBuffer/fromHex', () => {
function importExport(f): void { function importExport(f: FixtureTransactionValid): void {
const id = f.id || f.hash; const id = f.id || f.hash;
const txHex = f.hex || f.txHex; const txHex = f.hex || f.txHex;
@ -154,7 +162,7 @@ describe('Transaction', () => {
}); });
describe('addInput', () => { describe('addInput', () => {
let prevTxHash; let prevTxHash: Buffer;
beforeEach(() => { beforeEach(() => {
prevTxHash = Buffer.from( prevTxHash = Buffer.from(
'ffffffff00ffff000000000000000000000000000000000000000000101010ff', 'ffffffff00ffff000000000000000000000000000000000000000000101010ff',
@ -199,8 +207,8 @@ describe('Transaction', () => {
describe('clone', () => { describe('clone', () => {
fixtures.valid.forEach(f => { fixtures.valid.forEach(f => {
let actual; let actual: Transaction;
let expected; let expected: Transaction;
beforeEach(() => { beforeEach(() => {
expected = Transaction.fromHex(f.hex); expected = Transaction.fromHex(f.hex);
@ -218,7 +226,7 @@ describe('Transaction', () => {
}); });
describe('getHash/getId', () => { describe('getHash/getId', () => {
function verify(f): void { function verify(f: FixtureTransactionValid): void {
it('should return the id for ' + f.id + '(' + f.description + ')', () => { it('should return the id for ' + f.id + '(' + f.description + ')', () => {
const tx = Transaction.fromHex(f.whex || f.hex); const tx = Transaction.fromHex(f.whex || f.hex);
@ -231,7 +239,7 @@ describe('Transaction', () => {
}); });
describe('isCoinbase', () => { describe('isCoinbase', () => {
function verify(f): void { function verify(f: FixtureTransactionValid): void {
it( it(
'should return ' + 'should return ' +
f.coinbase + f.coinbase +
@ -268,7 +276,7 @@ describe('Transaction', () => {
// @ts-ignore // @ts-ignore
const original = tx.__toBuffer; const original = tx.__toBuffer;
// @ts-ignore // @ts-ignore
tx.__toBuffer = (a, b, c): Buffer => { tx.__toBuffer = (a?: Buffer, b?: number, c?: boolean): Buffer => {
if (c !== false) throw new Error('hashForSignature MUST pass false'); if (c !== false) throw new Error('hashForSignature MUST pass false');
return original.call(this, a, b, c); return original.call(this, a, b, c);

Loading…
Cancel
Save