diff --git a/lib/bcoin/bloom.js b/lib/bcoin/bloom.js index e4e8efa0..dba45694 100644 --- a/lib/bcoin/bloom.js +++ b/lib/bcoin/bloom.js @@ -6,6 +6,7 @@ */ var utils = require('./utils'); +var assert = utils.assert; /** * Bloom Filter @@ -67,7 +68,8 @@ Bloom.prototype.reset = function reset() { Bloom.prototype.add = function add(val, enc) { var i, bit, pos, shift; - val = utils.toBuffer(val, enc); + if (typeof val === 'string') + val = new Buffer(val, enc); for (i = 0; i < this.n; i++) { bit = this.hash(val, i); @@ -89,7 +91,8 @@ Bloom.prototype.add = function add(val, enc) { Bloom.prototype.test = function test(val, enc) { var i, bit, pos, shift; - val = utils.toBuffer(val, enc); + if (typeof val === 'string') + val = new Buffer(val, enc); for (i = 0; i < this.n; i++) { bit = this.hash(val, i); @@ -176,7 +179,7 @@ function murmur(data, seed) { var hash = seed; var i, w, r, j; - data = utils.toBuffer(data); + assert(Buffer.isBuffer(data)); for (i = 0; i + 4 <= data.length; i += 4) { w = data[i] diff --git a/lib/bcoin/chain.js b/lib/bcoin/chain.js index 0121e8df..7e4f1c83 100644 --- a/lib/bcoin/chain.js +++ b/lib/bcoin/chain.js @@ -1668,7 +1668,7 @@ Chain.prototype.purgePending = function purgePending() { /** * Test the chain to see if it has a block, orphan, or pending block. - * @param {Block|MerkleBlock|Buffer|Hash} hash + * @param {Hash} hash * @param {Function} callback - Returns [Error, Boolean]. */ @@ -1747,9 +1747,6 @@ Chain.prototype.byTime = function byTime(ts, callback, force) { */ Chain.prototype.hasBlock = function hasBlock(hash, callback) { - if (typeof hash === 'number') - return this.db.has(hash, callback); - return this.db.has(hash, callback); }; @@ -1780,8 +1777,6 @@ Chain.prototype.hasPending = function hasPending(hash) { */ Chain.prototype.getEntry = function getEntry(hash, callback) { - if (typeof hash === 'number') - return this.db.get(hash, callback); return this.db.get(hash, callback); }; diff --git a/lib/bcoin/merkleblock.js b/lib/bcoin/merkleblock.js index dc0546cd..5d59a9ab 100644 --- a/lib/bcoin/merkleblock.js +++ b/lib/bcoin/merkleblock.js @@ -42,7 +42,9 @@ function MerkleBlock(data) { bcoin.abstractblock.call(this, data); this.hashes = (data.hashes || []).map(function(hash) { - return utils.toBuffer(hash, 'hex'); + if (typeof hash === 'string') + hash = new Buffer(hash, 'hex'); + return hash; }); this.flags = data.flags || []; diff --git a/lib/bcoin/utils.js b/lib/bcoin/utils.js index 150bb6ac..8afee663 100644 --- a/lib/bcoin/utils.js +++ b/lib/bcoin/utils.js @@ -68,37 +68,10 @@ utils.slice = function slice(buf, start, end) { return clone; }; -/** - * Convert objects to a Buffer. - * @param {Buffer|Array|String} msg - * @param {String} enc - Any buffer-supported - * encoding as well as `base58`. - * @returns {Buffer} +/* + * Base58 */ -utils.toBuffer = function toBuffer(msg, enc) { - if (Buffer.isBuffer(msg)) - return msg; - - if (Array.isArray(msg)) - return new Buffer(msg); - - if (!msg) - return new Buffer([]); - - if (typeof msg === 'string') { - if (!enc) - return new Buffer(msg, 'ascii'); - - if (enc === 'base58') - return utils.fromBase58(msg); - - return new Buffer(msg, enc); - } - - assert(false); -}; - var base58 = '123456789ABCDEFGHJKLMNPQRSTUVWXYZ' + 'abcdefghijkmnopqrstuvwxyz'; diff --git a/scripts/gen.js b/scripts/gen.js index 99b453ce..cd56966c 100644 --- a/scripts/gen.js +++ b/scripts/gen.js @@ -59,7 +59,7 @@ function createGenesisBlock(options) { block = { version: options.version, prevBlock: constants.NULL_HASH, - merkleRoot: utils.toHex(utils.dsha256(txRaw)), + merkleRoot: utils.dsha256(txRaw).toString('hex'), ts: options.ts, bits: options.bits, nonce: options.nonce, @@ -71,7 +71,7 @@ function createGenesisBlock(options) { block = parser.parseBlock(blockRaw); block._hash = utils.dsha256(blockRaw.slice(0, 80)); - block.hash = utils.toHex(block._hash); + block.hash = block._hash.toString('hex'); block._raw = blockRaw; block._size = blockRaw.length; block._witnessSize = 0; @@ -81,7 +81,7 @@ function createGenesisBlock(options) { tx.height = 0; tx.ts = block.ts; tx._hash = block.merkleRoot; - tx.hash = utils.toHex(tx._hash); + tx.hash = tx._hash.toString('hex'); return block; } diff --git a/test/bloom-test.js b/test/bloom-test.js index 059e5d15..1b7b62b1 100644 --- a/test/bloom-test.js +++ b/test/bloom-test.js @@ -5,25 +5,25 @@ describe('Bloom', function() { it('should do proper murmur3', function() { var h = bcoin.bloom.hash; - assert.equal(h('', 0), 0); - assert.equal(h('', 0xfba4c795), 0x6a396f08); - assert.equal(h('00', 0xfba4c795), 0x2a101837); - assert.equal(h('hello world', 0), 0x5e928f0f); + assert.equal(h(new Buffer('', 'ascii'), 0), 0); + assert.equal(h(new Buffer('', 'ascii'), 0xfba4c795), 0x6a396f08); + assert.equal(h(new Buffer('00', 'ascii'), 0xfba4c795), 0x2a101837); + assert.equal(h(new Buffer('hello world', 'ascii'), 0), 0x5e928f0f); }); it('should test and add stuff', function() { var b = bcoin.bloom(512, 10, 156); - b.add('hello'); - assert(b.test('hello')); - assert(!b.test('hello!')); - assert(!b.test('ping')); + b.add('hello', 'ascii'); + assert(b.test('hello', 'ascii')); + assert(!b.test('hello!', 'ascii')); + assert(!b.test('ping', 'ascii')); - b.add('hello!'); - assert(b.test('hello!')); - assert(!b.test('ping')); + b.add('hello!', 'ascii'); + assert(b.test('hello!', 'ascii')); + assert(!b.test('ping', 'ascii')); - b.add('ping'); - assert(b.test('ping')); + b.add('ping', 'ascii'); + assert(b.test('ping', 'ascii')); }); });