package: remove buffer-compare/buffer-equals

This commit is contained in:
Daniel Cousens 2016-10-06 21:30:35 +11:00
parent f451863984
commit fd40232147
5 changed files with 30 additions and 13 deletions

View file

@ -54,8 +54,6 @@
"bigi": "^1.4.0",
"bip66": "^1.1.0",
"bs58check": "^1.0.5",
"buffer-compare": "^1.1.0",
"buffer-equals": "^1.0.3",
"buffer-reverse": "^1.0.0",
"create-hash": "^1.1.0",
"create-hmac": "^1.1.3",

View file

@ -1,7 +1,6 @@
var createHash = require('create-hash')
var bufferutils = require('./bufferutils')
var bcrypto = require('./crypto')
var bufferCompare = require('buffer-compare')
var bufferReverse = require('buffer-reverse')
var Transaction = require('./transaction')
@ -160,14 +159,14 @@ Block.prototype.checkMerkleRoot = function () {
if (!this.transactions) return false
var actualMerkleRoot = Block.calculateMerkleRoot(this.transactions)
return bufferCompare(this.merkleRoot, actualMerkleRoot) === 0
return this.merkleRoot.compare(actualMerkleRoot) === 0
}
Block.prototype.checkProofOfWork = function () {
var hash = bufferReverse(this.getHash())
var target = Block.calculateTarget(this.bits)
return bufferCompare(hash, target) <= 0
return hash.compare(target) <= 0
}
module.exports = Block

View file

@ -168,15 +168,17 @@ function varIntBuffer (i) {
}
module.exports = {
equal: require('buffer-equals'),
pushDataSize: pushDataSize,
readPushDataInt: readPushDataInt,
readUInt64LE: readUInt64LE,
readVarInt: readVarInt,
reverse: require('buffer-reverse'),
varIntBuffer: varIntBuffer,
varIntSize: varIntSize,
writePushDataInt: writePushDataInt,
writeUInt64LE: writeUInt64LE,
writeVarInt: writeVarInt
writeVarInt: writeVarInt,
// TODO: remove in 3.0.0
equal: function BufferEquals (a, b) { return a.equals(b) },
reverse: require('buffer-reverse')
}

View file

@ -1,7 +1,6 @@
var baddress = require('./address')
var bcrypto = require('./crypto')
var bscript = require('./script')
var bufferEquals = require('buffer-equals')
var bufferReverse = require('buffer-reverse')
var networks = require('./networks')
var ops = require('./opcodes.json')
@ -103,7 +102,7 @@ function expandOutput (script, ourPubKey) {
var pkh1 = scriptChunks[2]
var pkh2 = bcrypto.hash160(ourPubKey)
if (bufferEquals(pkh1, pkh2)) pubKeys = [ourPubKey]
if (pkh1.equals(pkh2)) pubKeys = [ourPubKey]
break
case 'pubkey':
@ -189,7 +188,7 @@ function prepareInput (input, kpPubKey, redeemScript, hashType) {
if (input.prevOutType !== 'scripthash') throw new Error('PrevOutScript must be P2SH')
var prevOutScriptScriptHash = bscript.decompile(input.prevOutScript)[1]
if (!bufferEquals(prevOutScriptScriptHash, redeemScriptHash)) throw new Error('Inconsistent hash160(RedeemScript)')
if (!prevOutScriptScriptHash.equals(redeemScriptHash)) throw new Error('Inconsistent hash160(RedeemScript)')
// or, we don't have a prevOutScript, so generate a P2SH script
} else {
@ -454,7 +453,7 @@ TransactionBuilder.prototype.sign = function (vin, keyPair, redeemScript, hashTy
if (canSign) {
// if redeemScript was provided, enforce consistency
if (redeemScript) {
if (!bufferEquals(input.redeemScript, redeemScript)) throw new Error('Inconsistent redeemScript')
if (!input.redeemScript.equals(redeemScript)) throw new Error('Inconsistent redeemScript')
}
if (input.hashType !== hashType) throw new Error('Inconsistent hashType')
@ -468,7 +467,7 @@ TransactionBuilder.prototype.sign = function (vin, keyPair, redeemScript, hashTy
// enforce in order signing of public keys
var valid = input.pubKeys.some(function (pubKey, i) {
if (!bufferEquals(kpPubKey, pubKey)) return false
if (!kpPubKey.equals(pubKey)) return false
if (input.signatures[i]) throw new Error('Signature already exists')
input.signatures[i] = keyPair.sign(signatureHash)

View file

@ -6,6 +6,25 @@ var bufferutils = require('../src/bufferutils')
var fixtures = require('./fixtures/bufferutils.json')
describe('bufferutils', function () {
// TODO: remove
describe('equals', function () {
it('works', function () {
var a = new Buffer('abcd', 'hex')
var b = new Buffer('bbbb', 'hex')
assert.strictEqual(bufferutils.equal(a, a), true)
assert.strictEqual(bufferutils.equal(a, b), false)
})
})
describe('reverse', function () {
it('works', function () {
var a = new Buffer('abcd', 'hex')
assert.strictEqual(bufferutils.reverse(a).toString('hex'), 'cdab')
})
})
describe('pushDataSize', function () {
fixtures.valid.forEach(function (f) {
it('determines the pushDataSize of ' + f.dec + ' correctly', function () {