From 57f71d5504abaff95b95b30581a35409edc25477 Mon Sep 17 00:00:00 2001 From: Richard Ramos Date: Mon, 16 Jul 2018 10:23:11 -0400 Subject: [PATCH] Symmetric and private keys can be set in whisper --- lib/modules/whisper/index.js | 12 ++++ lib/modules/whisper/js/embarkjs.js | 62 ++++++++++++++++--- templates/boilerplate/config/communication.js | 7 +++ templates/demo/config/communication.js | 7 +++ 4 files changed, 78 insertions(+), 10 deletions(-) diff --git a/lib/modules/whisper/index.js b/lib/modules/whisper/index.js index be1743400..25b6f5351 100644 --- a/lib/modules/whisper/index.js +++ b/lib/modules/whisper/index.js @@ -73,12 +73,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({ server: canonicalHost(connection.host || defaultHost), 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 + } + */ } };