include ipfs lib in embarkjs

This commit is contained in:
Iuri Matias 2017-12-13 09:01:53 -05:00
parent 3ddd492e83
commit 0b00f99f09
8 changed files with 118 additions and 95 deletions

View File

@ -213,6 +213,8 @@ EmbarkJS.Storage.getUrl = function(hash) {
};
EmbarkJS.Storage.setProvider = function(provider, options) {
console.log("setting provider");
console.log(arguments);
var self = this;
var promise = new Promise(function(resolve, reject) {
if (provider.toLowerCase() === EmbarkJS.Storage.Providers.IPFS) {
@ -229,8 +231,10 @@ EmbarkJS.Storage.setProvider = function(provider, options) {
self.ipfsConnection = IpfsApi(options.server, options.port);
self._getUrl = options.getUrl || "http://localhost:8080/ipfs/";
}
window.ipfsConnection = self.ipfsConnection;
resolve(self);
} catch (err) {
console.log(err);
self.ipfsConnection = null;
reject(new Error('Failed to connect to IPFS'));
}
@ -673,4 +677,6 @@ EmbarkJS.Utils = {
}
};
module.exports = EmbarkJS;
//module.exports = EmbarkJS;
export default EmbarkJS;

View File

@ -1,3 +1,6 @@
let fs = require('../core/fs.js');
var utils = require('../utils/utils.js');
require('ejs');
const Templates = {
utils: require('./code_templates/utils.js.ejs'),
@ -263,6 +266,28 @@ class CodeGenerator {
return contracts;
}
buildEmbarkJS() {
let embarkjsCode = fs.readFileSync(fs.embarkPath('js/embark.js')).toString();
let code = "";
code += "\nimport Web3 from '" + utils.joinPath(fs.embarkPath("js/web3-1.0.min.js")) + "'\n";
code += "\nimport web3 from 'Embark/web3';\n";
if (this.storageConfig !== {} && this.storageConfig.provider === 'ipfs' && this.storageConfig.enabled === true) {
//code += "\nimport IpfsApi from 'Embark/libs/IpfsApi';\n";
code += "\nimport IpfsApi from 'ipfs-api';\n";
}
code += "\n" + embarkjsCode + "\n";
code += this.generateCommunicationInitialization(true);
code += this.generateStorageInitialization(true);
let filePath = utils.joinPath(fs.dappPath(), ".embark", 'embark.js');
fs.mkdirpSync(utils.joinPath(fs.dappPath(), ".embark"));
fs.writeFileSync(filePath, code);
}
}
module.exports = CodeGenerator;

View File

@ -114,6 +114,7 @@ class Engine {
events: self.events
});
codeGenerator.listenToCommands();
codeGenerator.buildEmbarkJS();
self.events.emit('code-generator-ready');
};

View File

@ -23,7 +23,6 @@ class Pipeline {
this.buildContracts(contractsJSON);
self.buildWeb3JS(function() {
// limit:1 due to issues when downloading required files such as web3.js
@ -36,8 +35,10 @@ class Pipeline {
if (file.filename.indexOf('.js') >= 0) {
let importsList = {};
importsList["Embark/EmbarkJS"] = fs.embarkPath("js/embark.js");
//importsList["Embark/EmbarkJS"] = fs.embarkPath("js/embark.js");
importsList["Embark/EmbarkJS"] = utils.joinPath(fs.dappPath(), ".embark", 'embark.js');
importsList["Embark/web3"] = utils.joinPath(fs.dappPath(), ".embark", 'web3_instance.js');
importsList["Embark/libs/IpfsApi"] = fs.embarkPath('js/ipfs.js');
async.waterfall([
function findImports(next) {
@ -93,6 +94,7 @@ class Pipeline {
externals: function(context, request, callback) {
if (request === "Embark/contracts/all") {
return callback(null, fs.readFileSync(utils.joinPath(fs.dappPath(), '.embark', 'embark.js')));
// should be .toString() ?
}
callback();
},

View File

@ -30,7 +30,7 @@
"follow-redirects": "^1.2.4",
"fs-extra": "^2.0.0",
"globule": "^1.1.0",
"ipfs-api": "^14.0.3",
"ipfs-api": "6.0.2",
"merge": "^1.2.0",
"mocha": "^2.2.5",
"orbit-db": "^0.17.3",

View File

@ -2,27 +2,13 @@
import $ from './_vendor/jquery.min';
//import web3 from 'Embark/web3';
//console.log("new_index");
//console.log(web3);
//window.web3 = web3;
//console.log("finished importing web3");
import EmbarkJS from 'Embark/EmbarkJS';
window.EmbarkJS = EmbarkJS;
import SimpleStorage from 'Embark/contracts/SimpleStorage';
//import web3 from 'Embark/web3';
console.log("SimpleStorage")
console.log(SimpleStorage);
//console.log(EmbarkJS);
window.SimpleStorage = SimpleStorage;
import test3 from './foo';
import './foo.css';
console.log(test3);
////window.test_3 = test3;
////
var addToLog = function(id, txt) {
$(id + " .logs").append("<br>" + txt);
};
@ -32,7 +18,7 @@ var addToLog = function(id, txt) {
// ===========================
$(document).ready(function() {
console.log([1,2,3].map(v => v + 1));
alert('hello');
//alert('hello');
$("#blockchain button.set").click(function() {
var value = parseInt($("#blockchain input.text").val(), 10);
@ -65,80 +51,80 @@ $(document).ready(function() {
});
////// ===========================
////// Storage (IPFS) example
////// ===========================
////$(document).ready(function() {
//// // automatic set if config/storage.json has "enabled": true and "provider": "ipfs"
//// //EmbarkJS.Storage.setProvider('ipfs',{server: 'localhost', port: '5001'});
////
//// $("#storage .error").hide();
//// EmbarkJS.Storage.ipfsConnection.ping()
//// .then(function(){
//// $("#status-storage").addClass('status-online');
//// $("#storage-controls").show();
//// })
//// .catch(function(err) {
//// if(err){
//// console.log("IPFS Connection Error => " + err.message);
//// $("#storage .error").show();
//// $("#status-storage").addClass('status-offline');
//// $("#storage-controls").hide();
//// }
//// });
////
//// $("#storage button.setIpfsText").click(function() {
//// var value = $("#storage input.ipfsText").val();
//// EmbarkJS.Storage.saveText(value).then(function(hash) {
//// $("span.textHash").html(hash);
//// $("input.textHash").val(hash);
//// addToLog("#storage", "EmbarkJS.Storage.saveText('" + value + "').then(function(hash) { })");
//// })
//// .catch(function(err) {
//// if(err){
//// console.log("IPFS saveText Error => " + err.message);
//// }
//// });
//// });
////
//// $("#storage button.loadIpfsHash").click(function() {
//// var value = $("#storage input.textHash").val();
//// EmbarkJS.Storage.get(value).then(function(content) {
//// $("span.ipfsText").html(content);
//// addToLog("#storage", "EmbarkJS.Storage.get('" + value + "').then(function(content) { })");
//// })
//// .catch(function(err) {
//// if(err){
//// console.log("IPFS get Error => " + err.message);
//// }
//// });
//// });
////
//// $("#storage button.uploadFile").click(function() {
//// var input = $("#storage input[type=file]");
//// EmbarkJS.Storage.uploadFile(input).then(function(hash) {
//// $("span.fileIpfsHash").html(hash);
//// $("input.fileIpfsHash").val(hash);
//// addToLog("#storage", "EmbarkJS.Storage.uploadFile($('input[type=file]')).then(function(hash) { })");
//// })
//// .catch(function(err) {
//// if(err){
//// console.log("IPFS uploadFile Error => " + err.message);
//// }
//// });
//// });
////
//// $("#storage button.loadIpfsFile").click(function() {
//// var hash = $("#storage input.fileIpfsHash").val();
//// var url = EmbarkJS.Storage.getUrl(hash);
//// var link = '<a href="' + url + '" target="_blank">' + url + '</a>';
//// $("span.ipfsFileUrl").html(link);
//// $(".ipfsImage").attr('src', url);
//// addToLog("#storage", "EmbarkJS.Storage.getUrl('" + hash + "')");
//// });
////
////});
////
// ===========================
// Storage (IPFS) example
// ===========================
$(document).ready(function() {
// automatic set if config/storage.json has "enabled": true and "provider": "ipfs"
//EmbarkJS.Storage.setProvider('ipfs',{server: 'localhost', port: '5001'});
$("#storage .error").hide();
EmbarkJS.Storage.ipfsConnection.ping()
.then(function(){
$("#status-storage").addClass('status-online');
$("#storage-controls").show();
})
.catch(function(err) {
if(err){
console.log("IPFS Connection Error => " + err.message);
$("#storage .error").show();
$("#status-storage").addClass('status-offline');
$("#storage-controls").hide();
}
});
$("#storage button.setIpfsText").click(function() {
var value = $("#storage input.ipfsText").val();
EmbarkJS.Storage.saveText(value).then(function(hash) {
$("span.textHash").html(hash);
$("input.textHash").val(hash);
addToLog("#storage", "EmbarkJS.Storage.saveText('" + value + "').then(function(hash) { })");
})
.catch(function(err) {
if(err){
console.log("IPFS saveText Error => " + err.message);
}
});
});
$("#storage button.loadIpfsHash").click(function() {
var value = $("#storage input.textHash").val();
EmbarkJS.Storage.get(value).then(function(content) {
$("span.ipfsText").html(content);
addToLog("#storage", "EmbarkJS.Storage.get('" + value + "').then(function(content) { })");
})
.catch(function(err) {
if(err){
console.log("IPFS get Error => " + err.message);
}
});
});
$("#storage button.uploadFile").click(function() {
var input = $("#storage input[type=file]");
EmbarkJS.Storage.uploadFile(input).then(function(hash) {
$("span.fileIpfsHash").html(hash);
$("input.fileIpfsHash").val(hash);
addToLog("#storage", "EmbarkJS.Storage.uploadFile($('input[type=file]')).then(function(hash) { })");
})
.catch(function(err) {
if(err){
console.log("IPFS uploadFile Error => " + err.message);
}
});
});
$("#storage button.loadIpfsFile").click(function() {
var hash = $("#storage input.fileIpfsHash").val();
var url = EmbarkJS.Storage.getUrl(hash);
var link = '<a href="' + url + '" target="_blank">' + url + '</a>';
$("span.ipfsFileUrl").html(link);
$(".ipfsImage").attr('src', url);
addToLog("#storage", "EmbarkJS.Storage.getUrl('" + hash + "')");
});
});
////// ===========================
////// Communication (Whisper) example
////// ===========================

View File

@ -2,7 +2,8 @@
<head>
<title>Embark - SimpleStorage Demo</title>
<link rel="stylesheet" href="css/app.css">
<script src="js/embark.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.11.3/jquery.js"></script>
<script src="js/bootstrap.js"></script>
<script src="js/new_app.js"></script>
</head>
<body class="container">

View File

@ -2,6 +2,8 @@
"contracts": ["app/contracts/**"],
"app": {
"js/webpack_test.js": "app/js/index.js",
"js/jquery.js": "app/js/_vendor/jquery.min.js",
"js/bootstrap.js": "app/js/_vendor/bootstrap.min.js",
"css/app.css": ["app/css/**"],
"images/": ["app/images/**"],
"js/app.js": ["app/js/_vendor/jquery.min.js", "app/js/_vendor/bootstrap.min.js", "app/js/**", "!app/js/test.js", "!app/js/foo.js", "!app/js/foo.css", "!app/js/new_index.js"],