From 0c1652f5a2ada4f1416546b019b17edad471644f Mon Sep 17 00:00:00 2001 From: Richard Ramos Date: Wed, 14 Nov 2018 11:41:21 -0400 Subject: [PATCH 1/7] Adding mailservers and marking it as trusted peer --- src/data/mailservers.json | 11 +++++ src/index.js | 6 +++ src/mailservers.js | 84 +++++++++++++++++++++++++++++++++++++++ test-mailserver.js | 12 ++++++ 4 files changed, 113 insertions(+) create mode 100644 src/data/mailservers.json create mode 100644 src/mailservers.js create mode 100644 test-mailserver.js diff --git a/src/data/mailservers.json b/src/data/mailservers.json new file mode 100644 index 0000000..2e5beb2 --- /dev/null +++ b/src/data/mailservers.json @@ -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" +} diff --git a/src/index.js b/src/index.js index c3c1409..0f35289 100644 --- a/src/index.js +++ b/src/index.js @@ -1,5 +1,7 @@ const Web3 = require('web3'); const utils = require('./utils.js'); +const mailservers = require('./mailservers.js'); + const { utils: { asciiToHex, hexToAscii, sha3 } } = Web3; const POW_TIME = 1; @@ -37,12 +39,16 @@ class StatusJS { this.channels = {}; this.contacts = {}; this.userMessagesSubscription = null; + this.mailservers = null; } async connect(url) { let web3 = new Web3(); web3.setProvider(new Web3.providers.WebsocketProvider(url, {headers: {Origin: "statusjs"}})); + this.shh = web3.shh; + this.mailservers = new mailservers(web3); + await web3.shh.setMinPoW(POW_TARGET); this.sig = await web3.shh.newKeyPair(); } diff --git a/src/mailservers.js b/src/mailservers.js new file mode 100644 index 0000000..25916ef --- /dev/null +++ b/src/mailservers.js @@ -0,0 +1,84 @@ +const mailserverList = require('./data/mailservers.json'); + +class MailServers { + constructor(web3){ + this.web3 = web3; + this.mailserver = null; + } + + useMailserver(mailserver, cb){ + if(!mailserverList[mailserver]){ + if(!cb) return; + cb("unknown mailserver: " + mailserver); + } + + this.web3.currentProvider.send({ + method: "admin_addPeer", + params: [mailserverList[mailserver]], + jsonrpc: "2.0", + id: new Date().getTime() + }, (err, res) => { + if(err){ + if(cb) return cb(err); + return; + } + + if(!res.result){ + if(cb) return cb(err); + return; + } + + const peerId = mailserverList[mailserver].substr(8, 128); + + this.web3.shh.markTrustedPeer(peerId) + .then(() => { + this.mailserver = mailserver; + console.log("A"); + if (!cb) return; + cb(null, true); + }).catch((e) => { + if (!cb) return; + cb(e, false); + }); + + } + ); + + + + } + + requestMessages(cb){ + if(!this.mailserver){ + if(!cb) return; + return cb("Mailserver is not set"); + } + + /* + :topics topics + :mailServerPeer address + :symKeyID sym-key-id + :timeout request-timeout + :from from + :to to}) + */ + + + /* + symKeyID - String (optional): ID of symmetric key for message encryption (Either symKeyID or pubKey must be present. Can not be both.). + pubKey - String (optional): The public key for message encryption (Either symKeyID or pubKey must be present. Can not be both.). + sig - String (optional): The ID of the signing key. + ttl - Number: Time-to-live in seconds. + topic - String: 4 Bytes (mandatory when key is symmetric): Message topic. + payload - String: The payload of the message to be encrypted. + padding - Number (optional): Padding (byte array of arbitrary length). + powTime - Number (optional)?: Maximal time in seconds to be spent on proof of work. + powTarget - Number (optional)?: Minimal PoW target required for this message. + targetPeer - Number (optional): Peer ID (for peer-to-peer message only). */ + + } + + +} + +module.exports = MailServers; diff --git a/test-mailserver.js b/test-mailserver.js new file mode 100644 index 0000000..f437f49 --- /dev/null +++ b/test-mailserver.js @@ -0,0 +1,12 @@ +var StatusJS = require('./src/index.js'); + +(async () => { +var status = new StatusJS(); + await status.connect("ws://localhost:8546"); + + status.mailservers.useMailserver("mail-02.gc-us-central1-a.eth.beta", (err, res) => { + console.log(err); + console.log(res); + }); + +})() From bd4534d28bb96bd2b8d5933b288a59f36dfd5ef5 Mon Sep 17 00:00:00 2001 From: Richard Ramos Date: Wed, 14 Nov 2018 11:42:07 -0400 Subject: [PATCH 2/7] Adding admin to api list --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index a9109bb..6c0ffce 100644 --- a/README.md +++ b/README.md @@ -2,6 +2,6 @@ # 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) From d8adeb6b8ef14d5bfae18c73b8f55e9377223c14 Mon Sep 17 00:00:00 2001 From: Richard Ramos Date: Wed, 14 Nov 2018 13:51:12 -0400 Subject: [PATCH 3/7] Adding mailserver request for messages --- src/mailservers.js | 66 ++++++++++++++++++++++------------------------ test-mailserver.js | 7 +++-- 2 files changed, 37 insertions(+), 36 deletions(-) diff --git a/src/mailservers.js b/src/mailservers.js index 25916ef..7f4f4db 100644 --- a/src/mailservers.js +++ b/src/mailservers.js @@ -30,51 +30,49 @@ class MailServers { const peerId = mailserverList[mailserver].substr(8, 128); - this.web3.shh.markTrustedPeer(peerId) - .then(() => { - this.mailserver = mailserver; - console.log("A"); - if (!cb) return; - cb(null, true); - }).catch((e) => { - if (!cb) return; - cb(e, false); - }); - + this.web3.shh.markTrustedPeer(peerId) + .then(() => { + this.mailserver = peerId; + if (!cb) return; + cb(null, true); + }).catch((e) => { + if (!cb) return; + cb(e, false); + }); } ); - - - } - requestMessages(cb){ + async requestMessages(cb){ if(!this.mailserver){ if(!cb) return; return cb("Mailserver is not set"); } - /* - :topics topics - :mailServerPeer address - :symKeyID sym-key-id - :timeout request-timeout - :from from - :to to}) - */ + const symKeyID = await this.web3.shh.generateSymKeyFromPassword("status-offline-inbox"); + // TODO: extract this as parameters + const topic = await this.web3.shh.generateSymKeyFromPassword("mytest"); + const from = (new Date("2018-11-13 00:00:00")).getTime(); + const to = (new Date("2018-11-14 20:00:00")).getTime(); - /* - symKeyID - String (optional): ID of symmetric key for message encryption (Either symKeyID or pubKey must be present. Can not be both.). - pubKey - String (optional): The public key for message encryption (Either symKeyID or pubKey must be present. Can not be both.). - sig - String (optional): The ID of the signing key. - ttl - Number: Time-to-live in seconds. - topic - String: 4 Bytes (mandatory when key is symmetric): Message topic. - payload - String: The payload of the message to be encrypted. - padding - Number (optional): Padding (byte array of arbitrary length). - powTime - Number (optional)?: Maximal time in seconds to be spent on proof of work. - powTarget - Number (optional)?: Minimal PoW target required for this message. - targetPeer - Number (optional): Peer ID (for peer-to-peer message only). */ + this.web3.currentProvider.send({ + method: "shh_requestMessages", + params: [mailserverList[this.mailserver], topic, symKeyID, from, to ], + jsonrpc: "2.0", + id: new Date().getTime() + }, (err, res) => { + + // TODO: implement result handling + + if(err){ + console.log(err); + } + + console.log(res); + }); + + } diff --git a/test-mailserver.js b/test-mailserver.js index f437f49..1faa418 100644 --- a/test-mailserver.js +++ b/test-mailserver.js @@ -5,8 +5,11 @@ var status = new StatusJS(); await status.connect("ws://localhost:8546"); status.mailservers.useMailserver("mail-02.gc-us-central1-a.eth.beta", (err, res) => { - console.log(err); - console.log(res); + + + status.mailservers.requestMessages(); + + }); })() From 6ecee95f7a9ad54691b80b3209062b0d9a877462 Mon Sep 17 00:00:00 2001 From: Richard Ramos Date: Wed, 14 Nov 2018 17:22:38 -0400 Subject: [PATCH 4/7] 1.0.6 --- package-lock.json | 2 +- package.json | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/package-lock.json b/package-lock.json index 2ca8a88..32a664e 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "status-js-api", - "version": "1.0.5", + "version": "1.0.6", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/package.json b/package.json index 679c007..5263888 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "status-js-api", - "version": "1.0.5", + "version": "1.0.6", "description": "", "main": "src/index.js", "scripts": { From 9f58d07cb6810ad362105bcb79f223629ce80549 Mon Sep 17 00:00:00 2001 From: Richard Ramos Date: Fri, 16 Nov 2018 16:28:19 -0400 Subject: [PATCH 5/7] Added mailserver support --- src/index.js | 15 +++++--- src/mailservers.js | 85 ++++++++++++++++++++++++---------------------- test-mailserver.js | 15 +++++--- 3 files changed, 66 insertions(+), 49 deletions(-) diff --git a/src/index.js b/src/index.js index 0f35289..1ec980e 100644 --- a/src/index.js +++ b/src/index.js @@ -44,8 +44,13 @@ class StatusJS { async connect(url) { let web3 = new Web3(); - web3.setProvider(new Web3.providers.WebsocketProvider(url, {headers: {Origin: "statusjs"}})); - + if(url.startsWith("ws://")){ + 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.mailservers = new mailservers(web3); @@ -112,7 +117,8 @@ class StatusJS { this.channels[channelName].subscription = this.shh.subscribe("messages", { minPow: POW_TARGET, symKeyID: this.channels[channelName].channelKey, - topics: [this.channels[channelName].channelCode] + topics: [this.channels[channelName].channelCode], + allowP2P: true }).on('data', (data) => { let username = utils.generateUsernameFromSeed(data.sig); @@ -132,7 +138,8 @@ class StatusJS { this.userMessagesSubscription = this.shh.subscribe("messages", { minPow: POW_TARGET, privateKeyID: this.sig, - topics: [CONTACT_DISCOVERY_TOPIC] + topics: [CONTACT_DISCOVERY_TOPIC], + allowP2P: true }).on('data', (data) => { if(!this.contacts[data.sig]){ this.addContact(data.sig); diff --git a/src/mailservers.js b/src/mailservers.js index 7f4f4db..bbff637 100644 --- a/src/mailservers.js +++ b/src/mailservers.js @@ -6,77 +6,82 @@ class MailServers { this.mailserver = null; } - useMailserver(mailserver, cb){ - if(!mailserverList[mailserver]){ + 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: [mailserverList[mailserver]], + params: [enode], jsonrpc: "2.0", id: new Date().getTime() - }, (err, res) => { + }, (err, res) => { if(err){ - if(cb) return cb(err); + if(cb) return cb(err, false); return; } if(!res.result){ - if(cb) return cb(err); + if(cb) return cb(err, false); return; } - const peerId = mailserverList[mailserver].substr(8, 128); - - this.web3.shh.markTrustedPeer(peerId) - .then(() => { - this.mailserver = peerId; - if (!cb) return; - cb(null, true); - }).catch((e) => { - if (!cb) return; - cb(e, false); - }); - } - ); + this.web3.shh.markTrustedPeer(enode) + .then(res => { + this.mailserver = enode; + if (!cb) return true; + cb(null, true); + }).catch((e) => { + console.log(e); + if (!cb) return; + cb(e, false); + }); + }); } - async requestMessages(cb){ + async requestMessages(topic, options, cb){ if(!this.mailserver){ if(!cb) return; - return cb("Mailserver is not set"); + return cb("Mailserver is not set", false); } - const symKeyID = await this.web3.shh.generateSymKeyFromPassword("status-offline-inbox"); - - // TODO: extract this as parameters - const topic = await this.web3.shh.generateSymKeyFromPassword("mytest"); - const from = (new Date("2018-11-13 00:00:00")).getTime(); - const to = (new Date("2018-11-14 20:00:00")).getTime(); + 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: "shh_requestMessages", - params: [mailserverList[this.mailserver], topic, symKeyID, from, to ], + method: "shhext_requestMessages", + params: [{ + mailserverPeer, + symKeyID, + timeout, + topics, + from, + to, + limit + }], jsonrpc: "2.0", id: new Date().getTime() - }, (err, res) => { - - // TODO: implement result handling - + }, (err, res) => { if(err){ - console.log(err); + if(cb) return cb(err); + return false; } - console.log(res); + if(cb) return cb(null, true); + return true; }); - - - } - - } module.exports = MailServers; diff --git a/test-mailserver.js b/test-mailserver.js index 1faa418..bf67736 100644 --- a/test-mailserver.js +++ b/test-mailserver.js @@ -2,14 +2,19 @@ var StatusJS = require('./src/index.js'); (async () => { var status = new StatusJS(); - await status.connect("ws://localhost:8546"); + await status.connect("/home/richard/.statusd/geth.ipc"); - status.mailservers.useMailserver("mail-02.gc-us-central1-a.eth.beta", (err, res) => { - - - status.mailservers.requestMessages(); + const channel = "mytest"; + await status.joinChat(channel); + status.onMessage(channel, (err, data) => { + console.dir(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); })() From 173dafe1e7077d86eae3325337d9068ff408e5ab Mon Sep 17 00:00:00 2001 From: Richard Ramos Date: Fri, 16 Nov 2018 16:34:55 -0400 Subject: [PATCH 6/7] Adding timeout for marking peer as trusted --- src/mailservers.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/mailservers.js b/src/mailservers.js index bbff637..796b4f9 100644 --- a/src/mailservers.js +++ b/src/mailservers.js @@ -32,6 +32,7 @@ class MailServers { return; } + setTimeout(() => { this.web3.shh.markTrustedPeer(enode) .then(res => { this.mailserver = enode; @@ -42,6 +43,7 @@ class MailServers { if (!cb) return; cb(e, false); }); + }, 1000); }); } From c1161730d93c2dd8c9ba8c554b72cb176273abe9 Mon Sep 17 00:00:00 2001 From: Richard Ramos Date: Fri, 16 Nov 2018 17:57:21 -0400 Subject: [PATCH 7/7] Removing minPow to be able to receive messages from mailserver --- src/index.js | 1 - src/mailservers.js | 6 +++--- test-mailserver.js | 6 ++++-- 3 files changed, 7 insertions(+), 6 deletions(-) diff --git a/src/index.js b/src/index.js index 9998e40..91e82ac 100644 --- a/src/index.js +++ b/src/index.js @@ -123,7 +123,6 @@ class StatusJS { } this.channels[channelName].subscription = this.shh.subscribe("messages", { - minPow: POW_TARGET, symKeyID: this.channels[channelName].channelKey, topics: [this.channels[channelName].channelCode], allowP2P: true diff --git a/src/mailservers.js b/src/mailservers.js index 796b4f9..e41a633 100644 --- a/src/mailservers.js +++ b/src/mailservers.js @@ -15,7 +15,7 @@ class MailServers { } this.symKeyID = await this.web3.shh.generateSymKeyFromPassword("status-offline-inbox"); - + this.web3.currentProvider.send({ method: "admin_addPeer", params: [enode], @@ -33,13 +33,12 @@ class MailServers { } setTimeout(() => { - this.web3.shh.markTrustedPeer(enode) + this.web3.shh.markTrustedPeer(enode) .then(res => { this.mailserver = enode; if (!cb) return true; cb(null, true); }).catch((e) => { - console.log(e); if (!cb) return; cb(e, false); }); @@ -55,6 +54,7 @@ class MailServers { 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 diff --git a/test-mailserver.js b/test-mailserver.js index bf67736..bd55ded 100644 --- a/test-mailserver.js +++ b/test-mailserver.js @@ -9,11 +9,13 @@ var status = new StatusJS(); await status.joinChat(channel); status.onMessage(channel, (err, data) => { - console.dir(data.payload); + 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); }); + status.mailservers.requestMessages(channel, {}, (err, res) => { + if(err) console.log(err); + }); }); setInterval(() => { }, 3000);