mirror of https://github.com/embarklabs/embark.git
Symmetric and private keys can be set in whisper
This commit is contained in:
parent
98dcf6ffe3
commit
57f71d5504
|
@ -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) => {
|
||||
|
|
|
@ -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 = {
|
||||
|
|
|
@ -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
|
||||
}
|
||||
*/
|
||||
}
|
||||
};
|
||||
|
||||
|
|
|
@ -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
|
||||
}
|
||||
*/
|
||||
}
|
||||
};
|
||||
|
|
Loading…
Reference in New Issue