From 692d5638de80c73700225a1dc3d80f756373697d Mon Sep 17 00:00:00 2001 From: Richard Ramos Date: Mon, 16 Jul 2018 10:23:11 -0400 Subject: [PATCH 1/3] Symmetric and private keys can be set in whisper --- lib/modules/whisper/index.js | 16 ++++- lib/modules/whisper/js/embarkjs.js | 62 ++++++++++++++++--- templates/boilerplate/config/communication.js | 7 +++ templates/demo/config/communication.js | 7 +++ 4 files changed, 80 insertions(+), 12 deletions(-) diff --git a/lib/modules/whisper/index.js b/lib/modules/whisper/index.js index 727fe0ab9..c955d1138 100644 --- a/lib/modules/whisper/index.js +++ b/lib/modules/whisper/index.js @@ -71,12 +71,24 @@ class Whisper { addSetProvider() { let connection = this.communicationConfig.connection || {}; + let keys = this.communicationConfig.keys || {}; // todo: make the add code a function as well - let config = JSON.stringify({ + let config = { server: connection.host || 'localhost', port: connection.port || '8546', type: connection.type || 'ws' - }); + }; + + if (keys.symmetricKey) { + config.symKey = keys.symmetricKey; + } + + if (keys.privateKey) { + config.privateKey = keys.privateKey; + } + + config = JSON.stringify(config); + let code = "\nEmbarkJS.Messages.setProvider('whisper'," + config + ");"; let shouldInit = (communicationConfig) => { diff --git a/lib/modules/whisper/js/embarkjs.js b/lib/modules/whisper/js/embarkjs.js index 4cfa98fea..c41a32e1a 100644 --- a/lib/modules/whisper/js/embarkjs.js +++ b/lib/modules/whisper/js/embarkjs.js @@ -17,12 +17,27 @@ __embarkWhisperNewWeb3.setProvider = function (options) { 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; - }); + + if (options.symKey) { + self.web3.shh.addSymKey(options.symKey).then(id => { + self.symKeyID = id; + }); + } else { + self.web3.shh.newSymKey().then((id) => { + self.symKeyID = id; + }); + } + + if (options.privateKey) { + self.web3.shh.addPrivateKey(options.privateKey).then((id) => { + self.sig = id; + }); + } else { + self.web3.shh.newKeyPair().then((id) => { + self.sig = id; + }); + } + } else { throw new Error("version of whisper not supported"); } @@ -51,7 +66,6 @@ __embarkWhisperNewWeb3.sendMessage = function (options) { 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, @@ -60,6 +74,14 @@ __embarkWhisperNewWeb3.sendMessage = function (options) { powTarget: powTarget }; + 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 + } + this.web3.shh.post(message, function () { }); }; @@ -75,10 +97,30 @@ __embarkWhisperNewWeb3.listenTo = function (options, callback) { topics = topics.map((t) => this.web3.utils.toHex(t).slice(0, 10)); } - let filter = this.web3.shh.subscribe("messages", { - symKeyID: this.symKeyID, + let subOptions = { topics: topics - }).on('data', function (result) { + }; + + 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 = { diff --git a/templates/boilerplate/config/communication.js b/templates/boilerplate/config/communication.js index 035d95def..cf68c45a4 100644 --- a/templates/boilerplate/config/communication.js +++ b/templates/boilerplate/config/communication.js @@ -8,6 +8,13 @@ module.exports = { port: 8546, // Port of the blockchain node type: "ws" // Type of connection (ws or rpc) } + // Use this section when you need a specific symmetric or private keys in whisper + /* + ,keys: { + symmetricKey: "your_symmetric_key",// Symmetric key for message decryption + privateKey: "your_private_key" // Private Key to be used as a signing key and for message decryption + } + */ } }; diff --git a/templates/demo/config/communication.js b/templates/demo/config/communication.js index 8c4d1f918..61fef6979 100644 --- a/templates/demo/config/communication.js +++ b/templates/demo/config/communication.js @@ -8,5 +8,12 @@ module.exports = { port: 8546, // Port of the blockchain node type: "ws" // Type of connection (ws or rpc) } + // Use this section when you need a specific symmetric or private keys in whisper + /* + ,keys: { + symmetricKey: "your_symmetric_key",// Symmetric key for message decryption + privateKey: "your_private_key" // Private Key to be used as a signing key and for message decryption + } + */ } }; From b9378762ffb03e3c023ec3e776d653bb09926eea Mon Sep 17 00:00:00 2001 From: Richard Ramos Date: Mon, 16 Jul 2018 10:32:55 -0400 Subject: [PATCH 2/3] Removing code. Doesn't make sense to specify keys when everyone can read them --- lib/modules/whisper/index.js | 16 +++-------- lib/modules/whisper/js/embarkjs.js | 27 +++++-------------- templates/boilerplate/config/communication.js | 7 ----- templates/demo/config/communication.js | 7 ----- 4 files changed, 9 insertions(+), 48 deletions(-) diff --git a/lib/modules/whisper/index.js b/lib/modules/whisper/index.js index c955d1138..0b688971d 100644 --- a/lib/modules/whisper/index.js +++ b/lib/modules/whisper/index.js @@ -71,23 +71,13 @@ class Whisper { addSetProvider() { let connection = this.communicationConfig.connection || {}; - let keys = this.communicationConfig.keys || {}; + // todo: make the add code a function as well - let config = { + let config = JSON.stringify({ server: connection.host || 'localhost', port: connection.port || '8546', type: connection.type || 'ws' - }; - - if (keys.symmetricKey) { - config.symKey = keys.symmetricKey; - } - - if (keys.privateKey) { - config.privateKey = keys.privateKey; - } - - config = JSON.stringify(config); + }); let code = "\nEmbarkJS.Messages.setProvider('whisper'," + config + ");"; diff --git a/lib/modules/whisper/js/embarkjs.js b/lib/modules/whisper/js/embarkjs.js index c41a32e1a..18dd22a2f 100644 --- a/lib/modules/whisper/js/embarkjs.js +++ b/lib/modules/whisper/js/embarkjs.js @@ -17,27 +17,12 @@ __embarkWhisperNewWeb3.setProvider = function (options) { if (err) { console.log("whisper not available"); } else if (version >= 5) { - - if (options.symKey) { - self.web3.shh.addSymKey(options.symKey).then(id => { - self.symKeyID = id; - }); - } else { - self.web3.shh.newSymKey().then((id) => { - self.symKeyID = id; - }); - } - - if (options.privateKey) { - self.web3.shh.addPrivateKey(options.privateKey).then((id) => { - self.sig = id; - }); - } else { - self.web3.shh.newKeyPair().then((id) => { - self.sig = id; - }); - } - + 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"); } diff --git a/templates/boilerplate/config/communication.js b/templates/boilerplate/config/communication.js index cf68c45a4..035d95def 100644 --- a/templates/boilerplate/config/communication.js +++ b/templates/boilerplate/config/communication.js @@ -8,13 +8,6 @@ module.exports = { port: 8546, // Port of the blockchain node type: "ws" // Type of connection (ws or rpc) } - // Use this section when you need a specific symmetric or private keys in whisper - /* - ,keys: { - symmetricKey: "your_symmetric_key",// Symmetric key for message decryption - privateKey: "your_private_key" // Private Key to be used as a signing key and for message decryption - } - */ } }; diff --git a/templates/demo/config/communication.js b/templates/demo/config/communication.js index 61fef6979..8c4d1f918 100644 --- a/templates/demo/config/communication.js +++ b/templates/demo/config/communication.js @@ -8,12 +8,5 @@ module.exports = { port: 8546, // Port of the blockchain node type: "ws" // Type of connection (ws or rpc) } - // Use this section when you need a specific symmetric or private keys in whisper - /* - ,keys: { - symmetricKey: "your_symmetric_key",// Symmetric key for message decryption - privateKey: "your_private_key" // Private Key to be used as a signing key and for message decryption - } - */ } }; From 34ebd42be91cae8ab3b88ef73267e3c5d4df3079 Mon Sep 17 00:00:00 2001 From: Richard Ramos Date: Mon, 16 Jul 2018 12:18:31 -0400 Subject: [PATCH 3/3] Allow topics to be optional when pubKey/privKey is used --- lib/modules/whisper/js/embarkjs.js | 38 +++++++++++++++++------------- 1 file changed, 22 insertions(+), 16 deletions(-) diff --git a/lib/modules/whisper/js/embarkjs.js b/lib/modules/whisper/js/embarkjs.js index 18dd22a2f..ea72b9d0e 100644 --- a/lib/modules/whisper/js/embarkjs.js +++ b/lib/modules/whisper/js/embarkjs.js @@ -32,33 +32,34 @@ __embarkWhisperNewWeb3.setProvider = function (options) { __embarkWhisperNewWeb3.sendMessage = function (options) { var topics, data, ttl, payload; - topics = options.topic || options.topics; + topics = options.topic; 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); + 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, - topic: topics, 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) { @@ -67,24 +68,29 @@ __embarkWhisperNewWeb3.sendMessage = function (options) { 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 () { }); }; __embarkWhisperNewWeb3.listenTo = function (options, callback) { - var topics = options.topic || options.topics; + var topics = options.topic; 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 subOptions = {}; - let subOptions = { - topics: topics - }; + 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;