From 74375bfedf702cccadc426c086d53e9334110ec9 Mon Sep 17 00:00:00 2001 From: junderw Date: Wed, 20 Mar 2019 15:25:48 +0900 Subject: [PATCH] Fix class constructors --- src/block.js | 56 +++++++++++----------- src/ecpair.js | 12 ++--- src/transaction_builder.js | 19 ++++---- ts_src/block.ts | 87 +++++++++++++++------------------- ts_src/ecpair.ts | 13 +++-- ts_src/transaction.ts | 15 ++---- ts_src/transaction_builder.ts | 14 +++--- types/block.d.ts | 1 - types/ecpair.d.ts | 6 +-- types/transaction.d.ts | 1 - types/transaction_builder.d.ts | 2 +- 11 files changed, 101 insertions(+), 125 deletions(-) diff --git a/src/block.js b/src/block.js index 913ad9a..da17193 100644 --- a/src/block.js +++ b/src/block.js @@ -9,25 +9,17 @@ const typeforce = require('typeforce'); const varuint = require('varuint-bitcoin'); const errorMerkleNoTxes = new TypeError('Cannot compute merkle root for zero transactions'); const errorWitnessNotSegwit = new TypeError('Cannot compute witness commit for non-segwit block'); -function txesHaveWitnessCommit(transactions) { - return (transactions instanceof Array && - transactions[0] && - transactions[0].ins && - transactions[0].ins instanceof Array && - transactions[0].ins[0] && - transactions[0].ins[0].witness && - transactions[0].ins[0].witness instanceof Array && - transactions[0].ins[0].witness.length > 0); -} -function anyTxHasWitness(transactions) { - return (transactions instanceof Array && - transactions.some(tx => typeof tx === 'object' && - tx.ins instanceof Array && - tx.ins.some(input => typeof input === 'object' && - input.witness instanceof Array && - input.witness.length > 0))); -} class Block { + constructor() { + this.version = 1; + this.prevHash = undefined; + this.merkleRoot = undefined; + this.timestamp = 0; + this.witnessCommit = undefined; + this.bits = 0; + this.nonce = 0; + this.transactions = undefined; + } static fromBuffer(buffer) { if (buffer.length < 80) throw new Error('Buffer too small (< 80 bytes)'); @@ -99,16 +91,6 @@ class Block { ? bcrypto.hash256(Buffer.concat([rootHash, transactions[0].ins[0].witness[0]])) : rootHash; } - constructor() { - this.version = 1; - this.timestamp = 0; - this.bits = 0; - this.nonce = 0; - this.prevHash = undefined; - this.merkleRoot = undefined; - this.witnessCommit = undefined; - this.transactions = undefined; - } getWitnessCommit() { if (!txesHaveWitnessCommit(this.transactions)) return null; @@ -220,3 +202,21 @@ class Block { } } exports.Block = Block; +function txesHaveWitnessCommit(transactions) { + return (transactions instanceof Array && + transactions[0] && + transactions[0].ins && + transactions[0].ins instanceof Array && + transactions[0].ins[0] && + transactions[0].ins[0].witness && + transactions[0].ins[0].witness instanceof Array && + transactions[0].ins[0].witness.length > 0); +} +function anyTxHasWitness(transactions) { + return (transactions instanceof Array && + transactions.some(tx => typeof tx === 'object' && + tx.ins instanceof Array && + tx.ins.some(input => typeof input === 'object' && + input.witness instanceof Array && + input.witness.length > 0))); +} diff --git a/src/ecpair.js b/src/ecpair.js index 1335014..2026c63 100644 --- a/src/ecpair.js +++ b/src/ecpair.js @@ -11,18 +11,16 @@ const isOptions = typeforce.maybe(typeforce.compile({ network: types.maybe(types.Network), })); class ECPair { - constructor(d, Q, options) { + constructor(__D, __Q, options) { + this.__D = __D; + this.__Q = __Q; if (options === undefined) options = {}; this.compressed = options.compressed === undefined ? true : options.compressed; this.network = options.network || NETWORKS.bitcoin; - this.__D = undefined; - this.__Q = undefined; - if (d !== undefined) - this.__D = d; - if (Q !== undefined) - this.__Q = ecc.pointCompress(Q, this.compressed); + if (__Q !== undefined) + this.__Q = ecc.pointCompress(__Q, this.compressed); } get privateKey() { return this.__D; diff --git a/src/transaction_builder.js b/src/transaction_builder.js index 0b876a7..d3b2673 100644 --- a/src/transaction_builder.js +++ b/src/transaction_builder.js @@ -20,6 +20,16 @@ function txIsTransaction(tx) { return tx instanceof transaction_1.Transaction; } class TransactionBuilder { + // WARNING: maximumFeeRate is __NOT__ to be relied on, + // it's just another potential safety mechanism (safety in-depth) + constructor(network = networks.bitcoin, maximumFeeRate = 2500) { + this.network = network; + this.maximumFeeRate = maximumFeeRate; + this.__PREV_TX_SET = {}; + this.__INPUTS = []; + this.__TX = new transaction_1.Transaction(); + this.__TX.version = 2; + } static fromTransaction(transaction, network) { const txb = new TransactionBuilder(network); // Copy transaction fields @@ -43,15 +53,6 @@ class TransactionBuilder { }); return txb; } - constructor(network, maximumFeeRate) { - this.__PREV_TX_SET = {}; - this.network = network || networks.bitcoin; - // WARNING: This is __NOT__ to be relied on, its just another potential safety mechanism (safety in-depth) - this.maximumFeeRate = maximumFeeRate || 2500; - this.__INPUTS = []; - this.__TX = new transaction_1.Transaction(); - this.__TX.version = 2; - } setLockTime(locktime) { typeforce(types.UInt32, locktime); // if any signatures exist, throw diff --git a/ts_src/block.ts b/ts_src/block.ts index 9a9bd45..820c075 100644 --- a/ts_src/block.ts +++ b/ts_src/block.ts @@ -14,36 +14,6 @@ const errorWitnessNotSegwit = new TypeError( 'Cannot compute witness commit for non-segwit block', ); -function txesHaveWitnessCommit(transactions: Transaction[]): boolean { - return ( - transactions instanceof Array && - transactions[0] && - transactions[0].ins && - transactions[0].ins instanceof Array && - transactions[0].ins[0] && - transactions[0].ins[0].witness && - transactions[0].ins[0].witness instanceof Array && - transactions[0].ins[0].witness.length > 0 - ); -} - -function anyTxHasWitness(transactions: Transaction[]): boolean { - return ( - transactions instanceof Array && - transactions.some( - tx => - typeof tx === 'object' && - tx.ins instanceof Array && - tx.ins.some( - input => - typeof input === 'object' && - input.witness instanceof Array && - input.witness.length > 0, - ), - ) - ); -} - export class Block { static fromBuffer(buffer: Buffer): Block { if (buffer.length < 80) throw new Error('Buffer too small (< 80 bytes)'); @@ -137,25 +107,14 @@ export class Block { : rootHash; } - version: number; - prevHash?: Buffer; - merkleRoot?: Buffer; - timestamp: number; - witnessCommit?: Buffer; - bits: number; - nonce: number; - transactions?: Transaction[]; - - constructor() { - this.version = 1; - this.timestamp = 0; - this.bits = 0; - this.nonce = 0; - this.prevHash = undefined; - this.merkleRoot = undefined; - this.witnessCommit = undefined; - this.transactions = undefined; - } + version: number = 1; + prevHash?: Buffer = undefined; + merkleRoot?: Buffer = undefined; + timestamp: number = 0; + witnessCommit?: Buffer = undefined; + bits: number = 0; + nonce: number = 0; + transactions?: Transaction[] = undefined; getWitnessCommit(): Buffer | null { if (!txesHaveWitnessCommit(this.transactions!)) return null; @@ -294,3 +253,33 @@ export class Block { return this.witnessCommit!.compare(actualWitnessCommit) === 0; } } + +function txesHaveWitnessCommit(transactions: Transaction[]): boolean { + return ( + transactions instanceof Array && + transactions[0] && + transactions[0].ins && + transactions[0].ins instanceof Array && + transactions[0].ins[0] && + transactions[0].ins[0].witness && + transactions[0].ins[0].witness instanceof Array && + transactions[0].ins[0].witness.length > 0 + ); +} + +function anyTxHasWitness(transactions: Transaction[]): boolean { + return ( + transactions instanceof Array && + transactions.some( + tx => + typeof tx === 'object' && + tx.ins instanceof Array && + tx.ins.some( + input => + typeof input === 'object' && + input.witness instanceof Array && + input.witness.length > 0, + ), + ) + ); +} diff --git a/ts_src/ecpair.ts b/ts_src/ecpair.ts index ec8dc13..9e56919 100644 --- a/ts_src/ecpair.ts +++ b/ts_src/ecpair.ts @@ -33,19 +33,18 @@ export interface ECPairInterface { class ECPair implements ECPairInterface { compressed: boolean; network: Network; - private __D?: Buffer; - private __Q?: Buffer; - constructor(d?: Buffer, Q?: Buffer, options?: ECPairOptions) { + constructor( + private __D?: Buffer, + private __Q?: Buffer, + options?: ECPairOptions, + ) { if (options === undefined) options = {}; this.compressed = options.compressed === undefined ? true : options.compressed; this.network = options.network || NETWORKS.bitcoin; - this.__D = undefined; - this.__Q = undefined; - if (d !== undefined) this.__D = d; - if (Q !== undefined) this.__Q = ecc.pointCompress(Q, this.compressed); + if (__Q !== undefined) this.__Q = ecc.pointCompress(__Q, this.compressed); } get privateKey(): Buffer | undefined { diff --git a/ts_src/transaction.ts b/ts_src/transaction.ts index 995f1d7..b788d91 100644 --- a/ts_src/transaction.ts +++ b/ts_src/transaction.ts @@ -182,17 +182,10 @@ export class Transaction { return true; } - version: number; - locktime: number; - ins: Input[]; - outs: OpenOutput[]; - - constructor() { - this.version = 1; - this.locktime = 0; - this.ins = []; - this.outs = []; - } + version: number = 1; + locktime: number = 0; + ins: Input[] = []; + outs: OpenOutput[] = []; isCoinbase(): boolean { return ( diff --git a/ts_src/transaction_builder.ts b/ts_src/transaction_builder.ts index 4c939e1..575fbc2 100644 --- a/ts_src/transaction_builder.ts +++ b/ts_src/transaction_builder.ts @@ -91,19 +91,17 @@ export class TransactionBuilder { return txb; } - network: Network; - maximumFeeRate: number; private __PREV_TX_SET: { [index: string]: boolean }; private __INPUTS: TxbInput[]; private __TX: Transaction; - constructor(network?: Network, maximumFeeRate?: number) { + // WARNING: maximumFeeRate is __NOT__ to be relied on, + // it's just another potential safety mechanism (safety in-depth) + constructor( + public network: Network = networks.bitcoin, + public maximumFeeRate: number = 2500, + ) { this.__PREV_TX_SET = {}; - this.network = network || networks.bitcoin; - - // WARNING: This is __NOT__ to be relied on, its just another potential safety mechanism (safety in-depth) - this.maximumFeeRate = maximumFeeRate || 2500; - this.__INPUTS = []; this.__TX = new Transaction(); this.__TX.version = 2; diff --git a/types/block.d.ts b/types/block.d.ts index 6b6596c..dd4fc55 100644 --- a/types/block.d.ts +++ b/types/block.d.ts @@ -13,7 +13,6 @@ export declare class Block { bits: number; nonce: number; transactions?: Transaction[]; - constructor(); getWitnessCommit(): Buffer | null; hasWitnessCommit(): boolean; hasWitness(): boolean; diff --git a/types/ecpair.d.ts b/types/ecpair.d.ts index 674615b..33535bd 100644 --- a/types/ecpair.d.ts +++ b/types/ecpair.d.ts @@ -16,11 +16,11 @@ export interface ECPairInterface { getPublicKey?(): Buffer; } declare class ECPair implements ECPairInterface { - compressed: boolean; - network: Network; private __D?; private __Q?; - constructor(d?: Buffer, Q?: Buffer, options?: ECPairOptions); + compressed: boolean; + network: Network; + constructor(__D?: Buffer | undefined, __Q?: Buffer | undefined, options?: ECPairOptions); readonly privateKey: Buffer | undefined; readonly publicKey: Buffer | undefined; toWIF(): string; diff --git a/types/transaction.d.ts b/types/transaction.d.ts index 4f8a2b9..9bdba19 100644 --- a/types/transaction.d.ts +++ b/types/transaction.d.ts @@ -30,7 +30,6 @@ export declare class Transaction { locktime: number; ins: Input[]; outs: OpenOutput[]; - constructor(); isCoinbase(): boolean; addInput(hash: Buffer, index: number, sequence?: number, scriptSig?: Buffer): number; addOutput(scriptPubKey: Buffer, value: number): number; diff --git a/types/transaction_builder.d.ts b/types/transaction_builder.d.ts index b3dedb3..16adee6 100644 --- a/types/transaction_builder.d.ts +++ b/types/transaction_builder.d.ts @@ -3,9 +3,9 @@ import { ECPairInterface } from './ecpair'; import { Network } from './networks'; import { Transaction } from './transaction'; export declare class TransactionBuilder { - static fromTransaction(transaction: Transaction, network?: Network): TransactionBuilder; network: Network; maximumFeeRate: number; + static fromTransaction(transaction: Transaction, network?: Network): TransactionBuilder; private __PREV_TX_SET; private __INPUTS; private __TX;