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..105739d4
--- /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);
+ return reject(connectionError);
+ }
+ self._ipfsConnection.add(self._ipfsConnection.Buffer.from(text), function (err, result) {
+ if (err) {
+ return reject(err);
+ }
+
+ 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);
+ return 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);
+ return 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) {
+ return reject(err);
+ }
+
+ 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..fdce4c80 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 (_name, callback) {
+ callback(NotAvailable);
+};
+
+__embarkSwarm.register = function (_addr, callback) {
+ callback(NotAvailable);
+};
diff --git a/templates/demo/app/components/storage.js b/templates/demo/app/components/storage.js
index 1d153294..7c527009 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) {
@@ -215,6 +215,8 @@ class Storage extends React.Component {
+