2017-12-31 02:02:46 +00:00
|
|
|
/*global EmbarkJS, Web3, __MessageEvents */
|
2017-12-28 18:13:30 +00:00
|
|
|
|
2017-12-31 01:09:46 +00:00
|
|
|
// for the whisper v5 and web3.js 1.0
|
|
|
|
let __embarkWhisperNewWeb3 = {};
|
|
|
|
|
2018-05-01 13:38:13 +00:00
|
|
|
__embarkWhisperNewWeb3.setProvider = function (options) {
|
2017-12-31 01:09:46 +00:00
|
|
|
const self = this;
|
|
|
|
let provider;
|
|
|
|
if (options === undefined) {
|
|
|
|
provider = "localhost:8546";
|
|
|
|
} else {
|
|
|
|
provider = options.server + ':' + options.port;
|
2017-12-28 00:10:05 +00:00
|
|
|
}
|
2017-12-31 01:09:46 +00:00
|
|
|
// TODO: take into account type
|
2018-08-28 13:43:52 +00:00
|
|
|
self.web3 = new Web3(new Web3.providers.WebsocketProvider("ws://" + provider, options.providerOptions));
|
2018-05-01 13:38:13 +00:00
|
|
|
self.getWhisperVersion(function (err, version) {
|
2017-12-31 01:09:46 +00:00
|
|
|
if (err) {
|
|
|
|
console.log("whisper not available");
|
|
|
|
} else if (version >= 5) {
|
2018-07-16 14:32:55 +00:00
|
|
|
self.web3.shh.newSymKey().then((id) => {
|
|
|
|
self.symKeyID = id;
|
|
|
|
});
|
|
|
|
self.web3.shh.newKeyPair().then((id) => {
|
|
|
|
self.sig = id;
|
|
|
|
});
|
2017-12-31 01:09:46 +00:00
|
|
|
} else {
|
|
|
|
throw new Error("version of whisper not supported");
|
|
|
|
}
|
|
|
|
self.whisperVersion = self.web3.version.whisper;
|
|
|
|
});
|
2017-12-28 00:10:05 +00:00
|
|
|
};
|
|
|
|
|
2018-05-01 13:38:13 +00:00
|
|
|
__embarkWhisperNewWeb3.sendMessage = function (options) {
|
2017-12-31 01:09:46 +00:00
|
|
|
var topics, data, ttl, payload;
|
2018-07-16 16:18:31 +00:00
|
|
|
topics = options.topic;
|
2017-12-31 01:09:46 +00:00
|
|
|
data = options.data || options.payload;
|
|
|
|
ttl = options.ttl || 100;
|
|
|
|
var powTime = options.powTime || 3;
|
|
|
|
var powTarget = options.powTarget || 0.5;
|
2017-12-28 00:10:05 +00:00
|
|
|
|
2017-12-31 01:09:46 +00:00
|
|
|
if (data === undefined) {
|
|
|
|
throw new Error("missing option: data");
|
|
|
|
}
|
2017-12-28 00:10:05 +00:00
|
|
|
|
2018-07-16 16:18:31 +00:00
|
|
|
if (topics) {
|
|
|
|
topics = this.web3.utils.toHex(topics).slice(0, 10);
|
|
|
|
}
|
2017-12-28 00:10:05 +00:00
|
|
|
|
2017-12-31 01:09:46 +00:00
|
|
|
payload = JSON.stringify(data);
|
2017-12-28 00:10:05 +00:00
|
|
|
|
2017-12-31 01:09:46 +00:00
|
|
|
let message = {
|
|
|
|
sig: this.sig, // signs the message using the keyPair ID
|
|
|
|
ttl: ttl,
|
|
|
|
payload: EmbarkJS.Utils.fromAscii(payload),
|
|
|
|
powTime: powTime,
|
|
|
|
powTarget: powTarget
|
|
|
|
};
|
2017-12-28 00:10:05 +00:00
|
|
|
|
2018-07-16 16:18:31 +00:00
|
|
|
if (topics) {
|
|
|
|
message.topic = topics;
|
|
|
|
}
|
|
|
|
|
2018-07-16 14:23:11 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2018-07-16 16:18:31 +00:00
|
|
|
if (topics === undefined && message.symKeyID && !message.pubKey) {
|
|
|
|
throw new Error("missing option: topic");
|
|
|
|
}
|
|
|
|
|
2018-05-01 13:38:13 +00:00
|
|
|
this.web3.shh.post(message, function () {
|
|
|
|
});
|
2017-12-31 01:09:46 +00:00
|
|
|
};
|
2017-12-28 00:10:05 +00:00
|
|
|
|
2018-05-02 16:10:11 +00:00
|
|
|
__embarkWhisperNewWeb3.listenTo = function (options, callback) {
|
2018-07-16 16:18:31 +00:00
|
|
|
var topics = options.topic;
|
2017-12-28 00:10:05 +00:00
|
|
|
|
2017-12-31 02:02:46 +00:00
|
|
|
let promise = new __MessageEvents();
|
2017-12-28 00:10:05 +00:00
|
|
|
|
2018-07-16 16:18:31 +00:00
|
|
|
let subOptions = {};
|
2017-12-28 00:10:05 +00:00
|
|
|
|
2018-07-16 16:18:31 +00:00
|
|
|
if(topics){
|
|
|
|
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;
|
|
|
|
}
|
2018-07-16 14:23:11 +00:00
|
|
|
|
|
|
|
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) {
|
2017-12-31 01:09:46 +00:00
|
|
|
var payload = JSON.parse(EmbarkJS.Utils.toAscii(result.payload));
|
|
|
|
var data;
|
|
|
|
data = {
|
2018-05-02 16:10:11 +00:00
|
|
|
topic: EmbarkJS.Utils.toAscii(result.topic),
|
2017-12-31 01:09:46 +00:00
|
|
|
data: payload,
|
|
|
|
//from: result.from,
|
|
|
|
time: result.timestamp
|
2017-12-28 00:10:05 +00:00
|
|
|
};
|
|
|
|
|
2018-05-02 16:10:11 +00:00
|
|
|
if (callback) {
|
|
|
|
return callback(null, data);
|
|
|
|
}
|
2017-12-31 01:09:46 +00:00
|
|
|
promise.cb(payload, data, result);
|
|
|
|
});
|
2017-12-28 00:10:05 +00:00
|
|
|
|
2017-12-31 01:09:46 +00:00
|
|
|
promise.filter = filter;
|
2017-12-28 00:10:05 +00:00
|
|
|
|
2017-12-31 01:09:46 +00:00
|
|
|
return promise;
|
2017-12-28 00:10:05 +00:00
|
|
|
};
|
2017-12-28 01:19:41 +00:00
|
|
|
|
2018-05-01 13:38:13 +00:00
|
|
|
__embarkWhisperNewWeb3.getWhisperVersion = function (cb) {
|
|
|
|
this.web3.shh.getVersion(function (err, version) {
|
2017-12-31 01:09:46 +00:00
|
|
|
cb(err, version);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2018-05-01 13:38:13 +00:00
|
|
|
__embarkWhisperNewWeb3.isAvailable = function () {
|
2018-04-30 05:56:43 +00:00
|
|
|
return new Promise((resolve, reject) => {
|
2018-05-01 13:38:13 +00:00
|
|
|
if (!this.web3.shh) {
|
|
|
|
return resolve(false);
|
|
|
|
}
|
|
|
|
try {
|
2018-04-30 05:56:43 +00:00
|
|
|
this.getWhisperVersion((err) => {
|
|
|
|
resolve(Boolean(!err));
|
|
|
|
});
|
|
|
|
}
|
2018-05-01 13:38:13 +00:00
|
|
|
catch (err) {
|
|
|
|
reject(err);
|
|
|
|
}
|
2018-04-30 05:56:43 +00:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|