From e833ebd019c36c2ff76d83b41e7b11b7f819f4d8 Mon Sep 17 00:00:00 2001 From: Iuri Matias Date: Thu, 28 Dec 2017 12:16:50 -0500 Subject: [PATCH 1/4] move ipfs embarkjs code to module --- lib/contracts/code_generator.js | 12 +++++++----- lib/core/plugin.js | 6 ++++++ .../ipfs.js => lib/modules/ipfs/embarkjs.js | 9 +++++---- lib/modules/ipfs/index.js | 16 ++++++++++++++++ 4 files changed, 34 insertions(+), 9 deletions(-) rename js/embarkjs/ipfs.js => lib/modules/ipfs/embarkjs.js (95%) diff --git a/lib/contracts/code_generator.js b/lib/contracts/code_generator.js index 46f416291..d8577b5f0 100644 --- a/lib/contracts/code_generator.js +++ b/lib/contracts/code_generator.js @@ -285,13 +285,15 @@ class CodeGenerator { code += "\nimport Web3 from '" + utils.joinPath(fs.embarkPath("js/web3-1.0.min.js")) + "'\n"; code += "\nimport web3 from 'Embark/web3';\n"; - if (this.storageConfig !== {} && this.storageConfig.provider === 'ipfs' && this.storageConfig.enabled === true) { - code += "\nimport IpfsApi from 'ipfs-api';\n"; - } - code += "\n" + embarkjsCode + "\n"; - code += "\n" + fs.readFileSync(fs.embarkPath('js/embarkjs/ipfs.js')).toString(); + let pluginsWithCode = this.plugins.getPluginsFor('embarkjsCode'); + if (pluginsWithCode.length > 0) { + for (let plugin of pluginsWithCode) { + code += plugin.embarkjs_code.join('\n'); + } + } + code += "\n" + fs.readFileSync(fs.embarkPath('js/embarkjs/whisper.js')).toString(); //code += "\n" + fs.readFileSync(fs.embarkPath('js/embarkjs/orbit.js')).toString(); diff --git a/lib/core/plugin.js b/lib/core/plugin.js index 94e9a5099..3f5bdda62 100644 --- a/lib/core/plugin.js +++ b/lib/core/plugin.js @@ -21,6 +21,7 @@ var Plugin = function(options) { this.serviceChecks = []; this.pluginTypes = []; this.uploadCmds = []; + this.embarkjs_code = []; this.logger = options.logger; this.events = options.events; this.config = options.config; @@ -142,6 +143,11 @@ Plugin.prototype.registerUploadCommand = function(cmd, cb) { this.pluginTypes.push('uploadCmds'); }; +Plugin.prototype.addCodeToEmbarkJS = function(code) { + this.embarkjs_code.push(code); + this.pluginTypes.push('embarkjsCode'); +}; + Plugin.prototype.runCommands = function(cmd, options) { return this.console.map(function(cb) { return cb.call(this, cmd, options); diff --git a/js/embarkjs/ipfs.js b/lib/modules/ipfs/embarkjs.js similarity index 95% rename from js/embarkjs/ipfs.js rename to lib/modules/ipfs/embarkjs.js index e55850cfb..16e68c121 100644 --- a/js/embarkjs/ipfs.js +++ b/lib/modules/ipfs/embarkjs.js @@ -1,10 +1,10 @@ +import IpfsApi from 'ipfs-api'; + let __embarkIPFS = {}; __embarkIPFS.setProvider = function(options) { var self = this; var promise = new Promise(function(resolve, reject) { - self.currentStorage = EmbarkJS.Storage.IPFS; - try { if (options === undefined) { self.ipfsConnection = IpfsApi('localhost', '5001'); @@ -26,7 +26,7 @@ __embarkIPFS.setProvider = function(options) { __embarkIPFS.saveText = function(text) { const self = this; var promise = new Promise(function(resolve, reject) { - if (!this.ipfsConnection) { + if (!self.ipfsConnection) { var connectionError = new Error('No IPFS connection. Please ensure to call Embark.Storage.setProvider()'); reject(connectionError); } @@ -43,6 +43,7 @@ __embarkIPFS.saveText = function(text) { }; __embarkIPFS.get = function(hash) { + const self = this; // TODO: detect type, then convert if needed //var ipfsHash = web3.toAscii(hash); var promise = new Promise(function(resolve, reject) { @@ -61,6 +62,7 @@ __embarkIPFS.get = function(hash) { }; __embarkIPFS.uploadFile = function(inputSelector) { + const self = this; var file = inputSelector[0].files[0]; if (file === undefined) { @@ -94,4 +96,3 @@ __embarkIPFS.getUrl = function(hash) { return (self._getUrl || "http://localhost:8080/ipfs/") + hash; }; -EmbarkJS.Storage.registerProvider('ipfs', __embarkIPFS); diff --git a/lib/modules/ipfs/index.js b/lib/modules/ipfs/index.js index 76bf90ea4..acb3347d8 100644 --- a/lib/modules/ipfs/index.js +++ b/lib/modules/ipfs/index.js @@ -1,5 +1,6 @@ let UploadIPFS = require('./upload.js'); let utils = require('../../utils/utils.js'); +let fs = require('../../core/fs.js'); class IPFS { @@ -15,6 +16,7 @@ class IPFS { this.commandlineDeploy(); this.setServiceCheck(); + this.addIPFSToEmbarkJS(); } commandlineDeploy() { @@ -82,6 +84,20 @@ class IPFS { }); } + addIPFSToEmbarkJS() { + if (this.storageConfig === {}) { + return; + } + if(this.storageConfig.provider !== 'ipfs' || this.storageConfig.enabled !== true) { + return; + } + + let code = ""; + code += "\n" + fs.readFileSync(utils.joinPath(__dirname, 'embarkjs.js')).toString(); + code += "\nEmbarkJS.Storage.registerProvider('ipfs', __embarkIPFS);"; + + this.embark.addCodeToEmbarkJS(code); + } } module.exports = IPFS; From 310712567f8c17b8949173cabf072c221ccb611c Mon Sep 17 00:00:00 2001 From: Iuri Matias Date: Thu, 28 Dec 2017 12:40:11 -0500 Subject: [PATCH 2/4] move whisper to a module --- lib/contracts/code_generator.js | 1 - lib/core/engine.js | 12 ++--- .../modules/whisper/embarkjs.js | 0 lib/modules/whisper/index.js | 47 +++++++++++++++++++ 4 files changed, 51 insertions(+), 9 deletions(-) rename js/embarkjs/whisper.js => lib/modules/whisper/embarkjs.js (100%) create mode 100644 lib/modules/whisper/index.js diff --git a/lib/contracts/code_generator.js b/lib/contracts/code_generator.js index d8577b5f0..801a07ac0 100644 --- a/lib/contracts/code_generator.js +++ b/lib/contracts/code_generator.js @@ -294,7 +294,6 @@ class CodeGenerator { } } - code += "\n" + fs.readFileSync(fs.embarkPath('js/embarkjs/whisper.js')).toString(); //code += "\n" + fs.readFileSync(fs.embarkPath('js/embarkjs/orbit.js')).toString(); code += this.generateCommunicationInitialization(true); diff --git a/lib/core/engine.js b/lib/core/engine.js index 5a2927e95..7cbc199a3 100644 --- a/lib/core/engine.js +++ b/lib/core/engine.js @@ -200,14 +200,10 @@ class Engine { } }); - self.servicesMonitor.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'}); - } - }); + this.registerModule('whisper', { + addCheck: this.servicesMonitor.addCheck.bind(this.servicesMonitor), + communicationConfig: this.config.communicationConfig, + web3: this.web3 }); } } diff --git a/js/embarkjs/whisper.js b/lib/modules/whisper/embarkjs.js similarity index 100% rename from js/embarkjs/whisper.js rename to lib/modules/whisper/embarkjs.js diff --git a/lib/modules/whisper/index.js b/lib/modules/whisper/index.js new file mode 100644 index 000000000..788e99821 --- /dev/null +++ b/lib/modules/whisper/index.js @@ -0,0 +1,47 @@ +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(); + } + + 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() { + if (this.communicationConfig === {}) { + return; + } + if(this.communicationConfig.provider !== 'whisper' || this.communicationConfig.enabled !== true) { + return; + } + + let code = ""; + code += "\n" + fs.readFileSync(utils.joinPath(__dirname, 'embarkjs.js')).toString(); + code += "\nEmbarkJS.Storage.registerProvider('ipfs', __embarkWhisper);"; + + this.embark.addCodeToEmbarkJS(code); + } +} + +module.exports = Whisper; From fc24b04bf61f57b621f636eb1dee7d02a2d62191 Mon Sep 17 00:00:00 2001 From: Iuri Matias Date: Thu, 28 Dec 2017 13:13:30 -0500 Subject: [PATCH 3/4] comply with linter --- lib/modules/whisper/embarkjs.js | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/lib/modules/whisper/embarkjs.js b/lib/modules/whisper/embarkjs.js index 42bd1a090..dc377a8d2 100644 --- a/lib/modules/whisper/embarkjs.js +++ b/lib/modules/whisper/embarkjs.js @@ -1,3 +1,5 @@ +/*global EmbarkJS, Web3 */ + let __embarkWhisper = {}; __embarkWhisper.setProvider = function(options) { @@ -18,8 +20,8 @@ __embarkWhisper.setProvider = function(options) { console.log("whisper not available"); } else if (version >= 5) { if (self.web3CompatibleWithV5()) { - self.web3.shh.newSymKey().then((id) => {self.symKeyID = id;}); - self.web3.shh.newKeyPair().then((id) => {self.sig = id;}); + self.web3.shh.newSymKey().then((id) => { self.symKeyID = id; }); + self.web3.shh.newKeyPair().then((id) => { self.sig = id; }); } else { console.log("this version of whisper in this node"); } @@ -32,20 +34,21 @@ __embarkWhisper.setProvider = function(options) { __embarkWhisper.web3CompatibleWithV5 = function() { var _web3 = new Web3(); - if (typeof(_web3.version) === "string") { + if ((typeof _web3.version) === "string") { return true; } return parseInt(_web3.version.api.split('.')[1], 10) >= 20; }; __embarkWhisper.getWhisperVersion = function(cb) { + const self = this; if (EmbarkJS.isNewWeb3()) { this.web3.shh.getVersion(function(err, version) { cb(err, version); }); } else { - this.web3.version.getWhisper(function(err, res) { - cb(err, web3.version.whisper); + this.web3.version.getWhisper(function(err, _res) { + cb(err, self.web3.version.whisper); }); } }; @@ -88,7 +91,7 @@ __embarkWhisper.sendMessage = function(options) { data = options.data || options.payload; ttl = options.ttl || 100; priority = options.priority || 1000; - var identity = options.identity || this.identity || web3.shh.newIdentity(); + var identity = options.identity || this.identity || this.web3.shh.newIdentity(); var _topics; if (topics === undefined) { From 7d88b6b031d3e9c7d97234ac65fe938a8cb13d90 Mon Sep 17 00:00:00 2001 From: Iuri Matias Date: Thu, 28 Dec 2017 13:38:48 -0500 Subject: [PATCH 4/4] remove unused file --- script_test.js | 13 ------------- 1 file changed, 13 deletions(-) delete mode 100644 script_test.js diff --git a/script_test.js b/script_test.js deleted file mode 100644 index 03c627417..000000000 --- a/script_test.js +++ /dev/null @@ -1,13 +0,0 @@ -var Web3 = require('web3'); - -web3 = new Web3(); - -web3.setProvider(new web3.providers.HttpProvider('http://localhost:8545')); - -EmbarkJS = require('./js/embark.js'); - -MyToken = require('./test_app/dist/js/mytoken.js'); - -console.log(MyToken.address); - -MyToken.balanceOf(web3.eth.accounts[0]).then((x) => console.log(x.toNumber()));