diff --git a/test/fixtureTypes.js b/test/fixtureTypes.js new file mode 100644 index 0000000..c0b93be --- /dev/null +++ b/test/fixtureTypes.js @@ -0,0 +1,3 @@ +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); +// aa diff --git a/ts_test/address.ts b/ts_test/address.ts index 2305fe7..3fcbca2 100644 --- a/ts_test/address.ts +++ b/ts_test/address.ts @@ -1,9 +1,10 @@ import * as bitcoinjs from '..'; +import { FixtureAddress } from './fixtureTypes'; const baddress = bitcoinjs.address; const bscript = bitcoinjs.script; const { describe, it } = require('mocha'); const assert = require('assert'); -const fixtures = require('../ts_test/fixtures/address.json'); +const fixtures: FixtureAddress = require('../ts_test/fixtures/address.json'); // @ts-ignore const NETWORKS = Object.assign( { @@ -140,7 +141,7 @@ describe('address', () => { fixtures.invalid.toOutputScript.forEach(f => { it('throws when ' + f.exception, () => { assert.throws(() => { - baddress.toOutputScript(f.address, f.network); + baddress.toOutputScript(f.address, f.network as any); }, new RegExp(f.address + ' ' + f.exception)); }); }); diff --git a/ts_test/bitcoin.core.ts b/ts_test/bitcoin.core.ts index 4673f90..f9722dd 100644 --- a/ts_test/bitcoin.core.ts +++ b/ts_test/bitcoin.core.ts @@ -1,16 +1,27 @@ import * as bitcoin from '..'; +import { + CoreBase58Attributes, + CoreBase58EncodeDecode, + CoreBase58KeysInvalid, + CoreBase58KeysValid, + CoreBlocks, + CoreSigCanonical, + CoreSigHash, + CoreSigNonCanonical, + CoreTxValid, +} from './fixtureTypes'; const { describe, it } = require('mocha'); const assert = require('assert'); const base58 = require('bs58'); -const base58EncodeDecode = require('../ts_test/fixtures/core/base58_encode_decode.json'); -const base58KeysInvalid = require('../ts_test/fixtures/core/base58_keys_invalid.json'); -const base58KeysValid = require('../ts_test/fixtures/core/base58_keys_valid.json'); -const blocksValid = require('../ts_test/fixtures/core/blocks.json'); -const sigCanonical = require('../ts_test/fixtures/core/sig_canonical.json'); -const sigHash = require('../ts_test/fixtures/core/sighash.json'); -const sigNoncanonical = require('../ts_test/fixtures/core/sig_noncanonical.json'); -const txValid = require('../ts_test/fixtures/core/tx_valid.json'); +const base58EncodeDecode: CoreBase58EncodeDecode = require('../ts_test/fixtures/core/base58_encode_decode.json'); +const base58KeysInvalid: CoreBase58KeysInvalid = require('../ts_test/fixtures/core/base58_keys_invalid.json'); +const base58KeysValid: CoreBase58KeysValid = require('../ts_test/fixtures/core/base58_keys_valid.json'); +const blocksValid: CoreBlocks = require('../ts_test/fixtures/core/blocks.json'); +const sigCanonical: CoreSigCanonical = require('../ts_test/fixtures/core/sig_canonical.json'); +const sigHash: CoreSigHash = require('../ts_test/fixtures/core/sighash.json'); +const sigNoncanonical: CoreSigNonCanonical = require('../ts_test/fixtures/core/sig_noncanonical.json'); +const txValid: CoreTxValid = require('../ts_test/fixtures/core/tx_valid.json'); describe('Bitcoin-core', () => { // base58EncodeDecode @@ -44,8 +55,8 @@ describe('Bitcoin-core', () => { base58KeysValid.forEach(f => { const expected = f[0]; - const hash = Buffer.from(f[1], 'hex'); - const params = f[2]; + const hash = Buffer.from(f[1] as string, 'hex'); + const params = f[2] as CoreBase58Attributes; if (params.isPrivkey) return; @@ -92,9 +103,9 @@ describe('Bitcoin-core', () => { // base58KeysValid describe('ECPair', () => { base58KeysValid.forEach(f => { - const string0 = f[0]; + const string0 = f[0] as string; const hex = f[1]; - const params = f[2]; + const params = f[2] as CoreBase58Attributes; if (!params.isPrivkey) return; @@ -154,13 +165,13 @@ describe('Bitcoin-core', () => { // const verifyFlags = f[2] // TODO: do we need to test this? it('can decode ' + fhex, () => { - const transaction = bitcoin.Transaction.fromHex(fhex); + const transaction = bitcoin.Transaction.fromHex(fhex as string); transaction.ins.forEach((txIn, i) => { const input = inputs[i]; // reverse because test data is reversed - const prevOutHash = Buffer.from(input[0], 'hex').reverse(); + const prevOutHash = Buffer.from(input[0] as string, 'hex').reverse(); const prevOutIndex = input[1]; assert.deepStrictEqual(txIn.hash, prevOutHash); @@ -178,10 +189,10 @@ describe('Bitcoin-core', () => { // Objects that are only a single string are ignored if (f.length === 1) return; - const txHex = f[0]; - const scriptHex = f[1]; - const inIndex = f[2]; - const hashType = f[3]; + const txHex = f[0] as string; + const scriptHex = f[1] as string; + const inIndex = f[2] as number; + const hashType = f[3] as number; const expectedHash = f[4]; const hashTypes = []; diff --git a/ts_test/block.ts b/ts_test/block.ts index 8682897..1626f46 100644 --- a/ts_test/block.ts +++ b/ts_test/block.ts @@ -1,8 +1,9 @@ import { Block } from '..'; +import { FixtureBlock } from './fixtureTypes'; const { describe, it, beforeEach } = require('mocha'); const assert = require('assert'); -const fixtures = require('../ts_test/fixtures/block'); +const fixtures: FixtureBlock = require('../ts_test/fixtures/block'); describe('Block', () => { describe('version', () => { diff --git a/ts_test/fixtureTypes.ts b/ts_test/fixtureTypes.ts new file mode 100644 index 0000000..f8f5c23 --- /dev/null +++ b/ts_test/fixtureTypes.ts @@ -0,0 +1,524 @@ +// fixtures/core folder +export type CoreBase58EncodeDecode = [string[]]; +export type CoreBase58KeysInvalid = [string[]]; +export interface CoreBase58Attributes { + isPrivkey: boolean; + isTestnet: boolean; + isCompressed?: boolean; + addrType?: string; +} +export type CoreBase58KeysValid = [Array<string | CoreBase58Attributes>]; +export type CoreBlocks = [ + { + id: string; + transactions: number; + hex: string; + } +]; +export type CoreSigCanonical = string[]; +export type CoreSigNonCanonical = string[]; +export type CoreSigHash = [Array<string | number>]; +export type CoreTxValid = [string[] | Array<string | [Array<string | number>]>]; + +// fixtures folder +export interface FixtureAddress { + standard: Array<{ + network: string; + version: number; + script: string; + hash?: string; + base58check?: string; + bech32?: string; + data?: string; + }>; + bech32: Array<{ + bech32: string; + address: string; + version: number; + prefix: string; + data: string; + }>; + invalid: { + bech32: Array<{ + version: number; + prefix: string; + data: string; + bech32: string; + address: string; + exception: string; + }>; + fromBase58Check: Array<{ + address: string; + exception: string; + }>; + fromOutputScript: Array<{ + exception: string; + script: string; + }>; + toOutputScript: Array<{ + exception: string; + address: string; + network?: { + bech32: string; + }; + }>; + }; +} +export interface FixtureBlock { + targets: Array<{ + bits: string; + expected: string; + }>; + valid: Array<{ + description: string; + bits: number; + hash: string; + hex: string; + id: string; + merkleRoot: string; + nonce: number; + prevHash: string; + timestamp: number; + valid: boolean; + version: number; + height?: number; + witnessCommit?: string; + }>; + invalid: Array<{ + exception: string; + hex: string; + }>; +} +export interface FixtureBufferUtils { + valid: Array<{ + dec: number; + hex: string; + }>; + invalid: { + readUInt64LE: Array<{ + description: string; + exception: string; + hex: string; + dec: number; + }>; + }; +} +export type FixtureCrypto = Array<{ + hex: string; + hash160: string; + hash256: string; + ripemd160: string; + sha1: string; + sha256: string; +}>; +export interface FixtureEcdsa { + valid: { + ecdsa: Array<{ + d: string; + k: string; + message: string; + signature: { + r: string; + s: string; + }; + }>; + rfc6979: Array<{ + message: string; + d: string; + k0: string; + k1: string; + k15: string; + }>; + }; + invalid: { + verify: Array<{ + description: string; + d: string; + message: string; + signature: { + r: string; + s: string; + }; + }>; + }; +} +export interface FixtureECPair { + valid: Array<{ + d: string; + Q: string; + compressed: boolean; + network: string; + address: string; + WIF: string; + }>; + invalid: { + fromPrivateKey: Array<{ + exception: string; + d: string; + options?: { + network?: {}; + compressed?: number; + }; + }>; + fromPublicKey: Array<{ + exception: string; + Q: string; + options: { + network?: {}; + }; + description?: string; + }>; + fromWIF: Array<{ + exception: string; + WIF: string; + network?: string; + }>; + }; +} +export interface FixtureEmbed { + valid: Array<{ + description: string; + arguments: { + output?: string; + data?: string[]; + }; + options?: {}; + expected: { + output: string; + data: string[]; + input: null; + witness: null; + }; + }>; + invalid: Array<{ + exception: string; + arguments: { + output?: string; + }; + description?: string; + options?: {}; + }>; + dynamic: { + depends: { + data: string[]; + output: string[]; + }; + details: Array<{ + description: string; + data: string[]; + output: string; + }>; + }; +} + +export type FixtureScriptNumber = [ + { + hex: string; + number: number; + bytes?: number; + } +]; +export interface FixtureScript { + valid: Array<{ + asm: string; + script: string; + stack?: string[]; + nonstandard?: { + scriptSig: string; + scriptSigHex: string; + }; + }>; + invalid: { + decompile: Array<{ + description: string; + script: string; + }>; + fromASM: Array<{ + description: string; + script: string; + }>; + }; +} +export interface FixtureSignature { + valid: Array<{ + hex: string; + hashType: number; + raw: { + r: string; + s: string; + }; + }>; + invalid: Array<{ + exception: string; + hex: string; + hashType?: number; + raw?: { + r: string; + s: string; + }; + }>; +} +export interface FixtureTemplates { + valid: Array<{ + type: string; + typeIncomplete?: string; + pubKey?: string; + pubKeys?: string[]; + data?: string[]; + witnessScript?: string; + witnessData?: string[]; + signature?: string; + signatures?: Array<string | null>; + redeemScript?: string; + redeemScriptSig?: string; + input?: string; + inputHex?: string; + output?: string; + outputHex?: string; + inputStack?: string[]; + nonstandard?: { + input: string; + inputHex: string; + }; + }>; + invalid: { + pubKey: { + inputs: Array<{ + description: string; + input: string; + }>; + outputs: Array<{ + description: string; + outputHex?: string; + output?: string; + }>; + }; + pubKeyHash: { + inputs: Array<{ + description: string; + input: string; + }>; + outputs: Array<{ + description?: string; + outputHex?: string; + exception?: string; + hash?: string; + }>; + }; + scriptHash: { + inputs: Array<{ + description: string; + input: string; + }>; + outputs: Array<{ + description?: string; + outputHex?: string; + exception?: string; + hash?: string; + }>; + }; + multisig: { + inputs: Array<{ + output: string; + signatures: string[]; + exception?: string; + description?: string; + type?: string; + }>; + outputs: Array<{ + description?: string; + output?: string; + exception?: string; + m?: number; + pubKeys?: string[]; + signatures?: string[]; + }>; + }; + witnessPubKeyHash: { + inputs: []; + outputs: Array<{ + description?: string; + outputHex?: string; + exception?: string; + hash?: string; + }>; + }; + witnessScriptHash: { + inputs: []; + outputs: Array<{ + description?: string; + outputHex?: string; + exception?: string; + hash?: string; + }>; + }; + witnessCommitment: { + inputs: []; + outputs: Array<{ + exception?: string; + commitment?: string; + description?: string; + scriptPubKeyHex?: string; + }>; + }; + }; +} +export interface FixtureTransactionBuilder { + valid: { + build: Array<{ + description: string; + txHex: string; + version?: number | null; + locktime?: number; + network?: string; + incomplete?: boolean; + outputs: Array<{ + script: string; + value: number; + }>; + inputs: Array<{ + vout: number; + txHex?: string; + txId?: string; + sequence?: number; + prevTxScript?: string; + signs: Array<{ + keyPair: string; + throws?: boolean; + value?: number; + witnessScript?: string; + redeemScript?: string; + network?: string; + hashType?: number; + }>; + }>; + }>; + fromTransaction: Array<{ + description: string; + network: string; + incomplete?: boolean; + outputs: Array<{ + script: string; + value: number; + }>; + inputs: Array<{ + vout: number; + txHex?: string; + txId?: string; + prevTxScript?: string; + signs: Array<{ + keyPair: string; + throws?: boolean; + value?: number; + witnessScript?: string; + redeemScript?: string; + network?: string; + hashType?: number; + }>; + }>; + }>; + fromTransactionSequential: Array<{ + description: string; + network: string; + txHex: string; + txHexAfter: string; + version: number; + incomplete: boolean; + inputs: Array<{ + vout: number; + scriptSig: string; + scriptSigAfter: string; + signs: Array<{ + keyPair: string; + redeemScript?: string; + }>; + }>; + }>; + classification: { + hex: string; + }; + multisig: Array<{ + description: string; + network: string; + txHex: string; + version: number; + outputs: Array<{ + script: string; + value: number; + }>; + inputs: Array<{ + vout: number; + txHex?: string; + txId?: string; + prevTxScript?: string; + signs: Array<{ + keyPair: string; + throws?: boolean; + value?: number; + witnessScript?: string; + redeemScript?: string; + network?: string; + hashType?: number; + }>; + }>; + }>; + }; + invalid: { + build: Array<{ + exception: string; + description?: string; + network?: string; + incomplete?: boolean; + txHex?: string; + outputs?: Array<{ + script: string; + value: number; + }>; + inputs?: Array<{ + vout: number; + txHex?: string; + txId?: string; + prevTxScript?: string; + signs: Array<{ + keyPair: string; + throws?: boolean; + value?: number; + witnessScript?: string; + redeemScript?: string; + network?: string; + hashType?: number; + }>; + }>; + }>; + sign: Array<{ + description?: string; + network?: string; + exception: string; + inputs: Array<{ + vout: number; + txHex?: string; + txId?: string; + prevTxScript?: string; + signs: Array<{ + keyPair: string; + throws?: boolean; + value?: number; + witnessScript?: string; + redeemScript?: string; + network?: string; + hashType?: number; + }>; + }>; + outputs: Array<{ + script: string; + value: number; + }>; + }>; + fromTransaction: Array<{ + exception: string; + txHex: string; + }>; + }; +} + +// aa