From f4e81da630f9ebb6a3833b3b82fa5c76f9fe3c0a Mon Sep 17 00:00:00 2001 From: Iuri Matias Date: Sat, 30 Dec 2017 20:09:46 -0500 Subject: [PATCH] refactor whisper code; separate old version --- lib/modules/whisper/embarkjs.js | 405 ++++++++++++++++---------------- lib/modules/whisper/index.js | 3 +- 2 files changed, 211 insertions(+), 197 deletions(-) diff --git a/lib/modules/whisper/embarkjs.js b/lib/modules/whisper/embarkjs.js index dc377a8d2..a58869736 100644 --- a/lib/modules/whisper/embarkjs.js +++ b/lib/modules/whisper/embarkjs.js @@ -1,8 +1,9 @@ /*global EmbarkJS, Web3 */ -let __embarkWhisper = {}; +// for the old version of whisper and web3.js +let __embarkWhisperOld = {}; -__embarkWhisper.setProvider = function(options) { +__embarkWhisperOld.setProvider = function(options) { const self = this; let provider; if (options === undefined) { @@ -10,21 +11,12 @@ __embarkWhisper.setProvider = function(options) { } else { provider = options.server + ':' + options.port; } - if (EmbarkJS.isNewWeb3()) { - self.web3 = new Web3(new Web3.providers.WebsocketProvider("ws://" + provider)); - } else { - self.web3 = new Web3(new Web3.providers.HttpProvider("http://" + provider)); - } + self.web3 = new Web3(new Web3.providers.HttpProvider("http://" + provider)); self.getWhisperVersion(function(err, version) { if (err) { console.log("whisper not available"); } else if (version >= 5) { - if (self.web3CompatibleWithV5()) { - self.web3.shh.newSymKey().then((id) => { self.symKeyID = id; }); - self.web3.shh.newKeyPair().then((id) => { self.sig = id; }); - } else { - console.log("this version of whisper in this node"); - } + throw new Error("whisper 5 not supported with this version of web3.js"); } else { self.identity = self.web3.shh.newIdentity(); } @@ -32,201 +24,222 @@ __embarkWhisper.setProvider = function(options) { }); }; -__embarkWhisper.web3CompatibleWithV5 = function() { - var _web3 = new Web3(); - if ((typeof _web3.version) === "string") { - return true; - } - return parseInt(_web3.version.api.split('.')[1], 10) >= 20; -}; - -__embarkWhisper.getWhisperVersion = function(cb) { - const self = this; - if (EmbarkJS.isNewWeb3()) { - this.web3.shh.getVersion(function(err, version) { - cb(err, version); - }); - } else { - this.web3.version.getWhisper(function(err, _res) { - cb(err, self.web3.version.whisper); - }); - } -}; - -__embarkWhisper.sendMessage = function(options) { +__embarkWhisperOld.sendMessage = function(options) { var topics, data, ttl, priority, payload; - if (EmbarkJS.isNewWeb3()) { - topics = options.topic || options.topics; - data = options.data || options.payload; - ttl = options.ttl || 100; - priority = options.priority || 1000; - var powTime = options.powTime || 3; - var powTarget = options.powTarget || 0.5; + topics = options.topic || options.topics; + data = options.data || options.payload; + ttl = options.ttl || 100; + priority = options.priority || 1000; + var identity = options.identity || this.identity || this.web3.shh.newIdentity(); + var _topics; - if (topics === undefined) { - throw new Error("missing option: topic"); - } - - if (data === undefined) { - throw new Error("missing option: data"); - } - - topics = this.web3.utils.toHex(topics).slice(0, 10); - - payload = JSON.stringify(data); - - let message = { - symKeyID: this.symKeyID, // encrypts using the sym key ID - sig: this.sig, // signs the message using the keyPair ID - ttl: ttl, - topic: topics, - payload: EmbarkJS.Utils.fromAscii(payload), - powTime: powTime, - powTarget: powTarget - }; - - this.web3.shh.post(message, function() { }); - } else { - topics = options.topic || options.topics; - data = options.data || options.payload; - ttl = options.ttl || 100; - priority = options.priority || 1000; - var identity = options.identity || this.identity || this.web3.shh.newIdentity(); - var _topics; - - if (topics === undefined) { - throw new Error("missing option: topic"); - } - - if (data === undefined) { - throw new Error("missing option: data"); - } - - if (typeof topics === 'string') { - _topics = [EmbarkJS.Utils.fromAscii(topics)]; - } else { - _topics = topics.map((t) => EmbarkJS.Utils.fromAscii(t)); - } - topics = _topics; - - payload = JSON.stringify(data); - - var message; - message = { - from: identity, - topics: topics, - payload: EmbarkJS.Utils.fromAscii(payload), - ttl: ttl, - priority: priority - }; - - return this.web3.shh.post(message, function() { }); + if (topics === undefined) { + throw new Error("missing option: topic"); } + + if (data === undefined) { + throw new Error("missing option: data"); + } + + if (typeof topics === 'string') { + _topics = [EmbarkJS.Utils.fromAscii(topics)]; + } else { + _topics = topics.map((t) => EmbarkJS.Utils.fromAscii(t)); + } + topics = _topics; + + payload = JSON.stringify(data); + + var message; + message = { + from: identity, + topics: topics, + payload: EmbarkJS.Utils.fromAscii(payload), + ttl: ttl, + priority: priority + }; + + return this.web3.shh.post(message, function() { }); }; -__embarkWhisper.listenTo = function(options) { +__embarkWhisperOld.listenTo = function(options) { var topics, _topics, messageEvents; - if (EmbarkJS.isNewWeb3()) { - messageEvents = function() { - this.cb = function() {}; - }; + topics = options.topic || options.topics; + _topics = []; - messageEvents.prototype.then = function(cb) { - this.cb = cb; - }; + messageEvents = function() { + this.cb = function() {}; + }; - messageEvents.prototype.error = function(err) { - return err; - }; + messageEvents.prototype.then = function(cb) { + this.cb = cb; + }; - messageEvents.prototype.stop = function() { - this.filter.stopWatching(); - }; + messageEvents.prototype.error = function(err) { + return err; + }; - topics = options.topic || options.topics; - _topics = []; + messageEvents.prototype.stop = function() { + this.filter.stopWatching(); + }; - let promise = new messageEvents(); - - // listenTo - if (typeof topics === 'string') { - topics = [this.web3.utils.toHex(topics).slice(0, 10)]; - } else { - topics = topics.map((t) => this.web3.utils.toHex(t).slice(0, 10)); - } - - let filter = this.web3.shh.subscribe("messages", { - symKeyID: this.symKeyID, - topics: topics - }).on('data', function(result) { - var payload = JSON.parse(EmbarkJS.Utils.toAscii(result.payload)); - var data; - data = { - topic: result.topic, - data: payload, - //from: result.from, - time: result.timestamp - }; - - promise.cb(payload, data, result); - }); - - promise.filter = filter; - - return promise; + if (typeof topics === 'string') { + _topics = [topics]; } else { - topics = options.topic || options.topics; - _topics = []; - - messageEvents = function() { - this.cb = function() {}; - }; - - messageEvents.prototype.then = function(cb) { - this.cb = cb; - }; - - messageEvents.prototype.error = function(err) { - return err; - }; - - messageEvents.prototype.stop = function() { - this.filter.stopWatching(); - }; - - if (typeof topics === 'string') { - _topics = [topics]; - } else { - _topics = topics.map((t) => EmbarkJS.Utils.fromAscii(t)); - } - topics = _topics; - - var filterOptions = { - topics: topics - }; - - let promise = new messageEvents(); - - let filter = this.web3.shh.filter(filterOptions, function(err, result) { - var payload = JSON.parse(EmbarkJS.Utils.toAscii(result.payload)); - var data; - if (err) { - promise.error(err); - } else { - data = { - topic: topics, - data: payload, - from: result.from, - time: (new Date(result.sent * 1000)) - }; - promise.cb(payload, data, result); - } - }); - - promise.filter = filter; - - return promise; + _topics = topics.map((t) => EmbarkJS.Utils.fromAscii(t)); } + topics = _topics; + + var filterOptions = { + topics: topics + }; + + let promise = new messageEvents(); + + let filter = this.web3.shh.filter(filterOptions, function(err, result) { + var payload = JSON.parse(EmbarkJS.Utils.toAscii(result.payload)); + var data; + if (err) { + promise.error(err); + } else { + data = { + topic: topics, + data: payload, + from: result.from, + time: (new Date(result.sent * 1000)) + }; + promise.cb(payload, data, result); + } + }); + + promise.filter = filter; + + return promise; }; -EmbarkJS.Messages.registerProvider('whisper', __embarkWhisper); +__embarkWhisperOld.getWhisperVersion = function(cb) { + this.web3.version.getWhisper(function(err, _res) { + cb(err, self.web3.version.whisper); + }); +}; + +// for the whisper v5 and web3.js 1.0 +let __embarkWhisperNewWeb3 = {}; + +__embarkWhisperNewWeb3.setProvider = function(options) { + const self = this; + let provider; + if (options === undefined) { + provider = "localhost:8546"; + } else { + provider = options.server + ':' + options.port; + } + // TODO: take into account type + self.web3 = new Web3(new Web3.providers.WebsocketProvider("ws://" + provider)); + self.getWhisperVersion(function(err, version) { + if (err) { + console.log("whisper not available"); + } else if (version >= 5) { + self.web3.shh.newSymKey().then((id) => { self.symKeyID = id; }); + self.web3.shh.newKeyPair().then((id) => { self.sig = id; }); + } else { + throw new Error("version of whisper not supported"); + } + self.whisperVersion = self.web3.version.whisper; + }); +}; + +__embarkWhisperNewWeb3.sendMessage = function(options) { + var topics, data, ttl, payload; + topics = options.topic || options.topics; + data = options.data || options.payload; + ttl = options.ttl || 100; + var powTime = options.powTime || 3; + var powTarget = options.powTarget || 0.5; + + if (topics === undefined) { + throw new Error("missing option: topic"); + } + + if (data === undefined) { + throw new Error("missing option: data"); + } + + topics = this.web3.utils.toHex(topics).slice(0, 10); + + payload = JSON.stringify(data); + + let message = { + symKeyID: this.symKeyID, // encrypts using the sym key ID + sig: this.sig, // signs the message using the keyPair ID + ttl: ttl, + topic: topics, + payload: EmbarkJS.Utils.fromAscii(payload), + powTime: powTime, + powTarget: powTarget + }; + + this.web3.shh.post(message, function() { }); +}; + +__embarkWhisperNewWeb3.listenTo = function(options) { + var topics, messageEvents; + messageEvents = function() { + this.cb = function() {}; + }; + + messageEvents.prototype.then = function(cb) { + this.cb = cb; + }; + + messageEvents.prototype.error = function(err) { + return err; + }; + + messageEvents.prototype.stop = function() { + this.filter.stopWatching(); + }; + + topics = options.topic || options.topics; + + let promise = new messageEvents(); + + if (typeof topics === 'string') { + topics = [this.web3.utils.toHex(topics).slice(0, 10)]; + } else { + topics = topics.map((t) => this.web3.utils.toHex(t).slice(0, 10)); + } + + let filter = this.web3.shh.subscribe("messages", { + symKeyID: this.symKeyID, + topics: topics + }).on('data', function(result) { + var payload = JSON.parse(EmbarkJS.Utils.toAscii(result.payload)); + var data; + data = { + topic: result.topic, + data: payload, + //from: result.from, + time: result.timestamp + }; + + promise.cb(payload, data, result); + }); + + promise.filter = filter; + + return promise; +}; + +__embarkWhisperNewWeb3.getWhisperVersion = function(cb) { + this.web3.shh.getVersion(function(err, version) { + cb(err, version); + }); +}; + +if (EmbarkJS.isNewWeb3()) { + EmbarkJS.Messages.registerProvider('whisper', __embarkWhisperNewWeb3); +} else { + EmbarkJS.Messages.registerProvider('whisper', __embarkWhisperOld); +} + diff --git a/lib/modules/whisper/index.js b/lib/modules/whisper/index.js index 3b848f422..087c73fc9 100644 --- a/lib/modules/whisper/index.js +++ b/lib/modules/whisper/index.js @@ -39,8 +39,9 @@ class Whisper { } let code = ""; + code += "\n" + fs.readFileSync(utils.joinPath(__dirname, 'embarkjs.js')).toString(); - code += "\nEmbarkJS.Storage.registerProvider('ipfs', __embarkWhisper);"; + //code += "\nEmbarkJS.Storage.registerProvider('whisper', __embarkWhisper);"; this.embark.addCodeToEmbarkJS(code); }