embark-area-51/lib/modules/whisper/js/embarkjs.js

100 lines
2.6 KiB
JavaScript
Raw Normal View History

2017-12-31 02:02:46 +00:00
/*global EmbarkJS, Web3, __MessageEvents */
2017-12-28 18:13:30 +00:00
// for the whisper v5 and web3.js 1.0
let __embarkWhisperNewWeb3 = {};
__embarkWhisperNewWeb3.setProvider = function(options) {
const self = this;
let provider;
if (options === undefined) {
provider = "localhost:8546";
} else {
provider = options.server + ':' + options.port;
}
// TODO: take into account type
self.web3 = new Web3(new Web3.providers.WebsocketProvider("ws://" + provider));
self.getWhisperVersion(function(err, version) {
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; });
} else {
throw new Error("version of whisper not supported");
}
self.whisperVersion = self.web3.version.whisper;
});
};
__embarkWhisperNewWeb3.sendMessage = function(options) {
var topics, data, ttl, payload;
topics = options.topic || options.topics;
data = options.data || options.payload;
ttl = options.ttl || 100;
var powTime = options.powTime || 3;
var powTarget = options.powTarget || 0.5;
if (topics === undefined) {
throw new Error("missing option: topic");
}
if (data === undefined) {
throw new Error("missing option: data");
}
topics = this.web3.utils.toHex(topics).slice(0, 10);
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,
payload: EmbarkJS.Utils.fromAscii(payload),
powTime: powTime,
powTarget: powTarget
};
this.web3.shh.post(message, function() { });
};
__embarkWhisperNewWeb3.listenTo = function(options) {
2017-12-31 02:02:46 +00:00
var topics = options.topic || options.topics;
2017-12-31 02:02:46 +00:00
let promise = new __MessageEvents();
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));
}
let filter = this.web3.shh.subscribe("messages", {
symKeyID: this.symKeyID,
topics: topics
}).on('data', function(result) {
var payload = JSON.parse(EmbarkJS.Utils.toAscii(result.payload));
var data;
data = {
topic: result.topic,
data: payload,
//from: result.from,
time: result.timestamp
};
promise.cb(payload, data, result);
});
promise.filter = filter;
return promise;
};
2017-12-28 01:19:41 +00:00
__embarkWhisperNewWeb3.getWhisperVersion = function(cb) {
this.web3.shh.getVersion(function(err, version) {
cb(err, version);
});
};