mirror of
https://github.com/embarklabs/embark.git
synced 2025-02-20 01:18:52 +00:00
Adding ipns
This commit is contained in:
parent
174412bee1
commit
7194b045e6
@ -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}`);
|
|
||||||
});
|
|
||||||
};
|
|
47
lib/modules/ipfs/embarkjs/default.js
Normal file
47
lib/modules/ipfs/embarkjs/default.js
Normal 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);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
};
|
32
lib/modules/ipfs/embarkjs/name.js
Normal file
32
lib/modules/ipfs/embarkjs/name.js
Normal 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");
|
||||||
|
};
|
73
lib/modules/ipfs/embarkjs/storage.js
Normal file
73
lib/modules/ipfs/embarkjs/storage.js
Normal 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;
|
||||||
|
};
|
@ -13,17 +13,20 @@ class IPFS {
|
|||||||
this.events = embark.events;
|
this.events = embark.events;
|
||||||
this.buildDir = options.buildDir;
|
this.buildDir = options.buildDir;
|
||||||
this.storageConfig = embark.config.storageConfig;
|
this.storageConfig = embark.config.storageConfig;
|
||||||
|
this.namesystemConfig = embark.config.namesystemConfig;
|
||||||
this.embark = embark;
|
this.embark = embark;
|
||||||
|
|
||||||
this.webServerConfig = embark.config.webServerConfig;
|
this.webServerConfig = embark.config.webServerConfig;
|
||||||
this.blockchainConfig = embark.config.blockchainConfig;
|
this.blockchainConfig = embark.config.blockchainConfig;
|
||||||
|
|
||||||
if (!this.isIpfsEnabledInTheConfig()) {
|
if (this.isIpfsEnabledInTheConfig() || this.isIpnsEnabledInTheConfig()) {
|
||||||
return;
|
this.downloadIpfsApi();
|
||||||
|
this.addDefaultToEmbarkJS();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (this.isIpfsEnabledInTheConfig()) {
|
||||||
this.setServiceCheck();
|
this.setServiceCheck();
|
||||||
this.addProviderToEmbarkJS();
|
this.addStorageProviderToEmbarkJS();
|
||||||
this.addObjectToConsole();
|
this.addObjectToConsole();
|
||||||
this.registerUploadCommand();
|
this.registerUploadCommand();
|
||||||
|
|
||||||
@ -36,6 +39,25 @@ class IPFS {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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));
|
||||||
|
});
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
setServiceCheck() {
|
setServiceCheck() {
|
||||||
let self = this;
|
let self = this;
|
||||||
|
|
||||||
@ -80,21 +102,24 @@ class IPFS {
|
|||||||
utils.getJson(url, cb);
|
utils.getJson(url, cb);
|
||||||
}
|
}
|
||||||
|
|
||||||
addProviderToEmbarkJS() {
|
addDefaultToEmbarkJS() {
|
||||||
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));
|
|
||||||
});
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
let code = "";
|
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);";
|
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);";
|
code += "\nEmbarkJS.Names.registerProvider('ipns', __embarkIPFS);";
|
||||||
|
|
||||||
this.embark.addCodeToEmbarkJS(code);
|
this.embark.addCodeToEmbarkJS(code);
|
||||||
@ -136,6 +161,17 @@ class IPFS {
|
|||||||
return enabled && (available_providers.indexOf('ipfs') > 0 || dappConnection.find(c => c.provider === '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;
|
module.exports = IPFS;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user