diff --git a/lib/contracts/code_generator.js b/lib/contracts/code_generator.js index 46f41629..801a07ac 100644 --- a/lib/contracts/code_generator.js +++ b/lib/contracts/code_generator.js @@ -285,14 +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(); - code += "\n" + fs.readFileSync(fs.embarkPath('js/embarkjs/whisper.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/orbit.js')).toString(); code += this.generateCommunicationInitialization(true); diff --git a/lib/core/engine.js b/lib/core/engine.js index 5a2927e9..7cbc199a 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/lib/core/plugin.js b/lib/core/plugin.js index 94e9a509..3f5bdda6 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 e55850cf..16e68c12 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 76bf90ea..acb3347d 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; diff --git a/js/embarkjs/whisper.js b/lib/modules/whisper/embarkjs.js similarity index 92% rename from js/embarkjs/whisper.js rename to lib/modules/whisper/embarkjs.js index 42bd1a09..dc377a8d 100644 --- a/js/embarkjs/whisper.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) { diff --git a/lib/modules/whisper/index.js b/lib/modules/whisper/index.js new file mode 100644 index 00000000..788e9982 --- /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; diff --git a/script_test.js b/script_test.js deleted file mode 100644 index 03c62741..00000000 --- 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()));