TypeScript hates Buffer.prototype.reverse, so fixed it.
This commit is contained in:
parent
c17cdce348
commit
3124e50e52
5 changed files with 32 additions and 14 deletions
|
@ -1,6 +1,7 @@
|
||||||
import { Transaction } from './transaction'
|
import { Transaction } from './transaction'
|
||||||
import * as types from './types'
|
import * as types from './types'
|
||||||
import * as bcrypto from './crypto'
|
import * as bcrypto from './crypto'
|
||||||
|
import { reverseBuffer } from './bufferutils'
|
||||||
const Buffer = require('safe-buffer').Buffer
|
const Buffer = require('safe-buffer').Buffer
|
||||||
const fastMerkleRoot = require('merkle-lib/fastRoot')
|
const fastMerkleRoot = require('merkle-lib/fastRoot')
|
||||||
const typeforce = require('typeforce')
|
const typeforce = require('typeforce')
|
||||||
|
@ -153,7 +154,7 @@ export class Block {
|
||||||
}
|
}
|
||||||
|
|
||||||
getId (): string {
|
getId (): string {
|
||||||
return Buffer.from(this.getHash().reverse()).toString('hex')
|
return reverseBuffer(this.getHash()).toString('hex')
|
||||||
}
|
}
|
||||||
|
|
||||||
getUTCDate (): Date {
|
getUTCDate (): Date {
|
||||||
|
@ -223,7 +224,7 @@ export class Block {
|
||||||
}
|
}
|
||||||
|
|
||||||
checkProofOfWork (): boolean {
|
checkProofOfWork (): boolean {
|
||||||
const hash: Buffer = Buffer.from(this.getHash().reverse())
|
const hash: Buffer = reverseBuffer(this.getHash())
|
||||||
const target = Block.calculateTarget(this.bits)
|
const target = Block.calculateTarget(this.bits)
|
||||||
|
|
||||||
return hash.compare(target) <= 0
|
return hash.compare(target) <= 0
|
||||||
|
|
|
@ -22,3 +22,16 @@ export function writeUInt64LE (buffer: Buffer, value: number, offset: number): n
|
||||||
buffer.writeUInt32LE(Math.floor(value / 0x100000000), offset + 4)
|
buffer.writeUInt32LE(Math.floor(value / 0x100000000), offset + 4)
|
||||||
return offset + 8
|
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
|
||||||
|
}
|
||||||
|
|
|
@ -33,14 +33,16 @@ class ECPair implements ECPairInterface {
|
||||||
network: Network
|
network: Network
|
||||||
private __d: Buffer | null
|
private __d: Buffer | null
|
||||||
private __Q: 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 = {}
|
if (options === undefined) options = {}
|
||||||
this.compressed = options.compressed === undefined ? true : options.compressed
|
this.compressed = options.compressed === undefined ? true : options.compressed
|
||||||
this.network = options.network || NETWORKS.bitcoin
|
this.network = options.network || NETWORKS.bitcoin
|
||||||
|
|
||||||
this.__d = d || null
|
this.__d = null
|
||||||
this.__Q = 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 {
|
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)')
|
if (!ecc.isPrivate(buffer)) throw new TypeError('Private key not in range [1, n)')
|
||||||
typeforce(isOptions, options)
|
typeforce(isOptions, options)
|
||||||
|
|
||||||
return new ECPair(buffer, null, <ECPairOptions>options)
|
return new ECPair(buffer, undefined, <ECPairOptions>options)
|
||||||
}
|
}
|
||||||
|
|
||||||
function fromPublicKey (buffer: Buffer, options?: ECPairOptions): ECPair {
|
function fromPublicKey (buffer: Buffer, options?: ECPairOptions): ECPair {
|
||||||
typeforce(ecc.isPoint, buffer)
|
typeforce(ecc.isPoint, buffer)
|
||||||
typeforce(isOptions, options)
|
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 decoded = wif.decode(string)
|
||||||
const version = decoded.version
|
const version = decoded.version
|
||||||
|
|
||||||
|
|
|
@ -2,6 +2,7 @@ import * as bcrypto from './crypto'
|
||||||
import * as bscript from './script'
|
import * as bscript from './script'
|
||||||
import * as types from './types'
|
import * as types from './types'
|
||||||
import * as bufferutils from './bufferutils'
|
import * as bufferutils from './bufferutils'
|
||||||
|
import { reverseBuffer } from './bufferutils'
|
||||||
const Buffer = require('safe-buffer').Buffer
|
const Buffer = require('safe-buffer').Buffer
|
||||||
const opcodes = require('bitcoin-ops')
|
const opcodes = require('bitcoin-ops')
|
||||||
const typeforce = require('typeforce')
|
const typeforce = require('typeforce')
|
||||||
|
@ -74,7 +75,7 @@ export class Transaction {
|
||||||
this.outs = []
|
this.outs = []
|
||||||
}
|
}
|
||||||
|
|
||||||
static fromBuffer (buffer: Buffer, __noStrict: boolean): Transaction {
|
static fromBuffer (buffer: Buffer, __noStrict?: boolean): Transaction {
|
||||||
let offset: number = 0
|
let offset: number = 0
|
||||||
|
|
||||||
function readSlice (n: number): Buffer {
|
function readSlice (n: number): Buffer {
|
||||||
|
@ -234,7 +235,7 @@ export class Transaction {
|
||||||
return this.__byteLength(true)
|
return this.__byteLength(true)
|
||||||
}
|
}
|
||||||
|
|
||||||
__byteLength (__allowWitness: boolean): number {
|
private __byteLength (__allowWitness: boolean): number {
|
||||||
const hasWitnesses = __allowWitness && this.hasWitnesses()
|
const hasWitnesses = __allowWitness && this.hasWitnesses()
|
||||||
|
|
||||||
return (
|
return (
|
||||||
|
@ -454,7 +455,7 @@ export class Transaction {
|
||||||
return bcrypto.hash256(tbuffer)
|
return bcrypto.hash256(tbuffer)
|
||||||
}
|
}
|
||||||
|
|
||||||
getHash (forWitness: boolean): Buffer {
|
getHash (forWitness?: boolean): Buffer {
|
||||||
// wtxid for coinbase is always 32 bytes of 0x00
|
// wtxid for coinbase is always 32 bytes of 0x00
|
||||||
if (forWitness && this.isCoinbase()) return Buffer.alloc(32, 0)
|
if (forWitness && this.isCoinbase()) return Buffer.alloc(32, 0)
|
||||||
return bcrypto.hash256(this.__toBuffer(undefined, undefined, forWitness))
|
return bcrypto.hash256(this.__toBuffer(undefined, undefined, forWitness))
|
||||||
|
@ -462,14 +463,14 @@ export class Transaction {
|
||||||
|
|
||||||
getId (): string {
|
getId (): string {
|
||||||
// transaction hash's are displayed in reverse order
|
// 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 {
|
toBuffer (buffer?: Buffer, initialOffset?: number): Buffer {
|
||||||
return this.__toBuffer(buffer, initialOffset, true)
|
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)))
|
if (!buffer) buffer = <Buffer> Buffer.allocUnsafe(this.__byteLength((<boolean>__allowWitness)))
|
||||||
|
|
||||||
let offset = initialOffset || 0
|
let offset = initialOffset || 0
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
import { Network } from './networks'
|
import { Network } from './networks'
|
||||||
import * as networks from './networks'
|
import * as networks from './networks'
|
||||||
|
import { reverseBuffer } from './bufferutils'
|
||||||
import { Transaction, Output } from './transaction'
|
import { Transaction, Output } from './transaction'
|
||||||
import { ECPairInterface } from './ecpair'
|
import { ECPairInterface } from './ecpair'
|
||||||
import * as ECPair from './ecpair'
|
import * as ECPair from './ecpair'
|
||||||
|
@ -131,7 +132,7 @@ export class TransactionBuilder {
|
||||||
// is it a hex string?
|
// is it a hex string?
|
||||||
if (txIsString(txHash)) {
|
if (txIsString(txHash)) {
|
||||||
// transaction hashs's are displayed in reverse order, un-reverse it
|
// 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?
|
// is it a Transaction object?
|
||||||
} else if (txIsTransaction(txHash)) {
|
} else if (txIsTransaction(txHash)) {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue