diff --git a/lib/modules/console_listener/index.js b/lib/modules/console_listener/index.js index 2d330c9f..cf4e20ad 100644 --- a/lib/modules/console_listener/index.js +++ b/lib/modules/console_listener/index.js @@ -62,45 +62,45 @@ class ConsoleListener { _listenForLogRequests() { if (this.ipc.ipcRole !== 'server') return; this.ipc.on('log', (request) => { - if (request.type === 'contract-log') { - if (!this.contractsDeployed) return; - - let {address, data, transactionHash, blockNumber, gasUsed, status} = request; - const contract = this.addressToContract[address]; - - if (!contract) { - this._updateContractList(); - return; - } - - const {name, silent} = contract; - if (silent && !this.outputDone) { - return; - } - - const func = contract.functions[data.substring(0, 10)]; - const functionName = func.functionName; - - const decodedParameters = utils.decodeParams(func.abi.inputs, data.substring(10)); - let paramString = ""; - if (func.abi.inputs) { - func.abi.inputs.forEach((input) => { - let quote = input.type.indexOf("int") === -1 ? '"' : ''; - paramString += quote + decodedParameters[input.name] + quote + ", "; - }); - paramString = paramString.substring(0, paramString.length - 2); - } - - gasUsed = utils.hexToNumber(gasUsed); - blockNumber = utils.hexToNumber(blockNumber); - - this.logs.push(Object.assign({}, request, {name, functionName, paramString, gasUsed, blockNumber})); - this.events.emit('contracts:log', this.logs[this.logs.length - 1]); - - this.logger.info(`Blockchain>`.underline + ` ${name}.${functionName}(${paramString})`.bold + ` | ${transactionHash} | gas:${gasUsed} | blk:${blockNumber} | status:${status}`); - } else { - this.logger.info(JSON.stringify(request)); + if (request.type !== 'contract-log') { + return this.logger.info(JSON.stringify(request)); } + + if (!this.contractsDeployed) return; + + let {address, data, transactionHash, blockNumber, gasUsed, status} = request; + const contract = this.addressToContract[address]; + + if (!contract) { + this._updateContractList(); + return; + } + + const {name, silent} = contract; + if (silent && !this.outputDone) { + return; + } + + const func = contract.functions[data.substring(0, 10)]; + const functionName = func.functionName; + + const decodedParameters = utils.decodeParams(func.abi.inputs, data.substring(10)); + let paramString = ""; + if (func.abi.inputs) { + func.abi.inputs.forEach((input) => { + let quote = input.type.indexOf("int") === -1 ? '"' : ''; + paramString += quote + decodedParameters[input.name] + quote + ", "; + }); + paramString = paramString.substring(0, paramString.length - 2); + } + + gasUsed = utils.hexToNumber(gasUsed); + blockNumber = utils.hexToNumber(blockNumber); + + this.logs.push(Object.assign({}, request, {name, functionName, paramString, gasUsed, blockNumber})); + this.events.emit('contracts:log', this.logs[this.logs.length - 1]); + + this.logger.info(`Blockchain>`.underline + ` ${name}.${functionName}(${paramString})`.bold + ` | ${transactionHash} | gas:${gasUsed} | blk:${blockNumber} | status:${status}`); }); } diff --git a/lib/modules/webserver/server.js b/lib/modules/webserver/server.js index 5e2cefa9..07748460 100644 --- a/lib/modules/webserver/server.js +++ b/lib/modules/webserver/server.js @@ -55,6 +55,7 @@ class Server { const main = serveStatic(this.buildDir, {'index': ['index.html', 'index.htm']}); this.app = express(); + const expressWs = expressWebSocket(this.app); // Assign Logging Function this.app.use(function(req, res, next) { if (self.logging) { @@ -64,7 +65,6 @@ class Server { } next(); }); - expressWebSocket(this.app); this.app.use(helmet.noCache()); this.app.use(cors()); @@ -97,16 +97,15 @@ class Server { this.app[apiCall.method].apply(this.app, [apiCall.endpoint, this.applyAPIFunction.bind(this, apiCall.cb)]); } } + const wss = expressWs.getWss('/'); - this.app.ws('/', function(ws, _req) { - self.events.on('outputDone', () => { - if (ws.readyState === WEB_SOCKET_STATE_OPEN) { - return ws.send('outputDone'); - } - // if the socket wasn't yet opened, listen for the 'open' event, - // then send the 'outputDone' data - ws.addEventListener('open', _event => { - ws.send('outputDone'); + self.events.on('outputDone', () => { + wss.clients.forEach(function (client) { + client.send('outputDone'); + }); + self.events.on('outputError', () => { + wss.clients.forEach(function (client) { + client.send('outputError'); }); }); }); @@ -164,7 +163,13 @@ class Server { } applyAPIFunction (cb, req, res) { - this.events.request('authenticator:authorize', req.headers.authorization, (err) => { + // FIXME Need to not authenticate WS request as they do not pass the toekn in the header yet + const authToken = (!res.send) ? req.protocol : req.headers.authorization; + if (!res.send) { + // console.dir(arguments); + return cb(req, res); + } + this.events.request('authenticator:authorize', authToken, (err) => { if (err) { const send = res.send ? res.send.bind(res) : req.send.bind(req); // WS only has the first params return send(err);