Adding ipns

This commit is contained in:
Anthony Laibe 2018-07-27 10:51:17 +01:00 committed by Iuri Matias
parent 174412bee1
commit 7194b045e6
5 changed files with 212 additions and 180 deletions

View File

@ -1,156 +0,0 @@
import IpfsApi from 'ipfs-api';
let __embarkIPFS = {};
__embarkIPFS.setProvider = function (options) {
var self = this;
var promise = new Promise(function (resolve, reject) {
try {
if (options === undefined) {
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'));
}
});
return promise;
};
__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.isAvailable = function () {
return new Promise((resolve) => {
if (!this._ipfsConnection) {
return resolve(false);
}
this._ipfsConnection.id()
.then((id) => {
resolve(Boolean(id));
})
.catch(() => {
resolve(false);
});
});
};
__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('No IPFS connection. Please ensure to call Embark.Names.setProvider()');
return callback(connectionError);
}
this._ipfsConnection.name.resolve(name, (err, res) => {
if (err) {
return callback(name + " is not registered");
}
callback(err, res.value);
});
};
__embarkIPFS.lookup = function () {
console.error("Not Available");
};
__embarkIPFS.register = function(addr, callback) {
callback = callback || function () {};
if (!this._ipfsConnection) {
var connectionError = new Error('No IPFS connection. Please ensure to call Embark.Names.setProvider()');
return callback(connectionError);
}
this._ipfsConnection.name.publish(addr, function (err, res) {
if (err) {
return callback(addr + " is not publ");
}
callback(err, `https://gateway.ipfs.io/ipns/${res.name}`);
});
};

View File

@ -0,0 +1,47 @@
import IpfsApi from 'ipfs-api';
let __embarkIPFS = {};
__embarkIPFS.setProvider = function (options) {
var self = this;
var promise = new Promise(function (resolve, reject) {
try {
if (options === undefined) {
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'));
}
});
return promise;
};
__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

@ -0,0 +1,32 @@
__embarkIPFS.resolve = function (name, callback) {
callback = callback || function () {};
if (!this._ipfsConnection) {
var connectionError = new Error('No IPFS connection. Please ensure to call Embark.Names.setProvider()');
return callback(connectionError);
}
this._ipfsConnection.name.resolve(name, (err, res) => {
if (err) {
return callback(name + " is not registered");
}
callback(err, res.value);
});
};
__embarkIPFS.register = function(addr, options) {
if (!this._ipfsConnection) {
return new Error('No IPFS connection. Please ensure to call Embark.Names.setProvider()');
}
return this._ipfsConnection.name.publish(addr, options, function (err, res) {
if (err) {
return new Error('No IPFS connection. Please ensure to call Embark.Names.setProvider()');
}
return `https://gateway.ipfs.io/ipns/${res.name}`;
});
};
__embarkIPFS.lookup = function () {
return new Error("Not Implemented");
};

View File

@ -0,0 +1,73 @@
__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

@ -13,26 +13,48 @@ class IPFS {
this.events = embark.events;
this.buildDir = options.buildDir;
this.storageConfig = embark.config.storageConfig;
this.namesystemConfig = embark.config.namesystemConfig;
this.embark = embark;
this.webServerConfig = embark.config.webServerConfig;
this.blockchainConfig = embark.config.blockchainConfig;
if (!this.isIpfsEnabledInTheConfig()) {
return;
if (this.isIpfsEnabledInTheConfig() || this.isIpnsEnabledInTheConfig()) {
this.downloadIpfsApi();
this.addDefaultToEmbarkJS();
}
this.setServiceCheck();
this.addProviderToEmbarkJS();
this.addObjectToConsole();
this.registerUploadCommand();
if (this.isIpfsEnabledInTheConfig()) {
this.setServiceCheck();
this.addStorageProviderToEmbarkJS();
this.addObjectToConsole();
this.registerUploadCommand();
this._checkService((err) => {
if (!err) {
return;
this._checkService((err) => {
if (!err) {
return;
}
self.logger.info("IPFS node not found, attempting to start own node");
self.startProcess(() => {});
});
}
if (this.isIpnsEnabledInTheConfig()) {
this.addNamesystemProviderToEmbarkJS();
this.setNamesystemProvider();
}
}
downloadIpfsApi() {
const self = this;
self.events.request("version:get:ipfs-api", function(ipfsApiVersion) {
let currentIpfsApiVersion = require('../../../package.json').dependencies["ipfs-api"];
if (ipfsApiVersion !== currentIpfsApiVersion) {
self.events.request("version:getPackageLocation", "ipfs-api", ipfsApiVersion, function(err, location) {
self.embark.registerImportFile("ipfs-api", fs.dappPath(location));
});
}
self.logger.info("IPFS node not found, attempting to start own node");
self.startProcess(() => {});
});
}
@ -80,21 +102,24 @@ class IPFS {
utils.getJson(url, cb);
}
addProviderToEmbarkJS() {
const self = this;
self.events.request("version:get:ipfs-api", function(ipfsApiVersion) {
let currentIpfsApiVersion = require('../../../package.json').dependencies["ipfs-api"];
if (ipfsApiVersion !== currentIpfsApiVersion) {
self.events.request("version:getPackageLocation", "ipfs-api", ipfsApiVersion, function(err, location) {
self.embark.registerImportFile("ipfs-api", fs.dappPath(location));
});
}
});
addDefaultToEmbarkJS() {
let code = "";
code += "\n" + fs.readFileSync(utils.joinPath(__dirname, 'embarkjs.js')).toString();
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 += "\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);
@ -136,6 +161,17 @@ class IPFS {
return enabled && (available_providers.indexOf('ipfs') > 0 || dappConnection.find(c => c.provider === 'ipfs'));
}
isIpnsEnabledInTheConfig() {
let {enabled, available_providers, provider} = this.namesystemConfig;
return enabled && available_providers.indexOf('ipns') > 0 && provider === 'ipns';
}
setNamesystemProvider() {
let code = `\nEmbarkJS.Names.setProvider('ipns', ${JSON.stringify(this.storageConfig.dappConnection[0] || {})});`;
this.embark.addProviderInit('names', code, this.isIpnsEnabledInTheConfig.bind(this));
}
}
module.exports = IPFS;