diff --git a/.jshintrc b/.jshintrc index 787e816d..cf7ce01d 100644 --- a/.jshintrc +++ b/.jshintrc @@ -8,7 +8,7 @@ "noarg": true, "shadow": "inner", "undef": true, - "unused": true, + "unused": "vars", "boss": true, "expr": true, @@ -21,10 +21,11 @@ "-W018": true, "-W064": true, "-W086": true, - "+W032": true, + "-W032": true, + "-W021": true, - "browser": false, - "browserify": false, + "browser": true, + "browserify": true, "node": true, "nonstandard": true, "typed": true, diff --git a/lib/bcoin/address.js b/lib/bcoin/address.js index e1229c9e..70c919f2 100644 --- a/lib/bcoin/address.js +++ b/lib/bcoin/address.js @@ -6,8 +6,6 @@ */ var bcoin = require('./env'); -var bn = require('bn.js'); -var constants = bcoin.protocol.constants; var networks = bcoin.protocol.network; var utils = require('./utils'); var assert = utils.assert; @@ -146,7 +144,7 @@ Address.toBase58 = function toBase58(hash, type, version, network) { p.writeU8(prefix); if (version != null) { p.writeU8(version); - p.writeU8(0) + p.writeU8(0); } p.writeBytes(hash); p.writeChecksum(); @@ -162,7 +160,7 @@ Address.toBase58 = function toBase58(hash, type, version, network) { */ Address.parseBase58 = function parseBase58(address) { - var i, prefix, type, version, hash, network; + var i, prefix, type, version, hash, network, p; if (!Buffer.isBuffer(address)) address = utils.fromBase58(address); @@ -222,7 +220,7 @@ Address.fromBase58 = function fromBase58(address) { */ Address.parseScript = function parseScript(script) { - var program; + var program, hash; if (script.isWitnessProgram()) { program = script.getWitnessProgram(); diff --git a/lib/bcoin/aes.js b/lib/bcoin/aes.js index 6c94b94d..2f27c56d 100644 --- a/lib/bcoin/aes.js +++ b/lib/bcoin/aes.js @@ -8,6 +8,8 @@ * Entered into the public domain by Vincent Rijmen. */ +/* jshint latedef: false */ + var assert = require('assert'); /** diff --git a/lib/bcoin/block.js b/lib/bcoin/block.js index 628e5a84..c5291f88 100644 --- a/lib/bcoin/block.js +++ b/lib/bcoin/block.js @@ -6,13 +6,9 @@ */ var bcoin = require('./env'); -var bn = require('bn.js'); var utils = require('./utils'); var assert = utils.assert; var constants = bcoin.protocol.constants; -var network = bcoin.protocol.network; -var BufferWriter = require('./writer'); -var BufferReader = require('./reader'); /** * Represents a full block. @@ -39,7 +35,7 @@ var BufferReader = require('./reader'); */ function Block(data) { - var i, tx; + var i; if (!(this instanceof Block)) return new Block(data); diff --git a/lib/bcoin/bloom.js b/lib/bcoin/bloom.js index 7d3c6912..9f7412ba 100644 --- a/lib/bcoin/bloom.js +++ b/lib/bcoin/bloom.js @@ -43,7 +43,7 @@ function Bloom(size, n, tweak, update) { this.reset(); } - if (tweak == null || tweak == -1) + if (tweak == null || tweak === -1) tweak = (Math.random() * 0x100000000) >>> 0; if (update == null || update === -1) @@ -248,7 +248,7 @@ RollingFilter.prototype.reset = function reset() { */ RollingFilter.prototype.add = function add(val, enc) { - var i, j, hash, bits, pos, pos1, pos2, bit, oct; + var i, hash, bits, pos, pos1, pos2, bit, oct; var m1, m2, v1, v2, mhi, mlo; if (typeof val === 'string') diff --git a/lib/bcoin/bst.js b/lib/bcoin/bst.js index f4a10fa7..33977ae7 100644 --- a/lib/bcoin/bst.js +++ b/lib/bcoin/bst.js @@ -5,7 +5,6 @@ * https://github.com/indutny/bcoin */ -var bcoin = require('./env'); var utils = require('./utils'); var assert = utils.assert; var DUMMY = new Buffer([0]); @@ -112,7 +111,7 @@ BST.prototype.insert = function insert(key, value) { BST.prototype.remove = function remove(key) { var current = this.root; var left = false; - var parent, use; + var cmp, parent, use; if (typeof key === 'string') key = new Buffer(key, 'ascii'); @@ -426,8 +425,6 @@ BST.prototype.get = function get(key, options, callback) { */ BST.prototype.put = function put(key, value, options, callback) { - var item; - if (!callback) { callback = options; options = null; @@ -449,8 +446,6 @@ BST.prototype.put = function put(key, value, options, callback) { */ BST.prototype.del = function del(key, options, callback) { - var item; - if (!callback) { callback = options; options = null; @@ -690,7 +685,7 @@ Iterator.prototype.seek = function seek(key) { this.index += 1; }; -Iterator.prototype._end = function end(callback) { +Iterator.prototype._end = function end() { if (!this.tree) return; diff --git a/lib/bcoin/chain.js b/lib/bcoin/chain.js index baa33736..7a8e7255 100644 --- a/lib/bcoin/chain.js +++ b/lib/bcoin/chain.js @@ -348,7 +348,7 @@ Chain.prototype._preload = function _preload(callback) { stream.on('data', function(data) { var blocks = []; var need = 80 - buf.size; - var lastEntry, block, data, entry; + var i, lastEntry, block, entry; while (data.length >= need) { buf.data.push(data.slice(0, need)); @@ -441,7 +441,7 @@ Chain.prototype._verifyContext = function _verifyContext(block, prev, callback) if (err) return callback(err); - self._checkDuplicates(block, prev, function(err, result) { + self._checkDuplicates(block, prev, function(err) { if (err) return callback(err); diff --git a/lib/bcoin/chaindb.js b/lib/bcoin/chaindb.js index 5750f9bf..e43a4d92 100644 --- a/lib/bcoin/chaindb.js +++ b/lib/bcoin/chaindb.js @@ -445,7 +445,6 @@ ChainDB.prototype.get = function get(hash, callback) { */ ChainDB.prototype.save = function save(entry, block, connect, callback) { - var self = this; var batch, hash, height; callback = utils.ensure(callback); @@ -515,7 +514,6 @@ ChainDB.prototype.getTip = function getTip(callback) { */ ChainDB.prototype.connect = function connect(entry, block, callback) { - var self = this; var batch = this.db.batch(); var hash = new Buffer(entry.hash, 'hex'); @@ -725,8 +723,6 @@ ChainDB.prototype.has = function has(height, callback) { */ ChainDB.prototype.saveBlock = function saveBlock(block, batch, connect, callback) { - var i, j, tx, hash, addresses, address; - if (this.options.spv) return utils.nextTick(callback); @@ -748,7 +744,6 @@ ChainDB.prototype.saveBlock = function saveBlock(block, batch, connect, callback ChainDB.prototype.removeBlock = function removeBlock(hash, batch, callback) { var self = this; - var i, tx; if (this.options.spv) return utils.nextTick(callback); @@ -1133,6 +1128,10 @@ ChainDB.prototype.getCoinsByAddress = function getCoinsByAddress(addresses, call coins = coins.concat(coin); next(); }); + }, function(err) { + if (err) + return callback(err); + return callback(null, coins); }); }; @@ -1250,8 +1249,6 @@ ChainDB.prototype._ensureBlock = function _ensureBlock(hash, callback) { }; ChainDB.prototype._ensureHistory = function _ensureHistory(hash, callback) { - var self = this; - if (hash instanceof bcoin.block) return utils.asyncify(callback)(null, hash); @@ -1429,7 +1426,7 @@ ChainDB.prototype.isSpentTX = function isSpentTX(hash, callback) { }; ChainDB.prototype._pruneBlock = function _pruneBlock(block, batch, callback) { - var futureHeight, i, j, key, tx, input; + var futureHeight, key; if (this.options.spv) return callback(); diff --git a/lib/bcoin/coin.js b/lib/bcoin/coin.js index e1974c0f..ef1d2da5 100644 --- a/lib/bcoin/coin.js +++ b/lib/bcoin/coin.js @@ -6,7 +6,6 @@ */ var bcoin = require('./env'); -var bn = require('bn.js'); var utils = require('./utils'); var assert = utils.assert; diff --git a/lib/bcoin/coins.js b/lib/bcoin/coins.js index 98d09438..75e5433c 100644 --- a/lib/bcoin/coins.js +++ b/lib/bcoin/coins.js @@ -7,8 +7,6 @@ var bcoin = require('./env'); var utils = bcoin.utils; -var assert = utils.assert; -var constants = bcoin.protocol.constants; var BufferReader = require('./reader'); var BufferWriter = require('./writer'); diff --git a/lib/bcoin/coinview.js b/lib/bcoin/coinview.js index 76507cfd..26df58f3 100644 --- a/lib/bcoin/coinview.js +++ b/lib/bcoin/coinview.js @@ -6,9 +6,6 @@ */ var bcoin = require('./env'); -var utils = bcoin.utils; -var assert = utils.assert; -var constants = bcoin.protocol.constants; /** * A collections of {@link Coins} objects. diff --git a/lib/bcoin/compactblock.js b/lib/bcoin/compactblock.js index a45da193..7ae2d28c 100644 --- a/lib/bcoin/compactblock.js +++ b/lib/bcoin/compactblock.js @@ -6,9 +6,7 @@ */ var bcoin = require('./env'); -var bn = require('bn.js'); var utils = require('./utils'); -var assert = utils.assert; /** * A block object which is essentially a "placeholder" diff --git a/lib/bcoin/ec.js b/lib/bcoin/ec.js index e6f99de7..f8a04b43 100644 --- a/lib/bcoin/ec.js +++ b/lib/bcoin/ec.js @@ -6,6 +6,7 @@ */ var elliptic = require('elliptic'); +var bn = require('bn.js'); var utils = require('./utils'); var assert = utils.assert; var crypto, secp256k1; diff --git a/lib/bcoin/env.js b/lib/bcoin/env.js index 55f1b398..3e5e2dd4 100644 --- a/lib/bcoin/env.js +++ b/lib/bcoin/env.js @@ -6,7 +6,6 @@ */ var utils = require('./utils'); -var assert = utils.assert; var fs; if (!utils.isBrowser) diff --git a/lib/bcoin/hd.js b/lib/bcoin/hd.js index dc612cfa..9fccf681 100644 --- a/lib/bcoin/hd.js +++ b/lib/bcoin/hd.js @@ -589,8 +589,6 @@ HDPrivateKey.prototype.deriveAccount44 = function deriveAccount44(accountIndex) */ HDPrivateKey.prototype.derivePurpose45 = function derivePurpose45() { - var child; - if (this instanceof HDPublicKey) { assert(this.isPurpose45(), 'Cannot derive purpose 45.'); return this; diff --git a/lib/bcoin/http/base.js b/lib/bcoin/http/base.js index cda1d83b..cd431716 100644 --- a/lib/bcoin/http/base.js +++ b/lib/bcoin/http/base.js @@ -69,19 +69,18 @@ HTTPBase.prototype._init = function _init() { HTTPBase.prototype._initIO = function _initIO() { var self = this; - var io; + var IOServer; - return; try { - io = require('socket.io'); + IOServer = require('socket.io'); } catch (e) { ; } - if (!io) + if (!IOServer) return; - this.io = new io.Server(); + this.io = new IOServer(); this.io.attach(this.server); @@ -494,10 +493,6 @@ function parsePath(req) { } } -function escape(str) { - return encodeURIComponent(str).replace(/%20/g, '+'); -} - function unescape(str) { try { str = decodeURIComponent(str).replace(/\+/g, ' '); diff --git a/lib/bcoin/http/client.js b/lib/bcoin/http/client.js index 772da0c1..e974becf 100644 --- a/lib/bcoin/http/client.js +++ b/lib/bcoin/http/client.js @@ -43,19 +43,18 @@ utils.inherits(HTTPClient, EventEmitter); HTTPClient.prototype._init = function _init() { var self = this; - var io; + var IOClient; - return; try { - io = require('socket.io'); + IOClient = require('socket.io-client'); } catch (e) { ; } - if (!io) + if (!IOClient) return; - this.socket = new io.Socket(this.uri); + this.socket = new IOClient(this.uri); this.socket.on('error', function(err) { self.emit('error', err); @@ -325,7 +324,6 @@ HTTPClient.prototype._getWallet = function getWallet(id, callback) { HTTPClient.prototype.getWallet = function getWallet(id, passphrase, callback) { var self = this; - var provider; return this._getWallet(id, function(err, json) { if (err) diff --git a/lib/bcoin/http/request.js b/lib/bcoin/http/request.js index add19586..0c98e933 100644 --- a/lib/bcoin/http/request.js +++ b/lib/bcoin/http/request.js @@ -5,6 +5,8 @@ * https://github.com/indutny/bcoin */ +/* jshint -W069 */ + /** * @module request */ diff --git a/lib/bcoin/http/server.js b/lib/bcoin/http/server.js index 74fdfb39..c43f8d08 100644 --- a/lib/bcoin/http/server.js +++ b/lib/bcoin/http/server.js @@ -385,7 +385,7 @@ HTTPServer.prototype._init = function _init() { var now = req.options.now; var age = req.options.age; - self.walletdb.zapWallet(id, now, age, function(err, wallet) { + self.walletdb.zapWallet(id, now, age, function(err) { if (err) return next(err); @@ -427,9 +427,6 @@ HTTPServer.prototype._init = function _init() { if (err) return next(err); - if (!json) - return send(404); - send(200, { success: true }); }); }); @@ -592,7 +589,7 @@ HTTPServer.prototype._init = function _init() { this.post('/broadcast', function(req, res, next, send) { self.pool.sendTX(req.options.tx, function(err) { if (err) - return callback(err); + return next(err); send(200, { success: true diff --git a/lib/bcoin/input.js b/lib/bcoin/input.js index 28952098..993ec101 100644 --- a/lib/bcoin/input.js +++ b/lib/bcoin/input.js @@ -180,7 +180,7 @@ Input.prototype.getAddress = function getAddress() { * @returns {Hash} hash */ -Input.prototype.getHash = function getHash(enc) { +Input.prototype.getHash = function getHash() { var address = this.getAddress(); if (!address) return; diff --git a/lib/bcoin/keypair.js b/lib/bcoin/keypair.js index d5ac5d4f..b829a99e 100644 --- a/lib/bcoin/keypair.js +++ b/lib/bcoin/keypair.js @@ -167,7 +167,7 @@ KeyPair.parseSecret = function parseSecret(secret) { var data = utils.fromBase58(secret); var p = new BufferReader(data, true); var compressed = false; - var prefix, type, privateKey; + var i, prefix, type, privateKey; prefix = p.readU8(); diff --git a/lib/bcoin/keyring.js b/lib/bcoin/keyring.js index 0b370f8f..e3c4c48a 100644 --- a/lib/bcoin/keyring.js +++ b/lib/bcoin/keyring.js @@ -8,9 +8,6 @@ var bcoin = require('./env'); var utils = bcoin.utils; var assert = utils.assert; -var networks = bcoin.protocol.network; -var BufferWriter = require('./writer'); -var BufferReader = require('./reader'); /** * Represents a key ring which amounts to an address. Used for {@link Wallet}. diff --git a/lib/bcoin/ldb.js b/lib/bcoin/ldb.js index 44600bc4..04e512ed 100644 --- a/lib/bcoin/ldb.js +++ b/lib/bcoin/ldb.js @@ -68,7 +68,7 @@ function ldb(options) { */ ldb.getName = function getName(db) { - var ext; + var name, ext; if (!db) db = bcoin.db || 'leveldb'; diff --git a/lib/bcoin/mempool.js b/lib/bcoin/mempool.js index 4b6e0dd6..71937ba6 100644 --- a/lib/bcoin/mempool.js +++ b/lib/bcoin/mempool.js @@ -12,7 +12,6 @@ var bcoin = require('./env'); var EventEmitter = require('events').EventEmitter; -var bn = require('bn.js'); var constants = bcoin.protocol.constants; var utils = require('./utils'); var assert = utils.assert; @@ -21,6 +20,7 @@ var BufferReader = require('./reader'); var VerifyError = bcoin.errors.VerifyError; var pad32 = utils.pad32; var DUMMY = new Buffer([0]); +var ptrSize; /** * Represents a mempool. @@ -227,9 +227,7 @@ Mempool.prototype.destroy = function destroy(callback) { Mempool.prototype.addBlock = function addBlock(block, callback, force) { var self = this; - var txs = []; var unlock = this._lock(addBlock, [block, callback], force); - var entry; if (!unlock) return; @@ -238,7 +236,6 @@ Mempool.prototype.addBlock = function addBlock(block, callback, force) { utils.forEachSerial(block.txs.slice().reverse(), function(tx, next) { var hash = tx.hash('hex'); - var copy; if (tx.isCoinbase()) return next(); @@ -334,7 +331,7 @@ Mempool.prototype.limitMempoolSize = function limitMempoolSize(entryHash, callba if (err) return callback(err); - utils.forEachSerial(function(entry, next) { + utils.forEachSerial(entries, function(entry, next) { if (self.getSize() <= self.maxSize) return callback(null, trimmed); @@ -623,7 +620,7 @@ Mempool.prototype.addTX = function addTX(tx, callback, force) { var lockFlags = constants.flags.STANDARD_LOCKTIME_FLAGS; var hash = tx.hash('hex'); var ret = {}; - var now, entry; + var entry; var unlock = this._lock(addTX, [tx, callback], force); if (!unlock) @@ -840,7 +837,7 @@ Mempool.prototype.removeUnchecked = function removeUnchecked(entry, limit, callb callback = utils.wrap(callback, unlock); - this.fillAllHistory(entry.tx, function(err, tx) { + this.fillAllHistory(entry.tx, function(err) { if (err) return callback(err); @@ -1061,7 +1058,7 @@ Mempool.prototype.countAncestors = function countAncestors(tx, callback) { var self = this; var max = 0; - utils.forEachSerial(tx.inputs, function(input, next, i) { + utils.forEachSerial(tx.inputs, function(input, next) { var count = 0; self.getTX(input.prevout.hash, function(err, tx) { if (err) @@ -1099,7 +1096,7 @@ Mempool.prototype.countAncestors = function countAncestors(tx, callback) { Mempool.prototype.storeOrphan = function storeOrphan(tx) { var prevout = {}; - var i, hash, batch, input, prev; + var i, hash, input, prev; if (tx.getSize() > 5000) { bcoin.debug('Ignoring large orphan: %s', tx.rhash); @@ -1434,7 +1431,7 @@ Mempool.prototype.isDoubleSpend = function isDoubleSpend(tx, callback) { /** * Calculate bitcoinj-style confidence. - * @see https://github.com/bitcoinj/bitcoinj/blob/master/core/src/main/java/org/bitcoinj/core/TransactionConfidence.java + * @see http://bit.ly/1OVQwlO * @param {TX|Hash} hash * @param {Function} callback - Returns [Error, Number]. */ @@ -1513,7 +1510,6 @@ Mempool.prototype.getConfidence = function getConfidence(hash, callback) { */ Mempool.prototype._addUnchecked = function _addUnchecked(entry, callback) { - var self = this; var tx = entry.tx; var hash = tx.hash('hex'); var i, addresses, address, input, output, key, coin, batch; @@ -1579,7 +1575,7 @@ Mempool.prototype._addUnchecked = function _addUnchecked(entry, callback) { Mempool.prototype._removeUnchecked = function _removeUnchecked(hash, limit, callback) { var self = this; - var batch, i, addresses, output, tx; + var batch, i, addresses, tx; if (hash.tx) hash = hash.tx.hash('hex'); @@ -1642,7 +1638,8 @@ Mempool.prototype._removeUnchecked = function _removeUnchecked(hash, limit, call return callback(err); utils.forEachSerial(tx.outputs, function(output, next, i) { - key = hash + '/' + i; + var key = hash + '/' + i; + var address; if (output.script.isUnspendable()) return next(); @@ -1718,9 +1715,9 @@ Mempool.prototype.memUsageAccurate = function memUsageAccurate(tx) { Mempool.prototype.memUsageBitcoind = function memUsageBitcoind(tx) { var mem = 0; - var i, input; + var i, j, input; - mem += mallocUsage(tx.inputs.length) + mem += mallocUsage(tx.inputs.length); mem += mallocUsage(tx.outputs.length); for (i = 0; i < tx.inputs.length; i++) @@ -1908,7 +1905,7 @@ MempoolEntry.prototype.isFree = function isFree(height) { * @const {Number} */ -var ptrSize = (process.platform == null +ptrSize = (process.platform == null || process.platform === 'x64' || process.platform === 'ia64' || process.platform === 'arm') ? 8 : 4; diff --git a/lib/bcoin/merkleblock.js b/lib/bcoin/merkleblock.js index d87de24b..a6dae1c9 100644 --- a/lib/bcoin/merkleblock.js +++ b/lib/bcoin/merkleblock.js @@ -165,7 +165,6 @@ MerkleBlock.prototype.verifyPartial = function verifyPartial() { */ MerkleBlock.prototype.extractTree = function extractTree() { - var self = this; var hashes = this.hashes; var flags = this.flags; var bitsUsed = 0; @@ -221,9 +220,9 @@ MerkleBlock.prototype.extractTree = function extractTree() { } for (p = 0; p < bits.length; p++) - bits[p] = (flags[p / 8 | 0] & (1 << (p % 8))) !== 0; + bits[p] = +((flags[p / 8 | 0] & (1 << (p % 8))) !== 0); - if (totalTX == 0) + if (totalTX === 0) return; if (totalTX > constants.block.MAX_SIZE / 60) @@ -247,7 +246,7 @@ MerkleBlock.prototype.extractTree = function extractTree() { if (((bitsUsed + 7) / 8 | 0) !== ((bits.length + 7) / 8 | 0)) return; - if (hashUsed != hashes.length) + if (hashUsed !== hashes.length) return; return { diff --git a/lib/bcoin/miner.js b/lib/bcoin/miner.js index 5aa68040..2213129a 100644 --- a/lib/bcoin/miner.js +++ b/lib/bcoin/miner.js @@ -315,8 +315,6 @@ Miner.prototype.createBlock = function createBlock(version, callback) { */ Miner.prototype.mineBlock = function mineBlock(version, callback) { - var self = this; - if (typeof version === 'function') { callback = version; version = null; diff --git a/lib/bcoin/mtx.js b/lib/bcoin/mtx.js index 8d789d78..e108c0cd 100644 --- a/lib/bcoin/mtx.js +++ b/lib/bcoin/mtx.js @@ -6,12 +6,10 @@ */ var bcoin = require('./env'); -var bn = require('bn.js'); var utils = require('./utils'); var assert = utils.assert; var constants = bcoin.protocol.constants; var Script = bcoin.script; -var Witness = bcoin.witness; var opcodes = constants.opcodes; /** @@ -363,7 +361,7 @@ MTX.prototype.createSignature = function createSignature(index, prev, key, type, */ MTX.prototype.signInput = function signInput(index, addr, type) { - var input, prev, signature, index, signatures, i; + var input, prev, signature, keyIndex, signatures, i; var len, m, n, keys, vector, dummy, version; if (typeof index !== 'number') @@ -500,25 +498,25 @@ MTX.prototype.signInput = function signInput(index, addr, type) { // Find the key index so we can place // the signature in the same index. - index = utils.indexOf(keys, addr.publicKey); + keyIndex = utils.indexOf(keys, addr.publicKey); // Our public key is not in the prev_out // script. We tried to sign a transaction // that is not redeemable by us. - if (index === -1) + if (keyIndex === -1) return false; // Offset key index by one to turn it into // "sig index". Accounts for OP_0 byte at // the start. - index++; + keyIndex++; // Add our signature to the correct slot // and increment the total number of // signatures. - if (index < len && signatures < m) { - if (Script.isZero(vector[index])) { - vector[index] = signature; + if (keyIndex < len && signatures < m) { + if (Script.isZero(vector[keyIndex])) { + vector[keyIndex] = signature; signatures++; } } @@ -558,7 +556,6 @@ MTX.prototype.signInput = function signInput(index, addr, type) { MTX.prototype.isSigned = function isSigned() { var i, input, prev, vector, m, len, j; - var total = 0; for (i = 0; i < this.inputs.length; i++) { input = this.inputs[i]; @@ -946,7 +943,7 @@ MTX.prototype.selectCoins = function selectCoins(coins, options) { var index = 0; var tx = this.clone(); var outputValue = tx.getOutputValue(); - var tryFree, i, size, change, fee, min, output; + var tryFree, i, size, change, fee, min, output, err; if (!options) options = {}; diff --git a/lib/bcoin/output.js b/lib/bcoin/output.js index 798537a8..23243cf7 100644 --- a/lib/bcoin/output.js +++ b/lib/bcoin/output.js @@ -6,8 +6,8 @@ */ var bcoin = require('./env'); -var bn = require('bn.js'); var utils = require('./utils'); +var constants = bcoin.protocol.constants; var assert = utils.assert; var BufferWriter = require('./writer'); var Framer = bcoin.protocol.framer; diff --git a/lib/bcoin/peer.js b/lib/bcoin/peer.js index d8f57a20..38e86229 100644 --- a/lib/bcoin/peer.js +++ b/lib/bcoin/peer.js @@ -7,7 +7,6 @@ var bcoin = require('./env'); var EventEmitter = require('events').EventEmitter; -var bn = require('bn.js'); var utils = require('./utils'); var assert = utils.assert; var constants = bcoin.protocol.constants; @@ -314,7 +313,6 @@ Peer.prototype.createSocket = function createSocket(port, host) { */ Peer.prototype.announce = function announce(items) { - var self = this; var inv = []; var headers = []; var i, item; @@ -364,7 +362,6 @@ Peer.prototype.announce = function announce(items) { */ Peer.prototype.sendInv = function sendInv(items) { - var self = this; var inv = []; var i, item, chunk; @@ -392,7 +389,6 @@ Peer.prototype.sendInv = function sendInv(items) { */ Peer.prototype.sendHeaders = function sendHeaders(items) { - var self = this; var headers = []; var i, item, chunk; @@ -893,7 +889,7 @@ Peer.prototype._handleGetUTXOs = function _handleGetUTXOs(payload) { if (payload.prevout.length > 15) return done(); - utils.forEachSerial(payload.prevout, function(prevout, next, i) { + utils.forEachSerial(payload.prevout, function(prevout, next) { var hash = prevout.hash; var index = prevout.index; @@ -1079,6 +1075,7 @@ Peer.prototype._handleGetBlocks = function _handleGetBlocks(payload) { }; Peer.prototype._handleVersion = function _handleVersion(payload) { + var self = this; var version = payload.version; var services = payload.services; @@ -1451,7 +1448,7 @@ Peer.prototype._handleGetAddr = function _handleGetAddr() { var hosts = {}; var items = []; var ts = utils.now() - (process.uptime() | 0); - var i, seed, peer; + var i, seed; if (this.pool.options.selfish) return; diff --git a/lib/bcoin/pool.js b/lib/bcoin/pool.js index 01fb78b5..92823e5c 100644 --- a/lib/bcoin/pool.js +++ b/lib/bcoin/pool.js @@ -74,7 +74,6 @@ var VerifyError = bcoin.errors.VerifyError; */ function Pool(options) { - var self = this; var seeds; if (!(this instanceof Pool)) @@ -219,6 +218,7 @@ Pool.prototype.open = function open(callback) { Pool.prototype.connect = function connect() { var self = this; + var i; assert(this.loaded, 'Pool is not loaded.'); @@ -257,7 +257,6 @@ Pool.prototype.connect = function connect() { Pool.prototype._init = function _init() { var self = this; - var i; if (this.mempool) { this.mempool.on('bad orphan', function(tx) { @@ -725,12 +724,12 @@ Pool.prototype._handleHeaders = function _handleHeaders(headers, peer, callback) var hash = header.hash('hex'); if (last && header.prevBlock !== last) { - peer.sendReject(block, 'invalid', 'bad-prevblk', 100); + peer.sendReject(header, 'invalid', 'bad-prevblk', 100); return next(new Error('Bad header chain.')); } if (!header.verify(ret)) { - peer.sendReject(block, 'invalid', ret.reason, 100); + peer.sendReject(header, 'invalid', ret.reason, 100); self.rejects.add(header.hash()); return next(new VerifyError(header, 'invalid', ret.reason, ret.score)); } @@ -997,7 +996,7 @@ Pool.prototype._createPeer = function _createPeer(options) { }); peer.on('notfound', function(items) { - var i, item; + var i, item, req; for (i = 0; i < items.length; i++) { item = items[i]; @@ -1223,8 +1222,6 @@ Pool.prototype._addPeer = function _addPeer() { }); peer.once('ack', function() { - var i; - if (self.destroyed) return; @@ -1289,8 +1286,6 @@ Pool.prototype._removePeer = function _removePeer(peer) { */ Pool.prototype.watch = function watch(data) { - var self = this; - if (Buffer.isBuffer(data)) data = data.toString('hex'); @@ -1317,8 +1312,6 @@ Pool.prototype.watch = function watch(data) { */ Pool.prototype.unwatch = function unwatch(data) { - var self = this; - if (Buffer.isBuffer(data)) data = data.toString('hex'); @@ -1868,7 +1861,6 @@ Pool.prototype.getUTXOs = function getUTXOs(utxos, callback) { Pool.prototype.fillHistory = function fillHistory(tx, callback) { var utxos = []; - var reqs = []; var i, input; for (i = 0; i < tx.inputs.length; i++) { @@ -1965,7 +1957,7 @@ Pool.prototype._getRandom = function _getRandom(seeds, uniq) { */ Pool.prototype.setSeeds = function setSeeds(seeds) { - var i, seed; + var i; this.seeds = []; this.hosts = {}; @@ -2358,9 +2350,6 @@ BroadcastItem.prototype.addCallback = function addCallback(callback) { */ BroadcastItem.prototype.start = function start() { - var self = this; - var i; - assert(!this.timeout, 'Already started.'); assert(!this.pool.inv.map[this.hash], 'Already started.'); @@ -2429,7 +2418,6 @@ BroadcastItem.prototype.finish = function finish(err) { */ BroadcastItem.prototype.send = function send(peer, witness) { - var self = this; var data; if (!this.msg) { diff --git a/lib/bcoin/protocol/framer.js b/lib/bcoin/protocol/framer.js index a238b7a4..f0329070 100644 --- a/lib/bcoin/protocol/framer.js +++ b/lib/bcoin/protocol/framer.js @@ -837,10 +837,9 @@ Framer.witnessTX = function _witnessTX(tx, writer) { */ Framer.script = function _script(script, writer) { + var p = new BufferWriter(writer); var data; - p = new BufferWriter(writer); - if (script.encode) data = script.encode(); else @@ -979,7 +978,7 @@ Framer.extendedTX = function extendedTX(tx, saveCoins, writer) { var index = tx.index; var changeIndex = tx.changeIndex != null ? tx.changeIndex : -1; var p = new BufferWriter(writer); - var i, input, tmp; + var i, input; if (height === -1) height = 0x7fffffff; @@ -1141,7 +1140,7 @@ Framer.reject = function reject(details, writer) { var ccode = details.ccode; if (typeof ccode === 'string') - ccode = constants.reject[ccode.toUpperCase()] + ccode = constants.reject[ccode.toUpperCase()]; if (!ccode) ccode = constants.reject.INVALID; @@ -1293,7 +1292,7 @@ Framer.getUTXOs = function getUTXOs(data, writer) { Framer.UTXOs = function UTXOs(data, writer) { var p = new BufferWriter(writer); - var i, j, coin, height, index, map; + var i, coin, height, map; if (!data.map) { assert(data.hits); @@ -1515,7 +1514,7 @@ Framer.block.size = function blockSize(block) { */ Framer.tx.size = function txSize(tx) { - var writer = new BufferWriter() + var writer = new BufferWriter(); Framer.renderTX(tx, false, writer); return writer.written; }; diff --git a/lib/bcoin/protocol/network.js b/lib/bcoin/protocol/network.js index 103d020c..f1723079 100644 --- a/lib/bcoin/protocol/network.js +++ b/lib/bcoin/protocol/network.js @@ -7,7 +7,6 @@ var bn = require('bn.js'); var utils = require('../utils'); -var assert = utils.assert; /** * @module network diff --git a/lib/bcoin/protocol/parser.js b/lib/bcoin/protocol/parser.js index 306f6d31..dc3f672e 100644 --- a/lib/bcoin/protocol/parser.js +++ b/lib/bcoin/protocol/parser.js @@ -6,6 +6,7 @@ */ var bcoin = require('../env'); +var bn = require('bn.js'); var EventEmitter = require('events').EventEmitter; var utils = require('../utils'); var assert = utils.assert; @@ -181,7 +182,6 @@ Parser.parseMempool = function parseMempool(p) { */ Parser.parseSubmitOrder = function parseSubmitOrder(p) { - var hash, tx; p = new BufferReader(p); return { hash: p.readHash('hex'), @@ -206,7 +206,6 @@ Parser.parseCheckOrder = function parseCheckOrder(p) { */ Parser.parseReply = function parseReply(p) { - var hash, code, publicKey; p = new BufferReader(p); return { hash: p.readHash('hex'), @@ -403,7 +402,7 @@ Parser.parseGetUTXOs = function parseGetUTXOs(p) { Parser.parseUTXOs = function parseUTXOs(p) { var chainHeight, tip, map, count, coins; - var coin, version, height, i, hits, ch, j; + var coin, version, height, i, hits, j; p = new BufferReader(p); @@ -1245,7 +1244,7 @@ Parser.parseAlert = function parseAlert(p) { var version, relayUntil, expiration, id, cancel; var cancels, count, i, minVer, maxVer, subVers; var priority, comment, statusBar, reserved; - var payload; + var payload, signature; p = new BufferReader(p); diff --git a/lib/bcoin/script.js b/lib/bcoin/script.js index 102c4c02..894d6e05 100644 --- a/lib/bcoin/script.js +++ b/lib/bcoin/script.js @@ -123,7 +123,7 @@ Witness.prototype.getInputType = function getInputType() { * @returns {String|null} */ -Witness.prototype.getInputAddress = function getInputAddress(network) { +Witness.prototype.getInputAddress = function getInputAddress() { return bcoin.address.fromWitness(this); }; @@ -251,7 +251,7 @@ Witness.fromRaw = function fromRaw(data, enc) { */ Witness.fromString = function fromString(items) { - var result; + var i, result; if (!Array.isArray(items)) { if (typeof items !== 'string') @@ -991,7 +991,7 @@ Script.prototype.execute = function execute(stack, flags, tx, index, version) { var op, val, v1, v2, v3; var n, n1, n2, n3; var res, key, sig, type, subscript, hash; - var keys, i, j, m, ikey, isig; + var i, j, m, ikey, isig; var locktime; if (flags == null) @@ -1829,7 +1829,7 @@ Script.bool = function bool(value) { */ Script.num = function num(value, flags, size) { - var result, n; + var result; assert(Buffer.isBuffer(value)); @@ -3472,6 +3472,8 @@ Script.prototype.getScripthashSigops = function getScripthashSigops(input) { */ Script.witnessSigops = function witnessSigops(program, witness, flags) { + var redeem; + if (flags == null) flags = constants.flags.STANDARD_VERIFY_FLAGS; @@ -3599,7 +3601,7 @@ Script.fromString = function fromString(code) { if (op[0] === '\'') { assert(op[op.length - 1] === '\'', 'Unknown opcode.'); op = op.slice(1, -1); - op = new Buffer(op, 'ascii') + op = new Buffer(op, 'ascii'); p.writeBytes(Script.encode([op])); continue; } diff --git a/lib/bcoin/timedata.js b/lib/bcoin/timedata.js index bbdd78c7..cbfb546a 100644 --- a/lib/bcoin/timedata.js +++ b/lib/bcoin/timedata.js @@ -8,7 +8,6 @@ var bcoin = require('./env'); var EventEmitter = require('events').EventEmitter; var utils = require('./utils'); -var assert = utils.assert; /** * An object which handles "adjusted time". This may not diff --git a/lib/bcoin/tx.js b/lib/bcoin/tx.js index 986364e7..fdaace65 100644 --- a/lib/bcoin/tx.js +++ b/lib/bcoin/tx.js @@ -6,13 +6,11 @@ */ var bcoin = require('./env'); -var bn = require('bn.js'); var utils = require('./utils'); var assert = utils.assert; var constants = bcoin.protocol.constants; var Script = bcoin.script; var Stack = bcoin.stack; -var BufferReader = require('./reader'); var BufferWriter = require('./writer'); /** @@ -925,7 +923,7 @@ TX.prototype.hasCoins = function hasCoins() { TX.prototype.fillCoins = function fillCoins(coins) { var total = 0; - var inputs, txs, key, i, input, prevout; + var key, i, input, prevout; if ((coins instanceof bcoin.coin) || (coins instanceof bcoin.tx)) coins = [coins]; @@ -1369,7 +1367,7 @@ TX.prototype.checkInputs = function checkInputs(spendHeight, ret) { value = this.getOutputValue(); if (value > total) { - ret.reason = 'bad-txns-in-belowout' + ret.reason = 'bad-txns-in-belowout'; ret.score = 100; return false; } @@ -1906,7 +1904,7 @@ TX.fromRaw = function fromRaw(data, enc) { */ TX.prototype.toExtended = function toExtended(saveCoins, enc) { - var data; + var data, tmp; if (typeof saveCoins === 'string') { tmp = saveCoins; @@ -1932,6 +1930,8 @@ TX.prototype.toExtended = function toExtended(saveCoins, enc) { */ TX.parseExtended = function parseExtended(data, saveCoins, enc) { + var tmp; + if (typeof saveCoins === 'string') { tmp = saveCoins; saveCoins = enc; diff --git a/lib/bcoin/txdb.js b/lib/bcoin/txdb.js index d8845fb3..df9ed8a4 100644 --- a/lib/bcoin/txdb.js +++ b/lib/bcoin/txdb.js @@ -22,7 +22,6 @@ */ var bcoin = require('./env'); -var bn = require('bn.js'); var utils = require('./utils'); var assert = bcoin.utils.assert; var EventEmitter = require('events').EventEmitter; @@ -75,6 +74,7 @@ TXDB.prototype._lock = function _lock(func, args, force) { TXDB.prototype._loadFilter = function loadFilter(callback) { var self = this; + var i; if (!this.filter) return callback(); @@ -612,7 +612,7 @@ TXDB.prototype._removeSpenders = function removeSpenders(hash, ref, callback) { if (tx.ts !== 0) return callback(null, false); - if (ref.ts === 0 && ref.ps < ts.ps) + if (ref.ts === 0 && ref.ps < tx.ps) return callback(null, false); utils.forEachSerial(tx.outputs, function(output, next, i) { @@ -1089,6 +1089,7 @@ TXDB.prototype._unconfirm = function unconfirm(tx, map, callback, force) { TXDB.prototype.getHistoryHashes = function getHistoryHashes(address, callback) { var self = this; + var txs = []; if (typeof address === 'function') { callback = address; @@ -1137,6 +1138,7 @@ TXDB.prototype.getHistoryHashes = function getHistoryHashes(address, callback) { TXDB.prototype.getUnconfirmedHashes = function getUnconfirmedHashes(address, callback) { var self = this; + var txs = []; if (typeof address === 'function') { callback = address; @@ -1186,6 +1188,7 @@ TXDB.prototype.getUnconfirmedHashes = function getUnconfirmedHashes(address, cal TXDB.prototype.getCoinHashes = function getCoinHashes(address, callback) { var self = this; + var coins = []; if (typeof address === 'function') { callback = address; @@ -1667,7 +1670,6 @@ TXDB.prototype.getBalance = function getBalance(address, callback) { var self = this; var confirmed = 0; var unconfirmed = 0; - var i; if (typeof address === 'function') { callback = address; diff --git a/lib/bcoin/uri.js b/lib/bcoin/uri.js index 945f6551..12f4fca5 100644 --- a/lib/bcoin/uri.js +++ b/lib/bcoin/uri.js @@ -10,6 +10,7 @@ var url = require('url'); var querystring = require('querystring'); var utils = require('./utils'); +var assert = utils.assert; /** * Parse a bitcoin URI. diff --git a/lib/bcoin/utils.js b/lib/bcoin/utils.js index cb5f7fde..8d5922b7 100644 --- a/lib/bcoin/utils.js +++ b/lib/bcoin/utils.js @@ -5,6 +5,8 @@ * https://github.com/indutny/bcoin */ +/* global gc */ + /** * @exports utils */ @@ -903,7 +905,7 @@ utils.isIP = function isIP(ip) { * @returns {Boolean} */ -utils.isMapped = function isMapped(ip, version) { +utils.isMapped = function isMapped(ip) { var i; if (!Buffer.isBuffer(ip)) @@ -2588,6 +2590,8 @@ utils.binaryRemove = function binaryRemove(items, item, compare) { */ utils.global = (function() { + /* global self */ + if (this) return this; diff --git a/lib/bcoin/wallet.js b/lib/bcoin/wallet.js index a59911cb..8a16768c 100644 --- a/lib/bcoin/wallet.js +++ b/lib/bcoin/wallet.js @@ -1008,12 +1008,11 @@ Wallet.prototype.syncOutputDepth = function syncOutputDepth(tx) { /** * Get a redeem script or witness script by hash. * @param {Hash} hash - Can be a ripemd160 or a sha256. - * @param {Number} prefix - See {@link network.address.prefixes}. * @returns {Script} */ -Wallet.prototype.getRedeem = function getRedeem(hash, prefix) { - var addr, address; +Wallet.prototype.getRedeem = function getRedeem(hash) { + var address; if (typeof hash === 'string') hash = new Buffer(hash, 'hex'); @@ -1056,7 +1055,6 @@ Wallet.prototype.zap = function zap(now, age, callback) { Wallet.prototype.scan = function scan(getByAddress, callback) { var self = this; var res = false; - var i; return this._scan({}, getByAddress, function(err, depth, txs) { if (err) diff --git a/lib/bcoin/worker.js b/lib/bcoin/worker.js index 5ff65c8f..72dc9ae1 100644 --- a/lib/bcoin/worker.js +++ b/lib/bcoin/worker.js @@ -5,6 +5,8 @@ * https://github.com/indutny/bcoin */ +/* jshint worker: true */ + var bcoin, env; if (typeof importScripts !== 'undefined') { diff --git a/lib/bcoin/workers.js b/lib/bcoin/workers.js index 28ba21ec..00034748 100644 --- a/lib/bcoin/workers.js +++ b/lib/bcoin/workers.js @@ -8,12 +8,12 @@ var bcoin = require('./env'); var EventEmitter = require('events').EventEmitter; var bn = require('bn.js'); -var constants = bcoin.protocol.constants; var utils = require('./utils'); var global = utils.global; var assert = utils.assert; var BufferWriter = require('./writer'); var BufferReader = require('./reader'); +var jobs; /** * A worker pool. @@ -644,7 +644,7 @@ Master.listen = function listen(id, options) { * @const {Object} */ -var jobs = {}; +jobs = {}; /** * Execute tx.verify() on worker. @@ -710,7 +710,7 @@ Framer.prototype.packet = function packet(job, name, items) { p.writeU32(payload.length); p.writeBytes(payload); return p.render(); -} +}; Framer.prototype.body = function body(name, items) { var p = new BufferWriter(); diff --git a/package.json b/package.json index d5a21227..a4e0e955 100644 --- a/package.json +++ b/package.json @@ -37,7 +37,8 @@ }, "optionalDependencies": { "secp256k1": "3.0.0", - "socket.io": "1.4.5" + "socket.io": "1.4.6", + "socket.io-client": "1.4.6" }, "devDependencies": { "browserify": "13.0.0",