diff --git a/lib/modules/whisper/index.js b/lib/modules/whisper/index.js index bde2899d2..044365924 100644 --- a/lib/modules/whisper/index.js +++ b/lib/modules/whisper/index.js @@ -85,6 +85,7 @@ class Whisper { code += "\n" + fs.readFileSync(utils.joinPath(__dirname, 'js', 'embarkjs_old_web3.js')).toString(); code += "\nEmbarkJS.Messages.registerProvider('whisper', __embarkWhisperOld);"; } else { + code += "\n" + fs.readFileSync(utils.joinPath(__dirname, 'js', 'communicationFunctions.js')).toString(); code += "\n" + fs.readFileSync(utils.joinPath(__dirname, 'js', 'embarkjs.js')).toString(); code += "\nEmbarkJS.Messages.registerProvider('whisper', __embarkWhisperNewWeb3);"; } diff --git a/lib/modules/whisper/js/communicationFunctions.js b/lib/modules/whisper/js/communicationFunctions.js new file mode 100644 index 000000000..5f5a20997 --- /dev/null +++ b/lib/modules/whisper/js/communicationFunctions.js @@ -0,0 +1,112 @@ +function sendMessage(options, callback) { + let topics, ttl, payload; + topics = options.topic; + const data = options.data; + ttl = options.ttl || 100; + const powTime = options.powTime || 3; + const powTarget = options.powTarget || 0.5; + const sig = options.sig; + const fromAscii = options.fromAscii; + const toHex = options.toHex; + const symKeyID = options.symKeyID; + const post = options.post; + + if (topics) { + topics = toHex(topics).slice(0, 10); + } + + payload = JSON.stringify(data); + + let message = { + sig: sig, // signs the message using the keyPair ID + ttl: ttl, + payload: fromAscii(payload), + powTime: powTime, + powTarget: powTarget + }; + + if (topics) { + message.topic = topics; + } + + if (options.pubKey) { + message.pubKey = options.pubKey; // encrypt using a given pubKey + } else if(options.symKeyID) { + message.symKeyID = options.symKeyID; // encrypts using given sym key ID + } else { + message.symKeyID = symKeyID; // encrypts using the sym key ID + } + + if (topics === undefined && message.symKeyID && !message.pubKey) { + return callback("missing option: topic"); + } + + post(message, callback); +} + +function listenTo(options, callback) { + let topics = options.topic; + const messageEvents = options.messageEvents; + const toHex = options.toHex; + const toAscii = options.toAscii; + const sig = options.sig; + const symKeyID = options.symKeyID; + const subscribe = options.subscribe; + + let promise = new messageEvents(); + + let subOptions = {}; + + if(topics){ + if (typeof topics === 'string') { + topics = [toHex(topics).slice(0, 10)]; + } else { + topics = topics.map((t) => toHex(t).slice(0, 10)); + } + subOptions.topics = topics; + } + + if (options.minPow) { + subOptions.minPow = options.minPow; + } + + if (options.usePrivateKey === true) { + if (options.privateKeyID) { + subOptions.privateKeyID = options.privateKeyID; + } else { + subOptions.privateKeyID = sig; + } + } else { + if (options.symKeyID) { + subOptions.symKeyID = options.symKeyID; + } else { + subOptions.symKeyID = symKeyID; + } + } + + promise.filter = subscribe("messages", subOptions) + .on('data', function (result) { + var payload = JSON.parse(toAscii(result.payload)); + var data; + data = { + topic: toAscii(result.topic), + data: payload, + //from: result.from, + time: result.timestamp + }; + + if (callback) { + return callback(null, data); + } + promise.cb(payload, data, result); + }); + + return promise; +} + +if (typeof module !== 'undefined' && module.exports) { + module.exports = { + sendMessage, + listenTo + }; +} diff --git a/lib/modules/whisper/js/embarkjs.js b/lib/modules/whisper/js/embarkjs.js index d7a53a029..f1e03687c 100644 --- a/lib/modules/whisper/js/embarkjs.js +++ b/lib/modules/whisper/js/embarkjs.js @@ -1,4 +1,4 @@ -/*global EmbarkJS, Web3, __MessageEvents */ +/*global EmbarkJS, Web3, __MessageEvents, sendMessage, listenTo*/ // for the whisper v5 and web3.js 1.0 let __embarkWhisperNewWeb3 = {}; @@ -36,105 +36,36 @@ __embarkWhisperNewWeb3.setProvider = function(options) { }; __embarkWhisperNewWeb3.sendMessage = function(options) { - var topics, data, ttl, payload; - topics = options.topic; - data = options.data || options.payload; - ttl = options.ttl || 100; - var powTime = options.powTime || 3; - var powTarget = options.powTarget || 0.5; - - if (data === undefined) { + const data = options.data || options.payload; + if (!data) { throw new Error("missing option: data"); } + Object.assign(options, { + sig: this.sig, + fromAscii: EmbarkJS.Utils.fromAscii, + toHex: this.web3.utils.toHex, + symKeyID: options.symKeyID || this.symKeyID, + post: this.web3.shh.post, + data + }); - if (topics) { - topics = this.web3.utils.toHex(topics).slice(0, 10); - } - - payload = JSON.stringify(data); - - let message = { - sig: this.sig, // signs the message using the keyPair ID - ttl: ttl, - payload: EmbarkJS.Utils.fromAscii(payload), - powTime: powTime, - powTarget: powTarget - }; - - if (topics) { - message.topic = topics; - } - - if (options.pubKey) { - message.pubKey = options.pubKey; // encrypt using a given pubKey - } else if (options.symKeyID) { - message.symKeyID = options.symKeyID; // encrypts using given sym key ID - } else { - message.symKeyID = this.symKeyID; // encrypts using the sym key ID - } - - if (topics === undefined && message.symKeyID && !message.pubKey) { - throw new Error("missing option: topic"); - } - - this.web3.shh.post(message, function() { + sendMessage(options, (err) => { + if (err) { + throw new Error(err); + } }); }; -__embarkWhisperNewWeb3.listenTo = function(options, callback) { - var topics = options.topic; - - let promise = new __MessageEvents(); - - let subOptions = {}; - - if (topics) { - 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)); - } - subOptions.topics = topics; - } - - if (options.minPow) { - subOptions.minPow = options.minPow; - } - - if (options.usePrivateKey === true) { - if (options.privateKeyID) { - subOptions.privateKeyID = options.privateKeyID; - } else { - subOptions.privateKeyID = this.sig; - } - } else { - if (options.symKeyID) { - subOptions.symKeyID = options.symKeyID; - } else { - subOptions.symKeyID = this.symKeyID; - } - } - - let filter = this.web3.shh.subscribe("messages", subOptions) - .on('data', function(result) { - var payload = JSON.parse(EmbarkJS.Utils.toAscii(result.payload)); - var data; - data = { - topic: EmbarkJS.Utils.toAscii(result.topic), - data: payload, - //from: result.from, - time: result.timestamp - }; - - if (callback) { - return callback(null, data); - } - promise.cb(payload, data, result); - }); - - promise.filter = filter; - - return promise; +__embarkWhisperNewWeb3.listenTo = function (options, callback) { + Object.assign(options, { + sig: this.sig, + toAscii: EmbarkJS.Utils.toAscii, + toHex: this.web3.utils.toHex, + symKeyID: options.symKeyID || this.symKeyID, + messageEvents: __MessageEvents, + subscribe: this.web3.shh.subscribe + }); + listenTo(options, callback); }; __embarkWhisperNewWeb3.getWhisperVersion = function(cb) {