Allow topics to be optional when pubKey/privKey is used

This commit is contained in:
Richard Ramos 2018-07-16 12:18:31 -04:00
parent b9378762ff
commit 34ebd42be9

View File

@ -32,33 +32,34 @@ __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");
} }
if (topics) {
topics = this.web3.utils.toHex(topics).slice(0, 10); topics = this.web3.utils.toHex(topics).slice(0, 10);
}
payload = JSON.stringify(data); payload = JSON.stringify(data);
let message = { let message = {
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) { if (options.pubKey) {
message.pubKey = options.pubKey; // encrypt using a given pubKey message.pubKey = options.pubKey; // encrypt using a given pubKey
} else if(options.symKeyID) { } else if(options.symKeyID) {
@ -67,24 +68,29 @@ __embarkWhisperNewWeb3.sendMessage = function (options) {
message.symKeyID = this.symKeyID; // encrypts using the sym key ID 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();
let subOptions = {};
if(topics){
if (typeof topics === 'string') { if (typeof topics === 'string') {
topics = [this.web3.utils.toHex(topics).slice(0, 10)]; topics = [this.web3.utils.toHex(topics).slice(0, 10)];
} else { } else {
topics = topics.map((t) => this.web3.utils.toHex(t).slice(0, 10)); topics = topics.map((t) => this.web3.utils.toHex(t).slice(0, 10));
} }
subOptions.topics = topics;
let subOptions = { }
topics: topics
};
if (options.minPow) { if (options.minPow) {
subOptions.minPow = options.minPow; subOptions.minPow = options.minPow;