2018-08-06 17:30:56 +00:00
|
|
|
/*global EmbarkJS, Web3, __MessageEvents, sendMessage, listenTo*/
|
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-10-06 16:05:37 +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-09-22 12:59:19 +00:00
|
|
|
self.web3.currentProvider.on('connect', () => {
|
2018-10-06 16:05:37 +00:00
|
|
|
self.getWhisperVersion(function(err, version) {
|
2018-09-22 12:59:19 +00:00
|
|
|
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;
|
|
|
|
});
|
|
|
|
});
|
|
|
|
self.web3.currentProvider.on('error', () => {
|
|
|
|
console.log("whisper not available");
|
2017-12-31 01:09:46 +00:00
|
|
|
});
|
2017-12-28 00:10:05 +00:00
|
|
|
};
|
|
|
|
|
2018-10-06 16:05:37 +00:00
|
|
|
__embarkWhisperNewWeb3.sendMessage = function(options) {
|
2018-08-06 17:30:56 +00:00
|
|
|
const data = options.data || options.payload;
|
|
|
|
if (!data) {
|
2017-12-31 01:09:46 +00:00
|
|
|
throw new Error("missing option: data");
|
|
|
|
}
|
2018-08-06 17:30:56 +00:00
|
|
|
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
|
2018-05-01 13:38:13 +00:00
|
|
|
});
|
2017-12-28 00:10:05 +00:00
|
|
|
|
2018-08-06 17:30:56 +00:00
|
|
|
sendMessage(options, (err) => {
|
|
|
|
if (err) {
|
|
|
|
throw new Error(err);
|
2018-07-16 16:18:31 +00:00
|
|
|
}
|
2018-08-06 17:30:56 +00:00
|
|
|
});
|
|
|
|
};
|
2017-12-28 00:10:05 +00:00
|
|
|
|
2018-08-06 17:30:56 +00:00
|
|
|
__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);
|
2017-12-28 00:10:05 +00:00
|
|
|
};
|
2017-12-28 01:19:41 +00:00
|
|
|
|
2018-10-06 16:05:37 +00:00
|
|
|
__embarkWhisperNewWeb3.getWhisperVersion = function(cb) {
|
|
|
|
// 1) Parity does not implement shh_version JSON-RPC method
|
|
|
|
// 2) web3 1.0 still does not implement web3_clientVersion
|
|
|
|
// so we must do all by our own
|
|
|
|
const self = this;
|
|
|
|
self.web3._requestManager.send({method: 'web3_clientVersion', params: []}, (err, clientVersion) => {
|
|
|
|
if (err) return cb(err);
|
|
|
|
if (clientVersion.indexOf("Parity-Ethereum//v2") === 0) {
|
|
|
|
// This is Parity
|
|
|
|
self.web3.shh.getInfo(function(err) {
|
|
|
|
if (err) {
|
|
|
|
return cb(err, 0);
|
|
|
|
}
|
|
|
|
// TOFIX Assume Whisper v6 until there's a way to understand it via JSON-RPC
|
|
|
|
return cb(err, 6);
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
// Assume it is a Geth compliant client
|
|
|
|
self.web3.shh.getVersion(function(err, version) {
|
|
|
|
cb(err, version);
|
|
|
|
});
|
|
|
|
}
|
2017-12-31 01:09:46 +00:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2018-10-06 16:05:37 +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
|
|
|
});
|
|
|
|
};
|
|
|
|
|