diff --git a/.eslintrc.json b/.eslintrc.json index b1789709..e648de87 100644 --- a/.eslintrc.json +++ b/.eslintrc.json @@ -52,7 +52,7 @@ "no-extra-semi": "off", "no-fallthrough": "off", "no-func-assign": "off", - "no-implicit-coercion": "off", + "no-implicit-coercion": "error", "no-multi-assign": "error", "no-multiple-empty-lines": ["error", { "max": 1 diff --git a/bin/cli b/bin/cli index 332ffccf..1d37e4c4 100755 --- a/bin/cli +++ b/bin/cli @@ -173,7 +173,7 @@ CLI.prototype.getBlock = async function getBlock() { let hash = this.config.str(0); if (hash.length !== 64) - hash = +hash; + hash = parseInt(hash, 10); const block = await this.client.getBlock(hash); @@ -386,7 +386,7 @@ CLI.prototype.reset = async function reset() { let hash = this.config.str(0); if (hash.length !== 64) - hash = +hash; + hash = parseInt(hash, 10); await this.client.reset(hash); diff --git a/lib/blockchain/layout-browser.js b/lib/blockchain/layout-browser.js index 7eb90bad..701a627b 100644 --- a/lib/blockchain/layout-browser.js +++ b/lib/blockchain/layout-browser.js @@ -49,7 +49,7 @@ const layout = { vv: function vv(key) { assert(typeof key === 'string'); assert(key.length === 36); - return [+key.slice(1, 4), key.slice(4, 36)]; + return [parseInt(key.slice(1, 4), 10), key.slice(4, 36)]; }, T: function T(addr, hash) { addr = hex(addr); @@ -80,10 +80,10 @@ const layout = { let hash, index; if (key.length === 139) { hash = key.slice(65, 129); - index = +key.slice(129); + index = parseInt(key.slice(129), 10); } else if (key.length === 115) { hash = key.slice(41, 105); - index = +key.slice(105); + index = parseInt(key.slice(105), 10); } else { assert(false); } diff --git a/lib/btc/amount.js b/lib/btc/amount.js index a64e1e67..0dbdcd4d 100644 --- a/lib/btc/amount.js +++ b/lib/btc/amount.js @@ -365,7 +365,7 @@ Amount.serialize = function serialize(value, exp, num) { result = '-' + result; if (num) - return +result; + return Number(result); return result; }; @@ -385,7 +385,7 @@ Amount.serializeUnsafe = function serializeUnsafe(value, exp, num) { value = value.toFixed(exp); if (num) - return +value; + return Number(value); if (exp !== 0) { value = value.replace(/0+$/, ''); diff --git a/lib/crypto/secp256k1.js b/lib/crypto/secp256k1.js index 32813306..d2ee2f83 100644 --- a/lib/crypto/secp256k1.js +++ b/lib/crypto/secp256k1.js @@ -8,7 +8,7 @@ let native; -if (+process.env.BCOIN_NO_SECP256K1 !== 1) { +if (Number(process.env.BCOIN_NO_SECP256K1) !== 1) { try { native = require('secp256k1/bindings'); } catch (e) { diff --git a/lib/http/base.js b/lib/http/base.js index 355aa435..917d68ab 100644 --- a/lib/http/base.js +++ b/lib/http/base.js @@ -1495,7 +1495,7 @@ Response.prototype.send = function send(code, msg, type) { if (typeof msg === 'string') { const len = Buffer.byteLength(msg, 'utf8'); - this.setHeader('Content-Length', len + ''); + this.setHeader('Content-Length', len.toString(10)); try { this.write(msg, 'utf8'); this.end(); @@ -1506,7 +1506,7 @@ Response.prototype.send = function send(code, msg, type) { } if (Buffer.isBuffer(msg)) { - this.setHeader('Content-Length', msg.length + ''); + this.setHeader('Content-Length', msg.length.toString(10)); try { this.write(msg); this.end(); diff --git a/lib/http/request.js b/lib/http/request.js index bcc1da38..98fc6304 100644 --- a/lib/http/request.js +++ b/lib/http/request.js @@ -234,7 +234,7 @@ RequestOptions.prototype.getHeaders = function getHeaders() { headers['Content-Type'] = getType(this.type); if (this.body) - headers['Content-Length'] = this.body.length + ''; + headers['Content-Length'] = this.body.length.toString(10); if (this.auth) { const auth = `${this.auth.username}:${this.auth.password}`; diff --git a/lib/http/rpc.js b/lib/http/rpc.js index 7e1441bb..fc0c74c8 100644 --- a/lib/http/rpc.js +++ b/lib/http/rpc.js @@ -2184,7 +2184,7 @@ RPC.prototype.handleLongpoll = async function handleLongpoll(lpid) { throw new RPCError(errs.INVALID_PARAMETER, 'Invalid longpoll ID.'); const watched = lpid.slice(0, 64); - const lastTX = +lpid.slice(64, 74); + const lastTX = parseInt(lpid.slice(64, 74), 10); if (!util.isHex(watched) || !util.isUInt32(lastTX)) throw new RPCError(errs.INVALID_PARAMETER, 'Invalid longpoll ID.'); @@ -2450,7 +2450,7 @@ RPC.prototype.getHashRate = async function getHashRate(lookup, height) { const workDiff = tip.chainwork.sub(entry.chainwork); const timeDiff = maxTime - minTime; - const ps = +workDiff.toString(10) / timeDiff; + const ps = parseInt(workDiff.toString(10), 10) / timeDiff; return ps; }; diff --git a/lib/http/rpcclient.js b/lib/http/rpcclient.js index c7fd5302..75e869e6 100644 --- a/lib/http/rpcclient.js +++ b/lib/http/rpcclient.js @@ -86,7 +86,7 @@ function RPCError(msg, code) { Error.call(this); this.type = 'RPCError'; - this.message = msg + ''; + this.message = String(msg); this.code = code >>> 0; if (Error.captureStackTrace) diff --git a/lib/http/server.js b/lib/http/server.js index f8c6b589..09c2bf1e 100644 --- a/lib/http/server.js +++ b/lib/http/server.js @@ -262,7 +262,7 @@ HTTPServer.prototype.initRouter = function initRouter() { if (hash.length === 64) hash = util.revHex(hash); else - hash = +hash; + hash = parseInt(hash, 10); const block = await this.chain.db.getBlock(hash); diff --git a/lib/native.js b/lib/native.js index 0c9df733..cdd37639 100644 --- a/lib/native.js +++ b/lib/native.js @@ -8,7 +8,7 @@ exports.binding = null; -if (+process.env.BCOIN_NO_NATIVE !== 1) { +if (Number(process.env.BCOIN_NO_NATIVE) !== 1) { try { exports.binding = require('bcoin-native'); } catch (e) { diff --git a/lib/net/packets.js b/lib/net/packets.js index b5a77175..4c4d5c0a 100644 --- a/lib/net/packets.js +++ b/lib/net/packets.js @@ -1536,7 +1536,7 @@ RejectPacket.prototype.getCode = function getCode() { const code = RejectPacket.codesByVal[this.code]; if (!code) - return this.code + ''; + return this.code.toString(10); return code.toLowerCase(); }; diff --git a/lib/net/upnp.js b/lib/net/upnp.js index 84b60fbc..7a8293ca 100644 --- a/lib/net/upnp.js +++ b/lib/net/upnp.js @@ -374,7 +374,7 @@ UPNPService.prototype.soapRequest = async function soapRequest(action, args) { expect: 'xml', headers: { 'Content-Type': 'text/xml; charset="utf-8"', - 'Content-Length': Buffer.byteLength(req, 'utf8') + '', + 'Content-Length': Buffer.byteLength(req, 'utf8').toString(10), 'Connection': 'close', 'SOAPAction': JSON.stringify(`${type}#${action}`) }, @@ -683,7 +683,7 @@ function findError(el) { const ccode = child.find('errorCode'); if (ccode && /^\d+$/.test(ccode.text)) - code = +ccode.text; + code = parseInt(ccode.text, 10); let desc = 'Unknown'; const cdesc = child.find('errorDescription'); diff --git a/lib/node/logger.js b/lib/node/logger.js index 747c8b24..6f8eb73c 100644 --- a/lib/node/logger.js +++ b/lib/node/logger.js @@ -45,7 +45,7 @@ function Logger(options) { * @const {Boolean} */ -Logger.HAS_TTY = !!(process.stdout && process.stdout.isTTY); +Logger.HAS_TTY = Boolean(process.stdout && process.stdout.isTTY); /** * Maximum file size. @@ -625,7 +625,7 @@ Logger.prototype.logError = function logError(level, module, err) { console.error(err); } - let msg = (err.message + '').replace(/^ *Error: */, ''); + let msg = String(err.message).replace(/^ *Error: */, ''); if (level !== Logger.levels.ERROR) msg = `Error: ${msg}`; diff --git a/lib/primitives/abstractblock.js b/lib/primitives/abstractblock.js index 5f3a50f6..cc2b5af8 100644 --- a/lib/primitives/abstractblock.js +++ b/lib/primitives/abstractblock.js @@ -72,7 +72,7 @@ AbstractBlock.prototype.parseOptions = function parseOptions(options) { this.nonce = options.nonce; if (options.mutable != null) - this.mutable = !!options.mutable; + this.mutable = Boolean(options.mutable); return this; }; diff --git a/lib/primitives/coin.js b/lib/primitives/coin.js index f9e6a7f9..b5e73221 100644 --- a/lib/primitives/coin.js +++ b/lib/primitives/coin.js @@ -163,7 +163,7 @@ Coin.prototype.toKey = function toKey() { Coin.prototype.fromKey = function fromKey(key) { assert(key.length > 64); this.hash = key.slice(0, 64); - this.index = +key.slice(64); + this.index = parseInt(key.slice(64), 10); return this; }; diff --git a/lib/primitives/outpoint.js b/lib/primitives/outpoint.js index 9195c61c..a23400ea 100644 --- a/lib/primitives/outpoint.js +++ b/lib/primitives/outpoint.js @@ -110,7 +110,7 @@ Outpoint.prototype.toKey = function toKey() { Outpoint.prototype.fromKey = function fromKey(key) { assert(key.length > 64); this.hash = key.slice(0, 64); - this.index = +key.slice(64); + this.index = parseInt(key.slice(64), 10); return this; }; diff --git a/lib/protocol/network.js b/lib/protocol/network.js index 16d2144a..88599b43 100644 --- a/lib/protocol/network.js +++ b/lib/protocol/network.js @@ -100,7 +100,7 @@ Network.prototype._init = function _init() { for (const key of Object.keys(this.checkpointMap)) { const hash = this.checkpointMap[key]; - const height = +key; + const height = Number(key); this.checkpoints.push({ hash: hash, height: height }); } diff --git a/lib/utils/lru.js b/lib/utils/lru.js index e3378e97..b971ae3f 100644 --- a/lib/utils/lru.js +++ b/lib/utils/lru.js @@ -115,7 +115,7 @@ LRU.prototype.set = function set(key, value) { if (this.capacity === 0) return; - key = key + ''; + key = String(key); let item = this.map.get(key); @@ -151,7 +151,7 @@ LRU.prototype.get = function get(key) { if (this.capacity === 0) return null; - key = key + ''; + key = String(key); const item = this.map.get(key); @@ -173,7 +173,7 @@ LRU.prototype.get = function get(key) { LRU.prototype.has = function has(key) { if (this.capacity === 0) return false; - return this.map.has(key + ''); + return this.map.has(String(key)); }; /** @@ -186,7 +186,7 @@ LRU.prototype.remove = function remove(key) { if (this.capacity === 0) return false; - key = key + ''; + key = String(key); const item = this.map.get(key); diff --git a/lib/utils/util.js b/lib/utils/util.js index 19e2cac5..843046db 100644 --- a/lib/utils/util.js +++ b/lib/utils/util.js @@ -315,7 +315,7 @@ util.fmt = nodeUtil.format; util.format = function format(args, color) { if (args.length > 0 && args[0] && typeof args[0] === 'object') { if (color == null) - color = !!(process.stdout && process.stdout.isTTY); + color = Boolean(process.stdout && process.stdout.isTTY); return util.inspectify(args[0], color); } return util.fmt(...args); @@ -540,7 +540,7 @@ util.pad8 = function pad8(num) { assert(typeof num === 'number'); assert(num >= 0); - num = num + ''; + num = num.toString(10); switch (num.length) { case 1: @@ -565,7 +565,7 @@ util.pad32 = function pad32(num) { assert(typeof num === 'number'); assert(num >= 0); - num = num + ''; + num = num.toString(10); switch (num.length) { case 1: diff --git a/lib/wallet/account.js b/lib/wallet/account.js index 70e492c4..4eb12c67 100644 --- a/lib/wallet/account.js +++ b/lib/wallet/account.js @@ -187,7 +187,7 @@ Account.prototype.fromOptions = function fromOptions(options) { this.type = Account.types.MULTISIG; if (!this.name) - this.name = this.accountIndex + ''; + this.name = this.accountIndex.toString(10); if (this.m < 1 || this.m > this.n) throw new Error('m ranges between 1 and n'); diff --git a/lib/wallet/layout-browser.js b/lib/wallet/layout-browser.js index 440dcf1b..cbaa13ff 100644 --- a/lib/wallet/layout-browser.js +++ b/lib/wallet/layout-browser.js @@ -42,7 +42,7 @@ layouts.walletdb = { }, ww: function ww(key) { assert(typeof key === 'string'); - return +key.slice(1); + return parseInt(key.slice(1), 10); }, l: function l(id) { assert(typeof id === 'string'); @@ -61,7 +61,7 @@ layouts.walletdb = { }, ii: function ii(key) { assert(typeof key === 'string'); - return [+key.slice(1, 11), key.slice(11)]; + return [parseInt(key.slice(1, 11), 10), key.slice(11)]; }, n: function n(wid, index) { return 'n' + pad32(wid) + pad32(index); @@ -75,14 +75,14 @@ layouts.walletdb = { }, bb: function bb(key) { assert(typeof key === 'string'); - return +key.slice(1); + return parseInt(key.slice(1), 10); }, o: function o(hash, index) { assert(typeof hash === 'string'); return 'o' + hash + pad32(index); }, oo: function oo(key) { - return [key.slice(1, 65), +key.slice(65)]; + return [key.slice(1, 65), parseInt(key.slice(65), 10)]; } }; @@ -94,7 +94,7 @@ layouts.txdb = { }, pre: function pre(key) { assert(typeof key === 'string'); - return +key.slice(1, 11); + return parseInt(key.slice(1, 11), 10); }, R: 'R', hi: function hi(ch, hash, index) { @@ -104,7 +104,7 @@ layouts.txdb = { hii: function hii(key) { assert(typeof key === 'string'); key = key.slice(12); - return [key.slice(0, 64), +key.slice(64)]; + return [key.slice(0, 64), parseInt(key.slice(64), 10)]; }, ih: function ih(ch, index, hash) { assert(typeof hash === 'string'); @@ -113,7 +113,7 @@ layouts.txdb = { ihh: function ihh(key) { assert(typeof key === 'string'); key = key.slice(12); - return [+key.slice(0, 10), key.slice(10)]; + return [parseInt(key.slice(0, 10), 10), key.slice(10)]; }, iih: function iih(ch, index, num, hash) { assert(typeof hash === 'string'); @@ -122,7 +122,11 @@ layouts.txdb = { iihh: function iihh(key) { assert(typeof key === 'string'); key = key.slice(12); - return [+key.slice(0, 10), +key.slice(10, 20), key.slice(20)]; + return [ + parseInt(key.slice(0, 10), 10), + parseInt(key.slice(10, 20), 10), + key.slice(20) + ]; }, ihi: function ihi(ch, index, hash, num) { assert(typeof hash === 'string'); @@ -131,7 +135,11 @@ layouts.txdb = { ihii: function ihii(key) { assert(typeof key === 'string'); key = key.slice(12); - return [+key.slice(0, 10), key.slice(10, 74), +key.slice(74)]; + return [ + parseInt(key.slice(0, 10), 10), + key.slice(10, 74), + parseInt(key.slice(74), 10) + ]; }, ha: function ha(ch, hash) { assert(typeof hash === 'string'); @@ -229,6 +237,6 @@ layouts.txdb = { bb: function bb(key) { assert(typeof key === 'string'); key = key.slice(12); - return +key.slice(0); + return parseInt(key.slice(0), 10); } }; diff --git a/lib/wallet/wallet.js b/lib/wallet/wallet.js index 13a9541e..7f62fe7e 100644 --- a/lib/wallet/wallet.js +++ b/lib/wallet/wallet.js @@ -671,7 +671,7 @@ Wallet.prototype._createAccount = async function _createAccount(options, passphr let name = options.name; if (!name) - name = this.accountDepth + ''; + name = this.accountDepth.toString(10); if (await this.hasAccount(name)) throw new Error('Account already exists.'); diff --git a/lib/workers/child.js b/lib/workers/child.js index 8cc99543..6b1430aa 100644 --- a/lib/workers/child.js +++ b/lib/workers/child.js @@ -149,9 +149,9 @@ function listenExit(handler) { const onError = (err) => { if (err && err.stack) - console.error(err.stack + ''); + console.error(String(err.stack)); else - console.error(err + ''); + console.error(String(err)); process.exit(1); }; diff --git a/lib/workers/packets.js b/lib/workers/packets.js index b8a96117..85113d58 100644 --- a/lib/workers/packets.js +++ b/lib/workers/packets.js @@ -176,8 +176,8 @@ ErrorPacket.prototype.cmd = packetTypes.ERROR; ErrorPacket.prototype.getSize = function getSize() { let size = 0; - size += encoding.sizeVarString(this.error.message + '', 'utf8'); - size += encoding.sizeVarString(this.error.stack + '', 'utf8'); + size += encoding.sizeVarString(String(this.error.message), 'utf8'); + size += encoding.sizeVarString(String(this.error.stack), 'utf8'); size += encoding.sizeVarString(this.error.type || '', 'utf8'); switch (typeof this.error.code) { @@ -198,8 +198,8 @@ ErrorPacket.prototype.getSize = function getSize() { }; ErrorPacket.prototype.toWriter = function toWriter(bw) { - bw.writeVarString(this.error.message + '', 'utf8'); - bw.writeVarString(this.error.stack + '', 'utf8'); + bw.writeVarString(String(this.error.message), 'utf8'); + bw.writeVarString(String(this.error.stack), 'utf8'); bw.writeVarString(this.error.type || '', 'utf8'); switch (typeof this.error.code) { @@ -319,7 +319,7 @@ CheckResultPacket.prototype.getSize = function getSize() { size += 1; size += encoding.sizeVarString(err.code, 'utf8'); size += encoding.sizeVarString(err.message, 'utf8'); - size += encoding.sizeVarString(err.stack + '', 'utf8'); + size += encoding.sizeVarString(String(err.stack), 'utf8'); size += 1; size += 4; @@ -337,7 +337,7 @@ CheckResultPacket.prototype.toWriter = function toWriter(bw) { bw.writeU8(1); bw.writeVarString(err.code, 'utf8'); bw.writeVarString(err.message, 'utf8'); - bw.writeVarString(err.stack + '', 'utf8'); + bw.writeVarString(String(err.stack), 'utf8'); bw.writeU8(err.op === -1 ? 0xff : err.op); bw.writeU32(err.ip === -1 ? 0xffffffff : err.ip); }; diff --git a/migrate/walletdb2to3.js b/migrate/walletdb2to3.js index e8e9b09a..381cb976 100644 --- a/migrate/walletdb2to3.js +++ b/migrate/walletdb2to3.js @@ -73,7 +73,7 @@ async function updatePathMap() { const keys = Object.keys(oldPaths); for (let i = 0; i < keys.length; i++) { - keys[i] = +keys[i]; + keys[i] = Number(keys[i]); const key = keys[i]; const oldPath = oldPaths[key]; const path = new Path(oldPath);