TypeScript hates Buffer.prototype.reverse, so fixed it.

This commit is contained in:
junderw 2018-12-29 16:36:36 +09:00
parent c17cdce348
commit 3124e50e52
No known key found for this signature in database
GPG key ID: B256185D3A971908
5 changed files with 32 additions and 14 deletions

View file

@ -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

View file

@ -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
}

View file

@ -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, <ECPairOptions>options)
return new ECPair(buffer, undefined, <ECPairOptions>options)
}
function fromPublicKey (buffer: Buffer, options?: ECPairOptions): ECPair {
typeforce(ecc.isPoint, buffer)
typeforce(isOptions, options)
return new ECPair(null, buffer, <ECPairOptions>options)
return new ECPair(undefined, buffer, <ECPairOptions>options)
}
function fromWIF (string: string, network: Network | Array<Network>): ECPair {
function fromWIF (string: string, network?: Network | Array<Network>): ECPair {
const decoded = wif.decode(string)
const version = decoded.version

View file

@ -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> Buffer.allocUnsafe(this.__byteLength((<boolean>__allowWitness)))
let offset = initialOffset || 0

View file

@ -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> Buffer.from(txHash, 'hex').reverse()
txHash = reverseBuffer(Buffer.from(txHash, 'hex'))
// is it a Transaction object?
} else if (txIsTransaction(txHash)) {