From 83c01cffdc91bbe9586603000a05aaea41f97642 Mon Sep 17 00:00:00 2001 From: Anthony Laibe Date: Tue, 28 Aug 2018 14:43:52 +0100 Subject: [PATCH] Storage and Whisper connect --- lib/core/engine.js | 1 + lib/core/modules/coderunner/codeRunner.js | 23 +++++++++++++- lib/core/plugin.js | 7 +++++ lib/modules/code_generator/index.js | 38 +++++------------------ lib/modules/ens/index.js | 1 + lib/modules/storage/index.js | 1 + lib/modules/whisper/index.js | 17 +++++----- lib/modules/whisper/js/embarkjs.js | 2 +- 8 files changed, 49 insertions(+), 41 deletions(-) diff --git a/lib/core/engine.js b/lib/core/engine.js index 3c54aa36..acba25cc 100644 --- a/lib/core/engine.js +++ b/lib/core/engine.js @@ -132,6 +132,7 @@ class Engine { codeRunnerService(_options) { const CodeRunner = require('./modules/coderunner/codeRunner.js'); this.codeRunner = new CodeRunner({ + config: this.config, plugins: this.plugins, events: this.events, logger: this.logger, diff --git a/lib/core/modules/coderunner/codeRunner.js b/lib/core/modules/coderunner/codeRunner.js index be1bb964..1e98850b 100644 --- a/lib/core/modules/coderunner/codeRunner.js +++ b/lib/core/modules/coderunner/codeRunner.js @@ -5,6 +5,7 @@ const Web3 = require('web3'); class CodeRunner { constructor(options) { + this.config = options.config; this.plugins = options.plugins; this.logger = options.logger; this.events = options.events; @@ -35,8 +36,28 @@ class CodeRunner { this.runCode.registerVar('Web3', Web3); this.runCode.registerVar('EmbarkJS', EmbarkJS); this.events.on('code-generator-ready', () => { - this.events.request('code-generator:embarkjs:initialization-code', (code) => { + this.events.request('code-generator:embarkjs:provider-code', (code) => { this.runCode.doEval(code); + const codeTypes = { + 'communication': this.config.communicationConfig || {}, + 'names': this.config.namesystemConfig || {}, + 'storage': this.config.storageConfig || {} + }; + + let initProvidersCode = ''; + let initCodes = this.plugins.getPluginsFor('initConsoleCode'); + for (let plugin of initCodes) { + for (let codeTypeName of Object.keys(codeTypes)) { + let initCodes = plugin.embarkjs_init_console_code[codeTypeName] || []; + for (let initCode of initCodes) { + let [block, shouldInit] = initCode; + if (shouldInit.call(plugin, codeTypes[codeTypeName])) { + initProvidersCode += block; + } + } + } + } + this.runCode.doEval(initProvidersCode); }); }); } diff --git a/lib/core/plugin.js b/lib/core/plugin.js index 80e14ec0..70957ffa 100644 --- a/lib/core/plugin.js +++ b/lib/core/plugin.js @@ -25,6 +25,7 @@ var Plugin = function(options) { this.imports = []; this.embarkjs_code = []; this.embarkjs_init_code = {}; + this.embarkjs_init_console_code = {}; this.afterContractsDeployActions = []; this.onDeployActions = []; this.eventActions = {}; @@ -189,6 +190,12 @@ Plugin.prototype.addProviderInit = function(providerType, code, initCondition) { this.addPluginType('initCode'); }; +Plugin.prototype.addConsoleProviderInit = function(providerType, code, initCondition) { + this.embarkjs_init_console_code[providerType] = this.embarkjs_init_console_code[providerType] || []; + this.embarkjs_init_console_code[providerType].push([code, initCondition]); + this.addPluginType('initConsoleCode'); +}; + Plugin.prototype.registerImportFile = function(importName, importLocation) { this.imports.push([importName, importLocation]); this.addPluginType('imports'); diff --git a/lib/modules/code_generator/index.js b/lib/modules/code_generator/index.js index 367ee46d..cd5dfdf8 100644 --- a/lib/modules/code_generator/index.js +++ b/lib/modules/code_generator/index.js @@ -99,8 +99,8 @@ class CodeGenerator { self.buildPlaceholderPage(cb); }); - self.events.setCommandHandler('code-generator:embarkjs:initialization-code', (cb) => { - cb(self.getEmbarkJsInitializationCode()); + self.events.setCommandHandler('code-generator:embarkjs:provider-code', (cb) => { + cb(self.getEmbarkJsProviderCode()); }); } @@ -321,11 +321,8 @@ class CodeGenerator { }, function getJSCode(next) { code += "\n" + embarkjsCode + "\n"; - let pluginsWithCode = self.plugins.getPluginsFor('embarkjsCode'); - for (let plugin of pluginsWithCode) { - code += plugin.embarkjs_code.join('\n'); - } + code += self.getEmbarkJsProviderCode(); code += self.generateCommunicationInitialization(true); code += self.generateStorageInitialization(true); code += self.generateNamesInitialization(true); @@ -342,31 +339,10 @@ class CodeGenerator { }); } - getEmbarkJsInitializationCode() { - let code = ''; - let embarkjsCodes = this.plugins.getPluginsFor('embarkjsCode'); - for (let plugin of embarkjsCodes) { - code += plugin.embarkjs_code.join('\n'); - } - - const codeTypes = {'communication': this.communicationConfig, - 'names': this.namesystemConfig, - 'storage': this.storageConfig}; - - let initCodes = this.plugins.getPluginsFor('initCode'); - for (let plugin of initCodes) { - for (let codeTypeName of Object.keys(codeTypes)) { - let initCodes = plugin.embarkjs_init_code[codeTypeName] || []; - for (let initCode of initCodes) { - let [block, shouldInit] = initCode; - if (shouldInit.call(plugin, codeTypes[codeTypeName])) { - code += block; - } - } - } - } - - return code; + getEmbarkJsProviderCode() { + return this.plugins.getPluginsFor('embarkjsCode').reduce((code, plugin) => ( + code += plugin.embarkjs_code.join('\n') + ), ''); } buildContractJS(contractName, contractJSON, cb) { diff --git a/lib/modules/ens/index.js b/lib/modules/ens/index.js index 1cae88a3..23fa652e 100644 --- a/lib/modules/ens/index.js +++ b/lib/modules/ens/index.js @@ -314,6 +314,7 @@ class ENS { }; this.embark.addProviderInit('names', code, shouldInit); + this.embark.addConsoleProviderInit('names', code, shouldInit); } } diff --git a/lib/modules/storage/index.js b/lib/modules/storage/index.js index c6bbb32f..18643436 100644 --- a/lib/modules/storage/index.js +++ b/lib/modules/storage/index.js @@ -35,6 +35,7 @@ class Storage { }; this.embark.addProviderInit('storage', code, shouldInit); + this.embark.addConsoleProviderInit('storage', code, shouldInit); } } diff --git a/lib/modules/whisper/index.js b/lib/modules/whisper/index.js index 7b4a6dfb..2211d589 100644 --- a/lib/modules/whisper/index.js +++ b/lib/modules/whisper/index.js @@ -73,21 +73,22 @@ class Whisper { addSetProvider() { let connection = this.communicationConfig.connection || {}; + const shouldInit = (communicationConfig) => { + return (communicationConfig.provider === 'whisper' && communicationConfig.enabled === true); + }; // todo: make the add code a function as well - let config = { + const config = { server: canonicalHost(connection.host || defaultHost), port: connection.port || '8546', type: connection.type || 'ws' }; - - let code = `\nEmbarkJS.Messages.setProvider('whisper', ${JSON.stringify(config)});`; - - let shouldInit = (communicationConfig) => { - return (communicationConfig.provider === 'whisper' && communicationConfig.enabled === true); - }; - + const code = `\nEmbarkJS.Messages.setProvider('whisper', ${JSON.stringify(config)});`; this.embark.addProviderInit('communication', code, shouldInit); + + const consoleConfig = Object.assign({}, config, {providerOptions: {headers: {Origin: "embark"}}}); + const consoleCode = `\nEmbarkJS.Messages.setProvider('whisper', ${JSON.stringify(consoleConfig)});`; + this.embark.addConsoleProviderInit('communication', consoleCode, shouldInit); } } diff --git a/lib/modules/whisper/js/embarkjs.js b/lib/modules/whisper/js/embarkjs.js index ea72b9d0..7796b3cc 100644 --- a/lib/modules/whisper/js/embarkjs.js +++ b/lib/modules/whisper/js/embarkjs.js @@ -12,7 +12,7 @@ __embarkWhisperNewWeb3.setProvider = function (options) { provider = options.server + ':' + options.port; } // TODO: take into account type - self.web3 = new Web3(new Web3.providers.WebsocketProvider("ws://" + provider)); + self.web3 = new Web3(new Web3.providers.WebsocketProvider("ws://" + provider, options.providerOptions)); self.getWhisperVersion(function (err, version) { if (err) { console.log("whisper not available");