2017-12-28 17:40:11 +00:00
|
|
|
let utils = require('../../utils/utils.js');
|
|
|
|
let fs = require('../../core/fs.js');
|
2018-06-01 21:40:51 +00:00
|
|
|
let Web3 = require('web3');
|
2017-12-28 17:40:11 +00:00
|
|
|
|
|
|
|
class Whisper {
|
|
|
|
|
2018-06-01 23:35:41 +00:00
|
|
|
constructor(embark, _options) {
|
2017-12-28 17:40:11 +00:00
|
|
|
this.logger = embark.logger;
|
|
|
|
this.events = embark.events;
|
2018-05-30 16:26:49 +00:00
|
|
|
this.communicationConfig = embark.config.communicationConfig;
|
2018-06-01 21:40:51 +00:00
|
|
|
this.web3 = new Web3();
|
2017-12-28 17:40:11 +00:00
|
|
|
this.embark = embark;
|
|
|
|
|
2018-06-26 03:34:52 +00:00
|
|
|
if (!this.communicationConfig.enabled) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-06-01 21:40:51 +00:00
|
|
|
this.connectToProvider();
|
2017-12-28 17:40:11 +00:00
|
|
|
this.setServiceCheck();
|
|
|
|
this.addWhisperToEmbarkJS();
|
2017-12-28 23:10:43 +00:00
|
|
|
this.addSetProvider();
|
2017-12-28 17:40:11 +00:00
|
|
|
}
|
|
|
|
|
2018-06-01 21:40:51 +00:00
|
|
|
connectToProvider() {
|
2018-06-01 23:12:54 +00:00
|
|
|
let {host, port} = this.communicationConfig.connection;
|
2018-06-01 21:40:51 +00:00
|
|
|
let web3Endpoint = 'ws://' + host + ':' + port;
|
2018-06-20 06:19:25 +00:00
|
|
|
this.web3.setProvider(new Web3.providers.WebsocketProvider(web3Endpoint, {headers: {Origin: "embark"}}));
|
2018-06-01 21:40:51 +00:00
|
|
|
}
|
|
|
|
|
2017-12-28 17:40:11 +00:00
|
|
|
setServiceCheck() {
|
|
|
|
const self = this;
|
2018-06-01 15:58:11 +00:00
|
|
|
self.events.request("services:register", 'Whisper', function (cb) {
|
2018-06-01 21:40:51 +00:00
|
|
|
if (!self.web3.currentProvider || self.web3.currentProvider.connection.readyState !== 1) {
|
|
|
|
return self.connectToProvider();
|
|
|
|
}
|
2018-01-05 20:30:52 +00:00
|
|
|
self.web3.shh.getVersion(function (err, version) {
|
|
|
|
if (err || version == "2") {
|
2017-12-28 17:40:11 +00:00
|
|
|
return cb({name: 'Whisper', status: 'off'});
|
|
|
|
} else {
|
|
|
|
return cb({name: 'Whisper (version ' + version + ')', status: 'on'});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
addWhisperToEmbarkJS() {
|
2017-12-31 01:42:52 +00:00
|
|
|
const self = this;
|
2017-12-28 23:10:43 +00:00
|
|
|
// TODO: make this a shouldAdd condition
|
2017-12-28 17:40:11 +00:00
|
|
|
if (this.communicationConfig === {}) {
|
|
|
|
return;
|
|
|
|
}
|
2017-12-29 21:11:45 +00:00
|
|
|
if ((this.communicationConfig.available_providers.indexOf('whisper') < 0) && (this.communicationConfig.provider !== 'whisper' || this.communicationConfig.enabled !== true)) {
|
2017-12-28 17:40:11 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-12-31 01:42:52 +00:00
|
|
|
// TODO: possible race condition could be a concern
|
|
|
|
this.events.request("version:get:web3", function(web3Version) {
|
|
|
|
let code = "";
|
2017-12-31 02:02:46 +00:00
|
|
|
code += "\n" + fs.readFileSync(utils.joinPath(__dirname, 'js', 'message_events.js')).toString();
|
2017-12-28 17:40:11 +00:00
|
|
|
|
2017-12-31 01:42:52 +00:00
|
|
|
if (web3Version[0] === "0") {
|
2017-12-31 02:02:46 +00:00
|
|
|
code += "\n" + fs.readFileSync(utils.joinPath(__dirname, 'js', 'embarkjs_old_web3.js')).toString();
|
2017-12-31 01:42:52 +00:00
|
|
|
code += "\nEmbarkJS.Messages.registerProvider('whisper', __embarkWhisperOld);";
|
|
|
|
} else {
|
2017-12-31 02:02:46 +00:00
|
|
|
code += "\n" + fs.readFileSync(utils.joinPath(__dirname, 'js', 'embarkjs.js')).toString();
|
2017-12-31 01:42:52 +00:00
|
|
|
code += "\nEmbarkJS.Messages.registerProvider('whisper', __embarkWhisperNewWeb3);";
|
|
|
|
}
|
|
|
|
self.embark.addCodeToEmbarkJS(code);
|
|
|
|
});
|
2017-12-28 17:40:11 +00:00
|
|
|
}
|
2017-12-28 23:10:43 +00:00
|
|
|
|
|
|
|
addSetProvider() {
|
|
|
|
let connection = this.communicationConfig.connection || {};
|
2018-07-16 14:32:55 +00:00
|
|
|
|
2017-12-28 23:10:43 +00:00
|
|
|
// todo: make the add code a function as well
|
2018-07-16 14:32:55 +00:00
|
|
|
let config = JSON.stringify({
|
2017-12-28 23:10:43 +00:00
|
|
|
server: connection.host || 'localhost',
|
|
|
|
port: connection.port || '8546',
|
|
|
|
type: connection.type || 'ws'
|
2018-07-16 14:32:55 +00:00
|
|
|
});
|
2018-07-16 14:23:11 +00:00
|
|
|
|
2017-12-28 23:10:43 +00:00
|
|
|
let code = "\nEmbarkJS.Messages.setProvider('whisper'," + config + ");";
|
|
|
|
|
|
|
|
let shouldInit = (communicationConfig) => {
|
|
|
|
return (communicationConfig.provider === 'whisper' && communicationConfig.enabled === true);
|
|
|
|
};
|
|
|
|
|
|
|
|
this.embark.addProviderInit('communication', code, shouldInit);
|
|
|
|
}
|
|
|
|
|
2017-12-28 17:40:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = Whisper;
|