Merge pull request #254 from nogueian/develop
Refactored EmbarkJS to allow multiple storage providers
This commit is contained in:
commit
65c5006d0e
|
@ -32,6 +32,9 @@ $(document).ready(function() {
|
|||
//EmbarkJS.Storage.setProvider('ipfs',{server: 'localhost', port: '5001'});
|
||||
|
||||
$("#storage .error").hide();
|
||||
EmbarkJS.Storage.setProvider('ipfs')
|
||||
.then(function(){
|
||||
console.log('Provider set to IPFS');
|
||||
EmbarkJS.Storage.ipfsConnection.ping()
|
||||
.then(function(){
|
||||
$("#status-storage").addClass('status-online');
|
||||
|
@ -45,6 +48,13 @@ $(document).ready(function() {
|
|||
$("#storage-controls").hide();
|
||||
}
|
||||
});
|
||||
})
|
||||
.catch(function(err){
|
||||
console.log('Failed to set IPFS as Provider:', err.message);
|
||||
$("#storage .error").show();
|
||||
$("#status-storage").addClass('status-offline');
|
||||
$("#storage-controls").hide();
|
||||
});
|
||||
|
||||
$("#storage button.setIpfsText").click(function() {
|
||||
var value = $("#storage input.ipfsText").val();
|
||||
|
|
|
@ -10,7 +10,7 @@
|
|||
"license": "ISC",
|
||||
"homepage": "",
|
||||
"devDependencies": {
|
||||
"embark": "^2.4.0",
|
||||
"embark": "^2.4.1",
|
||||
"mocha": "^2.2.5"
|
||||
}
|
||||
}
|
||||
|
|
|
@ -74,8 +74,11 @@ var EmbarkJS =
|
|||
/*jshint esversion: 6 */
|
||||
//var Ipfs = require('./ipfs.js');
|
||||
|
||||
var EmbarkJS = {
|
||||
};
|
||||
//=========================================================
|
||||
// Embark Smart Contracts
|
||||
//=========================================================
|
||||
|
||||
var EmbarkJS = {};
|
||||
|
||||
EmbarkJS.Contract = function(options) {
|
||||
var self = this;
|
||||
|
@ -112,7 +115,7 @@ EmbarkJS.Contract = function(options) {
|
|||
};
|
||||
|
||||
this._originalContractObject = ContractClass.at(this.address);
|
||||
this._methods = Object.getOwnPropertyNames(this._originalContractObject).filter(function (p) {
|
||||
this._methods = Object.getOwnPropertyNames(this._originalContractObject).filter(function(p) {
|
||||
// TODO: check for forbidden properties
|
||||
if (self.eventList.indexOf(p) >= 0) {
|
||||
|
||||
|
@ -196,7 +199,11 @@ EmbarkJS.Contract.prototype.deploy = function(args, _options) {
|
|||
if (err) {
|
||||
reject(err);
|
||||
} else if (transaction.address !== undefined) {
|
||||
resolve(new EmbarkJS.Contract({abi: self.abi, code: self.code, address: transaction.address}));
|
||||
resolve(new EmbarkJS.Contract({
|
||||
abi: self.abi,
|
||||
code: self.code,
|
||||
address: transaction.address
|
||||
}));
|
||||
}
|
||||
});
|
||||
|
||||
|
@ -210,33 +217,78 @@ EmbarkJS.Contract.prototype.deploy = function(args, _options) {
|
|||
return promise;
|
||||
};
|
||||
|
||||
EmbarkJS.Storage = {
|
||||
IPFS : 'ipfs'
|
||||
//=========================================================
|
||||
// Embark Storage
|
||||
//=========================================================
|
||||
|
||||
EmbarkJS.Storage = {};
|
||||
|
||||
EmbarkJS.Storage.Providers = {
|
||||
IPFS: 'ipfs',
|
||||
SWARM: 'swarm'
|
||||
};
|
||||
|
||||
EmbarkJS.Storage.IPFS = {};
|
||||
|
||||
EmbarkJS.Storage.saveText = function(text) {
|
||||
return this.currentStorage.saveText(text);
|
||||
};
|
||||
|
||||
EmbarkJS.Storage.get = function(hash) {
|
||||
return this.currentStorage.get(hash);
|
||||
};
|
||||
|
||||
EmbarkJS.Storage.uploadFile = function(inputSelector) {
|
||||
return this.currentStorage.uploadFile(inputSelector);
|
||||
}
|
||||
|
||||
EmbarkJS.Storage.getUrl = function(hash) {
|
||||
return this.currentStorage.getUrl(hash);
|
||||
}
|
||||
|
||||
EmbarkJS.Storage.setProvider = function(provider, options) {
|
||||
if (provider.toLowerCase() === EmbarkJS.Storage.IPFS) {
|
||||
var self = this;
|
||||
var promise = new Promise(function(resolve, reject) {
|
||||
if (provider.toLowerCase() === EmbarkJS.Storage.Providers.IPFS) {
|
||||
//I don't think currentStorage is used anywhere, this might not be needed
|
||||
//for now until additional storage providers are supported. But keeping it
|
||||
//anyways
|
||||
this.currentStorage = EmbarkJS.Storage.IPFS;
|
||||
self.currentStorage = EmbarkJS.Storage.IPFS;
|
||||
|
||||
try {
|
||||
if (options === undefined) {
|
||||
this.ipfsConnection = IpfsApi('localhost', '5001');
|
||||
self.ipfsConnection = IpfsApi('localhost', '5001');
|
||||
} else {
|
||||
this.ipfsConnection = IpfsApi(options.server, options.port);
|
||||
self.ipfsConnection = IpfsApi(options.server, options.port);
|
||||
}
|
||||
resolve(self);
|
||||
} catch (err) {
|
||||
self.ipfsConnection = null;
|
||||
reject(new Error('Failed to connect to IPFS'));
|
||||
}
|
||||
} else if (provider.toLowerCase() === EmbarkJS.Storage.SWARM) {
|
||||
reject('Swarm not implemented');
|
||||
// TODO Implement Swarm
|
||||
// this.currentStorage = EmbarkJS.Storage.SWARM;
|
||||
// if (options === undefined) {
|
||||
// //Connect to default Swarm node
|
||||
// } else {
|
||||
// //Connect using options
|
||||
// }
|
||||
} else {
|
||||
throw Error('Unknown storage provider');
|
||||
reject('Unknown storage provider');
|
||||
}
|
||||
});
|
||||
return promise;
|
||||
};
|
||||
|
||||
EmbarkJS.Storage.saveText = function(text) {
|
||||
var self = this;
|
||||
if (!this.ipfsConnection) {
|
||||
this.setProvider('ipfs');
|
||||
}
|
||||
EmbarkJS.Storage.IPFS.saveText = function(text) {
|
||||
var promise = new Promise(function(resolve, reject) {
|
||||
self.ipfsConnection.add((new self.ipfsConnection.Buffer(text)), function(err, result) {
|
||||
if (!EmbarkJS.Storage.ipfsConnection) {
|
||||
var connectionError = new Error('No IPFS connection. Please ensure to call Embark.Storage.setProvider()');
|
||||
reject(connectionError);
|
||||
}
|
||||
EmbarkJS.Storage.ipfsConnection.add((new EmbarkJS.Storage.ipfsConnection.Buffer(text)), function(err, result) {
|
||||
if (err) {
|
||||
reject(err);
|
||||
} else {
|
||||
|
@ -248,24 +300,41 @@ EmbarkJS.Storage.saveText = function(text) {
|
|||
return promise;
|
||||
};
|
||||
|
||||
EmbarkJS.Storage.uploadFile = function(inputSelector) {
|
||||
var self = this;
|
||||
EmbarkJS.Storage.IPFS.get = function(hash) {
|
||||
// TODO: detect type, then convert if needed
|
||||
//var ipfsHash = web3.toAscii(hash);
|
||||
var promise = new Promise(function(resolve, reject) {
|
||||
if (!EmbarkJS.Storage.ipfsConnection) {
|
||||
var connectionError = new Error('No IPFS connection. Please ensure to call Embark.Storage.setProvider()');
|
||||
reject(connectionError);
|
||||
}
|
||||
EmbarkJS.Storage.ipfsConnection.object.get([hash]).then(function(node) {
|
||||
resolve(node.data);
|
||||
}).catch(function(err) {
|
||||
reject(err);
|
||||
});
|
||||
});
|
||||
|
||||
return promise;
|
||||
};
|
||||
|
||||
EmbarkJS.Storage.IPFS.uploadFile = function(inputSelector) {
|
||||
var file = inputSelector[0].files[0];
|
||||
|
||||
if (file === undefined) {
|
||||
throw new Error('no file found');
|
||||
}
|
||||
|
||||
if (!this.ipfsConnection) {
|
||||
this.setProvider('ipfs');
|
||||
}
|
||||
|
||||
var promise = new Promise(function(resolve, reject) {
|
||||
if (!EmbarkJS.Storage.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) {
|
||||
var buffer = EmbarkJS.Storage.ipfsConnection.Buffer.from(fileContent);
|
||||
EmbarkJS.Storage.ipfsConnection.add(buffer, function(err, result) {
|
||||
if (err) {
|
||||
reject(err);
|
||||
} else {
|
||||
|
@ -279,33 +348,17 @@ EmbarkJS.Storage.uploadFile = function(inputSelector) {
|
|||
return promise;
|
||||
};
|
||||
|
||||
EmbarkJS.Storage.get = function(hash) {
|
||||
var self = this;
|
||||
// TODO: detect type, then convert if needed
|
||||
//var ipfsHash = web3.toAscii(hash);
|
||||
if (!this.ipfsConnection) {
|
||||
this.setProvider('ipfs');
|
||||
}
|
||||
|
||||
var promise = new Promise(function(resolve, reject) {
|
||||
self.ipfsConnection.object.get([hash]).then(function(node) {
|
||||
resolve(node.data);
|
||||
}).catch(function (err){
|
||||
reject(err);
|
||||
});
|
||||
});
|
||||
|
||||
return promise;
|
||||
};
|
||||
|
||||
EmbarkJS.Storage.getUrl = function(hash) {
|
||||
EmbarkJS.Storage.IPFS.getUrl = function(hash) {
|
||||
//var ipfsHash = web3.toAscii(hash);
|
||||
|
||||
return 'http://localhost:8080/ipfs/' + hash;
|
||||
};
|
||||
|
||||
EmbarkJS.Messages = {
|
||||
};
|
||||
//=========================================================
|
||||
// Embark Messaging
|
||||
//=========================================================
|
||||
|
||||
EmbarkJS.Messages = {};
|
||||
|
||||
EmbarkJS.Messages.setProvider = function(provider, options) {
|
||||
var self = this;
|
||||
|
@ -336,7 +389,7 @@ EmbarkJS.Messages.setProvider = function(provider, options) {
|
|||
this.currentMessages.orbit = new Orbit(ipfs);
|
||||
this.currentMessages.orbit.connect(web3.eth.accounts[0]);
|
||||
} else {
|
||||
throw Error('unknown provider');
|
||||
throw Error('Unknown message provider');
|
||||
}
|
||||
};
|
||||
|
||||
|
@ -348,8 +401,7 @@ EmbarkJS.Messages.listenTo = function(options) {
|
|||
return this.currentMessages.listenTo(options);
|
||||
};
|
||||
|
||||
EmbarkJS.Messages.Whisper = {
|
||||
};
|
||||
EmbarkJS.Messages.Whisper = {};
|
||||
|
||||
EmbarkJS.Messages.Whisper.sendMessage = function(options) {
|
||||
var topics = options.topic || options.topics;
|
||||
|
@ -448,8 +500,7 @@ EmbarkJS.Messages.Whisper.listenTo = function(options) {
|
|||
return promise;
|
||||
};
|
||||
|
||||
EmbarkJS.Messages.Orbit = {
|
||||
};
|
||||
EmbarkJS.Messages.Orbit = {};
|
||||
|
||||
EmbarkJS.Messages.Orbit.sendMessage = function(options) {
|
||||
var topics = options.topic || options.topics;
|
||||
|
|
155
js/embark.js
155
js/embark.js
|
@ -1,8 +1,11 @@
|
|||
/*jshint esversion: 6 */
|
||||
//var Ipfs = require('./ipfs.js');
|
||||
|
||||
var EmbarkJS = {
|
||||
};
|
||||
//=========================================================
|
||||
// Embark Smart Contracts
|
||||
//=========================================================
|
||||
|
||||
var EmbarkJS = {};
|
||||
|
||||
EmbarkJS.Contract = function(options) {
|
||||
var self = this;
|
||||
|
@ -39,7 +42,7 @@ EmbarkJS.Contract = function(options) {
|
|||
};
|
||||
|
||||
this._originalContractObject = ContractClass.at(this.address);
|
||||
this._methods = Object.getOwnPropertyNames(this._originalContractObject).filter(function (p) {
|
||||
this._methods = Object.getOwnPropertyNames(this._originalContractObject).filter(function(p) {
|
||||
// TODO: check for forbidden properties
|
||||
if (self.eventList.indexOf(p) >= 0) {
|
||||
|
||||
|
@ -123,7 +126,11 @@ EmbarkJS.Contract.prototype.deploy = function(args, _options) {
|
|||
if (err) {
|
||||
reject(err);
|
||||
} else if (transaction.address !== undefined) {
|
||||
resolve(new EmbarkJS.Contract({abi: self.abi, code: self.code, address: transaction.address}));
|
||||
resolve(new EmbarkJS.Contract({
|
||||
abi: self.abi,
|
||||
code: self.code,
|
||||
address: transaction.address
|
||||
}));
|
||||
}
|
||||
});
|
||||
|
||||
|
@ -137,33 +144,78 @@ EmbarkJS.Contract.prototype.deploy = function(args, _options) {
|
|||
return promise;
|
||||
};
|
||||
|
||||
EmbarkJS.Storage = {
|
||||
IPFS : 'ipfs'
|
||||
//=========================================================
|
||||
// Embark Storage
|
||||
//=========================================================
|
||||
|
||||
EmbarkJS.Storage = {};
|
||||
|
||||
EmbarkJS.Storage.Providers = {
|
||||
IPFS: 'ipfs',
|
||||
SWARM: 'swarm'
|
||||
};
|
||||
|
||||
EmbarkJS.Storage.IPFS = {};
|
||||
|
||||
EmbarkJS.Storage.saveText = function(text) {
|
||||
return this.currentStorage.saveText(text);
|
||||
};
|
||||
|
||||
EmbarkJS.Storage.get = function(hash) {
|
||||
return this.currentStorage.get(hash);
|
||||
};
|
||||
|
||||
EmbarkJS.Storage.uploadFile = function(inputSelector) {
|
||||
return this.currentStorage.uploadFile(inputSelector);
|
||||
};
|
||||
|
||||
EmbarkJS.Storage.getUrl = function(hash) {
|
||||
return this.currentStorage.getUrl(hash);
|
||||
};
|
||||
|
||||
EmbarkJS.Storage.setProvider = function(provider, options) {
|
||||
if (provider.toLowerCase() === EmbarkJS.Storage.IPFS) {
|
||||
var self = this;
|
||||
var promise = new Promise(function(resolve, reject) {
|
||||
if (provider.toLowerCase() === EmbarkJS.Storage.Providers.IPFS) {
|
||||
//I don't think currentStorage is used anywhere, this might not be needed
|
||||
//for now until additional storage providers are supported. But keeping it
|
||||
//anyways
|
||||
this.currentStorage = EmbarkJS.Storage.IPFS;
|
||||
self.currentStorage = EmbarkJS.Storage.IPFS;
|
||||
|
||||
try {
|
||||
if (options === undefined) {
|
||||
this.ipfsConnection = IpfsApi('localhost', '5001');
|
||||
self.ipfsConnection = IpfsApi('localhost', '5001');
|
||||
} else {
|
||||
this.ipfsConnection = IpfsApi(options.server, options.port);
|
||||
self.ipfsConnection = IpfsApi(options.server, options.port);
|
||||
}
|
||||
resolve(self);
|
||||
} catch (err) {
|
||||
self.ipfsConnection = null;
|
||||
reject(new Error('Failed to connect to IPFS'));
|
||||
}
|
||||
} else if (provider.toLowerCase() === EmbarkJS.Storage.SWARM) {
|
||||
reject('Swarm not implemented');
|
||||
// TODO Implement Swarm
|
||||
// this.currentStorage = EmbarkJS.Storage.SWARM;
|
||||
// if (options === undefined) {
|
||||
// //Connect to default Swarm node
|
||||
// } else {
|
||||
// //Connect using options
|
||||
// }
|
||||
} else {
|
||||
throw Error('Unknown storage provider');
|
||||
reject('Unknown storage provider');
|
||||
}
|
||||
});
|
||||
return promise;
|
||||
};
|
||||
|
||||
EmbarkJS.Storage.saveText = function(text) {
|
||||
var self = this;
|
||||
if (!this.ipfsConnection) {
|
||||
this.setProvider('ipfs');
|
||||
}
|
||||
EmbarkJS.Storage.IPFS.saveText = function(text) {
|
||||
var promise = new Promise(function(resolve, reject) {
|
||||
self.ipfsConnection.add((new self.ipfsConnection.Buffer(text)), function(err, result) {
|
||||
if (!EmbarkJS.Storage.ipfsConnection) {
|
||||
var connectionError = new Error('No IPFS connection. Please ensure to call Embark.Storage.setProvider()');
|
||||
reject(connectionError);
|
||||
}
|
||||
EmbarkJS.Storage.ipfsConnection.add((new EmbarkJS.Storage.ipfsConnection.Buffer(text)), function(err, result) {
|
||||
if (err) {
|
||||
reject(err);
|
||||
} else {
|
||||
|
@ -175,24 +227,41 @@ EmbarkJS.Storage.saveText = function(text) {
|
|||
return promise;
|
||||
};
|
||||
|
||||
EmbarkJS.Storage.uploadFile = function(inputSelector) {
|
||||
var self = this;
|
||||
EmbarkJS.Storage.IPFS.get = function(hash) {
|
||||
// TODO: detect type, then convert if needed
|
||||
//var ipfsHash = web3.toAscii(hash);
|
||||
var promise = new Promise(function(resolve, reject) {
|
||||
if (!EmbarkJS.Storage.ipfsConnection) {
|
||||
var connectionError = new Error('No IPFS connection. Please ensure to call Embark.Storage.setProvider()');
|
||||
reject(connectionError);
|
||||
}
|
||||
EmbarkJS.Storage.ipfsConnection.object.get([hash]).then(function(node) {
|
||||
resolve(node.data);
|
||||
}).catch(function(err) {
|
||||
reject(err);
|
||||
});
|
||||
});
|
||||
|
||||
return promise;
|
||||
};
|
||||
|
||||
EmbarkJS.Storage.IPFS.uploadFile = function(inputSelector) {
|
||||
var file = inputSelector[0].files[0];
|
||||
|
||||
if (file === undefined) {
|
||||
throw new Error('no file found');
|
||||
}
|
||||
|
||||
if (!this.ipfsConnection) {
|
||||
this.setProvider('ipfs');
|
||||
}
|
||||
|
||||
var promise = new Promise(function(resolve, reject) {
|
||||
if (!EmbarkJS.Storage.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) {
|
||||
var buffer = EmbarkJS.Storage.ipfsConnection.Buffer.from(fileContent);
|
||||
EmbarkJS.Storage.ipfsConnection.add(buffer, function(err, result) {
|
||||
if (err) {
|
||||
reject(err);
|
||||
} else {
|
||||
|
@ -206,33 +275,17 @@ EmbarkJS.Storage.uploadFile = function(inputSelector) {
|
|||
return promise;
|
||||
};
|
||||
|
||||
EmbarkJS.Storage.get = function(hash) {
|
||||
var self = this;
|
||||
// TODO: detect type, then convert if needed
|
||||
//var ipfsHash = web3.toAscii(hash);
|
||||
if (!this.ipfsConnection) {
|
||||
this.setProvider('ipfs');
|
||||
}
|
||||
|
||||
var promise = new Promise(function(resolve, reject) {
|
||||
self.ipfsConnection.object.get([hash]).then(function(node) {
|
||||
resolve(node.data);
|
||||
}).catch(function (err){
|
||||
reject(err);
|
||||
});
|
||||
});
|
||||
|
||||
return promise;
|
||||
};
|
||||
|
||||
EmbarkJS.Storage.getUrl = function(hash) {
|
||||
EmbarkJS.Storage.IPFS.getUrl = function(hash) {
|
||||
//var ipfsHash = web3.toAscii(hash);
|
||||
|
||||
return 'http://localhost:8080/ipfs/' + hash;
|
||||
};
|
||||
|
||||
EmbarkJS.Messages = {
|
||||
};
|
||||
//=========================================================
|
||||
// Embark Messaging
|
||||
//=========================================================
|
||||
|
||||
EmbarkJS.Messages = {};
|
||||
|
||||
EmbarkJS.Messages.setProvider = function(provider, options) {
|
||||
var self = this;
|
||||
|
@ -263,7 +316,7 @@ EmbarkJS.Messages.setProvider = function(provider, options) {
|
|||
this.currentMessages.orbit = new Orbit(ipfs);
|
||||
this.currentMessages.orbit.connect(web3.eth.accounts[0]);
|
||||
} else {
|
||||
throw Error('unknown provider');
|
||||
throw Error('Unknown message provider');
|
||||
}
|
||||
};
|
||||
|
||||
|
@ -275,8 +328,7 @@ EmbarkJS.Messages.listenTo = function(options) {
|
|||
return this.currentMessages.listenTo(options);
|
||||
};
|
||||
|
||||
EmbarkJS.Messages.Whisper = {
|
||||
};
|
||||
EmbarkJS.Messages.Whisper = {};
|
||||
|
||||
EmbarkJS.Messages.Whisper.sendMessage = function(options) {
|
||||
var topics = options.topic || options.topics;
|
||||
|
@ -375,8 +427,7 @@ EmbarkJS.Messages.Whisper.listenTo = function(options) {
|
|||
return promise;
|
||||
};
|
||||
|
||||
EmbarkJS.Messages.Orbit = {
|
||||
};
|
||||
EmbarkJS.Messages.Orbit = {};
|
||||
|
||||
EmbarkJS.Messages.Orbit.sendMessage = function(options) {
|
||||
var topics = options.topic || options.topics;
|
||||
|
|
Loading…
Reference in New Issue