feat: add method to obtain mailserver messages from libp2p clients

This commit is contained in:
Richard Ramos 2019-02-14 10:44:18 -04:00
parent e0ad44cae7
commit 71693afa3a
3 changed files with 55 additions and 12 deletions

View File

@ -2,7 +2,7 @@ const StatusJS = require('../dist/index.js');
(async () => {
const status = new StatusJS();
await status.connect("ws://localhost:8546", "0x0011223344556677889900112233445566778899001122334455667788990011");
await status.connect("ws://localhost:8547", "0x0011223344556677889900112233445566778899001122334455667788990011");
console.log(await status.getPublicKey());

View File

@ -0,0 +1,30 @@
const StatusJS = require('../dist/index.js');
(async () => {
const status = new StatusJS();
await status.connect("ws://localhost:8546", "0x0011223344556677889900112233445566778899001122334455667788990011");
console.log("Public Key: " + await status.getPublicKey());
const channel = "mytest";
await status.joinChat(channel);
status.onMessage(channel, (err, data) => {
if(!err)
console.log("PubMessage: " + data.payload);
});
// mail-02.gc-us-central1-a.eth.beta
const enode = "enode://015e22f6cd2b44c8a51bd7a23555e271e0759c7d7f52432719665a74966f2da456d28e154e836bee6092b4d686fe67e331655586c57b718be3997c1629d24167@35.226.21.19:30504";
const bridgePeerId = "QmUXaxphguz1jdizRWPz7PJeF1pbog5MZtGcRnPASJJRuN";
status.mailservers.bridgeMailserver(enode, bridgePeerId, (err, res) => {
let from = parseInt((new Date()).getTime() / 1000 - 86400, 10);
let to = parseInt((new Date()).getTime() / 1000, 10);
status.mailservers.requestChannelMessages(channel, {from, to}, (err, res) => {
if(err) console.log(err);
});
});
setInterval(() => { }, 3000);
})();

View File

@ -5,12 +5,14 @@ const Topics = constants.topics;
class MailServers {
private web3: any;
private mailserver: string = "";
private bridgePeerId: string = "";
private symKeyID: string = "";
constructor(web3: any) {
this.web3 = web3;
}
public async useMailserver(enode: string, cb?: any) {
this.symKeyID = await this.web3.shh.generateSymKeyFromPassword("status-offline-inbox");
@ -53,6 +55,12 @@ class MailServers {
});
}
public async bridgeMailserver(enode: string, bridgePeerId: string, cb?: any){
await this.web3.shh.markTrustedPeer("libp2p:" + bridgePeerId);
this.bridgePeerId = bridgePeerId;
this.useMailserver(enode, cb);
}
public async requestUserMessages(options: any, cb?: any) {
await this.requestChannelMessages(constants.topics.CONTACT_DISCOVERY_TOPIC, options, cb);
}
@ -74,22 +82,27 @@ class MailServers {
const from = options.from || 0; // unix timestamp
const to = options.to || 0;
const limit = options.limit || 0;
const bridgePeerId = this.bridgePeerId ? this.bridgePeerId: null;
let paramObj = {
from,
limit,
mailserverPeer,
symKeyID,
timeout,
to,
topics
};
if(bridgePeerId) {
paramObj = Object.assign(paramObj, {bridgePeerId});
}
this.web3.currentProvider.send({
id: new Date().getTime(),
jsonrpc: "2.0",
method: "shhext_requestMessages",
params: [
{
from,
limit,
mailserverPeer,
symKeyID,
timeout,
to,
topics,
},
],
params: [paramObj],
},
(err?: any, res?: any) => {
if (err) {