Merge pull request #254 from nogueian/develop

Refactored EmbarkJS to allow multiple storage providers
This commit is contained in:
Iuri Matias 2017-03-21 19:28:37 -04:00 committed by GitHub
commit 65c5006d0e
4 changed files with 835 additions and 723 deletions

View File

@ -32,18 +32,28 @@ $(document).ready(function() {
//EmbarkJS.Storage.setProvider('ipfs',{server: 'localhost', port: '5001'}); //EmbarkJS.Storage.setProvider('ipfs',{server: 'localhost', port: '5001'});
$("#storage .error").hide(); $("#storage .error").hide();
EmbarkJS.Storage.ipfsConnection.ping() EmbarkJS.Storage.setProvider('ipfs')
.then(function(){ .then(function(){
$("#status-storage").addClass('status-online'); console.log('Provider set to IPFS');
$("#storage-controls").show(); 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();
}
});
}) })
.catch(function(err) { .catch(function(err){
if(err){ console.log('Failed to set IPFS as Provider:', err.message);
console.log("IPFS Connection Error => " + err.message); $("#storage .error").show();
$("#storage .error").show(); $("#status-storage").addClass('status-offline');
$("#status-storage").addClass('status-offline'); $("#storage-controls").hide();
$("#storage-controls").hide();
}
}); });
$("#storage button.setIpfsText").click(function() { $("#storage button.setIpfsText").click(function() {

View File

@ -10,7 +10,7 @@
"license": "ISC", "license": "ISC",
"homepage": "", "homepage": "",
"devDependencies": { "devDependencies": {
"embark": "^2.4.0", "embark": "^2.4.1",
"mocha": "^2.2.5" "mocha": "^2.2.5"
} }
} }

View File

@ -74,450 +74,501 @@ var EmbarkJS =
/*jshint esversion: 6 */ /*jshint esversion: 6 */
//var Ipfs = require('./ipfs.js'); //var Ipfs = require('./ipfs.js');
var EmbarkJS = { //=========================================================
}; // Embark Smart Contracts
//=========================================================
var EmbarkJS = {};
EmbarkJS.Contract = function(options) { EmbarkJS.Contract = function(options) {
var self = this; var self = this;
var i, abiElement; var i, abiElement;
this.abi = options.abi; this.abi = options.abi;
this.address = options.address; this.address = options.address;
this.code = '0x' + options.code; this.code = '0x' + options.code;
this.web3 = options.web3 || web3; this.web3 = options.web3 || web3;
var ContractClass = this.web3.eth.contract(this.abi); var ContractClass = this.web3.eth.contract(this.abi);
this.eventList = []; this.eventList = [];
if (this.abi) { if (this.abi) {
for (i = 0; i < this.abi.length; i++) { for (i = 0; i < this.abi.length; i++) {
abiElement = this.abi[i]; abiElement = this.abi[i];
if (abiElement.type === 'event') { if (abiElement.type === 'event') {
this.eventList.push(abiElement.name); 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() { var messageEvents = function() {
self.web3.eth.getTransactionReceipt(transaction, function(err, receipt) { this.cb = function() {};
if (err) { };
return reject(err);
}
if (receipt !== null) { messageEvents.prototype.then = function(cb) {
return resolve(receipt); this.cb = cb;
} };
setTimeout(getConfirmation, 1000); 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);
if (typeof(transaction) !== "string" || props.constant) { var promise = new Promise(function(resolve, reject) {
resolve(transaction); args.push(function(err, transaction) {
} else { promise.tx = transaction;
getConfirmation(); if (err) {
} return reject(err);
}); }
fn.apply(fn, args); var getConfirmation = function() {
}); self.web3.eth.getTransactionReceipt(transaction, function(err, receipt) {
if (err) {
return reject(err);
}
return promise; if (receipt !== null) {
}; return resolve(receipt);
return true; }
}
return false; 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) { EmbarkJS.Contract.prototype.deploy = function(args, _options) {
var self = this; var self = this;
var contractParams; var contractParams;
var options = _options || {}; var options = _options || {};
contractParams = args || []; contractParams = args || [];
contractParams.push({ contractParams.push({
from: this.web3.eth.accounts[0], from: this.web3.eth.accounts[0],
data: this.code, data: this.code,
gas: options.gas || 800000 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 var contractObject = this.web3.eth.contract(this.abi);
// deploys contract
// wraps it around EmbarkJS.Contract var promise = new Promise(function(resolve, reject) {
contractObject["new"].apply(contractObject, contractParams); 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; return promise;
}; };
EmbarkJS.Storage = { //=========================================================
IPFS : 'ipfs' // Embark Storage
//=========================================================
EmbarkJS.Storage = {};
EmbarkJS.Storage.Providers = {
IPFS: 'ipfs',
SWARM: 'swarm'
}; };
EmbarkJS.Storage.setProvider = function(provider, options) { EmbarkJS.Storage.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');
} else {
this.ipfsConnection = IpfsApi(options.server, options.port);
}
} else {
throw Error('Unknown storage provider');
}
};
EmbarkJS.Storage.saveText = function(text) { EmbarkJS.Storage.saveText = function(text) {
var self = this; return this.currentStorage.saveText(text);
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) { EmbarkJS.Storage.get = function(hash) {
var self = this; return this.currentStorage.get(hash);
// 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.uploadFile = function(inputSelector) {
return this.currentStorage.uploadFile(inputSelector);
}
EmbarkJS.Storage.getUrl = function(hash) { EmbarkJS.Storage.getUrl = function(hash) {
//var ipfsHash = web3.toAscii(hash); return this.currentStorage.getUrl(hash);
}
return 'http://localhost:8080/ipfs/' + hash; EmbarkJS.Storage.setProvider = function(provider, options) {
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
self.currentStorage = EmbarkJS.Storage.IPFS;
try {
if (options === undefined) {
self.ipfsConnection = IpfsApi('localhost', '5001');
} else {
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 {
reject('Unknown storage provider');
}
});
return promise;
}; };
EmbarkJS.Messages = { EmbarkJS.Storage.IPFS.saveText = function(text) {
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.add((new EmbarkJS.Storage.ipfsConnection.Buffer(text)), function(err, result) {
if (err) {
reject(err);
} else {
resolve(result[0].path);
}
});
});
return promise;
}; };
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');
}
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 = EmbarkJS.Storage.ipfsConnection.Buffer.from(fileContent);
EmbarkJS.Storage.ipfsConnection.add(buffer, function(err, result) {
if (err) {
reject(err);
} else {
resolve(result[0].path);
}
});
};
reader.readAsArrayBuffer(file);
});
return promise;
};
EmbarkJS.Storage.IPFS.getUrl = function(hash) {
//var ipfsHash = web3.toAscii(hash);
return 'http://localhost:8080/ipfs/' + hash;
};
//=========================================================
// Embark Messaging
//=========================================================
EmbarkJS.Messages = {};
EmbarkJS.Messages.setProvider = function(provider, options) { EmbarkJS.Messages.setProvider = function(provider, options) {
var self = this; var self = this;
var ipfs; var ipfs;
if (provider === 'whisper') { if (provider === 'whisper') {
this.currentMessages = EmbarkJS.Messages.Whisper; this.currentMessages = EmbarkJS.Messages.Whisper;
if (typeof variable === 'undefined') { if (typeof variable === 'undefined') {
if (options === undefined) { if (options === undefined) {
web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:8545")); web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:8545"));
} else { } else {
web3 = new Web3(new Web3.providers.HttpProvider("http://" + options.server + ':' + options.port)); web3 = new Web3(new Web3.providers.HttpProvider("http://" + options.server + ':' + options.port));
} }
} }
web3.version.getWhisper(function(err, res) { web3.version.getWhisper(function(err, res) {
if (err) { if (err) {
console.log("whisper not available"); console.log("whisper not available");
} else { } else {
self.currentMessages.identity = web3.shh.newIdentity(); self.currentMessages.identity = web3.shh.newIdentity();
} }
}); });
} else if (provider === 'orbit') { } else if (provider === 'orbit') {
this.currentMessages = EmbarkJS.Messages.Orbit; this.currentMessages = EmbarkJS.Messages.Orbit;
if (options === undefined) { if (options === undefined) {
ipfs = HaadIpfsApi('localhost', '5001'); 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 { } else {
ipfs = HaadIpfsApi(options.server, options.port); throw Error('Unknown message provider');
} }
this.currentMessages.orbit = new Orbit(ipfs);
this.currentMessages.orbit.connect(web3.eth.accounts[0]);
} else {
throw Error('unknown provider');
}
}; };
EmbarkJS.Messages.sendMessage = function(options) { EmbarkJS.Messages.sendMessage = function(options) {
return this.currentMessages.sendMessage(options); return this.currentMessages.sendMessage(options);
}; };
EmbarkJS.Messages.listenTo = function(options) { EmbarkJS.Messages.listenTo = function(options) {
return this.currentMessages.listenTo(options); return this.currentMessages.listenTo(options);
}; };
EmbarkJS.Messages.Whisper = { EmbarkJS.Messages.Whisper = {};
};
EmbarkJS.Messages.Whisper.sendMessage = function(options) { EmbarkJS.Messages.Whisper.sendMessage = function(options) {
var topics = options.topic || options.topics; var topics = options.topic || options.topics;
var data = options.data || options.payload; var data = options.data || options.payload;
var identity = options.identity || this.identity || web3.shh.newIdentity(); var identity = options.identity || this.identity || web3.shh.newIdentity();
var ttl = options.ttl || 100; var ttl = options.ttl || 100;
var priority = options.priority || 1000; var priority = options.priority || 1000;
var _topics; var _topics;
if (topics === undefined) { if (topics === undefined) {
throw new Error("missing option: topic"); 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); if (data === undefined) {
throw new Error("missing option: data");
}
var message = { // do fromAscii to each topics unless it's already a string
from: identity, if (typeof topics === 'string') {
topics: topics, _topics = [web3.fromAscii(topics)];
payload: web3.fromAscii(payload), } else {
ttl: ttl, // TODO: replace with es6 + babel;
priority: priority for (var i = 0; i < topics.length; i++) {
}; _topics.push(web3.fromAscii(topics[i]));
}
}
topics = _topics;
return web3.shh.post(message, function() {}); 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) { EmbarkJS.Messages.Whisper.listenTo = function(options) {
var topics = options.topic || options.topics; var topics = options.topic || options.topics;
var _topics = []; var _topics = [];
if (typeof topics === 'string') { if (typeof topics === 'string') {
_topics = [topics]; _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 { } else {
data = { // TODO: replace with es6 + babel;
topic: topics, for (var i = 0; i < topics.length; i++) {
data: payload, _topics.push(topics[i]);
from: result.from, }
time: (new Date(result.sent * 1000))
};
promise.cb(payload, data, result);
} }
}); topics = _topics;
promise.filter = filter; var filterOptions = {
topics: topics
};
return promise; 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 = {};
};
EmbarkJS.Messages.Orbit.sendMessage = function(options) { EmbarkJS.Messages.Orbit.sendMessage = function(options) {
var topics = options.topic || options.topics; var topics = options.topic || options.topics;
var data = options.data || options.payload; var data = options.data || options.payload;
if (topics === undefined) { if (topics === undefined) {
throw new Error("missing option: topic"); throw new Error("missing option: topic");
} }
if (data === undefined) { if (data === undefined) {
throw new Error("missing option: data"); throw new Error("missing option: data");
} }
if (typeof topics === 'string') { if (typeof topics === 'string') {
topics = topics; topics = topics;
} else { } else {
// TODO: better to just send to different channels instead // TODO: better to just send to different channels instead
topics = topics.join(','); topics = topics.join(',');
} }
this.orbit.join(topics); this.orbit.join(topics);
var payload = JSON.stringify(data); var payload = JSON.stringify(data);
this.orbit.send(topics, data); this.orbit.send(topics, data);
}; };
EmbarkJS.Messages.Orbit.listenTo = function(options) { EmbarkJS.Messages.Orbit.listenTo = function(options) {
var self = this; var self = this;
var topics = options.topic || options.topics; var topics = options.topic || options.topics;
if (typeof topics === 'string') { if (typeof topics === 'string') {
topics = topics; topics = topics;
} else { } else {
topics = topics.join(','); topics = topics.join(',');
} }
this.orbit.join(topics); this.orbit.join(topics);
var messageEvents = function() { var messageEvents = function() {
this.cb = function() {}; this.cb = function() {};
}; };
messageEvents.prototype.then = function(cb) { messageEvents.prototype.then = function(cb) {
this.cb = cb; this.cb = cb;
}; };
messageEvents.prototype.error = function(err) { messageEvents.prototype.error = function(err) {
return err; return err;
}; };
var promise = new messageEvents(); var promise = new messageEvents();
this.orbit.events.on('message', (channel, message) => { this.orbit.events.on('message', (channel, message) => {
// TODO: looks like sometimes it's receving messages from all topics // TODO: looks like sometimes it's receving messages from all topics
if (topics !== channel) return; if (topics !== channel) return;
self.orbit.getPost(message.payload.value, true).then((post) => { self.orbit.getPost(message.payload.value, true).then((post) => {
var data = { var data = {
topic: channel, topic: channel,
data: post.content, data: post.content,
from: post.meta.from.name, from: post.meta.from.name,
time: (new Date(post.meta.ts)) time: (new Date(post.meta.ts))
}; };
promise.cb(post.content, data, post); promise.cb(post.content, data, post);
});
}); });
});
return promise; return promise;
}; };
module.exports = EmbarkJS; module.exports = EmbarkJS;

View File

@ -1,450 +1,501 @@
/*jshint esversion: 6 */ /*jshint esversion: 6 */
//var Ipfs = require('./ipfs.js'); //var Ipfs = require('./ipfs.js');
var EmbarkJS = { //=========================================================
}; // Embark Smart Contracts
//=========================================================
var EmbarkJS = {};
EmbarkJS.Contract = function(options) { EmbarkJS.Contract = function(options) {
var self = this; var self = this;
var i, abiElement; var i, abiElement;
this.abi = options.abi; this.abi = options.abi;
this.address = options.address; this.address = options.address;
this.code = '0x' + options.code; this.code = '0x' + options.code;
this.web3 = options.web3 || web3; this.web3 = options.web3 || web3;
var ContractClass = this.web3.eth.contract(this.abi); var ContractClass = this.web3.eth.contract(this.abi);
this.eventList = []; this.eventList = [];
if (this.abi) { if (this.abi) {
for (i = 0; i < this.abi.length; i++) { for (i = 0; i < this.abi.length; i++) {
abiElement = this.abi[i]; abiElement = this.abi[i];
if (abiElement.type === 'event') { if (abiElement.type === 'event') {
this.eventList.push(abiElement.name); 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() { var messageEvents = function() {
self.web3.eth.getTransactionReceipt(transaction, function(err, receipt) { this.cb = function() {};
if (err) { };
return reject(err);
}
if (receipt !== null) { messageEvents.prototype.then = function(cb) {
return resolve(receipt); this.cb = cb;
} };
setTimeout(getConfirmation, 1000); 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);
if (typeof(transaction) !== "string" || props.constant) { var promise = new Promise(function(resolve, reject) {
resolve(transaction); args.push(function(err, transaction) {
} else { promise.tx = transaction;
getConfirmation(); if (err) {
} return reject(err);
}); }
fn.apply(fn, args); var getConfirmation = function() {
}); self.web3.eth.getTransactionReceipt(transaction, function(err, receipt) {
if (err) {
return reject(err);
}
return promise; if (receipt !== null) {
}; return resolve(receipt);
return true; }
}
return false; 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) { EmbarkJS.Contract.prototype.deploy = function(args, _options) {
var self = this; var self = this;
var contractParams; var contractParams;
var options = _options || {}; var options = _options || {};
contractParams = args || []; contractParams = args || [];
contractParams.push({ contractParams.push({
from: this.web3.eth.accounts[0], from: this.web3.eth.accounts[0],
data: this.code, data: this.code,
gas: options.gas || 800000 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 var contractObject = this.web3.eth.contract(this.abi);
// deploys contract
// wraps it around EmbarkJS.Contract var promise = new Promise(function(resolve, reject) {
contractObject["new"].apply(contractObject, contractParams); 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; return promise;
}; };
EmbarkJS.Storage = { //=========================================================
IPFS : 'ipfs' // Embark Storage
//=========================================================
EmbarkJS.Storage = {};
EmbarkJS.Storage.Providers = {
IPFS: 'ipfs',
SWARM: 'swarm'
}; };
EmbarkJS.Storage.setProvider = function(provider, options) { EmbarkJS.Storage.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');
} else {
this.ipfsConnection = IpfsApi(options.server, options.port);
}
} else {
throw Error('Unknown storage provider');
}
};
EmbarkJS.Storage.saveText = function(text) { EmbarkJS.Storage.saveText = function(text) {
var self = this; return this.currentStorage.saveText(text);
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) { EmbarkJS.Storage.get = function(hash) {
var self = this; return this.currentStorage.get(hash);
// 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) { EmbarkJS.Storage.uploadFile = function(inputSelector) {
self.ipfsConnection.object.get([hash]).then(function(node) { return this.currentStorage.uploadFile(inputSelector);
resolve(node.data);
}).catch(function (err){
reject(err);
});
});
return promise;
}; };
EmbarkJS.Storage.getUrl = function(hash) { EmbarkJS.Storage.getUrl = function(hash) {
//var ipfsHash = web3.toAscii(hash); return this.currentStorage.getUrl(hash);
return 'http://localhost:8080/ipfs/' + hash;
}; };
EmbarkJS.Messages = { EmbarkJS.Storage.setProvider = function(provider, options) {
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
self.currentStorage = EmbarkJS.Storage.IPFS;
try {
if (options === undefined) {
self.ipfsConnection = IpfsApi('localhost', '5001');
} else {
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 {
reject('Unknown storage provider');
}
});
return promise;
}; };
EmbarkJS.Storage.IPFS.saveText = function(text) {
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.add((new EmbarkJS.Storage.ipfsConnection.Buffer(text)), function(err, result) {
if (err) {
reject(err);
} else {
resolve(result[0].path);
}
});
});
return promise;
};
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');
}
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 = EmbarkJS.Storage.ipfsConnection.Buffer.from(fileContent);
EmbarkJS.Storage.ipfsConnection.add(buffer, function(err, result) {
if (err) {
reject(err);
} else {
resolve(result[0].path);
}
});
};
reader.readAsArrayBuffer(file);
});
return promise;
};
EmbarkJS.Storage.IPFS.getUrl = function(hash) {
//var ipfsHash = web3.toAscii(hash);
return 'http://localhost:8080/ipfs/' + hash;
};
//=========================================================
// Embark Messaging
//=========================================================
EmbarkJS.Messages = {};
EmbarkJS.Messages.setProvider = function(provider, options) { EmbarkJS.Messages.setProvider = function(provider, options) {
var self = this; var self = this;
var ipfs; var ipfs;
if (provider === 'whisper') { if (provider === 'whisper') {
this.currentMessages = EmbarkJS.Messages.Whisper; this.currentMessages = EmbarkJS.Messages.Whisper;
if (typeof variable === 'undefined') { if (typeof variable === 'undefined') {
if (options === undefined) { if (options === undefined) {
web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:8545")); web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:8545"));
} else { } else {
web3 = new Web3(new Web3.providers.HttpProvider("http://" + options.server + ':' + options.port)); web3 = new Web3(new Web3.providers.HttpProvider("http://" + options.server + ':' + options.port));
} }
} }
web3.version.getWhisper(function(err, res) { web3.version.getWhisper(function(err, res) {
if (err) { if (err) {
console.log("whisper not available"); console.log("whisper not available");
} else { } else {
self.currentMessages.identity = web3.shh.newIdentity(); self.currentMessages.identity = web3.shh.newIdentity();
} }
}); });
} else if (provider === 'orbit') { } else if (provider === 'orbit') {
this.currentMessages = EmbarkJS.Messages.Orbit; this.currentMessages = EmbarkJS.Messages.Orbit;
if (options === undefined) { if (options === undefined) {
ipfs = HaadIpfsApi('localhost', '5001'); 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 { } else {
ipfs = HaadIpfsApi(options.server, options.port); throw Error('Unknown message provider');
} }
this.currentMessages.orbit = new Orbit(ipfs);
this.currentMessages.orbit.connect(web3.eth.accounts[0]);
} else {
throw Error('unknown provider');
}
}; };
EmbarkJS.Messages.sendMessage = function(options) { EmbarkJS.Messages.sendMessage = function(options) {
return this.currentMessages.sendMessage(options); return this.currentMessages.sendMessage(options);
}; };
EmbarkJS.Messages.listenTo = function(options) { EmbarkJS.Messages.listenTo = function(options) {
return this.currentMessages.listenTo(options); return this.currentMessages.listenTo(options);
}; };
EmbarkJS.Messages.Whisper = { EmbarkJS.Messages.Whisper = {};
};
EmbarkJS.Messages.Whisper.sendMessage = function(options) { EmbarkJS.Messages.Whisper.sendMessage = function(options) {
var topics = options.topic || options.topics; var topics = options.topic || options.topics;
var data = options.data || options.payload; var data = options.data || options.payload;
var identity = options.identity || this.identity || web3.shh.newIdentity(); var identity = options.identity || this.identity || web3.shh.newIdentity();
var ttl = options.ttl || 100; var ttl = options.ttl || 100;
var priority = options.priority || 1000; var priority = options.priority || 1000;
var _topics; var _topics;
if (topics === undefined) { if (topics === undefined) {
throw new Error("missing option: topic"); 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); if (data === undefined) {
throw new Error("missing option: data");
}
var message = { // do fromAscii to each topics unless it's already a string
from: identity, if (typeof topics === 'string') {
topics: topics, _topics = [web3.fromAscii(topics)];
payload: web3.fromAscii(payload), } else {
ttl: ttl, // TODO: replace with es6 + babel;
priority: priority for (var i = 0; i < topics.length; i++) {
}; _topics.push(web3.fromAscii(topics[i]));
}
}
topics = _topics;
return web3.shh.post(message, function() {}); 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) { EmbarkJS.Messages.Whisper.listenTo = function(options) {
var topics = options.topic || options.topics; var topics = options.topic || options.topics;
var _topics = []; var _topics = [];
if (typeof topics === 'string') { if (typeof topics === 'string') {
_topics = [topics]; _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 { } else {
data = { // TODO: replace with es6 + babel;
topic: topics, for (var i = 0; i < topics.length; i++) {
data: payload, _topics.push(topics[i]);
from: result.from, }
time: (new Date(result.sent * 1000))
};
promise.cb(payload, data, result);
} }
}); topics = _topics;
promise.filter = filter; var filterOptions = {
topics: topics
};
return promise; 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 = {};
};
EmbarkJS.Messages.Orbit.sendMessage = function(options) { EmbarkJS.Messages.Orbit.sendMessage = function(options) {
var topics = options.topic || options.topics; var topics = options.topic || options.topics;
var data = options.data || options.payload; var data = options.data || options.payload;
if (topics === undefined) { if (topics === undefined) {
throw new Error("missing option: topic"); throw new Error("missing option: topic");
} }
if (data === undefined) { if (data === undefined) {
throw new Error("missing option: data"); throw new Error("missing option: data");
} }
if (typeof topics === 'string') { if (typeof topics === 'string') {
topics = topics; topics = topics;
} else { } else {
// TODO: better to just send to different channels instead // TODO: better to just send to different channels instead
topics = topics.join(','); topics = topics.join(',');
} }
this.orbit.join(topics); this.orbit.join(topics);
var payload = JSON.stringify(data); var payload = JSON.stringify(data);
this.orbit.send(topics, data); this.orbit.send(topics, data);
}; };
EmbarkJS.Messages.Orbit.listenTo = function(options) { EmbarkJS.Messages.Orbit.listenTo = function(options) {
var self = this; var self = this;
var topics = options.topic || options.topics; var topics = options.topic || options.topics;
if (typeof topics === 'string') { if (typeof topics === 'string') {
topics = topics; topics = topics;
} else { } else {
topics = topics.join(','); topics = topics.join(',');
} }
this.orbit.join(topics); this.orbit.join(topics);
var messageEvents = function() { var messageEvents = function() {
this.cb = function() {}; this.cb = function() {};
}; };
messageEvents.prototype.then = function(cb) { messageEvents.prototype.then = function(cb) {
this.cb = cb; this.cb = cb;
}; };
messageEvents.prototype.error = function(err) { messageEvents.prototype.error = function(err) {
return err; return err;
}; };
var promise = new messageEvents(); var promise = new messageEvents();
this.orbit.events.on('message', (channel, message) => { this.orbit.events.on('message', (channel, message) => {
// TODO: looks like sometimes it's receving messages from all topics // TODO: looks like sometimes it's receving messages from all topics
if (topics !== channel) return; if (topics !== channel) return;
self.orbit.getPost(message.payload.value, true).then((post) => { self.orbit.getPost(message.payload.value, true).then((post) => {
var data = { var data = {
topic: channel, topic: channel,
data: post.content, data: post.content,
from: post.meta.from.name, from: post.meta.from.name,
time: (new Date(post.meta.ts)) time: (new Date(post.meta.ts))
}; };
promise.cb(post.content, data, post); promise.cb(post.content, data, post);
});
}); });
});
return promise; return promise;
}; };
module.exports = EmbarkJS; module.exports = EmbarkJS;