diff --git a/src/block.ts b/src/block.ts index f91ad32..d79de9d 100644 --- a/src/block.ts +++ b/src/block.ts @@ -1,6 +1,7 @@ import { Transaction } from './transaction' import * as types from './types' import * as bcrypto from './crypto' +import { reverseBuffer } from './bufferutils' const Buffer = require('safe-buffer').Buffer const fastMerkleRoot = require('merkle-lib/fastRoot') const typeforce = require('typeforce') @@ -153,7 +154,7 @@ export class Block { } getId (): string { - return Buffer.from(this.getHash().reverse()).toString('hex') + return reverseBuffer(this.getHash()).toString('hex') } getUTCDate (): Date { @@ -223,7 +224,7 @@ export class Block { } checkProofOfWork (): boolean { - const hash: Buffer = Buffer.from(this.getHash().reverse()) + const hash: Buffer = reverseBuffer(this.getHash()) const target = Block.calculateTarget(this.bits) return hash.compare(target) <= 0 diff --git a/src/bufferutils.ts b/src/bufferutils.ts index b66c84a..3ef7492 100644 --- a/src/bufferutils.ts +++ b/src/bufferutils.ts @@ -22,3 +22,16 @@ export function writeUInt64LE (buffer: Buffer, value: number, offset: number): n buffer.writeUInt32LE(Math.floor(value / 0x100000000), offset + 4) return offset + 8 } + +export function reverseBuffer (buffer: Buffer): Buffer { + if (buffer.length < 1) return buffer + let j = buffer.length - 1 + let tmp = 0 + for (let i = 0; i < buffer.length / 2; i++) { + tmp = buffer[i] + buffer[i] = buffer[j] + buffer[j] = tmp + j-- + } + return buffer +} diff --git a/src/ecpair.ts b/src/ecpair.ts index 9a54b1a..321a5ed 100644 --- a/src/ecpair.ts +++ b/src/ecpair.ts @@ -33,14 +33,16 @@ class ECPair implements ECPairInterface { network: Network private __d: Buffer | null private __Q: Buffer | null - constructor (d: Buffer | null, Q: Buffer | null, options: ECPairOptions) { + + constructor (d?: Buffer, Q?: Buffer, options?: ECPairOptions) { if (options === undefined) options = {} this.compressed = options.compressed === undefined ? true : options.compressed this.network = options.network || NETWORKS.bitcoin - this.__d = d || null + this.__d = null this.__Q = null - if (Q) 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 (): Buffer | null { @@ -72,16 +74,16 @@ function fromPrivateKey (buffer: Buffer, options?: ECPairOptions): ECPair { if (!ecc.isPrivate(buffer)) throw new TypeError('Private key not in range [1, n)') typeforce(isOptions, options) - return new ECPair(buffer, null, options) + return new ECPair(buffer, undefined, options) } function fromPublicKey (buffer: Buffer, options?: ECPairOptions): ECPair { typeforce(ecc.isPoint, buffer) typeforce(isOptions, options) - return new ECPair(null, buffer, options) + return new ECPair(undefined, buffer, options) } -function fromWIF (string: string, network: Network | Array): ECPair { +function fromWIF (string: string, network?: Network | Array): ECPair { const decoded = wif.decode(string) const version = decoded.version diff --git a/src/transaction.ts b/src/transaction.ts index b40e368..515ffb6 100644 --- a/src/transaction.ts +++ b/src/transaction.ts @@ -2,6 +2,7 @@ import * as bcrypto from './crypto' import * as bscript from './script' import * as types from './types' import * as bufferutils from './bufferutils' +import { reverseBuffer } from './bufferutils' const Buffer = require('safe-buffer').Buffer const opcodes = require('bitcoin-ops') const typeforce = require('typeforce') @@ -74,7 +75,7 @@ export class Transaction { this.outs = [] } - static fromBuffer (buffer: Buffer, __noStrict: boolean): Transaction { + static fromBuffer (buffer: Buffer, __noStrict?: boolean): Transaction { let offset: number = 0 function readSlice (n: number): Buffer { @@ -234,7 +235,7 @@ export class Transaction { return this.__byteLength(true) } - __byteLength (__allowWitness: boolean): number { + private __byteLength (__allowWitness: boolean): number { const hasWitnesses = __allowWitness && this.hasWitnesses() return ( @@ -454,7 +455,7 @@ export class Transaction { return bcrypto.hash256(tbuffer) } - getHash (forWitness: boolean): Buffer { + getHash (forWitness?: boolean): Buffer { // wtxid for coinbase is always 32 bytes of 0x00 if (forWitness && this.isCoinbase()) return Buffer.alloc(32, 0) return bcrypto.hash256(this.__toBuffer(undefined, undefined, forWitness)) @@ -462,14 +463,14 @@ export class Transaction { getId (): string { // transaction hash's are displayed in reverse order - return Buffer.from(this.getHash(false).reverse()).toString('hex') + return reverseBuffer(this.getHash(false)).toString('hex') } toBuffer (buffer?: Buffer, initialOffset?: number): Buffer { return this.__toBuffer(buffer, initialOffset, true) } - __toBuffer (buffer?: Buffer, initialOffset?: number, __allowWitness?: boolean): Buffer { + private __toBuffer (buffer?: Buffer, initialOffset?: number, __allowWitness?: boolean): Buffer { if (!buffer) buffer = Buffer.allocUnsafe(this.__byteLength((__allowWitness))) let offset = initialOffset || 0 diff --git a/src/transaction_builder.ts b/src/transaction_builder.ts index a6b3b48..a920130 100644 --- a/src/transaction_builder.ts +++ b/src/transaction_builder.ts @@ -1,5 +1,6 @@ import { Network } from './networks' import * as networks from './networks' +import { reverseBuffer } from './bufferutils' import { Transaction, Output } from './transaction' import { ECPairInterface } from './ecpair' import * as ECPair from './ecpair' @@ -131,7 +132,7 @@ export class TransactionBuilder { // is it a hex string? if (txIsString(txHash)) { // transaction hashs's are displayed in reverse order, un-reverse it - txHash = Buffer.from(txHash, 'hex').reverse() + txHash = reverseBuffer(Buffer.from(txHash, 'hex')) // is it a Transaction object? } else if (txIsTransaction(txHash)) {