diff --git a/browser/wsproxy.js b/browser/wsproxy.js index 8d25738c..22f7b0b9 100644 --- a/browser/wsproxy.js +++ b/browser/wsproxy.js @@ -61,7 +61,7 @@ WSProxy.prototype.handleSocket = function handleSocket(ws) { this.emit('error', err); }); - ws.listen('tcp connect', (port, host, nonce) => { + ws.bind('tcp connect', (port, host, nonce) => { this.handleConnect(ws, port, host, nonce); }); }; @@ -184,33 +184,33 @@ WSProxy.prototype.handleConnect = function handleConnect(ws, port, host, nonce) ws.destroy(); }); - ws.listen('tcp data', (data) => { + ws.bind('tcp data', (data) => { if (typeof data !== 'string') return; socket.write(Buffer.from(data, 'hex')); }); - ws.listen('tcp keep alive', (enable, delay) => { + ws.bind('tcp keep alive', (enable, delay) => { socket.setKeepAlive(enable, delay); }); - ws.listen('tcp no delay', (enable) => { + ws.bind('tcp no delay', (enable) => { socket.setNoDelay(enable); }); - ws.listen('tcp set timeout', (timeout) => { + ws.bind('tcp set timeout', (timeout) => { socket.setTimeout(timeout); }); - ws.listen('tcp pause', () => { + ws.bind('tcp pause', () => { socket.pause(); }); - ws.listen('tcp resume', () => { + ws.bind('tcp resume', () => { socket.resume(); }); - ws.listen('disconnect', () => { + ws.bind('disconnect', () => { socket.destroy(); }); }; @@ -229,7 +229,7 @@ function SocketState(server, socket) { this.target = server.target; this.snonce = nonce(); this.socket = null; - this.host = IP.normalize(socket.conn.remoteAddress); + this.host = socket.host; this.remoteHost = null; } diff --git a/lib/net/bip150.js b/lib/net/bip150.js index 0a1ee204..e3be95a9 100644 --- a/lib/net/bip150.js +++ b/lib/net/bip150.js @@ -12,18 +12,18 @@ const assert = require('assert'); const path = require('path'); const EventEmitter = require('events'); const fs = require('bfile'); -const co = require('../utils/co'); +const IP = require('binet'); +const Logger = require('blgr'); +const ccmp = require('bcrypto/lib/ccmp'); const digest = require('bcrypto/lib/digest'); const random = require('bcrypto/lib/random'); -const ccmp = require('bcrypto/lib/ccmp'); -const packets = require('./packets'); const secp256k1 = require('bcrypto/lib/secp256k1'); const StaticWriter = require('bbuf/lib/staticwriter'); -const base58 = require('bstr/lib/base58'); const encoding = require('bbuf/lib/encoding'); -const IP = require('binet'); +const base58 = require('bstr/lib/base58'); +const co = require('../utils/co'); +const packets = require('./packets'); const dns = require('./dns'); -const Logger = require('blgr'); /** * Represents a BIP150 input/output stream. diff --git a/lib/net/proxysocket.js b/lib/net/proxysocket.js index 61ccfb9d..769db5d7 100644 --- a/lib/net/proxysocket.js +++ b/lib/net/proxysocket.js @@ -38,7 +38,7 @@ function ProxySocket(uri) { Object.setPrototypeOf(ProxySocket.prototype, EventEmitter.prototype); ProxySocket.prototype._init = function _init() { - this.socket.listen('info', (info) => { + this.socket.bind('info', (info) => { if (this.closed) return; @@ -56,7 +56,7 @@ ProxySocket.prototype._init = function _init() { console.error(err); }); - this.socket.listen('tcp connect', (addr, port) => { + this.socket.bind('tcp connect', (addr, port) => { if (this.closed) return; this.remoteAddress = addr; @@ -64,7 +64,7 @@ ProxySocket.prototype._init = function _init() { this.emit('connect'); }); - this.socket.listen('tcp data', (data) => { + this.socket.bind('tcp data', (data) => { data = Buffer.from(data, 'hex'); if (this.paused) { this.recvBuffer.push(data); @@ -74,24 +74,24 @@ ProxySocket.prototype._init = function _init() { this.emit('data', data); }); - this.socket.listen('tcp close', (data) => { + this.socket.bind('tcp close', (data) => { if (this.closed) return; this.closed = true; this.emit('close'); }); - this.socket.listen('tcp error', (e) => { + this.socket.bind('tcp error', (e) => { const err = new Error(e.message); err.code = e.code; this.emit('error', err); }); - this.socket.listen('tcp timeout', () => { + this.socket.bind('tcp timeout', () => { this.emit('timeout'); }); - this.socket.listen('disconnect', () => { + this.socket.bind('disconnect', () => { if (this.closed) return; this.closed = true; diff --git a/lib/net/socks.js b/lib/net/socks.js index 80fa5c19..1a462c71 100644 --- a/lib/net/socks.js +++ b/lib/net/socks.js @@ -610,7 +610,7 @@ Proxy.prototype.connect = async function connect(port, host) { }); for (const op of this.ops) - op.call(this); + op(); this.ops.length = 0; diff --git a/lib/node/node.js b/lib/node/node.js index b0f510f5..484e86cb 100644 --- a/lib/node/node.js +++ b/lib/node/node.js @@ -8,13 +8,12 @@ 'use strict'; const assert = require('assert'); -const AsyncObject = require('../utils/asyncobject'); -const util = require('../utils/util'); -const Network = require('../protocol/network'); +const fs = require('bfile'); const Logger = require('blgr'); -const WorkerPool = require('../workers/workerpool'); -const secp256k1 = require('bcrypto/lib/secp256k1'); const Config = require('bcfg'); +const Network = require('../protocol/network'); +const AsyncObject = require('../utils/asyncobject'); +const WorkerPool = require('../workers/workerpool'); /** * Base class from which every other @@ -141,8 +140,11 @@ Node.prototype.init = function init(file) { * @returns {Promise} */ -Node.prototype.ensure = function ensure() { - return this.config.ensure(); +Node.prototype.ensure = async function ensure() { + if (fs.unsupported) + return undefined; + + return fs.mkdirp(this.config.prefix); }; /** @@ -186,7 +188,7 @@ Node.prototype.handlePreopen = async function handlePreopen() { */ Node.prototype.handleOpen = async function handleOpen() { - this.startTime = util.now(); + this.startTime = Date.now(); if (!this.workers.enabled) { this.logger.warning('Warning: worker pool is disabled.'); @@ -252,7 +254,7 @@ Node.prototype.uptime = function uptime() { if (this.startTime === -1) return 0; - return util.now() - this.startTime; + return Math.floor((Date.now() - this.startTime) / 1000); }; /** diff --git a/lib/wallet/rpc.js b/lib/wallet/rpc.js index 51bd83a6..a4cbe281 100644 --- a/lib/wallet/rpc.js +++ b/lib/wallet/rpc.js @@ -749,7 +749,13 @@ class RPC extends RPCBase { if (fs.unsupported) throw new RPCError(errs.INTERNAL_ERROR, 'FS not available.'); - const data = await fs.readFile(file, 'utf8'); + let data; + try { + data = await fs.readFile(file, 'utf8'); + } catch (e) { + throw new RPCError(errs.INTERNAL_ERROR, e.code || ''); + } + const lines = data.split(/\n+/); const keys = []; diff --git a/test/bech32-test.js b/test/bech32-test.js index cb23ae7e..d780956f 100644 --- a/test/bech32-test.js +++ b/test/bech32-test.js @@ -27,7 +27,6 @@ 'use strict'; const assert = require('./util/assert'); -const bech32 = require('bstr/lib/bech32'); const Address = require('../lib/primitives/address'); const validAddresses = [ @@ -94,29 +93,6 @@ const invalidAddresses = [ 'tb1qrp33g0q5c5txsp9arysrx4k6zdkfs4nce4xj0gdcccefvpysxf3pjxtptv' ]; -function fromAddress(hrp, addr) { - const dec = bech32.decode(addr); - - if (dec.hrp !== hrp) - throw new Error('Invalid bech32 prefix or data length.'); - - if (dec.version === 0 && dec.hash.length !== 20 && dec.hash.length !== 32) - throw new Error('Malformed witness program.'); - - return { - version: dec.version, - program: dec.hash - }; -} - -function toAddress(hrp, version, program) { - const ret = bech32.encode(hrp, version, program); - - fromAddress(hrp, ret); - - return ret; -} - function createProgram(version, program) { const data = Buffer.allocUnsafe(2 + program.length); data[0] = version ? version + 0x80 : 0;