Merge pull request #3 from status-im/mail-servers
Add mailserver support (only with statusd).
This commit is contained in:
commit
198ba4d21d
|
@ -2,6 +2,6 @@
|
||||||
|
|
||||||
# Config:
|
# Config:
|
||||||
|
|
||||||
`geth --testnet --syncmode=light --ws --wsport=8546 --wsaddr=localhost --wsorigins=http://localhost:8000,http://localhost:8080 --maxpeers=25 --shh --shh.pow=0.002 --wsapi=eth,web3,net,shh,debug`
|
`geth --testnet --syncmode=light --ws --wsport=8546 --wsaddr=localhost --wsorigins=statusjs --maxpeers=25 --shh --shh.pow=0.002 --wsapi=eth,web3,net,shh,debug,admin`
|
||||||
|
|
||||||
web3.ssh options: `powTime (1), ttl (10) and powTarget (0.002)
|
web3.ssh options: `powTime (1), ttl (10) and powTarget (0.002)
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
{
|
{
|
||||||
"name": "status-js-api",
|
"name": "status-js-api",
|
||||||
"version": "1.0.5",
|
"version": "1.0.6",
|
||||||
"lockfileVersion": 1,
|
"lockfileVersion": 1,
|
||||||
"requires": true,
|
"requires": true,
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
{
|
{
|
||||||
"name": "status-js-api",
|
"name": "status-js-api",
|
||||||
"version": "1.0.5",
|
"version": "1.0.6",
|
||||||
"description": "",
|
"description": "",
|
||||||
"main": "src/index.js",
|
"main": "src/index.js",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
|
|
|
@ -0,0 +1,11 @@
|
||||||
|
{
|
||||||
|
"mail-01.ac-cn-hongkong-c.eth.beta": "enode://da61e9eff86a56633b635f887d8b91e0ff5236bbc05b8169834292e92afb92929dcf6efdbf373a37903da8fe0384d5a0a8247e83f1ce211aa429200b6d28c548@47.91.156.93:30504",
|
||||||
|
"mail-01.do-ams3.eth.beta": "enode://c42f368a23fa98ee546fd247220759062323249ef657d26d357a777443aec04db1b29a3a22ef3e7c548e18493ddaf51a31b0aed6079bd6ebe5ae838fcfaf3a49@206.189.243.162:30504",
|
||||||
|
"mail-01.gc-us-central1-a.eth.beta": "enode://7de99e4cb1b3523bd26ca212369540646607c721ad4f3e5c821ed9148150ce6ce2e72631723002210fac1fd52dfa8bbdf3555e05379af79515e1179da37cc3db@35.188.19.210:30504",
|
||||||
|
"mail-02.ac-cn-hongkong-c.eth.beta": "enode://744098ab6d3308af5cd03920aea60c46d16b2cd3d33bf367cbaf1d01c2fcd066ff8878576d0967897cd7dbb0e63f873cc0b4f7e4b0f1d7222e6b3451a78d9bda@47.89.20.15:30504",
|
||||||
|
"mail-02.do-ams3.eth.beta": "enode://7aa648d6e855950b2e3d3bf220c496e0cae4adfddef3e1e6062e6b177aec93bc6cdcf1282cb40d1656932ebfdd565729da440368d7c4da7dbd4d004b1ac02bf8@206.189.243.169:30504",
|
||||||
|
"mail-02.gc-us-central1-a.eth.beta": "enode://015e22f6cd2b44c8a51bd7a23555e271e0759c7d7f52432719665a74966f2da456d28e154e836bee6092b4d686fe67e331655586c57b718be3997c1629d24167@35.226.21.19:30504",
|
||||||
|
"mail-03.ac-cn-hongkong-c.eth.beta": "enode://74957e361ab290e6af45a124536bc9adee39fbd2f995a77ace6ed7d05d9a1c7c98b78b2df5f8071c439b9c0afe4a69893ede4ad633473f96bc195ddf33f6ce00@47.52.255.195:30504",
|
||||||
|
"mail-03.do-ams3.eth.beta": "enode://8a64b3c349a2e0ef4a32ea49609ed6eb3364be1110253c20adc17a3cebbc39a219e5d3e13b151c0eee5d8e0f9a8ba2cd026014e67b41a4ab7d1d5dd67ca27427@206.189.243.168:30504",
|
||||||
|
"mail-03.gc-us-central1-a.eth.beta": "enode://531e252ec966b7e83f5538c19bf1cde7381cc7949026a6e499b6e998e695751aadf26d4c98d5a4eabfb7cefd31c3c88d600a775f14ed5781520a88ecd25da3c6@35.225.227.79:30504"
|
||||||
|
}
|
18
src/index.js
18
src/index.js
|
@ -1,5 +1,7 @@
|
||||||
const Web3 = require('web3');
|
const Web3 = require('web3');
|
||||||
const utils = require('./utils.js');
|
const utils = require('./utils.js');
|
||||||
|
const mailservers = require('./mailservers.js');
|
||||||
|
|
||||||
const { utils: { asciiToHex, hexToAscii, sha3 } } = Web3;
|
const { utils: { asciiToHex, hexToAscii, sha3 } } = Web3;
|
||||||
|
|
||||||
const POW_TIME = 1;
|
const POW_TIME = 1;
|
||||||
|
@ -38,12 +40,21 @@ class StatusJS {
|
||||||
this.channels = {};
|
this.channels = {};
|
||||||
this.contacts = {};
|
this.contacts = {};
|
||||||
this.userMessagesSubscription = null;
|
this.userMessagesSubscription = null;
|
||||||
|
this.mailservers = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
async connect(url, privateKey) {
|
async connect(url, privateKey) {
|
||||||
let web3 = new Web3();
|
let web3 = new Web3();
|
||||||
|
if(url.startsWith("ws://")){
|
||||||
web3.setProvider(new Web3.providers.WebsocketProvider(url, {headers: {Origin: "statusjs"}}));
|
web3.setProvider(new Web3.providers.WebsocketProvider(url, {headers: {Origin: "statusjs"}}));
|
||||||
|
} else {
|
||||||
|
const net = require('net');
|
||||||
|
web3.setProvider(new Web3.providers.IpcProvider(url, net));
|
||||||
|
}
|
||||||
|
|
||||||
this.shh = web3.shh;
|
this.shh = web3.shh;
|
||||||
|
this.mailservers = new mailservers(web3);
|
||||||
|
|
||||||
await web3.shh.setMinPoW(POW_TARGET);
|
await web3.shh.setMinPoW(POW_TARGET);
|
||||||
_sig.set(
|
_sig.set(
|
||||||
this,
|
this,
|
||||||
|
@ -116,9 +127,9 @@ class StatusJS {
|
||||||
}
|
}
|
||||||
|
|
||||||
this.channels[channelName].subscription = this.shh.subscribe("messages", {
|
this.channels[channelName].subscription = this.shh.subscribe("messages", {
|
||||||
minPow: POW_TARGET,
|
|
||||||
symKeyID: this.channels[channelName].channelKey,
|
symKeyID: this.channels[channelName].channelKey,
|
||||||
topics: [this.channels[channelName].channelCode]
|
topics: [this.channels[channelName].channelCode],
|
||||||
|
allowP2P: true
|
||||||
}).on('data', (data) => {
|
}).on('data', (data) => {
|
||||||
let username = utils.generateUsernameFromSeed(data.sig);
|
let username = utils.generateUsernameFromSeed(data.sig);
|
||||||
|
|
||||||
|
@ -138,7 +149,8 @@ class StatusJS {
|
||||||
this.userMessagesSubscription = this.shh.subscribe("messages", {
|
this.userMessagesSubscription = this.shh.subscribe("messages", {
|
||||||
minPow: POW_TARGET,
|
minPow: POW_TARGET,
|
||||||
privateKeyID: _sig.get(this),
|
privateKeyID: _sig.get(this),
|
||||||
topics: [CONTACT_DISCOVERY_TOPIC]
|
topics: [CONTACT_DISCOVERY_TOPIC],
|
||||||
|
allowP2P: true
|
||||||
}).on('data', (data) => {
|
}).on('data', (data) => {
|
||||||
if(!this.contacts[data.sig]){
|
if(!this.contacts[data.sig]){
|
||||||
this.addContact(data.sig);
|
this.addContact(data.sig);
|
||||||
|
|
|
@ -0,0 +1,89 @@
|
||||||
|
const mailserverList = require('./data/mailservers.json');
|
||||||
|
|
||||||
|
class MailServers {
|
||||||
|
constructor(web3){
|
||||||
|
this.web3 = web3;
|
||||||
|
this.mailserver = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
async useMailserver(mailserver, cb){
|
||||||
|
var enode = mailserverList[mailserver];
|
||||||
|
|
||||||
|
if(!enode){
|
||||||
|
if(!cb) return;
|
||||||
|
cb("unknown mailserver: " + mailserver);
|
||||||
|
}
|
||||||
|
|
||||||
|
this.symKeyID = await this.web3.shh.generateSymKeyFromPassword("status-offline-inbox");
|
||||||
|
|
||||||
|
this.web3.currentProvider.send({
|
||||||
|
method: "admin_addPeer",
|
||||||
|
params: [enode],
|
||||||
|
jsonrpc: "2.0",
|
||||||
|
id: new Date().getTime()
|
||||||
|
}, (err, res) => {
|
||||||
|
if(err){
|
||||||
|
if(cb) return cb(err, false);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(!res.result){
|
||||||
|
if(cb) return cb(err, false);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
setTimeout(() => {
|
||||||
|
this.web3.shh.markTrustedPeer(enode)
|
||||||
|
.then(res => {
|
||||||
|
this.mailserver = enode;
|
||||||
|
if (!cb) return true;
|
||||||
|
cb(null, true);
|
||||||
|
}).catch((e) => {
|
||||||
|
if (!cb) return;
|
||||||
|
cb(e, false);
|
||||||
|
});
|
||||||
|
}, 1000);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
async requestMessages(topic, options, cb){
|
||||||
|
if(!this.mailserver){
|
||||||
|
if(!cb) return;
|
||||||
|
return cb("Mailserver is not set", false);
|
||||||
|
}
|
||||||
|
|
||||||
|
const topics = [this.web3.utils.sha3(topic).slice(0, 10)];
|
||||||
|
const mailserverPeer = this.mailserver;
|
||||||
|
|
||||||
|
const timeout = options.timeout || 30; // seconds
|
||||||
|
const symKeyID = this.symKeyID;
|
||||||
|
const from = options.from || 0; // unix timestamp
|
||||||
|
const to = options.to || 0;
|
||||||
|
const limit = options.limit || 0;
|
||||||
|
|
||||||
|
this.web3.currentProvider.send({
|
||||||
|
method: "shhext_requestMessages",
|
||||||
|
params: [{
|
||||||
|
mailserverPeer,
|
||||||
|
symKeyID,
|
||||||
|
timeout,
|
||||||
|
topics,
|
||||||
|
from,
|
||||||
|
to,
|
||||||
|
limit
|
||||||
|
}],
|
||||||
|
jsonrpc: "2.0",
|
||||||
|
id: new Date().getTime()
|
||||||
|
}, (err, res) => {
|
||||||
|
if(err){
|
||||||
|
if(cb) return cb(err);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(cb) return cb(null, true);
|
||||||
|
return true;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = MailServers;
|
|
@ -0,0 +1,22 @@
|
||||||
|
var StatusJS = require('./src/index.js');
|
||||||
|
|
||||||
|
(async () => {
|
||||||
|
var status = new StatusJS();
|
||||||
|
await status.connect("/home/richard/.statusd/geth.ipc");
|
||||||
|
|
||||||
|
const channel = "mytest";
|
||||||
|
|
||||||
|
await status.joinChat(channel);
|
||||||
|
|
||||||
|
status.onMessage(channel, (err, data) => {
|
||||||
|
console.log(data.payload);
|
||||||
|
});
|
||||||
|
|
||||||
|
status.mailservers.useMailserver("mail-02.gc-us-central1-a.eth.beta", (err, res) => {
|
||||||
|
status.mailservers.requestMessages(channel, {}, (err, res) => {
|
||||||
|
if(err) console.log(err);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
setInterval(() => { }, 3000);
|
||||||
|
})()
|
Loading…
Reference in New Issue