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

77 lines
2.4 KiB
JavaScript
Raw Normal View History

2017-12-28 17:40:11 +00:00
let utils = require('../../utils/utils.js');
let fs = require('../../core/fs.js');
class Whisper {
constructor(embark, options) {
this.logger = embark.logger;
this.events = embark.events;
this.communicationConfig = options.communicationConfig;
this.addCheck = options.addCheck;
this.web3 = options.web3;
this.embark = embark;
this.setServiceCheck();
this.addWhisperToEmbarkJS();
2017-12-28 23:10:43 +00:00
this.addSetProvider();
2017-12-28 17:40:11 +00:00
}
setServiceCheck() {
const self = this;
self.addCheck('Whisper', function (cb) {
self.web3.version.getWhisper(function (err, version) {
if (err) {
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;
}
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-28 17:40:11 +00:00
2017-12-31 01:42:52 +00:00
if (web3Version[0] === "0") {
code += "\n" + fs.readFileSync(utils.joinPath(__dirname, 'embarkjs_old_web3.js')).toString();
code += "\nEmbarkJS.Messages.registerProvider('whisper', __embarkWhisperOld);";
} else {
code += "\n" + fs.readFileSync(utils.joinPath(__dirname, 'embarkjs.js')).toString();
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 || {};
// todo: make the add code a function as well
let config = JSON.stringify({
server: connection.host || 'localhost',
port: connection.port || '8546',
type: connection.type || 'ws'
});
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;