From 4cddc83016af9fbbaf677a72b09bfe97e7756e19 Mon Sep 17 00:00:00 2001 From: junderw Date: Sat, 29 Dec 2018 00:53:54 +0900 Subject: [PATCH] noImplicitAny is now true --- package.json | 2 +- src/address.ts | 6 +++--- src/block.ts | 2 +- src/ecpair.ts | 14 +++++++------- src/index.ts | 2 +- src/payments/p2pkh.ts | 7 +------ src/payments/p2sh.ts | 7 +------ src/script.ts | 6 +++--- src/script_number.ts | 2 +- src/script_signature.ts | 2 +- src/transaction.ts | 32 ++++++++++++++++---------------- src/transaction_builder.ts | 10 +++++----- src/types.ts | 16 ++++++++++++++-- tsconfig.json | 2 +- 14 files changed, 56 insertions(+), 54 deletions(-) diff --git a/package.json b/package.json index 2583afc..55454bc 100644 --- a/package.json +++ b/package.json @@ -16,7 +16,7 @@ "scripts": { "coverage-report": "nyc report --reporter=lcov", "coverage-html": "nyc report --reporter=html", - "coverage": "nyc --check-coverage --branches 80 --functions 80 mocha", + "coverage": "nyc --check-coverage --branches 80 --functions 80 --lines 80 mocha", "integration": "npm run build && mocha --timeout 50000 test/integration/", "standard": "standard", "test": "npm run build && npm run standard && npm run coverage", diff --git a/src/address.ts b/src/address.ts index e657d24..f273b43 100644 --- a/src/address.ts +++ b/src/address.ts @@ -1,13 +1,13 @@ +import * as Networks from './networks' +import { Network } from './networks' +import * as types from './types' const Buffer = require('safe-buffer').Buffer const bech32 = require('bech32') const bs58check = require('bs58check') const bscript = require('./script') const networks = require('./networks') const typeforce = require('typeforce') -const types = require('./types') const payments = require('./payments') -import * as Networks from './networks' -import { Network } from './networks' export type Base58CheckResult = { hash: Buffer; diff --git a/src/block.ts b/src/block.ts index 92c4fb1..03ff901 100644 --- a/src/block.ts +++ b/src/block.ts @@ -1,9 +1,9 @@ import { Transaction } from './transaction' +import * as types from './types' const Buffer = require('safe-buffer').Buffer const bcrypto = require('./crypto') const fastMerkleRoot = require('merkle-lib/fastRoot') const typeforce = require('typeforce') -const types = require('./types') const varuint = require('varuint-bitcoin') const errorMerkleNoTxes = new TypeError('Cannot compute merkle root for zero transactions') diff --git a/src/ecpair.ts b/src/ecpair.ts index eceb2a1..cb835d1 100644 --- a/src/ecpair.ts +++ b/src/ecpair.ts @@ -1,9 +1,9 @@ import { Network } from './networks' import * as NETWORKS from './networks' +import * as types from './types' const ecc = require('tiny-secp256k1') const randomBytes = require('randombytes') const typeforce = require('typeforce') -const types = require('./types') const wif = require('wif') const isOptions = typeforce.maybe(typeforce.compile({ @@ -14,7 +14,7 @@ const isOptions = typeforce.maybe(typeforce.compile({ interface ECPairOptions { compressed?: boolean network?: Network - rng?(Buffer): Buffer + rng?(arg0: Buffer): Buffer } export interface ECPairInterface { @@ -75,19 +75,19 @@ function fromPrivateKey (buffer: Buffer, options: ECPairOptions): ECPair { return new ECPair(buffer, null, options) } -function fromPublicKey (buffer, options): ECPair { +function fromPublicKey (buffer: Buffer, options: ECPairOptions): ECPair { typeforce(ecc.isPoint, buffer) typeforce(isOptions, options) return new ECPair(null, buffer, options) } -function fromWIF (string, network): ECPair { +function fromWIF (string: string, network: Network | Array): ECPair { const decoded = wif.decode(string) const version = decoded.version // list of networks? if (types.Array(network)) { - network = network.filter(function (x) { + network = (>network).filter(function (x: Network) { return version === x.wif }).pop() @@ -97,12 +97,12 @@ function fromWIF (string, network): ECPair { } else { network = network || NETWORKS.bitcoin - if (version !== network.wif) throw new Error('Invalid network version') + if (version !== (network).wif) throw new Error('Invalid network version') } return fromPrivateKey(decoded.privateKey, { compressed: decoded.compressed, - network: network + network: network }) } diff --git a/src/index.ts b/src/index.ts index 8019cb7..d93f587 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,11 +1,11 @@ const opcodes = require('bitcoin-ops') +const bip32 = require('bip32') import { Block } from './block' import * as ECPair from './ecpair' import { Transaction } from './transaction' import { TransactionBuilder } from './transaction_builder' import * as address from './address' -import * as bip32 from 'bip32' import * as crypto from './crypto' import * as networks from './networks' import * as payments from './payments' diff --git a/src/payments/p2pkh.ts b/src/payments/p2pkh.ts index 4324c2a..e32f933 100644 --- a/src/payments/p2pkh.ts +++ b/src/payments/p2pkh.ts @@ -41,12 +41,7 @@ export function p2pkh (a: Payment, opts: PaymentOpts): Payment { const _chunks = lazy.value(function () { return bscript.decompile(a.input) }) const network = a.network || BITCOIN_NETWORK - const o = { - network, - hash: undefined, - pubkey: undefined, - input: undefined, - } + const o: Payment = { network } lazy.prop(o, 'address', function () { if (!o.hash) return diff --git a/src/payments/p2sh.ts b/src/payments/p2sh.ts index efd453a..2219f33 100644 --- a/src/payments/p2sh.ts +++ b/src/payments/p2sh.ts @@ -51,12 +51,7 @@ export function p2sh (a: Payment, opts: PaymentOpts): Payment { network = (a.redeem && a.redeem.network) || BITCOIN_NETWORK } - const o = { - network, - hash: undefined, - redeem: undefined, - input: undefined, - } + const o: Payment = { network } const _address = lazy.value(function () { const payload = bs58check.decode(a.address) diff --git a/src/script.ts b/src/script.ts index 98b018a..88bc46e 100644 --- a/src/script.ts +++ b/src/script.ts @@ -1,9 +1,9 @@ +import * as types from './types' const Buffer = require('safe-buffer').Buffer const bip66 = require('bip66') const ecc = require('tiny-secp256k1') const pushdata = require('pushdata-bitcoin') const typeforce = require('typeforce') -const types = require('./types') const scriptNumber = require('./script_number') const OPS = require('bitcoin-ops') @@ -100,7 +100,7 @@ export function decompile (buffer: Buffer | Array): Array = [] let i = 0 while (i < buffer.length) { @@ -123,7 +123,7 @@ export function decompile (buffer: Buffer | Array): Arrayop) } else { chunks.push(data) } diff --git a/src/script_number.ts b/src/script_number.ts index 8178c95..0ff4a7e 100644 --- a/src/script_number.ts +++ b/src/script_number.ts @@ -32,7 +32,7 @@ export function decode (buffer: Buffer, maxLength?: number, minimal?: boolean): return result } -function scriptNumSize (i) { +function scriptNumSize (i: number): number { return i > 0x7fffffff ? 5 : i > 0x7fffff ? 4 : i > 0x7fff ? 3 diff --git a/src/script_signature.ts b/src/script_signature.ts index fefd09d..c24e88a 100644 --- a/src/script_signature.ts +++ b/src/script_signature.ts @@ -1,7 +1,7 @@ +import * as types from './types' const bip66 = require('bip66') const Buffer = require('safe-buffer').Buffer const typeforce = require('typeforce') -const types = require('./types') const ZERO = Buffer.alloc(1, 0) function toDER (x: Buffer): Buffer { diff --git a/src/transaction.ts b/src/transaction.ts index 88e4280..91956c5 100644 --- a/src/transaction.ts +++ b/src/transaction.ts @@ -1,10 +1,10 @@ import * as bcrypto from './crypto' +import * as bscript from './script' +import * as types from './types' const Buffer = require('safe-buffer').Buffer -const bscript = require('./script') const bufferutils = require('./bufferutils') const opcodes = require('bitcoin-ops') const typeforce = require('typeforce') -const types = require('./types') const varuint = require('varuint-bitcoin') function varSliceSize (someScript: Buffer): number { @@ -73,7 +73,7 @@ export class Transaction { this.ins = [] this.outs = [] } - + static fromBuffer (buffer: Buffer, __noStrict: boolean): Transaction { let offset: number = 0 @@ -294,7 +294,7 @@ export class Transaction { // ignore OP_CODESEPARATOR const ourScript = bscript.compile(bscript.decompile(prevOutScript).filter((x) => { - return x !== opcodes.OP_CODESEPARATOR + return x !== opcodes.OP_CODESEPARATOR })) const txTmp = this.clone() @@ -465,46 +465,46 @@ export class Transaction { return Buffer.from(this.getHash(false).reverse()).toString('hex') } - toBuffer (buffer: Buffer | void, initialOffset: number | void): Buffer { + toBuffer (buffer?: Buffer, initialOffset?: number): Buffer { return this.__toBuffer(buffer, initialOffset, true) } - __toBuffer (buffer: Buffer | void, initialOffset: number | void, __allowWitness: boolean | void): Buffer { + __toBuffer (buffer?: Buffer, initialOffset?: number, __allowWitness?: boolean): Buffer { if (!buffer) buffer = Buffer.allocUnsafe(this.__byteLength((__allowWitness))) let offset = initialOffset || 0 - function writeSlice (slice) { + function writeSlice (slice: Buffer): void { offset += slice.copy(buffer, offset) } - function writeUInt8 (i) { + function writeUInt8 (i: number) { offset = (buffer).writeUInt8(i, offset) } - function writeUInt32 (i) { + function writeUInt32 (i: number) { offset = (buffer).writeUInt32LE(i, offset) } - function writeInt32 (i) { + function writeInt32 (i: number) { offset = (buffer).writeInt32LE(i, offset) } - function writeUInt64 (i) { + function writeUInt64 (i: number) { offset = bufferutils.writeUInt64LE(buffer, i, offset) } - function writeVarInt (i) { + function writeVarInt (i: number) { varuint.encode(i, buffer, offset) offset += varuint.encode.bytes } - function writeVarSlice (slice) { + function writeVarSlice (slice: Buffer) { writeVarInt(slice.length) writeSlice(slice) } - function writeVector (vector) { + function writeVector (vector: Array) { writeVarInt(vector.length) vector.forEach(writeVarSlice) } @@ -555,13 +555,13 @@ export class Transaction { return this.toBuffer(undefined, undefined).toString('hex') } - setInputScript (index, scriptSig) { + setInputScript (index: number, scriptSig: Buffer) { typeforce(types.tuple(types.Number, types.Buffer), arguments) this.ins[index].script = scriptSig } - setWitness (index, witness) { + setWitness (index: number, witness: Array) { typeforce(types.tuple(types.Number, [types.Buffer]), arguments) this.ins[index].witness = witness diff --git a/src/transaction_builder.ts b/src/transaction_builder.ts index 01717ab..e4c9c09 100644 --- a/src/transaction_builder.ts +++ b/src/transaction_builder.ts @@ -2,6 +2,7 @@ import { Network } from './networks' import * as networks from './networks' import { Transaction, Output } from './transaction' import { ECPairInterface } from './ecpair' +import * as types from './types' const Buffer = require('safe-buffer').Buffer const baddress = require('./address') const bcrypto = require('./crypto') @@ -9,7 +10,6 @@ const bscript = require('./script') const ops = require('bitcoin-ops') const payments = require('./payments') const typeforce = require('typeforce') -const types = require('./types') const classify = require('./classify') const SCRIPT_TYPES = classify.types @@ -53,7 +53,7 @@ function txIsTransaction(tx: Buffer | string | Transaction): tx is Transaction { export class TransactionBuilder { network: Network maximumFeeRate: number - private __prevTxSet: Object + private __prevTxSet: { [index: string]: boolean } private __inputs: Array private __tx: Transaction @@ -192,7 +192,7 @@ export class TransactionBuilder { return vin } - addOutput (scriptPubKey: string | Buffer, value): number { + addOutput (scriptPubKey: string | Buffer, value: number): number { if (!this.__canModifyOutputs()) { throw new Error('No, this would invalidate signatures') } @@ -282,7 +282,7 @@ export class TransactionBuilder { } // ready to sign - let signatureHash + let signatureHash: Buffer if (input.hasWitness) { signatureHash = this.__tx.hashForWitnessV0(vin, input.signScript, input.value, hashType) } else { @@ -573,7 +573,7 @@ function expandOutput (script: Buffer, ourPubKey?: Buffer): TxbOutput { return { type, pubkeys: p2ms.pubkeys, - signatures: p2ms.pubkeys.map(() => undefined), + signatures: p2ms.pubkeys.map((): undefined => undefined), maxSignatures: p2ms.m } } diff --git a/src/types.ts b/src/types.ts index ad603c5..8eacbdc 100644 --- a/src/types.ts +++ b/src/types.ts @@ -33,5 +33,17 @@ export const Network = typeforce.compile({ export const Buffer256bit = typeforce.BufferN(32) export const Hash160bit = typeforce.BufferN(20) export const Hash256bit = typeforce.BufferN(32) -export * from 'typeforce' -export { Number, Array } from 'typeforce' +export const Number = typeforce.Number +export const Array = typeforce.Array +export const Boolean = typeforce.Boolean +export const String = typeforce.String +export const Buffer = typeforce.Buffer +export const Hex = typeforce.Hex +export const maybe = typeforce.maybe +export const tuple = typeforce.tuple +export const UInt8 = typeforce.UInt8 +export const UInt32 = typeforce.UInt32 +export const Function = typeforce.Function +export const BufferN = typeforce.BufferN +export const Null = typeforce.Null +export const oneOf = typeforce.oneOf diff --git a/tsconfig.json b/tsconfig.json index 8803b83..f8bd5e2 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -9,7 +9,7 @@ ], "allowJs": false, "strict": false, - "noImplicitAny": false, + "noImplicitAny": true, "strictNullChecks": false, "strictFunctionTypes": true, "strictBindCallApply": true,