From b0e0989359e9244be0ebc720c9f5272a9cf0978b Mon Sep 17 00:00:00 2001 From: Anthony Laibe Date: Wed, 22 Aug 2018 10:11:57 +0100 Subject: [PATCH] Move ipns function to ipfs --- lib/modules/ens/embarkjs.js | 4 - lib/modules/ipfs/embarkjs.js | 154 +++++++++++++++++++++++ lib/modules/ipfs/embarkjs/default.js | 46 ------- lib/modules/ipfs/embarkjs/name.js | 43 ------- lib/modules/ipfs/embarkjs/storage.js | 75 ----------- lib/modules/ipfs/index.js | 45 +------ lib/modules/swarm/embarkjs.js | 9 ++ lib/modules/whisper/index.js | 8 +- templates/demo/app/components/storage.js | 8 +- templates/demo/app/dapp.js | 16 +-- templates/demo/config/namesystem.js | 2 +- 11 files changed, 181 insertions(+), 229 deletions(-) create mode 100644 lib/modules/ipfs/embarkjs.js delete mode 100644 lib/modules/ipfs/embarkjs/default.js delete mode 100644 lib/modules/ipfs/embarkjs/name.js delete mode 100644 lib/modules/ipfs/embarkjs/storage.js diff --git a/lib/modules/ens/embarkjs.js b/lib/modules/ens/embarkjs.js index cac08229..3a8418da 100644 --- a/lib/modules/ens/embarkjs.js +++ b/lib/modules/ens/embarkjs.js @@ -247,7 +247,3 @@ __embarkENS.registerSubDomain = function (name, address, callback) { __embarkENS.isAvailable = function () { return Boolean(this.isAvailable); }; - -__embarkENS.register = function () { - return new Error("Not available with ENS"); -}; diff --git a/lib/modules/ipfs/embarkjs.js b/lib/modules/ipfs/embarkjs.js new file mode 100644 index 00000000..bf81a6f5 --- /dev/null +++ b/lib/modules/ipfs/embarkjs.js @@ -0,0 +1,154 @@ +import IpfsApi from 'ipfs-api'; + +let __embarkIPFS = {}; + +const NoConnectionError = 'No IPFS connection. Please ensure to call Embark.Storage.setProvider()'; + +__embarkIPFS.setProvider = function (options) { + var self = this; + return new Promise(function (resolve, reject) { + try { + if (!options) { + self._config = options; + self._ipfsConnection = IpfsApi('localhost', '5001'); + self._getUrl = "http://localhost:8080/ipfs/"; + } else { + var ipfsOptions = {host: options.host || options.server, protocol: 'http'}; + if (options.protocol) { + ipfsOptions.protocol = options.protocol; + } + if (options.port && options.port !== 'false') { + ipfsOptions.port = options.port; + } + self._ipfsConnection = IpfsApi(ipfsOptions); + self._getUrl = options.getUrl || "http://localhost:8080/ipfs/"; + } + resolve(self); + } catch (err) { + console.error(err); + self._ipfsConnection = null; + reject(new Error('Failed to connect to IPFS')); + } + }); +}; + +__embarkIPFS.isAvailable = function () { + return new Promise((resolve) => { + if (!this._ipfsConnection) { + return resolve(false); + } + this._ipfsConnection.id() + .then((id) => { + resolve(Boolean(id)); + }) + .catch((err) => { + console.error(err); + resolve(false); + }); + }); +}; + +__embarkIPFS.saveText = function (text) { + const self = this; + var promise = new Promise(function (resolve, reject) { + if (!self._ipfsConnection) { + var connectionError = new Error(NoConnectionError); + reject(connectionError); + } + self._ipfsConnection.add(self._ipfsConnection.Buffer.from(text), function (err, result) { + if (err) { + reject(err); + } else { + resolve(result[0].path); + } + }); + }); + + return promise; +}; + +__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) { + if (!self._ipfsConnection) { + var connectionError = new Error(NoConnectionError); + reject(connectionError); + } + self._ipfsConnection.get(hash, function (err, files) { + if (err) { + return reject(err); + } + resolve(files[0].content.toString()); + }); + }); + + return promise; +}; + +__embarkIPFS.uploadFile = function (inputSelector) { + const self = this; + var file = inputSelector[0].files[0]; + + if (file === undefined) { + throw new Error('no file found'); + } + + var promise = new Promise(function (resolve, reject) { + if (!self._ipfsConnection) { + var connectionError = new Error(NoConnectionError); + reject(connectionError); + } + var reader = new FileReader(); + reader.onloadend = function () { + var fileContent = reader.result; + var buffer = self._ipfsConnection.Buffer.from(fileContent); + self._ipfsConnection.add(buffer, function (err, result) { + if (err) { + reject(err); + } else { + resolve(result[0].path); + } + }); + }; + reader.readAsArrayBuffer(file); + }); + + return promise; +}; + +__embarkIPFS.getUrl = function (hash) { + return (this._getUrl || "http://localhost:8080/ipfs/") + hash; +}; + +__embarkIPFS.resolve = function (name, callback) { + callback = callback || function () {}; + if (!this._ipfsConnection) { + var connectionError = new Error(NoConnectionError); + return callback(connectionError); + } + + this._ipfsConnection.name.resolve(name) + .then(res => { + callback(null, res.Path); + }) + .catch(() => { + callback(name + " is not registered"); + }); +}; + +__embarkIPFS.register = function(addr, callback) { + callback = callback || function () {}; + if (!this._ipfsConnection) { + return new Error(NoConnectionError); + } + + this._ipfsConnection.name.publish("/ipfs/" + addr) + .then(res => { + callback(null, res.Name); + }) + .catch(() => { + callback(addr + " could not be registered"); + }); +}; diff --git a/lib/modules/ipfs/embarkjs/default.js b/lib/modules/ipfs/embarkjs/default.js deleted file mode 100644 index 1334d069..00000000 --- a/lib/modules/ipfs/embarkjs/default.js +++ /dev/null @@ -1,46 +0,0 @@ -import IpfsApi from 'ipfs-api'; - -let __embarkIPFS = {}; - -__embarkIPFS.setProvider = function (options) { - var self = this; - return new Promise(function (resolve, reject) { - try { - if (!options) { - self._config = options; - self._ipfsConnection = IpfsApi('localhost', '5001'); - self._getUrl = "http://localhost:8080/ipfs/"; - } else { - var ipfsOptions = {host: options.host || options.server, protocol: 'http'}; - if (options.protocol) { - ipfsOptions.protocol = options.protocol; - } - if (options.port && options.port !== 'false') { - ipfsOptions.port = options.port; - } - self._ipfsConnection = IpfsApi(ipfsOptions); - self._getUrl = options.getUrl || "http://localhost:8080/ipfs/"; - } - resolve(self); - } catch (err) { - console.error(err); - self._ipfsConnection = null; - reject(new Error('Failed to connect to IPFS')); - } - }); -}; - -__embarkIPFS.isAvailable = function () { - return new Promise((resolve) => { - if (!this._ipfsConnection) { - return resolve(false); - } - this._ipfsConnection.id() - .then((id) => { - resolve(Boolean(id)); - }) - .catch(() => { - resolve(false); - }); - }); -}; diff --git a/lib/modules/ipfs/embarkjs/name.js b/lib/modules/ipfs/embarkjs/name.js deleted file mode 100644 index 73c64485..00000000 --- a/lib/modules/ipfs/embarkjs/name.js +++ /dev/null @@ -1,43 +0,0 @@ -/*global __embarkIPFS*/ - -const NoConnectionError = 'No IPFS connection. Please ensure to call Embark.Names.setProvider()'; -const NotAvailableError = 'Not available with ipns'; - -__embarkIPFS.resolve = function (name, callback) { - callback = callback || function () {}; - if (!this._ipfsConnection) { - var connectionError = new Error(NoConnectionError); - return callback(connectionError); - } - - this._ipfsConnection.name.resolve(name) - .then(res => { - callback(null, res.Path); - }) - .catch(() => { - callback(name + " is not registered"); - }); -}; - -__embarkIPFS.register = function(addr, callback) { - callback = callback || function () {}; - if (!this._ipfsConnection) { - return new Error(NoConnectionError); - } - - this._ipfsConnection.name.publish("/ipfs/" + addr) - .then(res => { - callback(null, res.Name); - }) - .catch(() => { - callback(addr + " could not be registered"); - }); -}; - -__embarkIPFS.lookup = function () { - return new Error(NotAvailableError); -}; - -__embarkIPFS.registerSubDomain = function () { - return new Error(NotAvailableError); -}; diff --git a/lib/modules/ipfs/embarkjs/storage.js b/lib/modules/ipfs/embarkjs/storage.js deleted file mode 100644 index ee9a0aab..00000000 --- a/lib/modules/ipfs/embarkjs/storage.js +++ /dev/null @@ -1,75 +0,0 @@ -/*global __embarkIPFS*/ - -__embarkIPFS.saveText = function (text) { - const self = this; - var promise = new Promise(function (resolve, reject) { - if (!self._ipfsConnection) { - var connectionError = new Error('No IPFS connection. Please ensure to call Embark.Storage.setProvider()'); - reject(connectionError); - } - self._ipfsConnection.add(self._ipfsConnection.Buffer.from(text), function (err, result) { - if (err) { - reject(err); - } else { - resolve(result[0].path); - } - }); - }); - - return promise; -}; - -__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) { - if (!self._ipfsConnection) { - var connectionError = new Error('No IPFS connection. Please ensure to call Embark.Storage.setProvider()'); - reject(connectionError); - } - self._ipfsConnection.get(hash, function (err, files) { - if (err) { - return reject(err); - } - resolve(files[0].content.toString()); - }); - }); - - return promise; -}; - -__embarkIPFS.uploadFile = function (inputSelector) { - const self = this; - var file = inputSelector[0].files[0]; - - if (file === undefined) { - throw new Error('no file found'); - } - - var promise = new Promise(function (resolve, reject) { - if (!self._ipfsConnection) { - var connectionError = new Error('No IPFS connection. Please ensure to call Embark.Storage.setProvider()'); - reject(connectionError); - } - var reader = new FileReader(); - reader.onloadend = function () { - var fileContent = reader.result; - var buffer = self._ipfsConnection.Buffer.from(fileContent); - self._ipfsConnection.add(buffer, function (err, result) { - if (err) { - reject(err); - } else { - resolve(result[0].path); - } - }); - }; - reader.readAsArrayBuffer(file); - }); - - return promise; -}; - -__embarkIPFS.getUrl = function (hash) { - return (this._getUrl || "http://localhost:8080/ipfs/") + hash; -}; diff --git a/lib/modules/ipfs/index.js b/lib/modules/ipfs/index.js index b870ef0e..75dd9b7f 100644 --- a/lib/modules/ipfs/index.js +++ b/lib/modules/ipfs/index.js @@ -19,12 +19,8 @@ class IPFS { this.webServerConfig = embark.config.webServerConfig; this.blockchainConfig = embark.config.blockchainConfig; - if (this.isIpfsStorageEnabledInTheConfig() || this.isIpfsNameEnabledInTheConfig()) { - this.downloadIpfsApi(); - this.addDefaultToEmbarkJS(); - } - if (this.isIpfsStorageEnabledInTheConfig()) { + this.downloadIpfsApi(); this.setServiceCheck(); this.addStorageProviderToEmbarkJS(); this.addObjectToConsole(); @@ -38,11 +34,6 @@ class IPFS { self.startProcess(() => {}); }); } - - if (this.isIpfsNameEnabledInTheConfig()) { - this.addNamesystemProviderToEmbarkJS(); - this.setNamesystemProvider(); - } } downloadIpfsApi() { @@ -102,29 +93,14 @@ class IPFS { utils.getJson(url, cb); } - addDefaultToEmbarkJS() { - let code = ""; - code += "\n" + fs.readFileSync(utils.joinPath(__dirname, 'embarkjs' , 'default.js')).toString(); - - this.embark.addCodeToEmbarkJS(code); - } - addStorageProviderToEmbarkJS() { let code = ""; - code += "\n" + fs.readFileSync(utils.joinPath(__dirname, 'embarkjs', 'storage.js')).toString(); + code += "\n" + fs.readFileSync(utils.joinPath(__dirname, 'embarkjs.js')).toString(); code += "\nEmbarkJS.Storage.registerProvider('ipfs', __embarkIPFS);"; this.embark.addCodeToEmbarkJS(code); } - addNamesystemProviderToEmbarkJS() { - let code = ""; - code += "\n" + fs.readFileSync(utils.joinPath(__dirname, 'embarkjs', 'name.js')).toString(); - code += "\nEmbarkJS.Names.registerProvider('ipns', __embarkIPFS);"; - - this.embark.addCodeToEmbarkJS(code); - } - addObjectToConsole() { let ipfs = IpfsApi(this.host, this.port); this.events.emit("runcode:register", "ipfs", ipfs); @@ -160,23 +136,6 @@ class IPFS { let {enabled, available_providers, dappConnection} = this.storageConfig; return enabled && (available_providers.indexOf('ipfs') > 0 || dappConnection.find(c => c.provider === 'ipfs')); } - - isIpfsNameEnabledInTheConfig() { - let {enabled, available_providers} = this.namesystemConfig; - return enabled && available_providers.indexOf('ipns') > 0; - } - - setNamesystemProvider() { - let code = `\nEmbarkJS.Names.setProvider('ipns', ${JSON.stringify(this.storageConfig.dappConnection[0] || {})});`; - - let shouldInit = (namesystemConfig) => { - return (namesystemConfig.provider === 'ipns' && namesystemConfig.enabled === true); - }; - - this.embark.addProviderInit('names', code, shouldInit); - } - - } module.exports = IPFS; diff --git a/lib/modules/swarm/embarkjs.js b/lib/modules/swarm/embarkjs.js index 553cdb6f..1845de6c 100644 --- a/lib/modules/swarm/embarkjs.js +++ b/lib/modules/swarm/embarkjs.js @@ -103,3 +103,12 @@ __embarkSwarm.getUrl = function (hash) { return `${this._config.getUrl || this._connectUrl + '/bzz:/'}${hash}`; }; +const NotAvailable = "Not available with Swarm" + +__embarkSwarm.resolve = function () { + return new Error(NotAvailable); +}; + +__embarkSwarm.register = function () { + return new Error(NotAvailable); +}; diff --git a/lib/modules/whisper/index.js b/lib/modules/whisper/index.js index 3ce7ddfa..7b4a6dfb 100644 --- a/lib/modules/whisper/index.js +++ b/lib/modules/whisper/index.js @@ -75,15 +75,13 @@ class Whisper { let connection = this.communicationConfig.connection || {}; // todo: make the add code a function as well - let config = JSON.stringify({ + let config = { server: canonicalHost(connection.host || defaultHost), port: connection.port || '8546', type: connection.type || 'ws' - }); + }; - config = JSON.stringify(config); - - let code = "\nEmbarkJS.Messages.setProvider('whisper'," + config + ");"; + let code = `\nEmbarkJS.Messages.setProvider('whisper', ${JSON.stringify(config)});`; let shouldInit = (communicationConfig) => { return (communicationConfig.provider === 'whisper' && communicationConfig.enabled === true); diff --git a/templates/demo/app/components/storage.js b/templates/demo/app/components/storage.js index 1d153294..5c7540af 100644 --- a/templates/demo/app/components/storage.js +++ b/templates/demo/app/components/storage.js @@ -110,8 +110,8 @@ class Storage extends React.Component { ipnsRegister(e) { e.preventDefault(); this.setState({ registering: true, responseRegister: false }); - this.addToLog("EmbarkJS.Names.register(this.state.ipfsHash).then(function(hash) { })"); - EmbarkJS.Names.register(this.state.valueRegister, (err, name) => { + this.addToLog("EmbarkJS.Storage.register(this.state.ipfsHash).then(function(hash) { })"); + EmbarkJS.Storage.register(this.state.valueRegister, (err, name) => { let responseRegister; let isRegisterError = false; if (err) { @@ -132,8 +132,8 @@ class Storage extends React.Component { ipnsResolve(e) { e.preventDefault(); this.setState({ resolving: true, responseResolver: false }); - this.addToLog("EmbarkJS.Names.resolve(this.state.ipnsName, function(err, path) { })"); - EmbarkJS.Names.resolve(this.state.valueResolver, (err, path) => { + this.addToLog("EmbarkJS.Storage.resolve(this.state.ipnsName, function(err, path) { })"); + EmbarkJS.Storage.resolve(this.state.valueResolver, (err, path) => { let responseResolver; let isResolverError = false; if (err) { diff --git a/templates/demo/app/dapp.js b/templates/demo/app/dapp.js index c48ab0bd..77a837fb 100644 --- a/templates/demo/app/dapp.js +++ b/templates/demo/app/dapp.js @@ -44,10 +44,15 @@ class App extends React.Component { }); } } + + EmbarkJS.Storage.isAvailable().then((result) => { + this.setState({storageEnabled: result}); + }).catch(() => { + this.setState({storageEnabled: false}); + }); + this.setState({ - storageEnabled: EmbarkJS.Storage.isAvailable(), - ensEnabled: EmbarkJS.Names.isAvailable(), - ensNameSystems: EmbarkJS.Names.currentNameSystems + ensEnabled: EmbarkJS.Names.isAvailable() }); }); } @@ -61,11 +66,6 @@ class App extends React.Component { } handleSelect(key) { - if (key === 2) { - EmbarkJS.Names.setProvider('ipns', {server: 'localhost', port: '5001'}); - } else if (key === 4) { - EmbarkJS.Names.currentNameSystems = this.state.ensNameSystems - } this.setState({ activeKey: key }); } diff --git a/templates/demo/config/namesystem.js b/templates/demo/config/namesystem.js index 722445eb..86519d42 100644 --- a/templates/demo/config/namesystem.js +++ b/templates/demo/config/namesystem.js @@ -1,6 +1,6 @@ module.exports = { default: { - available_providers: ["ens", "ipns"], + available_providers: ["ens"], provider: "ens" },