migrate mailservers to ts

This commit is contained in:
Iuri Matias 2018-12-01 15:45:42 -05:00
parent e7678d2c60
commit 01d175b97e
4 changed files with 54 additions and 34 deletions

View File

@ -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;

View File

@ -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;

1
src/types.d.ts vendored Normal file
View File

@ -0,0 +1 @@
declare module 'chance'

View File

@ -8,6 +8,7 @@
"strict": true,
"isolatedModules": true,
"resolveJsonModule": true,
"suppressImplicitAnyIndexErrors": true,
"esModuleInterop": true,
"typeRoots": ["node_modules/@types"],
"rootDir": ".",