mirror of
https://github.com/status-im/embark-area-51.git
synced 2025-01-11 14:34:40 +00:00
Merge branch 'develop' of github.com:iurimatias/embark-framework into develop
This commit is contained in:
commit
915fef9580
@ -44,23 +44,33 @@ $(document).ready(function() {
|
||||
$("#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);
|
||||
}
|
||||
});
|
||||
addToLog("#storage", "EmbarkJS.Storage.saveText('" + value + "').then(function(hash) { })");
|
||||
});
|
||||
|
||||
$("#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);
|
||||
}
|
||||
});
|
||||
addToLog("#storage", "EmbarkJS.Storage.get('" + value + "').then(function(content) { })");
|
||||
});
|
||||
|
||||
$("#storage button.uploadFile").click(function() {
|
||||
@ -68,8 +78,13 @@ $(document).ready(function() {
|
||||
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);
|
||||
}
|
||||
});
|
||||
addToLog("#storage", "EmbarkJS.Storage.uploadFile($('input[type=file]')).then(function(hash) { })");
|
||||
});
|
||||
|
||||
$("#storage button.loadIpfsFile").click(function() {
|
||||
|
@ -13,13 +13,25 @@ The current available storage is IPFS. it can be initialized as
|
||||
|
||||
.. code:: javascript
|
||||
|
||||
EmbarkJS.Storage.saveText("hello world").then(function(hash) {});
|
||||
EmbarkJS.Storage.saveText("hello world")
|
||||
.then(function(hash) {})
|
||||
.catch(function(err) {
|
||||
if(err){
|
||||
console.log("IPFS saveText Error => " + err.message);
|
||||
}
|
||||
});
|
||||
|
||||
**Retrieving Data/Text**
|
||||
|
||||
.. code:: javascript
|
||||
|
||||
EmbarkJS.Storage.get(hash).then(function(content) {});
|
||||
EmbarkJS.Storage.get(hash)
|
||||
.then(function(content) {})
|
||||
.catch(function(err) {
|
||||
if(err){
|
||||
console.log("IPFS get Error => " + err.message);
|
||||
}
|
||||
});
|
||||
|
||||
**Uploading a file**
|
||||
|
||||
@ -30,7 +42,13 @@ The current available storage is IPFS. it can be initialized as
|
||||
.. code:: javascript
|
||||
|
||||
var input = $("input[type=file"]);
|
||||
EmbarkJS.Storage.uploadFile(input).then(function(hash) {});
|
||||
EmbarkJS.Storage.uploadFile(input)
|
||||
.then(function(hash) {})
|
||||
.catch(function(err) {
|
||||
if(err){
|
||||
console.log("IPFS uploadFile Error => " + err.message);
|
||||
}
|
||||
});
|
||||
|
||||
**Generate URL to file**
|
||||
|
||||
|
@ -2,496 +2,526 @@ var EmbarkJS =
|
||||
/******/ (function(modules) { // webpackBootstrap
|
||||
/******/ // The module cache
|
||||
/******/ var installedModules = {};
|
||||
|
||||
/******/
|
||||
/******/ // The require function
|
||||
/******/ function __webpack_require__(moduleId) {
|
||||
|
||||
/******/
|
||||
/******/ // Check if module is in cache
|
||||
/******/ if(installedModules[moduleId])
|
||||
/******/ return installedModules[moduleId].exports;
|
||||
|
||||
/******/
|
||||
/******/ // Create a new module (and put it into the cache)
|
||||
/******/ var module = installedModules[moduleId] = {
|
||||
/******/ exports: {},
|
||||
/******/ id: moduleId,
|
||||
/******/ loaded: false
|
||||
/******/ i: moduleId,
|
||||
/******/ l: false,
|
||||
/******/ exports: {}
|
||||
/******/ };
|
||||
|
||||
/******/
|
||||
/******/ // Execute the module function
|
||||
/******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__);
|
||||
|
||||
/******/
|
||||
/******/ // Flag the module as loaded
|
||||
/******/ module.loaded = true;
|
||||
|
||||
/******/ module.l = true;
|
||||
/******/
|
||||
/******/ // Return the exports of the module
|
||||
/******/ return module.exports;
|
||||
/******/ }
|
||||
|
||||
|
||||
/******/
|
||||
/******/
|
||||
/******/ // expose the modules object (__webpack_modules__)
|
||||
/******/ __webpack_require__.m = modules;
|
||||
|
||||
/******/
|
||||
/******/ // expose the module cache
|
||||
/******/ __webpack_require__.c = installedModules;
|
||||
|
||||
/******/
|
||||
/******/ // identity function for calling harmony imports with the correct context
|
||||
/******/ __webpack_require__.i = function(value) { return value; };
|
||||
/******/
|
||||
/******/ // define getter function for harmony exports
|
||||
/******/ __webpack_require__.d = function(exports, name, getter) {
|
||||
/******/ if(!__webpack_require__.o(exports, name)) {
|
||||
/******/ Object.defineProperty(exports, name, {
|
||||
/******/ configurable: false,
|
||||
/******/ enumerable: true,
|
||||
/******/ get: getter
|
||||
/******/ });
|
||||
/******/ }
|
||||
/******/ };
|
||||
/******/
|
||||
/******/ // getDefaultExport function for compatibility with non-harmony modules
|
||||
/******/ __webpack_require__.n = function(module) {
|
||||
/******/ var getter = module && module.__esModule ?
|
||||
/******/ function getDefault() { return module['default']; } :
|
||||
/******/ function getModuleExports() { return module; };
|
||||
/******/ __webpack_require__.d(getter, 'a', getter);
|
||||
/******/ return getter;
|
||||
/******/ };
|
||||
/******/
|
||||
/******/ // Object.prototype.hasOwnProperty.call
|
||||
/******/ __webpack_require__.o = function(object, property) { return Object.prototype.hasOwnProperty.call(object, property); };
|
||||
/******/
|
||||
/******/ // __webpack_public_path__
|
||||
/******/ __webpack_require__.p = "";
|
||||
|
||||
/******/
|
||||
/******/ // Load entry module and return exports
|
||||
/******/ return __webpack_require__(0);
|
||||
/******/ return __webpack_require__(__webpack_require__.s = 0);
|
||||
/******/ })
|
||||
/************************************************************************/
|
||||
/******/ ([
|
||||
/* 0 */
|
||||
/***/ function(module, exports) {
|
||||
|
||||
/*jshint esversion: 6 */
|
||||
//var Ipfs = require('./ipfs.js');
|
||||
|
||||
var EmbarkJS = {
|
||||
};
|
||||
|
||||
EmbarkJS.Contract = function(options) {
|
||||
var self = this;
|
||||
var i, abiElement;
|
||||
|
||||
this.abi = options.abi;
|
||||
this.address = options.address;
|
||||
this.code = '0x' + options.code;
|
||||
this.web3 = options.web3 || web3;
|
||||
|
||||
var ContractClass = this.web3.eth.contract(this.abi);
|
||||
|
||||
this.eventList = [];
|
||||
|
||||
if (this.abi) {
|
||||
for (i = 0; i < this.abi.length; i++) {
|
||||
abiElement = this.abi[i];
|
||||
if (abiElement.type === 'event') {
|
||||
this.eventList.push(abiElement.name);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
var messageEvents = function() {
|
||||
this.cb = function() {};
|
||||
};
|
||||
|
||||
messageEvents.prototype.then = function(cb) {
|
||||
this.cb = cb;
|
||||
};
|
||||
|
||||
messageEvents.prototype.error = function(err) {
|
||||
return err;
|
||||
};
|
||||
|
||||
this._originalContractObject = ContractClass.at(this.address);
|
||||
this._methods = Object.getOwnPropertyNames(this._originalContractObject).filter(function (p) {
|
||||
// TODO: check for forbidden properties
|
||||
if (self.eventList.indexOf(p) >= 0) {
|
||||
|
||||
self[p] = function() {
|
||||
var promise = new messageEvents();
|
||||
var args = Array.prototype.slice.call(arguments);
|
||||
args.push(function(err, result) {
|
||||
if (err) {
|
||||
promise.error(err);
|
||||
} else {
|
||||
promise.cb(result);
|
||||
}
|
||||
});
|
||||
|
||||
self._originalContractObject[p].apply(self._originalContractObject[p], args);
|
||||
return promise;
|
||||
};
|
||||
return true;
|
||||
} else if (typeof self._originalContractObject[p] === 'function') {
|
||||
self[p] = function(_args) {
|
||||
var args = Array.prototype.slice.call(arguments);
|
||||
var fn = self._originalContractObject[p];
|
||||
var props = self.abi.find((x) => x.name == p);
|
||||
|
||||
var promise = new Promise(function(resolve, reject) {
|
||||
args.push(function(err, transaction) {
|
||||
promise.tx = transaction;
|
||||
if (err) {
|
||||
return reject(err);
|
||||
}
|
||||
|
||||
var getConfirmation = function() {
|
||||
self.web3.eth.getTransactionReceipt(transaction, function(err, receipt) {
|
||||
if (err) {
|
||||
return reject(err);
|
||||
}
|
||||
|
||||
if (receipt !== null) {
|
||||
return resolve(receipt);
|
||||
}
|
||||
|
||||
setTimeout(getConfirmation, 1000);
|
||||
});
|
||||
};
|
||||
|
||||
if (typeof(transaction) !== "string" || props.constant) {
|
||||
resolve(transaction);
|
||||
} else {
|
||||
getConfirmation();
|
||||
}
|
||||
});
|
||||
|
||||
fn.apply(fn, args);
|
||||
});
|
||||
|
||||
return promise;
|
||||
};
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
});
|
||||
};
|
||||
|
||||
EmbarkJS.Contract.prototype.deploy = function(args, _options) {
|
||||
var self = this;
|
||||
var contractParams;
|
||||
var options = _options || {};
|
||||
|
||||
contractParams = args || [];
|
||||
|
||||
contractParams.push({
|
||||
from: this.web3.eth.accounts[0],
|
||||
data: this.code,
|
||||
gas: options.gas || 800000
|
||||
});
|
||||
|
||||
var contractObject = this.web3.eth.contract(this.abi);
|
||||
|
||||
var promise = new Promise(function(resolve, reject) {
|
||||
contractParams.push(function(err, transaction) {
|
||||
if (err) {
|
||||
reject(err);
|
||||
} else if (transaction.address !== undefined) {
|
||||
resolve(new EmbarkJS.Contract({abi: self.abi, code: self.code, address: transaction.address}));
|
||||
}
|
||||
});
|
||||
|
||||
// returns promise
|
||||
// deploys contract
|
||||
// wraps it around EmbarkJS.Contract
|
||||
contractObject["new"].apply(contractObject, contractParams);
|
||||
});
|
||||
|
||||
|
||||
return promise;
|
||||
};
|
||||
|
||||
EmbarkJS.IPFS = 'ipfs';
|
||||
|
||||
EmbarkJS.Storage = {
|
||||
};
|
||||
|
||||
EmbarkJS.Storage.setProvider = function(provider, options) {
|
||||
if (provider === 'ipfs') {
|
||||
this.currentStorage = EmbarkJS.Storage.IPFS;
|
||||
if (options === undefined) {
|
||||
this.ipfsConnection = IpfsApi('localhost', '5001');
|
||||
} else {
|
||||
this.ipfsConnection = IpfsApi(options.server, options.port);
|
||||
}
|
||||
} else {
|
||||
throw Error('unknown provider');
|
||||
}
|
||||
};
|
||||
|
||||
EmbarkJS.Storage.saveText = function(text) {
|
||||
var self = this;
|
||||
if (!this.ipfsConnection) {
|
||||
this.setProvider('ipfs');
|
||||
}
|
||||
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];
|
||||
|
||||
if (file === undefined) {
|
||||
throw new Error('no file found');
|
||||
}
|
||||
|
||||
if (!this.ipfsConnection) {
|
||||
this.setProvider('ipfs');
|
||||
}
|
||||
|
||||
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;
|
||||
// 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);
|
||||
});
|
||||
});
|
||||
|
||||
return promise;
|
||||
};
|
||||
|
||||
EmbarkJS.Storage.getUrl = function(hash) {
|
||||
//var ipfsHash = web3.toAscii(hash);
|
||||
|
||||
return 'http://localhost:8080/ipfs/' + hash;
|
||||
};
|
||||
|
||||
EmbarkJS.Messages = {
|
||||
};
|
||||
|
||||
EmbarkJS.Messages.setProvider = function(provider, options) {
|
||||
var self = this;
|
||||
var ipfs;
|
||||
if (provider === 'whisper') {
|
||||
this.currentMessages = EmbarkJS.Messages.Whisper;
|
||||
if (typeof variable === 'undefined') {
|
||||
if (options === undefined) {
|
||||
web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:8545"));
|
||||
} else {
|
||||
web3 = new Web3(new Web3.providers.HttpProvider("http://" + options.server + ':' + options.port));
|
||||
}
|
||||
}
|
||||
web3.version.getWhisper(function(err, res) {
|
||||
if (err) {
|
||||
console.log("whisper not available");
|
||||
} else {
|
||||
self.currentMessages.identity = web3.shh.newIdentity();
|
||||
}
|
||||
});
|
||||
} else if (provider === 'orbit') {
|
||||
this.currentMessages = EmbarkJS.Messages.Orbit;
|
||||
if (options === undefined) {
|
||||
ipfs = HaadIpfsApi('localhost', '5001');
|
||||
} else {
|
||||
ipfs = HaadIpfsApi(options.server, options.port);
|
||||
}
|
||||
this.currentMessages.orbit = new Orbit(ipfs);
|
||||
this.currentMessages.orbit.connect(web3.eth.accounts[0]);
|
||||
} else {
|
||||
throw Error('unknown provider');
|
||||
}
|
||||
};
|
||||
|
||||
EmbarkJS.Messages.sendMessage = function(options) {
|
||||
return this.currentMessages.sendMessage(options);
|
||||
};
|
||||
|
||||
EmbarkJS.Messages.listenTo = function(options) {
|
||||
return this.currentMessages.listenTo(options);
|
||||
};
|
||||
|
||||
EmbarkJS.Messages.Whisper = {
|
||||
};
|
||||
|
||||
EmbarkJS.Messages.Whisper.sendMessage = function(options) {
|
||||
var topics = options.topic || options.topics;
|
||||
var data = options.data || options.payload;
|
||||
var identity = options.identity || this.identity || web3.shh.newIdentity();
|
||||
var ttl = options.ttl || 100;
|
||||
var priority = options.priority || 1000;
|
||||
var _topics;
|
||||
|
||||
if (topics === undefined) {
|
||||
throw new Error("missing option: topic");
|
||||
}
|
||||
|
||||
if (data === undefined) {
|
||||
throw new Error("missing option: data");
|
||||
}
|
||||
|
||||
// do fromAscii to each topics unless it's already a string
|
||||
if (typeof topics === 'string') {
|
||||
_topics = [web3.fromAscii(topics)];
|
||||
} else {
|
||||
// TODO: replace with es6 + babel;
|
||||
for (var i = 0; i < topics.length; i++) {
|
||||
_topics.push(web3.fromAscii(topics[i]));
|
||||
}
|
||||
}
|
||||
topics = _topics;
|
||||
|
||||
var payload = JSON.stringify(data);
|
||||
|
||||
var message = {
|
||||
from: identity,
|
||||
topics: topics,
|
||||
payload: web3.fromAscii(payload),
|
||||
ttl: ttl,
|
||||
priority: priority
|
||||
};
|
||||
|
||||
return web3.shh.post(message, function() {});
|
||||
};
|
||||
|
||||
EmbarkJS.Messages.Whisper.listenTo = function(options) {
|
||||
var topics = options.topic || options.topics;
|
||||
var _topics = [];
|
||||
|
||||
if (typeof topics === 'string') {
|
||||
_topics = [topics];
|
||||
} else {
|
||||
// TODO: replace with es6 + babel;
|
||||
for (var i = 0; i < topics.length; i++) {
|
||||
_topics.push(topics[i]);
|
||||
}
|
||||
}
|
||||
topics = _topics;
|
||||
|
||||
var filterOptions = {
|
||||
topics: topics
|
||||
};
|
||||
|
||||
var messageEvents = function() {
|
||||
this.cb = function() {};
|
||||
};
|
||||
|
||||
messageEvents.prototype.then = function(cb) {
|
||||
this.cb = cb;
|
||||
};
|
||||
|
||||
messageEvents.prototype.error = function(err) {
|
||||
return err;
|
||||
};
|
||||
|
||||
messageEvents.prototype.stop = function() {
|
||||
this.filter.stopWatching();
|
||||
};
|
||||
|
||||
var promise = new messageEvents();
|
||||
|
||||
var filter = web3.shh.filter(filterOptions, function(err, result) {
|
||||
var payload = JSON.parse(web3.toAscii(result.payload));
|
||||
var data;
|
||||
if (err) {
|
||||
promise.error(err);
|
||||
} else {
|
||||
data = {
|
||||
topic: topics,
|
||||
data: payload,
|
||||
from: result.from,
|
||||
time: (new Date(result.sent * 1000))
|
||||
};
|
||||
promise.cb(payload, data, result);
|
||||
}
|
||||
});
|
||||
|
||||
promise.filter = filter;
|
||||
|
||||
return promise;
|
||||
};
|
||||
|
||||
EmbarkJS.Messages.Orbit = {
|
||||
};
|
||||
|
||||
EmbarkJS.Messages.Orbit.sendMessage = function(options) {
|
||||
var topics = options.topic || options.topics;
|
||||
var data = options.data || options.payload;
|
||||
|
||||
if (topics === undefined) {
|
||||
throw new Error("missing option: topic");
|
||||
}
|
||||
|
||||
if (data === undefined) {
|
||||
throw new Error("missing option: data");
|
||||
}
|
||||
|
||||
if (typeof topics === 'string') {
|
||||
topics = topics;
|
||||
} else {
|
||||
// TODO: better to just send to different channels instead
|
||||
topics = topics.join(',');
|
||||
}
|
||||
|
||||
this.orbit.join(topics);
|
||||
|
||||
var payload = JSON.stringify(data);
|
||||
|
||||
this.orbit.send(topics, data);
|
||||
};
|
||||
|
||||
EmbarkJS.Messages.Orbit.listenTo = function(options) {
|
||||
var self = this;
|
||||
var topics = options.topic || options.topics;
|
||||
|
||||
if (typeof topics === 'string') {
|
||||
topics = topics;
|
||||
} else {
|
||||
topics = topics.join(',');
|
||||
}
|
||||
|
||||
this.orbit.join(topics);
|
||||
|
||||
var messageEvents = function() {
|
||||
this.cb = function() {};
|
||||
};
|
||||
|
||||
messageEvents.prototype.then = function(cb) {
|
||||
this.cb = cb;
|
||||
};
|
||||
|
||||
messageEvents.prototype.error = function(err) {
|
||||
return err;
|
||||
};
|
||||
|
||||
var promise = new messageEvents();
|
||||
|
||||
this.orbit.events.on('message', (channel, message) => {
|
||||
// TODO: looks like sometimes it's receving messages from all topics
|
||||
if (topics !== channel) return;
|
||||
self.orbit.getPost(message.payload.value, true).then((post) => {
|
||||
var data = {
|
||||
topic: channel,
|
||||
data: post.content,
|
||||
from: post.meta.from.name,
|
||||
time: (new Date(post.meta.ts))
|
||||
};
|
||||
promise.cb(post.content, data, post);
|
||||
});
|
||||
});
|
||||
|
||||
return promise;
|
||||
};
|
||||
|
||||
module.exports = EmbarkJS;
|
||||
|
||||
|
||||
/***/ }
|
||||
/***/ (function(module, exports) {
|
||||
|
||||
/*jshint esversion: 6 */
|
||||
//var Ipfs = require('./ipfs.js');
|
||||
|
||||
var EmbarkJS = {
|
||||
};
|
||||
|
||||
EmbarkJS.Contract = function(options) {
|
||||
var self = this;
|
||||
var i, abiElement;
|
||||
|
||||
this.abi = options.abi;
|
||||
this.address = options.address;
|
||||
this.code = '0x' + options.code;
|
||||
this.web3 = options.web3 || web3;
|
||||
|
||||
var ContractClass = this.web3.eth.contract(this.abi);
|
||||
|
||||
this.eventList = [];
|
||||
|
||||
if (this.abi) {
|
||||
for (i = 0; i < this.abi.length; i++) {
|
||||
abiElement = this.abi[i];
|
||||
if (abiElement.type === 'event') {
|
||||
this.eventList.push(abiElement.name);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
var messageEvents = function() {
|
||||
this.cb = function() {};
|
||||
};
|
||||
|
||||
messageEvents.prototype.then = function(cb) {
|
||||
this.cb = cb;
|
||||
};
|
||||
|
||||
messageEvents.prototype.error = function(err) {
|
||||
return err;
|
||||
};
|
||||
|
||||
this._originalContractObject = ContractClass.at(this.address);
|
||||
this._methods = Object.getOwnPropertyNames(this._originalContractObject).filter(function (p) {
|
||||
// TODO: check for forbidden properties
|
||||
if (self.eventList.indexOf(p) >= 0) {
|
||||
|
||||
self[p] = function() {
|
||||
var promise = new messageEvents();
|
||||
var args = Array.prototype.slice.call(arguments);
|
||||
args.push(function(err, result) {
|
||||
if (err) {
|
||||
promise.error(err);
|
||||
} else {
|
||||
promise.cb(result);
|
||||
}
|
||||
});
|
||||
|
||||
self._originalContractObject[p].apply(self._originalContractObject[p], args);
|
||||
return promise;
|
||||
};
|
||||
return true;
|
||||
} else if (typeof self._originalContractObject[p] === 'function') {
|
||||
self[p] = function(_args) {
|
||||
var args = Array.prototype.slice.call(arguments);
|
||||
var fn = self._originalContractObject[p];
|
||||
var props = self.abi.find((x) => x.name == p);
|
||||
|
||||
var promise = new Promise(function(resolve, reject) {
|
||||
args.push(function(err, transaction) {
|
||||
promise.tx = transaction;
|
||||
if (err) {
|
||||
return reject(err);
|
||||
}
|
||||
|
||||
var getConfirmation = function() {
|
||||
self.web3.eth.getTransactionReceipt(transaction, function(err, receipt) {
|
||||
if (err) {
|
||||
return reject(err);
|
||||
}
|
||||
|
||||
if (receipt !== null) {
|
||||
return resolve(receipt);
|
||||
}
|
||||
|
||||
setTimeout(getConfirmation, 1000);
|
||||
});
|
||||
};
|
||||
|
||||
if (typeof(transaction) !== "string" || props.constant) {
|
||||
resolve(transaction);
|
||||
} else {
|
||||
getConfirmation();
|
||||
}
|
||||
});
|
||||
|
||||
fn.apply(fn, args);
|
||||
});
|
||||
|
||||
return promise;
|
||||
};
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
});
|
||||
};
|
||||
|
||||
EmbarkJS.Contract.prototype.deploy = function(args, _options) {
|
||||
var self = this;
|
||||
var contractParams;
|
||||
var options = _options || {};
|
||||
|
||||
contractParams = args || [];
|
||||
|
||||
contractParams.push({
|
||||
from: this.web3.eth.accounts[0],
|
||||
data: this.code,
|
||||
gas: options.gas || 800000
|
||||
});
|
||||
|
||||
var contractObject = this.web3.eth.contract(this.abi);
|
||||
|
||||
var promise = new Promise(function(resolve, reject) {
|
||||
contractParams.push(function(err, transaction) {
|
||||
if (err) {
|
||||
reject(err);
|
||||
} else if (transaction.address !== undefined) {
|
||||
resolve(new EmbarkJS.Contract({abi: self.abi, code: self.code, address: transaction.address}));
|
||||
}
|
||||
});
|
||||
|
||||
// returns promise
|
||||
// deploys contract
|
||||
// wraps it around EmbarkJS.Contract
|
||||
contractObject["new"].apply(contractObject, contractParams);
|
||||
});
|
||||
|
||||
|
||||
return promise;
|
||||
};
|
||||
|
||||
EmbarkJS.Storage = {
|
||||
IPFS : 'ipfs'
|
||||
};
|
||||
|
||||
EmbarkJS.Storage.setProvider = function(provider, options) {
|
||||
if (provider.toLowerCase() === EmbarkJS.Storage.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;
|
||||
if (options === undefined) {
|
||||
this.ipfsConnection = IpfsApi('localhost', '5001');
|
||||
} else {
|
||||
this.ipfsConnection = IpfsApi(options.server, options.port);
|
||||
}
|
||||
} else {
|
||||
throw Error('Unknown storage provider');
|
||||
}
|
||||
};
|
||||
|
||||
EmbarkJS.Storage.saveText = function(text) {
|
||||
var self = this;
|
||||
if (!this.ipfsConnection) {
|
||||
this.setProvider('ipfs');
|
||||
}
|
||||
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];
|
||||
|
||||
if (file === undefined) {
|
||||
throw new Error('no file found');
|
||||
}
|
||||
|
||||
if (!this.ipfsConnection) {
|
||||
this.setProvider('ipfs');
|
||||
}
|
||||
|
||||
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;
|
||||
// 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) {
|
||||
//var ipfsHash = web3.toAscii(hash);
|
||||
|
||||
return 'http://localhost:8080/ipfs/' + hash;
|
||||
};
|
||||
|
||||
EmbarkJS.Messages = {
|
||||
};
|
||||
|
||||
EmbarkJS.Messages.setProvider = function(provider, options) {
|
||||
var self = this;
|
||||
var ipfs;
|
||||
if (provider === 'whisper') {
|
||||
this.currentMessages = EmbarkJS.Messages.Whisper;
|
||||
if (typeof variable === 'undefined') {
|
||||
if (options === undefined) {
|
||||
web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:8545"));
|
||||
} else {
|
||||
web3 = new Web3(new Web3.providers.HttpProvider("http://" + options.server + ':' + options.port));
|
||||
}
|
||||
}
|
||||
web3.version.getWhisper(function(err, res) {
|
||||
if (err) {
|
||||
console.log("whisper not available");
|
||||
} else {
|
||||
self.currentMessages.identity = web3.shh.newIdentity();
|
||||
}
|
||||
});
|
||||
} else if (provider === 'orbit') {
|
||||
this.currentMessages = EmbarkJS.Messages.Orbit;
|
||||
if (options === undefined) {
|
||||
ipfs = HaadIpfsApi('localhost', '5001');
|
||||
} else {
|
||||
ipfs = HaadIpfsApi(options.server, options.port);
|
||||
}
|
||||
this.currentMessages.orbit = new Orbit(ipfs);
|
||||
this.currentMessages.orbit.connect(web3.eth.accounts[0]);
|
||||
} else {
|
||||
throw Error('unknown provider');
|
||||
}
|
||||
};
|
||||
|
||||
EmbarkJS.Messages.sendMessage = function(options) {
|
||||
return this.currentMessages.sendMessage(options);
|
||||
};
|
||||
|
||||
EmbarkJS.Messages.listenTo = function(options) {
|
||||
return this.currentMessages.listenTo(options);
|
||||
};
|
||||
|
||||
EmbarkJS.Messages.Whisper = {
|
||||
};
|
||||
|
||||
EmbarkJS.Messages.Whisper.sendMessage = function(options) {
|
||||
var topics = options.topic || options.topics;
|
||||
var data = options.data || options.payload;
|
||||
var identity = options.identity || this.identity || web3.shh.newIdentity();
|
||||
var ttl = options.ttl || 100;
|
||||
var priority = options.priority || 1000;
|
||||
var _topics;
|
||||
|
||||
if (topics === undefined) {
|
||||
throw new Error("missing option: topic");
|
||||
}
|
||||
|
||||
if (data === undefined) {
|
||||
throw new Error("missing option: data");
|
||||
}
|
||||
|
||||
// do fromAscii to each topics unless it's already a string
|
||||
if (typeof topics === 'string') {
|
||||
_topics = [web3.fromAscii(topics)];
|
||||
} else {
|
||||
// TODO: replace with es6 + babel;
|
||||
for (var i = 0; i < topics.length; i++) {
|
||||
_topics.push(web3.fromAscii(topics[i]));
|
||||
}
|
||||
}
|
||||
topics = _topics;
|
||||
|
||||
var payload = JSON.stringify(data);
|
||||
|
||||
var message = {
|
||||
from: identity,
|
||||
topics: topics,
|
||||
payload: web3.fromAscii(payload),
|
||||
ttl: ttl,
|
||||
priority: priority
|
||||
};
|
||||
|
||||
return web3.shh.post(message, function() {});
|
||||
};
|
||||
|
||||
EmbarkJS.Messages.Whisper.listenTo = function(options) {
|
||||
var topics = options.topic || options.topics;
|
||||
var _topics = [];
|
||||
|
||||
if (typeof topics === 'string') {
|
||||
_topics = [topics];
|
||||
} else {
|
||||
// TODO: replace with es6 + babel;
|
||||
for (var i = 0; i < topics.length; i++) {
|
||||
_topics.push(topics[i]);
|
||||
}
|
||||
}
|
||||
topics = _topics;
|
||||
|
||||
var filterOptions = {
|
||||
topics: topics
|
||||
};
|
||||
|
||||
var messageEvents = function() {
|
||||
this.cb = function() {};
|
||||
};
|
||||
|
||||
messageEvents.prototype.then = function(cb) {
|
||||
this.cb = cb;
|
||||
};
|
||||
|
||||
messageEvents.prototype.error = function(err) {
|
||||
return err;
|
||||
};
|
||||
|
||||
messageEvents.prototype.stop = function() {
|
||||
this.filter.stopWatching();
|
||||
};
|
||||
|
||||
var promise = new messageEvents();
|
||||
|
||||
var filter = web3.shh.filter(filterOptions, function(err, result) {
|
||||
var payload = JSON.parse(web3.toAscii(result.payload));
|
||||
var data;
|
||||
if (err) {
|
||||
promise.error(err);
|
||||
} else {
|
||||
data = {
|
||||
topic: topics,
|
||||
data: payload,
|
||||
from: result.from,
|
||||
time: (new Date(result.sent * 1000))
|
||||
};
|
||||
promise.cb(payload, data, result);
|
||||
}
|
||||
});
|
||||
|
||||
promise.filter = filter;
|
||||
|
||||
return promise;
|
||||
};
|
||||
|
||||
EmbarkJS.Messages.Orbit = {
|
||||
};
|
||||
|
||||
EmbarkJS.Messages.Orbit.sendMessage = function(options) {
|
||||
var topics = options.topic || options.topics;
|
||||
var data = options.data || options.payload;
|
||||
|
||||
if (topics === undefined) {
|
||||
throw new Error("missing option: topic");
|
||||
}
|
||||
|
||||
if (data === undefined) {
|
||||
throw new Error("missing option: data");
|
||||
}
|
||||
|
||||
if (typeof topics === 'string') {
|
||||
topics = topics;
|
||||
} else {
|
||||
// TODO: better to just send to different channels instead
|
||||
topics = topics.join(',');
|
||||
}
|
||||
|
||||
this.orbit.join(topics);
|
||||
|
||||
var payload = JSON.stringify(data);
|
||||
|
||||
this.orbit.send(topics, data);
|
||||
};
|
||||
|
||||
EmbarkJS.Messages.Orbit.listenTo = function(options) {
|
||||
var self = this;
|
||||
var topics = options.topic || options.topics;
|
||||
|
||||
if (typeof topics === 'string') {
|
||||
topics = topics;
|
||||
} else {
|
||||
topics = topics.join(',');
|
||||
}
|
||||
|
||||
this.orbit.join(topics);
|
||||
|
||||
var messageEvents = function() {
|
||||
this.cb = function() {};
|
||||
};
|
||||
|
||||
messageEvents.prototype.then = function(cb) {
|
||||
this.cb = cb;
|
||||
};
|
||||
|
||||
messageEvents.prototype.error = function(err) {
|
||||
return err;
|
||||
};
|
||||
|
||||
var promise = new messageEvents();
|
||||
|
||||
this.orbit.events.on('message', (channel, message) => {
|
||||
// TODO: looks like sometimes it's receving messages from all topics
|
||||
if (topics !== channel) return;
|
||||
self.orbit.getPost(message.payload.value, true).then((post) => {
|
||||
var data = {
|
||||
topic: channel,
|
||||
data: post.content,
|
||||
from: post.meta.from.name,
|
||||
time: (new Date(post.meta.ts))
|
||||
};
|
||||
promise.cb(post.content, data, post);
|
||||
});
|
||||
});
|
||||
|
||||
return promise;
|
||||
};
|
||||
|
||||
module.exports = EmbarkJS;
|
||||
|
||||
|
||||
/***/ })
|
||||
/******/ ]);
|
16
js/embark.js
16
js/embark.js
@ -133,17 +133,19 @@ EmbarkJS.Contract.prototype.deploy = function(args, _options) {
|
||||
contractObject["new"].apply(contractObject, contractParams);
|
||||
});
|
||||
|
||||
|
||||
|
||||
return promise;
|
||||
};
|
||||
|
||||
EmbarkJS.IPFS = 'ipfs';
|
||||
|
||||
EmbarkJS.Storage = {
|
||||
IPFS : 'ipfs'
|
||||
};
|
||||
|
||||
EmbarkJS.Storage.setProvider = function(provider, options) {
|
||||
if (provider === 'ipfs') {
|
||||
if (provider.toLowerCase() === EmbarkJS.Storage.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;
|
||||
if (options === undefined) {
|
||||
this.ipfsConnection = IpfsApi('localhost', '5001');
|
||||
@ -151,7 +153,7 @@ EmbarkJS.Storage.setProvider = function(provider, options) {
|
||||
this.ipfsConnection = IpfsApi(options.server, options.port);
|
||||
}
|
||||
} else {
|
||||
throw Error('unknown provider');
|
||||
throw Error('Unknown storage provider');
|
||||
}
|
||||
};
|
||||
|
||||
@ -187,7 +189,7 @@ EmbarkJS.Storage.uploadFile = function(inputSelector) {
|
||||
|
||||
var promise = new Promise(function(resolve, reject) {
|
||||
var reader = new FileReader();
|
||||
reader.onloadend = function() {
|
||||
reader.onloadend = function() {
|
||||
var fileContent = reader.result;
|
||||
var buffer = self.ipfsConnection.Buffer.from(fileContent);
|
||||
self.ipfsConnection.add(buffer, function(err, result) {
|
||||
@ -215,6 +217,8 @@ EmbarkJS.Storage.get = function(hash) {
|
||||
var promise = new Promise(function(resolve, reject) {
|
||||
self.ipfsConnection.object.get([hash]).then(function(node) {
|
||||
resolve(node.data);
|
||||
}).catch(function (err){
|
||||
reject(err);
|
||||
});
|
||||
});
|
||||
|
||||
|
101
lib/core/test.js
101
lib/core/test.js
@ -1,14 +1,6 @@
|
||||
var async = require('async');
|
||||
var Web3 = require('web3');
|
||||
|
||||
var Engine = require('./engine.js');
|
||||
var RunCode = require('./runCode.js');
|
||||
var TestLogger = require('./test_logger.js');
|
||||
|
||||
var getSimulator = function() {
|
||||
try {
|
||||
var sim = require('ethereumjs-testrpc');
|
||||
return sim;
|
||||
return require('ethereumjs-testrpc');
|
||||
} catch (e) {
|
||||
if (e.code === 'MODULE_NOT_FOUND') {
|
||||
console.log('Simulator not found; Please install it with "npm install ethereumjs-testrpc --save"');
|
||||
@ -25,30 +17,44 @@ var getSimulator = function() {
|
||||
}
|
||||
};
|
||||
|
||||
var Test = function(options) {
|
||||
this.options = options || {};
|
||||
var simOptions = this.options.simulatorOptions || {};
|
||||
var Test;
|
||||
Test = (function (options) {
|
||||
var async = require('async');
|
||||
var opts = options === undefined ? {} : options;
|
||||
opts.logLevel = opts.hasOwnProperty('logLevel') ? opts.logLevel : 'debug';
|
||||
opts.simulatorOptions = opts.hasOwnProperty('simulatorOptions') ? opts.simulatorOptions : {};
|
||||
var sim = getSimulator();
|
||||
|
||||
this.engine = new Engine({
|
||||
env: this.options.env || 'test',
|
||||
// TODO: confi will need to detect if this is a obj
|
||||
embarkConfig: this.options.embarkConfig || 'embark.json',
|
||||
interceptLogs: false
|
||||
});
|
||||
function newWebThree() {
|
||||
try {
|
||||
var Web3 = require('web3');
|
||||
var web3 = new Web3();
|
||||
web3.setProvider(sim.provider(opts.simulatorOptions));
|
||||
return web3;
|
||||
} catch (e) {
|
||||
throw new Error(e);
|
||||
}
|
||||
}
|
||||
|
||||
this.engine.init({
|
||||
logger: new TestLogger({logLevel: this.options.logLevel || 'debug'})
|
||||
});
|
||||
function deployAll(contractsConfig, cb) {
|
||||
var RunCode = require('./runCode.js');
|
||||
var self = this;
|
||||
|
||||
this.sim = getSimulator();
|
||||
this.web3 = new Web3();
|
||||
this.web3.setProvider(this.sim.provider(simOptions));
|
||||
};
|
||||
function newEngine () {
|
||||
var Engine = require('./engine.js');
|
||||
return new Engine({
|
||||
env: opts.env || 'test',
|
||||
// TODO: confi will need to detect if this is a obj
|
||||
embarkConfig: opts.embarkConfig || 'embark.json',
|
||||
interceptLogs: false
|
||||
});
|
||||
}
|
||||
|
||||
Test.prototype.deployAll = function(contractsConfig, cb) {
|
||||
var self = this;
|
||||
self.web3 = newWebThree();
|
||||
self.engine = newEngine();
|
||||
self.engine.init();
|
||||
|
||||
async.waterfall([
|
||||
async.waterfall([
|
||||
function getConfig(callback) {
|
||||
self.engine.config.contractsConfig = {contracts: contractsConfig};
|
||||
callback();
|
||||
@ -63,32 +69,39 @@ Test.prototype.deployAll = function(contractsConfig, cb) {
|
||||
callback();
|
||||
},
|
||||
function deploy(callback) {
|
||||
self.engine.events.on('abi-contracts-vanila', function(vanillaABI) {
|
||||
self.engine.events.on('abi-contracts-vanila', function (vanillaABI) {
|
||||
callback(null, vanillaABI);
|
||||
});
|
||||
self.engine.deployManager.deployContracts(function(err, result) {
|
||||
self.engine.deployManager.deployContracts(function (err, result) {
|
||||
if (err) {
|
||||
console.log(err);
|
||||
callback(err);
|
||||
}
|
||||
});
|
||||
}
|
||||
], function(err, result) {
|
||||
if (err) {
|
||||
console.log("got error");
|
||||
process.exit();
|
||||
}
|
||||
// this should be part of the waterfall and not just something done at the
|
||||
// end
|
||||
self.web3.eth.getAccounts(function(err, accounts) {
|
||||
], function (err, result) {
|
||||
if (err) {
|
||||
throw new Error(err);
|
||||
console.log("got error");
|
||||
process.exit();
|
||||
}
|
||||
self.web3.eth.defaultAccount = accounts[0];
|
||||
RunCode.doEval(result, self.web3); // jshint ignore:line
|
||||
cb();
|
||||
// this should be part of the waterfall and not just something done at the
|
||||
// end
|
||||
self.web3.eth.getAccounts(function (err, accounts) {
|
||||
if (err) {
|
||||
throw new Error(err);
|
||||
}
|
||||
self.web3.eth.defaultAccount = accounts[0];
|
||||
RunCode.doEval(result, self.web3); // jshint ignore:line
|
||||
cb();
|
||||
});
|
||||
});
|
||||
});
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
return {
|
||||
deployAll: deployAll,
|
||||
sim: sim
|
||||
};
|
||||
}());
|
||||
|
||||
module.exports = Test;
|
||||
|
@ -1,13 +1,11 @@
|
||||
/*jshint esversion: 6 */
|
||||
var async = require('async');
|
||||
//require("./utils/debug_util.js")(__filename, async);
|
||||
// require("./utils/debug_util.js")(__filename, async);
|
||||
|
||||
var colors = require('colors');
|
||||
|
||||
var Engine = require('./core/engine.js');
|
||||
|
||||
var Test = require('./core/test.js');
|
||||
|
||||
var IPFS = require('./upload/ipfs.js');
|
||||
var Swarm = require('./upload/swarm.js');
|
||||
|
||||
@ -167,6 +165,7 @@ var Embark = (function () {
|
||||
}
|
||||
|
||||
function initTests (options) {
|
||||
var Test = require('./core/test.js');
|
||||
return new Test(options);
|
||||
}
|
||||
|
||||
|
13
test/cmd.js
13
test/cmd.js
@ -2,8 +2,8 @@ var Embark = require('../lib/index');
|
||||
var Cmd = require('../lib/cmd');
|
||||
|
||||
describe('embark.Cmd', function () {
|
||||
this.timeout(0);
|
||||
var cmd = new Cmd(Embark);
|
||||
|
||||
describe('#new', function () {
|
||||
it('it should not create an app without a name', function (done) {
|
||||
cmd.newApp(undefined, function (output) {
|
||||
@ -27,4 +27,15 @@ describe('embark.Cmd', function () {
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
// describe("#help", function () {
|
||||
// it('it should spit out helpful text if no arguments are supplied', function (done) {
|
||||
// cmd.process([], function (output) {
|
||||
// var lines = output.split('\n');
|
||||
// assert.equal(lines[0], '\n');
|
||||
// assert.equal(lines[1], 'Usage:');
|
||||
// done();
|
||||
// });
|
||||
// })
|
||||
// })
|
||||
});
|
@ -10,7 +10,7 @@
|
||||
"license": "ISC",
|
||||
"homepage": "",
|
||||
"devDependencies": {
|
||||
"embark": "../",
|
||||
"embark": "file:../",
|
||||
"mocha": "^2.2.5"
|
||||
},
|
||||
"dependencies": {
|
||||
|
@ -1,7 +1,5 @@
|
||||
var assert = require('assert');
|
||||
var Embark = require('embark');
|
||||
var EmbarkSpec = Embark.initTests();
|
||||
var web3 = EmbarkSpec.web3;
|
||||
var EmbarkSpec = require('embark/lib/core/test.js');
|
||||
|
||||
describe("AnotherStorage", function() {
|
||||
before(function(done) {
|
||||
|
@ -1,7 +1,5 @@
|
||||
var assert = require('assert');
|
||||
var Embark = require('embark');
|
||||
var EmbarkSpec = Embark.initTests();
|
||||
var web3 = EmbarkSpec.web3;
|
||||
var EmbarkSpec = require('embark/lib/core/test.js');
|
||||
|
||||
describe("SimpleStorage", function() {
|
||||
before(function(done) {
|
||||
|
@ -1,7 +1,5 @@
|
||||
var assert = require('assert');
|
||||
var Embark = require('embark');
|
||||
var EmbarkSpec = Embark.initTests();
|
||||
var web3 = EmbarkSpec.web3;
|
||||
var EmbarkSpec = require('embark/lib/core/test.js');
|
||||
|
||||
describe("Token", function() {
|
||||
before(function(done) {
|
||||
|
Loading…
x
Reference in New Issue
Block a user