support IPFS

This commit is contained in:
Iuri Matias 2016-08-26 07:01:22 -04:00
parent b9c93fe97d
commit e9e71da81a
7 changed files with 67660 additions and 10 deletions

32
js/_ipfs.js Normal file
View File

@ -0,0 +1,32 @@
$(document).ready(function() {
window.ipfsConnection = window.IpfsApi('localhost', '5001');
//ipfs.object.put
//ipfs.add((new ipfs.Buffer("heydudehowareyou")), function(err, result) { console.log(result[0].path) })
// see https://github.com/ConsenSys/ipfs.js for conversion to hex
// id = "QmdAvyxUkvJFYeHmBFVkfDdYZvyummU32Q2MNDKbwrCMvP"
//SimpleStorage.setHash(id)
//web3.toAscii(SimpleStorage.foo())
window.saveFileToIpfs = function(file, cb) {
var reader = new FileReader();
reader.onloadend = function() {
var fileContent = reader.result;
var buffer = ipfsConnection.Buffer.from(fileContent);
ipfsConnection.add(buffer, function(err, result) {
cb(err, result[0].path);
});
};
reader.readAsArrayBuffer(file);
};
window.saveTextToIpfs = function(content, cb) {
ipfsConnection.add((new ipfsConnection.Buffer(content)), function(err, result) {
cb(err, result[0].path);
});
};
});

5856
js/build/embark.bundle.js Normal file

File diff suppressed because it is too large Load Diff

View File

@ -1,10 +1,13 @@
if (typeof module !== 'undefined') { var Promise = require('bluebird');
var Promise = require('bluebird'); //var Ipfs = require('./ipfs.js');
}
var EmbarkJS = { var EmbarkJS = {
}; };
Ipfs = IpfsApi;
//EmbarkJS.Ipfs = Ipfs;
options = { options = {
abi: {}, abi: {},
address: {}, address: {},
@ -77,6 +80,81 @@ EmbarkJS.Contract.prototype.deploy = function(args) {
return promise; return promise;
}; };
EmbarkJS.IPFS = 'ipfs';
EmbarkJS.Storage = {
};
// EmbarkJS.Storage.setProvider('ipfs',{server: 'localhost', port: '5001'})<F37>
//{server: localhost, port: 5001};
EmbarkJS.Storage.setProvider = function(provider, options) {
if (provider === 'ipfs') {
this.currentStorage = EmbarkJS.Storage.IPFS;
this.ipfsConnection = Ipfs(options.server, options.port);
} else {
throw Error('unknown provider');
}
};
EmbarkJS.Storage.saveText = function(text) {
var self = this;
var promise = new Promise(function(resolve, reject) {
self.ipfsConnection.add((new self.ipfsConnection.Buffer(text)), function(err, result) {
if (err) {
reject(err);
} else {
resolve(result[0].path);
}
});
});
return promise;
};
EmbarkJS.Storage.uploadFile = function(inputSelector) {
var self = this;
var file = inputSelector[0].files[0];
var promise = new Promise(function(resolve, reject) {
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;
};
EmbarkJS.Storage.get = function(hash) {
var self = this;
var ipfsHash = this.web3.toAscii(hash);
var promise = new Promise(function(resolve, reject) {
self.ipfsConnection.object.get([ipfsHash]).then(function(node) {
resolve(node.data);
});
});
return promise;
};
EmbarkJS.Storage.getUrl = function(hash) {
var self = this;
var ipfsHash = web3.toAscii(hash);
return 'http://localhost:8080/ipfs/' + ipfsHash;
};
EmbarkJS.Messages = { EmbarkJS.Messages = {
}; };
@ -98,7 +176,4 @@ EmbarkJS.Messages.Whisper.sendMessage = function(options) {
EmbarkJS.Messages.Whisper.listenTo = function(options) { EmbarkJS.Messages.Whisper.listenTo = function(options) {
}; };
module.exports = EmbarkJS;
if (typeof module !== 'undefined') {
module.exports = EmbarkJS;
}

61676
js/ipfs.js Normal file

File diff suppressed because one or more lines are too long

View File

@ -53,9 +53,11 @@ Config.prototype.loadFiles = function(files) {
return file.indexOf('.') >= 0; return file.indexOf('.') >= 0;
}).filter(function(file) { }).filter(function(file) {
if (file === 'embark.js') { if (file === 'embark.js') {
readFiles.push({filename: 'bluebird.js', content: fs.readFileSync("../js/bluebird.js").toString()}); //readFiles.push({filename: 'bluebird.js', content: fs.readFileSync("../js/bluebird.js").toString()});
readFiles.push({filename: 'web3.js', content: fs.readFileSync("../js/web3.js").toString()}); readFiles.push({filename: 'web3.js', content: fs.readFileSync("../js/web3.js").toString()});
readFiles.push({filename: 'embark.js', content: fs.readFileSync("../js/embark.js").toString()}); //readFiles.push({filename: 'embark.js', content: fs.readFileSync("../js/ipfs.js").toString()+ fs.readFileSync("../js/build/embark.bundle.js").toString()});
readFiles.push({filename: 'ipfs.js', content: fs.readFileSync("../js/ipfs.js").toString()});
readFiles.push({filename: 'embark.js', content: fs.readFileSync("../js/build/embark.bundle.js").toString()});
} else { } else {
readFiles.push({filename: file, content: fs.readFileSync(file).toString()}); readFiles.push({filename: file, content: fs.readFileSync(file).toString()});
} }

View File

@ -1,7 +1,7 @@
var Web3 = require('web3'); var Web3 = require('web3');
var Deploy = require('./deploy.js'); var Deploy = require('./deploy.js');
var ContractsManager = require('./contracts.js'); var ContractsManager = require('./contracts.js');
var EmbarkJS = require('../js/embark.js'); //var EmbarkJS = require('../js/embark.js');
var initAccounts = function(sim, web3, done) { var initAccounts = function(sim, web3, done) {
sim.createAccounts(10, function() { sim.createAccounts(10, function() {

9
webpack.config.js Normal file
View File

@ -0,0 +1,9 @@
module.exports = {
entry: './js/embark.js',
output: {
libraryTarget: 'var',
library: 'EmbarkJS',
path: './js/build',
filename: 'embark.bundle.js'
}
};