support IPFS
This commit is contained in:
parent
b9c93fe97d
commit
e9e71da81a
|
@ -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);
|
||||
});
|
||||
};
|
||||
|
||||
});
|
File diff suppressed because it is too large
Load Diff
89
js/embark.js
89
js/embark.js
|
@ -1,10 +1,13 @@
|
|||
if (typeof module !== 'undefined') {
|
||||
var Promise = require('bluebird');
|
||||
}
|
||||
var Promise = require('bluebird');
|
||||
//var Ipfs = require('./ipfs.js');
|
||||
|
||||
var EmbarkJS = {
|
||||
};
|
||||
|
||||
|
||||
Ipfs = IpfsApi;
|
||||
//EmbarkJS.Ipfs = Ipfs;
|
||||
|
||||
options = {
|
||||
abi: {},
|
||||
address: {},
|
||||
|
@ -77,6 +80,81 @@ EmbarkJS.Contract.prototype.deploy = function(args) {
|
|||
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 = {
|
||||
};
|
||||
|
||||
|
@ -98,7 +176,4 @@ EmbarkJS.Messages.Whisper.sendMessage = function(options) {
|
|||
EmbarkJS.Messages.Whisper.listenTo = function(options) {
|
||||
};
|
||||
|
||||
|
||||
if (typeof module !== 'undefined') {
|
||||
module.exports = EmbarkJS;
|
||||
}
|
||||
module.exports = EmbarkJS;
|
||||
|
|
File diff suppressed because one or more lines are too long
|
@ -53,9 +53,11 @@ Config.prototype.loadFiles = function(files) {
|
|||
return file.indexOf('.') >= 0;
|
||||
}).filter(function(file) {
|
||||
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: '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 {
|
||||
readFiles.push({filename: file, content: fs.readFileSync(file).toString()});
|
||||
}
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
var Web3 = require('web3');
|
||||
var Deploy = require('./deploy.js');
|
||||
var ContractsManager = require('./contracts.js');
|
||||
var EmbarkJS = require('../js/embark.js');
|
||||
//var EmbarkJS = require('../js/embark.js');
|
||||
|
||||
var initAccounts = function(sim, web3, done) {
|
||||
sim.createAccounts(10, function() {
|
||||
|
|
|
@ -0,0 +1,9 @@
|
|||
module.exports = {
|
||||
entry: './js/embark.js',
|
||||
output: {
|
||||
libraryTarget: 'var',
|
||||
library: 'EmbarkJS',
|
||||
path: './js/build',
|
||||
filename: 'embark.bundle.js'
|
||||
}
|
||||
};
|
Loading…
Reference in New Issue