embark/lib/modules/whisper/index.js

197 lines
7.0 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');
let Web3 = require('web3');
2018-08-08 16:35:01 +00:00
const {parallel} = require('async');
const {sendMessage, listenTo} = require('./js/communicationFunctions');
const messageEvents = require('./js/message_events');
2017-12-28 17:40:11 +00:00
2018-07-16 00:36:52 +00:00
const {canonicalHost, defaultHost} = require('../../utils/host');
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;
this.web3 = new Web3();
2017-12-28 17:40:11 +00:00
this.embark = embark;
if (!this.communicationConfig.enabled) {
return;
}
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
}
connectToProvider() {
2018-06-01 23:12:54 +00:00
let {host, port} = this.communicationConfig.connection;
let web3Endpoint = 'ws://' + host + ':' + port;
// Note: dont't pass to the provider things like {headers: {Origin: "embark"}}. Origin header is for browser to fill
// to protect user, it has no meaning if it is used server-side. See here for more details: https://github.com/ethereum/go-ethereum/issues/16608
// Moreover, Parity reject origins that are not urls so if you try to connect with Origin: "embark" it gives the followin error:
// << Blocked connection to WebSockets server from untrusted origin: Some("embark") >>
// The best choice is to use void origin, BUT Geth rejects void origin, so to keep both clients happy we can use http://embark
this.web3.setProvider(new Web3.providers.WebsocketProvider(web3Endpoint, {headers: {Origin: "http://embark"}}));
}
2017-12-28 17:40:11 +00:00
setServiceCheck() {
const self = this;
self.events.request("services:register", 'Whisper', function(cb) {
if (!self.web3.currentProvider || self.web3.currentProvider.connection.readyState !== 1) {
return self.connectToProvider();
}
// 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
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
return self.web3.shh.getInfo(function(err) {
if (err) {
return cb({name: 'Whisper', status: 'off'});
}
// TOFIX Assume Whisper v6 until there's a way to understand it via JSON-RPC
return cb({name: 'Whisper (version 6)', status: 'on'});
});
2018-08-02 19:17:40 +00:00
}
// Assume it is a Geth compliant client
self.web3.shh.getVersion(function(err, version) {
if (err || version == "2") {
return cb({name: 'Whisper', status: 'off'});
}
return cb({name: 'Whisper (version ' + version + ')', status: 'on'});
});
2017-12-28 17:40:11 +00:00
});
});
}
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-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") {
2018-08-08 16:35:55 +00:00
self.isOldWeb3 = true;
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 {
code += "\n" + fs.readFileSync(utils.joinPath(__dirname, 'js', 'communicationFunctions.js')).toString();
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-08-28 13:43:52 +00:00
const shouldInit = (communicationConfig) => {
return (communicationConfig.provider === 'whisper' && communicationConfig.enabled === true);
};
2017-12-28 23:10:43 +00:00
// todo: make the add code a function as well
2018-08-28 13:43:52 +00:00
const config = {
2018-07-16 00:36:52 +00:00
server: canonicalHost(connection.host || defaultHost),
2017-12-28 23:10:43 +00:00
port: connection.port || '8546',
type: connection.type || 'ws'
2018-08-21 23:33:39 +00:00
};
2018-08-28 13:43:52 +00:00
const code = `\nEmbarkJS.Messages.setProvider('whisper', ${JSON.stringify(config)});`;
2017-12-28 23:10:43 +00:00
this.embark.addProviderInit('communication', code, shouldInit);
2018-08-28 13:43:52 +00:00
const consoleConfig = Object.assign({}, config, {providerOptions: {headers: {Origin: "http://embark"}}});
2018-08-28 13:43:52 +00:00
const consoleCode = `\nEmbarkJS.Messages.setProvider('whisper', ${JSON.stringify(consoleConfig)});`;
this.embark.addConsoleProviderInit('communication', consoleCode, shouldInit);
2017-12-28 23:10:43 +00:00
}
2018-08-08 16:35:01 +00:00
registerAPICalls() {
const self = this;
if (self.apiCallsRegistered) {
return;
}
self.apiCallsRegistered = true;
let symKeyID, sig;
parallel([
function(paraCb) {
self.web3.shh.newSymKey((err, id) => {
symKeyID = id;
paraCb(err);
});
},
function(paraCb) {
self.web3.shh.newKeyPair((err, id) => {
sig = id;
paraCb(err);
});
}
], (err) => {
if (err) {
self.logger.error('Error getting Whisper keys:', err.message || err);
return;
}
self.embark.registerAPICall(
'post',
'/embark-api/communication/sendMessage',
(req, res) => {
sendMessage({
topic: req.body.topic,
data: req.body.message,
sig,
symKeyID,
fromAscii: self.web3.utils.asciiToHex,
toHex: self.web3.utils.toHex,
post: self.web3.shh.post
}, (err, result) => {
if (err) {
return res.status(500).send({error: err});
}
res.send(result);
});
});
self.embark.registerAPICall(
'ws',
2018-08-07 13:55:14 +00:00
'/embark-api/communication/listenTo/:topic',
(ws, req) => {
2018-08-08 16:35:01 +00:00
listenTo({
2018-08-07 13:55:14 +00:00
topic: req.params.topic,
2018-08-08 16:35:01 +00:00
messageEvents,
toHex: self.web3.utils.toHex,
toAscii: self.web3.utils.hexToAscii,
sig,
symKeyID,
subscribe: self.web3.shh.subscribe
}, (err, result) => {
if (err) {
return ws.status(500).send(JSON.stringify({error: err}));
}
ws.send(JSON.stringify(result));
});
});
2018-08-08 16:35:55 +00:00
self.embark.registerAPICall(
'get',
'/embark-api/communication/version',
(req, res) => {
res.send(self.isOldWeb3 ? -1 : self.version || 0);
}
);
2018-08-08 16:35:01 +00:00
});
}
2017-12-28 17:40:11 +00:00
}
module.exports = Whisper;