From 37a126a6b8c0efabdf0a3015a11b6c16a8992ddd Mon Sep 17 00:00:00 2001 From: Iuri Matias Date: Sat, 30 Dec 2017 20:42:52 -0500 Subject: [PATCH] move old code to a separate file --- lib/modules/whisper/embarkjs.js | 129 ----------------------- lib/modules/whisper/embarkjs_old_web3.js | 125 ++++++++++++++++++++++ lib/modules/whisper/index.js | 18 +++- 3 files changed, 138 insertions(+), 134 deletions(-) create mode 100644 lib/modules/whisper/embarkjs_old_web3.js diff --git a/lib/modules/whisper/embarkjs.js b/lib/modules/whisper/embarkjs.js index a5886973..2199bb06 100644 --- a/lib/modules/whisper/embarkjs.js +++ b/lib/modules/whisper/embarkjs.js @@ -1,128 +1,5 @@ /*global EmbarkJS, Web3 */ -// for the old version of whisper and web3.js -let __embarkWhisperOld = {}; - -__embarkWhisperOld.setProvider = function(options) { - const self = this; - let provider; - if (options === undefined) { - provider = "localhost:8546"; - } else { - provider = options.server + ':' + options.port; - } - 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) { - throw new Error("whisper 5 not supported with this version of web3.js"); - } else { - self.identity = self.web3.shh.newIdentity(); - } - self.whisperVersion = self.web3.version.whisper; - }); -}; - -__embarkWhisperOld.sendMessage = function(options) { - var topics, data, ttl, priority, payload; - 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() { }); -}; - -__embarkWhisperOld.listenTo = function(options) { - var topics, _topics, messageEvents; - 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; -}; - -__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 = {}; @@ -237,9 +114,3 @@ __embarkWhisperNewWeb3.getWhisperVersion = function(cb) { }); }; -if (EmbarkJS.isNewWeb3()) { - EmbarkJS.Messages.registerProvider('whisper', __embarkWhisperNewWeb3); -} else { - EmbarkJS.Messages.registerProvider('whisper', __embarkWhisperOld); -} - diff --git a/lib/modules/whisper/embarkjs_old_web3.js b/lib/modules/whisper/embarkjs_old_web3.js new file mode 100644 index 00000000..c2f52a4c --- /dev/null +++ b/lib/modules/whisper/embarkjs_old_web3.js @@ -0,0 +1,125 @@ +/*global EmbarkJS, Web3 */ + +// for the old version of whisper and web3.js +let __embarkWhisperOld = {}; + +__embarkWhisperOld.setProvider = function(options) { + const self = this; + let provider; + if (options === undefined) { + provider = "localhost:8546"; + } else { + provider = options.server + ':' + options.port; + } + 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) { + throw new Error("whisper 5 not supported with this version of web3.js"); + } else { + self.identity = self.web3.shh.newIdentity(); + } + self.whisperVersion = self.web3.version.whisper; + }); +}; + +__embarkWhisperOld.sendMessage = function(options) { + var topics, data, ttl, priority, payload; + 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() { }); +}; + +__embarkWhisperOld.listenTo = function(options) { + var topics, _topics, messageEvents; + 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; +}; + +__embarkWhisperOld.getWhisperVersion = function(cb) { + this.web3.version.getWhisper(function(err, _res) { + cb(err, self.web3.version.whisper); + }); +}; + diff --git a/lib/modules/whisper/index.js b/lib/modules/whisper/index.js index 087c73fc..d1a3b80f 100644 --- a/lib/modules/whisper/index.js +++ b/lib/modules/whisper/index.js @@ -30,6 +30,7 @@ class Whisper { } addWhisperToEmbarkJS() { + const self = this; // TODO: make this a shouldAdd condition if (this.communicationConfig === {}) { return; @@ -38,12 +39,19 @@ class Whisper { return; } - let code = ""; + // TODO: possible race condition could be a concern + this.events.request("version:get:web3", function(web3Version) { + let code = ""; - code += "\n" + fs.readFileSync(utils.joinPath(__dirname, 'embarkjs.js')).toString(); - //code += "\nEmbarkJS.Storage.registerProvider('whisper', __embarkWhisper);"; - - this.embark.addCodeToEmbarkJS(code); + if (web3Version[0] === "0") { + code += "\n" + fs.readFileSync(utils.joinPath(__dirname, 'embarkjs_old_web3.js')).toString(); + code += "\nEmbarkJS.Messages.registerProvider('whisper', __embarkWhisperOld);"; + } else { + code += "\n" + fs.readFileSync(utils.joinPath(__dirname, 'embarkjs.js')).toString(); + code += "\nEmbarkJS.Messages.registerProvider('whisper', __embarkWhisperNewWeb3);"; + } + self.embark.addCodeToEmbarkJS(code); + }); } addSetProvider() {