/*global EmbarkJS, Web3, __MessageEvents, sendMessage, listenTo*/ // 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) { const data = options.data || options.payload; if (!data) { throw new Error("missing option: data"); } Object.assign(options, { sig: this.sig, fromAscii: EmbarkJS.Utils.fromAscii, toHex: this.web3.utils.toHex, symKeyID: options.symKeyID || this.symKeyID, post: this.web3.shh.post, data }); sendMessage(options, (err) => { if (err) { throw new Error(err); } }); }; __embarkWhisperNewWeb3.listenTo = function (options, callback) { Object.assign(options, { sig: this.sig, toAscii: EmbarkJS.Utils.toAscii, toHex: this.web3.utils.toHex, symKeyID: options.symKeyID || this.symKeyID, messageEvents: __MessageEvents, subscribe: this.web3.shh.subscribe }); listenTo(options, callback); }; __embarkWhisperNewWeb3.getWhisperVersion = function (cb) { this.web3.shh.getVersion(function (err, version) { cb(err, version); }); }; __embarkWhisperNewWeb3.isAvailable = function () { return new Promise((resolve, reject) => { if (!this.web3.shh) { return resolve(false); } try { this.getWhisperVersion((err) => { resolve(Boolean(!err)); }); } catch (err) { reject(err); } }); };