diff --git a/packages/cactus-plugin-ledger-connector-go-ethereum-socketio/src/main/typescript/common/core/bin/www.ts b/packages/cactus-plugin-ledger-connector-go-ethereum-socketio/src/main/typescript/common/core/bin/www.ts index 52444e2a5b..08df7236dc 100644 --- a/packages/cactus-plugin-ledger-connector-go-ethereum-socketio/src/main/typescript/common/core/bin/www.ts +++ b/packages/cactus-plugin-ledger-connector-go-ethereum-socketio/src/main/typescript/common/core/bin/www.ts @@ -69,7 +69,7 @@ server.on("listening", onListening); * Normalize a port into a number, string, or false. */ -function normalizePort(val) { +function normalizePort(val: string) { const port = parseInt(val, 10); if (isNaN(port)) { @@ -89,7 +89,7 @@ function normalizePort(val) { * Event listener for HTTPS server "error" event. */ -function onError(error) { +function onError(error: any) { if (error.syscall !== "listen") { throw error; } @@ -118,6 +118,12 @@ function onError(error) { function onListening() { const addr = server.address(); + + if (!addr) { + logger.error("Could not get running server address - exit."); + process.exit(1); + } + const bind = typeof addr === "string" ? "pipe " + addr : "port " + addr.port; debug("Listening on " + bind); } @@ -144,14 +150,14 @@ io.on("connection", function (client) { // Check for the existence of the specified function and call it if it exists. if (Splug.isExistFunction(func)) { // Can be called with Server plugin function name. - Splug[func](args) - .then((respObj) => { + (Splug as any)[func](args) + .then((respObj: unknown) => { logger.info("*** RESPONSE ***"); logger.info("Client ID :" + client.id); logger.info("Response :" + JSON.stringify(respObj)); client.emit("response", respObj); }) - .catch((errObj) => { + .catch((errObj: unknown) => { logger.error("*** ERROR ***"); logger.error("Client ID :" + client.id); logger.error("Detail :" + JSON.stringify(errObj)); @@ -172,11 +178,12 @@ io.on("connection", function (client) { // TODO: "request2" -> "request" client.on("request2", function (data) { const methodType = data.method.type; - // const args = data.args; - const args = {}; - args["contract"] = data.contract; - args["method"] = data.method; - args["args"] = data.args; + let args: Record = { + contract: data.contract, + method: data.method, + args: data.args, + }; + if (data.reqID !== undefined) { logger.info(`##add reqID: ${data.reqID}`); args["reqID"] = data.reqID; @@ -223,14 +230,14 @@ io.on("connection", function (client) { // Check for the existence of the specified function and call it if it exists. if (Splug.isExistFunction(func)) { // Can be called with Server plugin function name. - Splug[func](args) - .then((respObj) => { + (Splug as any)[func](args) + .then((respObj: unknown) => { logger.info("*** RESPONSE ***"); logger.info("Client ID :" + client.id); logger.info("Response :" + JSON.stringify(respObj)); client.emit("response", respObj); }) - .catch((errObj) => { + .catch((errObj: unknown) => { logger.error("*** ERROR ***"); logger.error("Client ID :" + client.id); logger.error("Detail :" + JSON.stringify(errObj)); @@ -262,19 +269,16 @@ io.on("connection", function (client) { * startMonitor: starting block generation event monitoring **/ client.on("startMonitor", function () { - // Callback to receive monitoring results - const cb = function (callbackData) { + Smonitor.startMonitor(client.id, (event) => { let emitType = ""; - if (callbackData.status == 200) { + if (event.status == 200) { emitType = "eventReceived"; logger.info("event data callbacked."); } else { emitType = "monitor_error"; } - client.emit(emitType, callbackData); - }; - - Smonitor.startMonitor(client.id, cb); + client.emit(emitType, event); + }); }); /** diff --git a/packages/cactus-plugin-ledger-connector-go-ethereum-socketio/src/main/typescript/connector/PluginUtil.ts b/packages/cactus-plugin-ledger-connector-go-ethereum-socketio/src/main/typescript/connector/PluginUtil.ts index e2a554be11..f9b322ce66 100644 --- a/packages/cactus-plugin-ledger-connector-go-ethereum-socketio/src/main/typescript/connector/PluginUtil.ts +++ b/packages/cactus-plugin-ledger-connector-go-ethereum-socketio/src/main/typescript/connector/PluginUtil.ts @@ -23,7 +23,7 @@ * 3.78*10^14 * 3.78e14 */ -export function convNum(value, defaultValue) { +export function convNum(value: number | string, defaultValue: number | string) { let retValue = 0; let defValue = 0; diff --git a/packages/cactus-plugin-ledger-connector-go-ethereum-socketio/src/main/typescript/connector/ServerMonitorPlugin.ts b/packages/cactus-plugin-ledger-connector-go-ethereum-socketio/src/main/typescript/connector/ServerMonitorPlugin.ts index 51d383ae95..5684c539f9 100644 --- a/packages/cactus-plugin-ledger-connector-go-ethereum-socketio/src/main/typescript/connector/ServerMonitorPlugin.ts +++ b/packages/cactus-plugin-ledger-connector-go-ethereum-socketio/src/main/typescript/connector/ServerMonitorPlugin.ts @@ -25,21 +25,18 @@ import { ValidatorAuthentication } from "./ValidatorAuthentication"; const Web3 = require("web3"); import safeStringify from "fast-safe-stringify"; +export type MonitorCallback = (callback: { + status: number; + blockData?: string; + errorDetail?: string; +}) => void; + /* * ServerMonitorPlugin * Class definitions of server monitoring */ export class ServerMonitorPlugin { - _filterTable: object; - - /* - * constructors - */ - constructor() { - // Define dependent specific settings - // Initialize monitored filter - this._filterTable = {}; - } + _filterTable = new Map(); /* * startMonitor @@ -47,11 +44,11 @@ export class ServerMonitorPlugin { * @param {string} clientId: Client ID from which monitoring start request was made * @param {function} cb: A callback function that receives monitoring results at any time. */ - startMonitor(clientId, cb) { + startMonitor(clientId: string, cb: MonitorCallback) { logger.info("*** START MONITOR ***"); logger.info("Client ID :" + clientId); // Implement handling to receive events from an end-chain and return them in a callback function - let filter = this._filterTable[clientId]; + let filter = this._filterTable.get(clientId); if (!filter) { logger.info("create new web3 filter and start watching."); try { @@ -63,8 +60,8 @@ export class ServerMonitorPlugin { filter = web3.eth.filter("latest"); // filter should be managed by client ID // (You should never watch multiple urls from the same client) - this._filterTable[clientId] = filter; - filter.watch(function (error, blockHash) { + this._filterTable.set(clientId, filter); + filter.watch(function (error: any, blockHash: string) { if (!error) { console.log("##[HL-BC] Notify new block data(D2)"); const blockData = web3.eth.getBlock(blockHash, true); @@ -92,7 +89,7 @@ export class ServerMonitorPlugin { } else { const errObj = { status: 504, - errorDetail: error, + errorDetail: safeStringify(error), }; cb(errObj); } @@ -115,14 +112,14 @@ export class ServerMonitorPlugin { * monitoring stop * @param {string} clientId: Client ID from which monitoring stop request was made */ - stopMonitor(clientId) { + stopMonitor(clientId: string) { // Implement a process to end EC monitoring - const filter = this._filterTable[clientId]; + let filter = this._filterTable.get(clientId); if (filter) { // Stop the filter & Remove it from table logger.info("stop watching and remove filter."); filter.stopWatching(); - delete this._filterTable[clientId]; + this._filterTable.delete(clientId); } } } diff --git a/packages/cactus-plugin-ledger-connector-go-ethereum-socketio/src/main/typescript/connector/ServerPlugin.ts b/packages/cactus-plugin-ledger-connector-go-ethereum-socketio/src/main/typescript/connector/ServerPlugin.ts index bfe8b4c34c..13daf07fc0 100644 --- a/packages/cactus-plugin-ledger-connector-go-ethereum-socketio/src/main/typescript/connector/ServerPlugin.ts +++ b/packages/cactus-plugin-ledger-connector-go-ethereum-socketio/src/main/typescript/connector/ServerPlugin.ts @@ -47,8 +47,8 @@ export class ServerPlugin { * Scope of this function is in this class * Functions that should not be called directly should be implemented outside this class like utilities. */ - isExistFunction(funcName) { - if (this[funcName] != undefined) { + isExistFunction(funcName: string) { + if ((this as any)[funcName]) { return true; } else { return false; @@ -67,11 +67,11 @@ export class ServerPlugin { * } * @return {Object} JSON object */ - getNumericBalance(args) { + getNumericBalance(args: any) { // * The Web3 API can be used synchronously, but each function is always an asynchronous specification because of the use of other APIs such as REST, return new Promise((resolve, reject) => { logger.info("getNumericBalance start"); - let retObj = {}; + let retObj: Record; const referedAddress = args.args.args[0]; const reqID = args["reqID"]; @@ -139,11 +139,11 @@ export class ServerPlugin { * } * @return {Object} JSON object */ - transferNumericAsset(args) { + transferNumericAsset(args: any) { return new Promise((resolve, reject) => { logger.info("transferNumericAsset start"); - let retObj = {}; + let retObj: Record; let sendArgs = {}; const sendFunction = "sendTransaction"; // const funcParam = args; @@ -231,11 +231,11 @@ export class ServerPlugin { * } * @return {Object} JSON object */ - getNonce(args) { + getNonce(args: any) { // * The Web3 API can be used synchronously, but each function is always an asynchronous specification because of the use of other APIs such as REST, return new Promise((resolve, reject) => { logger.info("getNonce start"); - let retObj = {}; + let retObj: Record; const targetAddress = args.args.args.args[0]; const reqID = args["reqID"]; @@ -316,11 +316,11 @@ export class ServerPlugin { * } * @return {Object} JSON object */ - toHex(args) { + toHex(args: any) { // * The Web3 API can be used synchronously, but each function is always an asynchronous specification because of the use of other APIs such as REST, return new Promise((resolve, reject) => { logger.info("toHex start"); - let retObj = {}; + let retObj: Record; const targetValue = args.args.args.args[0]; const reqID = args["reqID"]; @@ -393,11 +393,11 @@ export class ServerPlugin { * } * @return {Object} JSON object */ - sendRawTransaction(args) { + sendRawTransaction(args: any) { return new Promise((resolve, reject) => { logger.info("sendRawTransaction(start"); - let retObj = {}; + let retObj: Record; const sendArgs = {}; const sendFunction = "sendTransaction"; const funcParam = args.args.args[0]; @@ -451,11 +451,11 @@ export class ServerPlugin { * } * @return {Object} JSON object */ - web3Eth(args) { + web3Eth(args: any) { return new Promise((resolve, reject) => { logger.info("web3Eth start"); - let retObj = {}; + let retObj: Record; const sendFunction = args.method.command; const sendArgs = args.args.args[0]; const reqID = args["reqID"]; @@ -518,11 +518,11 @@ export class ServerPlugin { * } * @return {Object} JSON object */ - contract(args) { + contract(args: any) { return new Promise((resolve, reject) => { logger.info("contract start"); - let retObj = {}; + let retObj: Record; const sendCommand = args.method.command; const sendFunction = args.method.function; const sendArgs = args.args.args; diff --git a/packages/cactus-plugin-ledger-connector-go-ethereum-socketio/tsconfig.json b/packages/cactus-plugin-ledger-connector-go-ethereum-socketio/tsconfig.json index 7713871089..90eae72ba8 100644 --- a/packages/cactus-plugin-ledger-connector-go-ethereum-socketio/tsconfig.json +++ b/packages/cactus-plugin-ledger-connector-go-ethereum-socketio/tsconfig.json @@ -8,7 +8,6 @@ "emitDecoratorMetadata": true, "experimentalDecorators": true, "tsBuildInfoFile": "../../.build-cache/cactus-plugin-ledger-connector-go-ethereum-socketio.tsbuildinfo", - "strict": false // TODO - True, fix warnings }, "include": [ "./src/main/typescript/common/core/*.ts",