Fix class constructors
This commit is contained in:
parent
fabd1d6c9d
commit
74375bfedf
11 changed files with 101 additions and 125 deletions
56
src/block.js
56
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)));
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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,
|
||||
),
|
||||
)
|
||||
);
|
||||
}
|
||||
|
|
|
@ -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 {
|
||||
|
|
|
@ -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 (
|
||||
|
|
|
@ -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;
|
||||
|
|
1
types/block.d.ts
vendored
1
types/block.d.ts
vendored
|
@ -13,7 +13,6 @@ export declare class Block {
|
|||
bits: number;
|
||||
nonce: number;
|
||||
transactions?: Transaction[];
|
||||
constructor();
|
||||
getWitnessCommit(): Buffer | null;
|
||||
hasWitnessCommit(): boolean;
|
||||
hasWitness(): boolean;
|
||||
|
|
6
types/ecpair.d.ts
vendored
6
types/ecpair.d.ts
vendored
|
@ -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;
|
||||
|
|
1
types/transaction.d.ts
vendored
1
types/transaction.d.ts
vendored
|
@ -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;
|
||||
|
|
2
types/transaction_builder.d.ts
vendored
2
types/transaction_builder.d.ts
vendored
|
@ -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;
|
||||
|
|
Loading…
Reference in a new issue