mirror of https://github.com/embarklabs/embark.git
Merge pull request #630 from embark-framework/whisper-options
Extending options of sendMessage() and listenTo() of whisper to enter specific keys.
This commit is contained in:
commit
e61b38e3a4
|
@ -71,12 +71,14 @@ class Whisper {
|
||||||
|
|
||||||
addSetProvider() {
|
addSetProvider() {
|
||||||
let connection = this.communicationConfig.connection || {};
|
let connection = this.communicationConfig.connection || {};
|
||||||
|
|
||||||
// todo: make the add code a function as well
|
// todo: make the add code a function as well
|
||||||
let config = JSON.stringify({
|
let config = JSON.stringify({
|
||||||
server: connection.host || 'localhost',
|
server: connection.host || 'localhost',
|
||||||
port: connection.port || '8546',
|
port: connection.port || '8546',
|
||||||
type: connection.type || 'ws'
|
type: connection.type || 'ws'
|
||||||
});
|
});
|
||||||
|
|
||||||
let code = "\nEmbarkJS.Messages.setProvider('whisper'," + config + ");";
|
let code = "\nEmbarkJS.Messages.setProvider('whisper'," + config + ");";
|
||||||
|
|
||||||
let shouldInit = (communicationConfig) => {
|
let shouldInit = (communicationConfig) => {
|
||||||
|
|
|
@ -32,53 +32,86 @@ __embarkWhisperNewWeb3.setProvider = function (options) {
|
||||||
|
|
||||||
__embarkWhisperNewWeb3.sendMessage = function (options) {
|
__embarkWhisperNewWeb3.sendMessage = function (options) {
|
||||||
var topics, data, ttl, payload;
|
var topics, data, ttl, payload;
|
||||||
topics = options.topic || options.topics;
|
topics = options.topic;
|
||||||
data = options.data || options.payload;
|
data = options.data || options.payload;
|
||||||
ttl = options.ttl || 100;
|
ttl = options.ttl || 100;
|
||||||
var powTime = options.powTime || 3;
|
var powTime = options.powTime || 3;
|
||||||
var powTarget = options.powTarget || 0.5;
|
var powTarget = options.powTarget || 0.5;
|
||||||
|
|
||||||
if (topics === undefined) {
|
|
||||||
throw new Error("missing option: topic");
|
|
||||||
}
|
|
||||||
|
|
||||||
if (data === undefined) {
|
if (data === undefined) {
|
||||||
throw new Error("missing option: data");
|
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);
|
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,
|
|
||||||
payload: EmbarkJS.Utils.fromAscii(payload),
|
payload: EmbarkJS.Utils.fromAscii(payload),
|
||||||
powTime: powTime,
|
powTime: powTime,
|
||||||
powTarget: powTarget
|
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 () {
|
this.web3.shh.post(message, function () {
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
__embarkWhisperNewWeb3.listenTo = function (options, callback) {
|
__embarkWhisperNewWeb3.listenTo = function (options, callback) {
|
||||||
var topics = options.topic || options.topics;
|
var topics = options.topic;
|
||||||
|
|
||||||
let promise = new __MessageEvents();
|
let promise = new __MessageEvents();
|
||||||
|
|
||||||
if (typeof topics === 'string') {
|
let subOptions = {};
|
||||||
topics = [this.web3.utils.toHex(topics).slice(0, 10)];
|
|
||||||
} else {
|
if(topics){
|
||||||
topics = topics.map((t) => this.web3.utils.toHex(t).slice(0, 10));
|
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;
|
||||||
}
|
}
|
||||||
|
|
||||||
let filter = this.web3.shh.subscribe("messages", {
|
if (options.minPow) {
|
||||||
symKeyID: this.symKeyID,
|
subOptions.minPow = options.minPow;
|
||||||
topics: topics
|
}
|
||||||
}).on('data', function (result) {
|
|
||||||
|
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 = {
|
||||||
|
|
Loading…
Reference in New Issue