Symmetric and private keys can be set in whisper

This commit is contained in:
Richard Ramos 2018-07-16 10:23:11 -04:00
parent 91cec97a76
commit 692d5638de
4 changed files with 80 additions and 12 deletions

View File

@ -71,12 +71,24 @@ class Whisper {
addSetProvider() { addSetProvider() {
let connection = this.communicationConfig.connection || {}; let connection = this.communicationConfig.connection || {};
let keys = this.communicationConfig.keys || {};
// todo: make the add code a function as well // todo: make the add code a function as well
let config = JSON.stringify({ let config = {
server: connection.host || 'localhost', server: connection.host || 'localhost',
port: connection.port || '8546', port: connection.port || '8546',
type: connection.type || 'ws' 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 code = "\nEmbarkJS.Messages.setProvider('whisper'," + config + ");";
let shouldInit = (communicationConfig) => { let shouldInit = (communicationConfig) => {

View File

@ -17,12 +17,27 @@ __embarkWhisperNewWeb3.setProvider = function (options) {
if (err) { if (err) {
console.log("whisper not available"); console.log("whisper not available");
} else if (version >= 5) { } 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.web3.shh.newSymKey().then((id) => {
self.symKeyID = 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.web3.shh.newKeyPair().then((id) => {
self.sig = id; self.sig = id;
}); });
}
} else { } else {
throw new Error("version of whisper not supported"); throw new Error("version of whisper not supported");
} }
@ -51,7 +66,6 @@ __embarkWhisperNewWeb3.sendMessage = function (options) {
payload = JSON.stringify(data); payload = JSON.stringify(data);
let message = { let message = {
symKeyID: this.symKeyID, // encrypts using the sym key ID
sig: this.sig, // signs the message using the keyPair ID sig: this.sig, // signs the message using the keyPair ID
ttl: ttl, ttl: ttl,
topic: topics, topic: topics,
@ -60,6 +74,14 @@ __embarkWhisperNewWeb3.sendMessage = function (options) {
powTarget: powTarget 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 () { 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)); topics = topics.map((t) => this.web3.utils.toHex(t).slice(0, 10));
} }
let filter = this.web3.shh.subscribe("messages", { let subOptions = {
symKeyID: this.symKeyID,
topics: topics 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 payload = JSON.parse(EmbarkJS.Utils.toAscii(result.payload));
var data; var data;
data = { data = {

View File

@ -8,6 +8,13 @@ module.exports = {
port: 8546, // Port of the blockchain node port: 8546, // Port of the blockchain node
type: "ws" // Type of connection (ws or rpc) 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
}
*/
} }
}; };

View File

@ -8,5 +8,12 @@ module.exports = {
port: 8546, // Port of the blockchain node port: 8546, // Port of the blockchain node
type: "ws" // Type of connection (ws or rpc) 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
}
*/
} }
}; };