diff --git a/src/index.js b/src/index.js index 97a4363..3522ec6 100644 --- a/src/index.js +++ b/src/index.js @@ -1,11 +1,10 @@ const Web3 = require('web3'); import utils from './utils.js'; -const mailservers = require('./mailservers.js'); +import mailservers from './mailservers.js'; const constants = require('./constants'); const { utils: { asciiToHex, hexToAscii } } = Web3; - function createStatusPayload(content, messageType, clockValue, isJson) { const tag = constants.messageTags.message; const oneMonthInMs = 60 * 60 * 24 * 31 * 1000; diff --git a/src/mailservers.js b/src/mailservers.ts similarity index 53% rename from src/mailservers.js rename to src/mailservers.ts index b908ca4..a2cff79 100644 --- a/src/mailservers.js +++ b/src/mailservers.ts @@ -1,55 +1,70 @@ -const mailserverList = require('./data/mailservers.json'); +import mailserverList from "./data/mailservers.json"; class MailServers { - constructor(web3){ + private web3: any; + private mailserver: string = ""; + private symKeyID: string = ""; + + constructor(web3: any) { this.web3 = web3; - this.mailserver = null; } - async useMailserver(mailserver, cb){ - var enode = mailserverList[mailserver]; + public async useMailserver(mailserver: string, cb?: Function) { + const enode: string = mailserverList[mailserver]; - if(!enode){ - if(!cb) return; + if (!enode) { + if (!cb) { + return; + } cb("unknown mailserver: " + mailserver); } this.symKeyID = await this.web3.shh.generateSymKeyFromPassword("status-offline-inbox"); this.web3.currentProvider.send({ + id: new Date().getTime(), + jsonrpc: "2.0", method: "admin_addPeer", params: [enode], - jsonrpc: "2.0", - id: new Date().getTime() }, - (err, res) => { - if(err){ - if(cb) return cb(err, false); + (err: any, res: any) => { + if (err) { + if (cb) { + return cb(err, false); + } return; } - if(!res.result){ - if(cb) return cb(err, false); + if (!res.result) { + if (cb) { + return cb(err, false); + } return; } setTimeout(() => { this.web3.shh.markTrustedPeer(enode) - .then(_res => { + .then(() => { this.mailserver = enode; - if (!cb) return true; + if (!cb) { + return true; + } cb(null, true); - }).catch((e) => { - if (!cb) return; + }).catch((e?: any) => { + if (!cb) { + return; + } cb(e, false); }); }, 1000); }); } - async requestMessages(topic, options, cb){ - if(!this.mailserver){ - if(!cb) return; + public async requestMessages(topic: string, options: any, cb?: any) { + if (this.mailserver === "") { + if (!cb) { + return; + } return cb("Mailserver is not set", false); } @@ -63,31 +78,35 @@ class MailServers { const limit = options.limit || 0; this.web3.currentProvider.send({ + id: new Date().getTime(), + jsonrpc: "2.0", method: "shhext_requestMessages", params: [ { + from, + limit, mailserverPeer, symKeyID, timeout, - topics, - from, to, - limit - } + topics, + }, ], - jsonrpc: "2.0", - id: new Date().getTime() }, - (err, _res) => { - if(err){ - if(cb) return cb(err); + (err?: any, res?: any) => { + if (err) { + if (cb) { + return cb(err); + } return false; } - if(cb) return cb(null, true); + if (cb) { + return cb(null, true); + } return true; }); } } -module.exports = MailServers; +export default MailServers; diff --git a/src/types.d.ts b/src/types.d.ts new file mode 100644 index 0000000..e44bcb0 --- /dev/null +++ b/src/types.d.ts @@ -0,0 +1 @@ +declare module 'chance' diff --git a/tsconfig.json b/tsconfig.json index 58ef8ad..04f7b97 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -8,6 +8,7 @@ "strict": true, "isolatedModules": true, "resolveJsonModule": true, + "suppressImplicitAnyIndexErrors": true, "esModuleInterop": true, "typeRoots": ["node_modules/@types"], "rootDir": ".",