From 712a445b992d4a0f588f693f614d01066cde4117 Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Thu, 19 May 2016 21:04:06 -0700 Subject: [PATCH] utils and createServer callback. --- lib/bcoin/pool.js | 49 ++++++++++++++++++++++++++++------------------ lib/bcoin/utils.js | 19 ++++++++++-------- test/aes-test.js | 8 ++++---- 3 files changed, 45 insertions(+), 31 deletions(-) diff --git a/lib/bcoin/pool.js b/lib/bcoin/pool.js index fccffa61..2f67c6ab 100644 --- a/lib/bcoin/pool.js +++ b/lib/bcoin/pool.js @@ -17,30 +17,38 @@ var VerifyError = bcoin.errors.VerifyError; * @exports Pool * @constructor * @param {Object} options - * @param {Chain} chain - * @param {Mempool?} mempool - * @param {Number?} [size=8] - Maximum number of peers. + * @param {Chain} options.chain + * @param {Mempool?} options.mempool + * @param {Number?} [options.size=8] - Maximum number of peers. * @param {Boolean?} options.spv - Do an SPV sync. * @param {Boolean?} options.relay - Whether to ask * for relayed transactions. * @param {Boolean?} options.headers - Whether * to use `getheaders` for sync. - * @param {Number?} [loadTimeout=120000] - Sync timeout before + * @param {Number?} [options.loadTimeout=120000] - Sync timeout before * finding a new loader peer. - * @param {Number?} [loadInterval=20000] - Timeout before attempting to + * @param {Number?} [options.loadInterval=20000] - Timeout before attempting to * send another getblocks request. - * @param {Number?} [requestTimeout=120000] - Timeout for in-flight blocks. - * @param {Number?} [invTimeout=60000] - Timeout for broadcasted objects. - * @param {Boolean?} listen - Whether to spin up a server socket + * @param {Number?} [options.requestTimeout=120000] - Timeout for in-flight + * blocks. + * @param {Number?} [options.invTimeout=60000] - Timeout for broadcasted + * objects. + * @param {Boolean?} options.listen - Whether to spin up a server socket * and listen for peers. - * @param {Boolean?} selfish - A selfish pool. Will not serve blocks, + * @param {Boolean?} options.selfish - A selfish pool. Will not serve blocks, * headers, hashes, utxos, or transactions to peers. - * @param {Boolean?} broadcast - Whether to automatically broadcast + * @param {Boolean?} options.broadcast - Whether to automatically broadcast * transactions accepted to our mempool. - * @param {Boolean?} witness - Request witness blocks and transactions. + * @param {Boolean?} options.witness - Request witness blocks and transactions. * Only deal with witness peers. - * @param {Boolean} [discoverPeers=true] Automatically discover new peers. - * @param {(String[]|Seed[])?} seeds + * @param {Boolean} [options.discoverPeers=true] Automatically discover new + * peers. + * @param {(String[]|Seed[])?} options.seeds + * @param {Function?} options.createSocket - Custom function to create a socket. + * Must accept (port, host) and return a node-like socket. + * @param {Function?} options.createServer - Custom function to create a server. + * Must return a node-like server. + * @emits Pool#block * @emits Pool#block * @emits Pool#tx * @emits Pool#peer @@ -115,6 +123,7 @@ function Pool(options) { this.maxLeeches = options.maxLeeches || 8; this.connected = false; this.uid = 0; + this._createServer = options.createServer; this.syncing = false; this.synced = false; @@ -393,14 +402,16 @@ Pool.prototype.listen = function listen(callback) { callback = utils.ensure(callback); - if (bcoin.isBrowser) - return utils.nextTick(callback); - - net = require('n' + 'et'); - assert(!this.server, 'Server already listening.'); - this.server = new net.Server(); + if (this._createServer) { + this.server = this._createServer(); + } else { + if (bcoin.isBrowser) + return utils.nextTick(callback); + net = require('n' + 'et'); + this.server = new net.Server(); + } this.server.on('connection', function(socket) { if (self.peers.leeches.length >= self.maxLeeches) { diff --git a/lib/bcoin/utils.js b/lib/bcoin/utils.js index 695dccef..81a311f3 100644 --- a/lib/bcoin/utils.js +++ b/lib/bcoin/utils.js @@ -414,7 +414,7 @@ utils.encrypt = function encrypt(data, passphrase) { if (typeof passphrase === 'string') passphrase = new Buffer(passphrase, 'utf8'); - key = utils.pbkdf2key(passphrase, null, 2048, 32, 16); + key = utils.pbkdf2key(passphrase, 2048, 32, 16); if (!crypto) { out = aes.cbc.encrypt(data, key.key, key.iv); @@ -455,7 +455,7 @@ utils.decrypt = function decrypt(data, passphrase) { if (typeof passphrase === 'string') passphrase = new Buffer(passphrase, 'utf8'); - key = utils.pbkdf2key(passphrase, null, 2048, 32, 16); + key = utils.pbkdf2key(passphrase, 2048, 32, 16); if (!crypto) { out = aes.cbc.decrypt(data, key.key, key.iv); @@ -488,23 +488,26 @@ utils.decrypt = function decrypt(data, passphrase) { * @returns {Buffer} */ -utils.pbkdf2key = function pbkdf2key(passphrase, salt, iterations, dkLen, ivLen, alg) { - var key = utils.pbkdf2(passphrase, salt || '', iterations, dkLen + ivLen, alg); +utils.pbkdf2key = function pbkdf2key(passphrase, iterations, dkLen, ivLen, alg) { + var key = utils.pbkdf2(passphrase, '', iterations, dkLen + ivLen, alg); return { key: key.slice(0, dkLen), - iv: key.slice(dkLen) + iv: key.slice(dkLen, dkLen + ivLen) }; }; /** * Test whether a string is hex. Note that this - * may yield a false positive on base58 strings. + * _could_ yield a false positive on base58 + * strings. * @param {String?} obj * @returns {Boolean} */ utils.isHex = function isHex(obj) { - return typeof obj === 'string' && /^[0-9a-f]+$/i.test(obj); + return typeof obj === 'string' + && /^[0-9a-f]+$/i.test(obj) + && obj.length % 2 === 0; }; /** @@ -2423,8 +2426,8 @@ utils.revMap = function revMap(map) { * Perform a binary search on a sorted array. * @param {Array} items * @param {Object} key + * @param {Function} compare * @param {Boolean?} insert - * @param {Function?} compare * @returns {Number} Index. */ diff --git a/test/aes-test.js b/test/aes-test.js index 6a8eea9d..ca4de665 100644 --- a/test/aes-test.js +++ b/test/aes-test.js @@ -17,7 +17,7 @@ describe('AES', function() { if (typeof passphrase === 'string') passphrase = new Buffer(passphrase, 'utf8'); - key = utils.pbkdf2key(passphrase, null, 2048, 32, 16); + key = utils.pbkdf2key(passphrase, 2048, 32, 16); cipher = crypto.createCipheriv('aes-256-cbc', key.key, key.iv); return Buffer.concat([ @@ -38,7 +38,7 @@ describe('AES', function() { if (typeof passphrase === 'string') passphrase = new Buffer(passphrase, 'utf8'); - key = utils.pbkdf2key(passphrase, null, 2048, 32, 16); + key = utils.pbkdf2key(passphrase, 2048, 32, 16); decipher = crypto.createDecipheriv('aes-256-cbc', key.key, key.iv); return Buffer.concat([ @@ -59,7 +59,7 @@ describe('AES', function() { if (typeof passphrase === 'string') passphrase = new Buffer(passphrase, 'utf8'); - key = utils.pbkdf2key(passphrase, null, 2048, 32, 16); + key = utils.pbkdf2key(passphrase, 2048, 32, 16); return aes.cbc.encrypt(data, key.key, key.iv); } @@ -76,7 +76,7 @@ describe('AES', function() { if (typeof passphrase === 'string') passphrase = new Buffer(passphrase, 'utf8'); - key = utils.pbkdf2key(passphrase, null, 2048, 32, 16); + key = utils.pbkdf2key(passphrase, 2048, 32, 16); return aes.cbc.decrypt(data, key.key, key.iv); }