Fix class constructors

This commit is contained in:
junderw 2019-03-20 15:25:48 +09:00
parent fabd1d6c9d
commit 74375bfedf
No known key found for this signature in database
GPG key ID: B256185D3A971908
11 changed files with 101 additions and 125 deletions

View file

@ -9,25 +9,17 @@ const typeforce = require('typeforce');
const varuint = require('varuint-bitcoin'); const varuint = require('varuint-bitcoin');
const errorMerkleNoTxes = new TypeError('Cannot compute merkle root for zero transactions'); const errorMerkleNoTxes = new TypeError('Cannot compute merkle root for zero transactions');
const errorWitnessNotSegwit = new TypeError('Cannot compute witness commit for non-segwit block'); 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 { 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) { static fromBuffer(buffer) {
if (buffer.length < 80) if (buffer.length < 80)
throw new Error('Buffer too small (< 80 bytes)'); 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]])) ? bcrypto.hash256(Buffer.concat([rootHash, transactions[0].ins[0].witness[0]]))
: rootHash; : 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() { getWitnessCommit() {
if (!txesHaveWitnessCommit(this.transactions)) if (!txesHaveWitnessCommit(this.transactions))
return null; return null;
@ -220,3 +202,21 @@ class Block {
} }
} }
exports.Block = 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)));
}

View file

@ -11,18 +11,16 @@ const isOptions = typeforce.maybe(typeforce.compile({
network: types.maybe(types.Network), network: types.maybe(types.Network),
})); }));
class ECPair { class ECPair {
constructor(d, Q, options) { constructor(__D, __Q, options) {
this.__D = __D;
this.__Q = __Q;
if (options === undefined) if (options === undefined)
options = {}; options = {};
this.compressed = this.compressed =
options.compressed === undefined ? true : options.compressed; options.compressed === undefined ? true : options.compressed;
this.network = options.network || NETWORKS.bitcoin; this.network = options.network || NETWORKS.bitcoin;
this.__D = undefined; if (__Q !== undefined)
this.__Q = undefined; this.__Q = ecc.pointCompress(__Q, this.compressed);
if (d !== undefined)
this.__D = d;
if (Q !== undefined)
this.__Q = ecc.pointCompress(Q, this.compressed);
} }
get privateKey() { get privateKey() {
return this.__D; return this.__D;

View file

@ -20,6 +20,16 @@ function txIsTransaction(tx) {
return tx instanceof transaction_1.Transaction; return tx instanceof transaction_1.Transaction;
} }
class TransactionBuilder { 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) { static fromTransaction(transaction, network) {
const txb = new TransactionBuilder(network); const txb = new TransactionBuilder(network);
// Copy transaction fields // Copy transaction fields
@ -43,15 +53,6 @@ class TransactionBuilder {
}); });
return txb; 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) { setLockTime(locktime) {
typeforce(types.UInt32, locktime); typeforce(types.UInt32, locktime);
// if any signatures exist, throw // if any signatures exist, throw

View file

@ -14,36 +14,6 @@ const errorWitnessNotSegwit = new TypeError(
'Cannot compute witness commit for non-segwit block', '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 { export class Block {
static fromBuffer(buffer: Buffer): Block { static fromBuffer(buffer: Buffer): Block {
if (buffer.length < 80) throw new Error('Buffer too small (< 80 bytes)'); if (buffer.length < 80) throw new Error('Buffer too small (< 80 bytes)');
@ -137,25 +107,14 @@ export class Block {
: rootHash; : rootHash;
} }
version: number; version: number = 1;
prevHash?: Buffer; prevHash?: Buffer = undefined;
merkleRoot?: Buffer; merkleRoot?: Buffer = undefined;
timestamp: number; timestamp: number = 0;
witnessCommit?: Buffer; witnessCommit?: Buffer = undefined;
bits: number; bits: number = 0;
nonce: number; nonce: number = 0;
transactions?: Transaction[]; transactions?: Transaction[] = undefined;
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(): Buffer | null { getWitnessCommit(): Buffer | null {
if (!txesHaveWitnessCommit(this.transactions!)) return null; if (!txesHaveWitnessCommit(this.transactions!)) return null;
@ -294,3 +253,33 @@ export class Block {
return this.witnessCommit!.compare(actualWitnessCommit) === 0; 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,
),
)
);
}

View file

@ -33,19 +33,18 @@ export interface ECPairInterface {
class ECPair implements ECPairInterface { class ECPair implements ECPairInterface {
compressed: boolean; compressed: boolean;
network: Network; 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 = {}; if (options === undefined) options = {};
this.compressed = this.compressed =
options.compressed === undefined ? true : options.compressed; options.compressed === undefined ? true : options.compressed;
this.network = options.network || NETWORKS.bitcoin; this.network = options.network || NETWORKS.bitcoin;
this.__D = undefined; if (__Q !== undefined) this.__Q = ecc.pointCompress(__Q, this.compressed);
this.__Q = undefined;
if (d !== undefined) this.__D = d;
if (Q !== undefined) this.__Q = ecc.pointCompress(Q, this.compressed);
} }
get privateKey(): Buffer | undefined { get privateKey(): Buffer | undefined {

View file

@ -182,17 +182,10 @@ export class Transaction {
return true; return true;
} }
version: number; version: number = 1;
locktime: number; locktime: number = 0;
ins: Input[]; ins: Input[] = [];
outs: OpenOutput[]; outs: OpenOutput[] = [];
constructor() {
this.version = 1;
this.locktime = 0;
this.ins = [];
this.outs = [];
}
isCoinbase(): boolean { isCoinbase(): boolean {
return ( return (

View file

@ -91,19 +91,17 @@ export class TransactionBuilder {
return txb; return txb;
} }
network: Network;
maximumFeeRate: number;
private __PREV_TX_SET: { [index: string]: boolean }; private __PREV_TX_SET: { [index: string]: boolean };
private __INPUTS: TxbInput[]; private __INPUTS: TxbInput[];
private __TX: Transaction; 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.__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.__INPUTS = [];
this.__TX = new Transaction(); this.__TX = new Transaction();
this.__TX.version = 2; this.__TX.version = 2;

1
types/block.d.ts vendored
View file

@ -13,7 +13,6 @@ export declare class Block {
bits: number; bits: number;
nonce: number; nonce: number;
transactions?: Transaction[]; transactions?: Transaction[];
constructor();
getWitnessCommit(): Buffer | null; getWitnessCommit(): Buffer | null;
hasWitnessCommit(): boolean; hasWitnessCommit(): boolean;
hasWitness(): boolean; hasWitness(): boolean;

6
types/ecpair.d.ts vendored
View file

@ -16,11 +16,11 @@ export interface ECPairInterface {
getPublicKey?(): Buffer; getPublicKey?(): Buffer;
} }
declare class ECPair implements ECPairInterface { declare class ECPair implements ECPairInterface {
compressed: boolean;
network: Network;
private __D?; private __D?;
private __Q?; 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 privateKey: Buffer | undefined;
readonly publicKey: Buffer | undefined; readonly publicKey: Buffer | undefined;
toWIF(): string; toWIF(): string;

View file

@ -30,7 +30,6 @@ export declare class Transaction {
locktime: number; locktime: number;
ins: Input[]; ins: Input[];
outs: OpenOutput[]; outs: OpenOutput[];
constructor();
isCoinbase(): boolean; isCoinbase(): boolean;
addInput(hash: Buffer, index: number, sequence?: number, scriptSig?: Buffer): number; addInput(hash: Buffer, index: number, sequence?: number, scriptSig?: Buffer): number;
addOutput(scriptPubKey: Buffer, value: number): number; addOutput(scriptPubKey: Buffer, value: number): number;

View file

@ -3,9 +3,9 @@ import { ECPairInterface } from './ecpair';
import { Network } from './networks'; import { Network } from './networks';
import { Transaction } from './transaction'; import { Transaction } from './transaction';
export declare class TransactionBuilder { export declare class TransactionBuilder {
static fromTransaction(transaction: Transaction, network?: Network): TransactionBuilder;
network: Network; network: Network;
maximumFeeRate: number; maximumFeeRate: number;
static fromTransaction(transaction: Transaction, network?: Network): TransactionBuilder;
private __PREV_TX_SET; private __PREV_TX_SET;
private __INPUTS; private __INPUTS;
private __TX; private __TX;