Move ipns function to ipfs

This commit is contained in:
Anthony Laibe 2018-08-22 10:11:57 +01:00
parent b4e118c85b
commit b0e0989359
11 changed files with 181 additions and 229 deletions

View File

@ -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");
};

View File

@ -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");
});
};

View File

@ -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);
});
});
};

View File

@ -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);
};

View File

@ -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;
};

View File

@ -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;

View File

@ -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);
};

View File

@ -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);

View File

@ -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) {

View File

@ -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 });
}

View File

@ -1,6 +1,6 @@
module.exports = {
default: {
available_providers: ["ens", "ipns"],
available_providers: ["ens"],
provider: "ens"
},