2017-01-08 21:15:25 +00:00
|
|
|
/*jshint esversion: 6 */
|
2016-08-26 11:01:22 +00:00
|
|
|
//var Ipfs = require('./ipfs.js');
|
2016-08-18 11:45:08 +00:00
|
|
|
|
2016-08-21 14:42:42 +00:00
|
|
|
var EmbarkJS = {
|
2016-08-18 11:45:08 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
EmbarkJS.Contract = function(options) {
|
|
|
|
var self = this;
|
2016-10-30 12:40:37 +00:00
|
|
|
var i, abiElement;
|
2016-08-18 11:45:08 +00:00
|
|
|
|
|
|
|
this.abi = options.abi;
|
|
|
|
this.address = options.address;
|
2017-03-07 03:44:16 +00:00
|
|
|
this.code = '0x' + options.code;
|
2016-08-18 11:45:08 +00:00
|
|
|
this.web3 = options.web3 || web3;
|
|
|
|
|
2016-08-21 22:05:35 +00:00
|
|
|
var ContractClass = this.web3.eth.contract(this.abi);
|
2016-08-18 11:45:08 +00:00
|
|
|
|
2016-10-30 12:40:37 +00:00
|
|
|
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;
|
|
|
|
};
|
|
|
|
|
2016-08-18 11:45:08 +00:00
|
|
|
this._originalContractObject = ContractClass.at(this.address);
|
|
|
|
this._methods = Object.getOwnPropertyNames(this._originalContractObject).filter(function (p) {
|
|
|
|
// TODO: check for forbidden properties
|
2016-10-30 12:40:37 +00:00
|
|
|
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') {
|
2017-02-28 01:32:26 +00:00
|
|
|
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;
|
|
|
|
};
|
2016-08-18 11:45:08 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2017-02-22 11:51:02 +00:00
|
|
|
EmbarkJS.Contract.prototype.deploy = function(args, _options) {
|
2016-08-18 11:45:08 +00:00
|
|
|
var self = this;
|
|
|
|
var contractParams;
|
2017-02-22 11:51:02 +00:00
|
|
|
var options = _options || {};
|
2016-08-18 11:45:08 +00:00
|
|
|
|
2016-10-23 16:08:29 +00:00
|
|
|
contractParams = args || [];
|
2016-08-18 11:45:08 +00:00
|
|
|
|
|
|
|
contractParams.push({
|
2016-08-21 22:05:35 +00:00
|
|
|
from: this.web3.eth.accounts[0],
|
2016-08-18 11:45:08 +00:00
|
|
|
data: this.code,
|
2017-02-22 11:51:02 +00:00
|
|
|
gas: options.gas || 800000
|
2016-08-18 11:45:08 +00:00
|
|
|
});
|
|
|
|
|
2016-08-21 22:05:35 +00:00
|
|
|
var contractObject = this.web3.eth.contract(this.abi);
|
2016-08-18 11:45:08 +00:00
|
|
|
|
|
|
|
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;
|
|
|
|
};
|
|
|
|
|
2016-08-26 11:01:22 +00:00
|
|
|
EmbarkJS.IPFS = 'ipfs';
|
|
|
|
|
|
|
|
EmbarkJS.Storage = {
|
|
|
|
};
|
|
|
|
|
|
|
|
EmbarkJS.Storage.setProvider = function(provider, options) {
|
|
|
|
if (provider === 'ipfs') {
|
|
|
|
this.currentStorage = EmbarkJS.Storage.IPFS;
|
2016-12-02 11:48:02 +00:00
|
|
|
if (options === undefined) {
|
|
|
|
this.ipfsConnection = IpfsApi('localhost', '5001');
|
|
|
|
} else {
|
|
|
|
this.ipfsConnection = IpfsApi(options.server, options.port);
|
|
|
|
}
|
2016-08-26 11:01:22 +00:00
|
|
|
} else {
|
|
|
|
throw Error('unknown provider');
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
EmbarkJS.Storage.saveText = function(text) {
|
|
|
|
var self = this;
|
2016-12-02 11:48:02 +00:00
|
|
|
if (!this.ipfsConnection) {
|
|
|
|
this.setProvider('ipfs');
|
|
|
|
}
|
2016-08-26 11:01:22 +00:00
|
|
|
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];
|
|
|
|
|
2016-10-13 00:01:21 +00:00
|
|
|
if (file === undefined) {
|
|
|
|
throw new Error('no file found');
|
|
|
|
}
|
|
|
|
|
2016-12-02 11:48:02 +00:00
|
|
|
if (!this.ipfsConnection) {
|
|
|
|
this.setProvider('ipfs');
|
|
|
|
}
|
|
|
|
|
2016-08-26 11:01:22 +00:00
|
|
|
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;
|
2016-10-12 12:20:20 +00:00
|
|
|
// TODO: detect type, then convert if needed
|
|
|
|
//var ipfsHash = web3.toAscii(hash);
|
2016-12-02 11:48:02 +00:00
|
|
|
if (!this.ipfsConnection) {
|
|
|
|
this.setProvider('ipfs');
|
|
|
|
}
|
2016-08-26 11:01:22 +00:00
|
|
|
|
|
|
|
var promise = new Promise(function(resolve, reject) {
|
2016-10-12 12:20:20 +00:00
|
|
|
self.ipfsConnection.object.get([hash]).then(function(node) {
|
2016-08-26 11:01:22 +00:00
|
|
|
resolve(node.data);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
return promise;
|
|
|
|
};
|
|
|
|
|
|
|
|
EmbarkJS.Storage.getUrl = function(hash) {
|
2016-10-12 12:20:20 +00:00
|
|
|
//var ipfsHash = web3.toAscii(hash);
|
2016-08-26 11:01:22 +00:00
|
|
|
|
2016-10-12 12:20:20 +00:00
|
|
|
return 'http://localhost:8080/ipfs/' + hash;
|
2016-08-26 11:01:22 +00:00
|
|
|
};
|
|
|
|
|
2016-08-21 14:42:42 +00:00
|
|
|
EmbarkJS.Messages = {
|
|
|
|
};
|
|
|
|
|
2017-01-07 05:03:03 +00:00
|
|
|
EmbarkJS.Messages.setProvider = function(provider, options) {
|
2017-03-07 03:44:16 +00:00
|
|
|
var self = this;
|
2017-01-07 05:03:03 +00:00
|
|
|
var ipfs;
|
2016-10-12 11:54:40 +00:00
|
|
|
if (provider === 'whisper') {
|
|
|
|
this.currentMessages = EmbarkJS.Messages.Whisper;
|
2017-03-01 04:29:16 +00:00
|
|
|
if (typeof variable === 'undefined') {
|
2017-02-20 22:12:13 +00:00
|
|
|
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));
|
|
|
|
}
|
|
|
|
}
|
2017-03-07 03:44:16 +00:00
|
|
|
web3.version.getWhisper(function(err, res) {
|
|
|
|
if (err) {
|
|
|
|
console.log("whisper not available");
|
|
|
|
} else {
|
|
|
|
self.currentMessages.identity = web3.shh.newIdentity();
|
|
|
|
}
|
|
|
|
});
|
2017-01-07 05:03:03 +00:00
|
|
|
} 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]);
|
2016-10-12 11:54:40 +00:00
|
|
|
} else {
|
|
|
|
throw Error('unknown provider');
|
|
|
|
}
|
2016-08-21 14:42:42 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
EmbarkJS.Messages.sendMessage = function(options) {
|
2017-01-07 05:03:03 +00:00
|
|
|
return this.currentMessages.sendMessage(options);
|
2016-08-21 14:42:42 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
EmbarkJS.Messages.listenTo = function(options) {
|
2017-01-07 05:03:03 +00:00
|
|
|
return this.currentMessages.listenTo(options);
|
2016-08-21 14:42:42 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
EmbarkJS.Messages.Whisper = {
|
|
|
|
};
|
|
|
|
|
|
|
|
EmbarkJS.Messages.Whisper.sendMessage = function(options) {
|
2016-10-12 11:54:40 +00:00
|
|
|
var topics = options.topic || options.topics;
|
|
|
|
var data = options.data || options.payload;
|
2017-01-07 13:38:34 +00:00
|
|
|
var identity = options.identity || this.identity || web3.shh.newIdentity();
|
2016-10-12 11:54:40 +00:00
|
|
|
var ttl = options.ttl || 100;
|
|
|
|
var priority = options.priority || 1000;
|
2017-01-08 21:15:25 +00:00
|
|
|
var _topics;
|
2016-10-12 11:54:40 +00:00
|
|
|
|
|
|
|
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') {
|
2017-01-07 14:32:10 +00:00
|
|
|
_topics = [web3.fromAscii(topics)];
|
2016-10-12 11:54:40 +00:00
|
|
|
} else {
|
|
|
|
// TODO: replace with es6 + babel;
|
|
|
|
for (var i = 0; i < topics.length; i++) {
|
|
|
|
_topics.push(web3.fromAscii(topics[i]));
|
|
|
|
}
|
|
|
|
}
|
2017-01-07 14:32:10 +00:00
|
|
|
topics = _topics;
|
2016-10-12 11:54:40 +00:00
|
|
|
|
|
|
|
var payload = JSON.stringify(data);
|
|
|
|
|
|
|
|
var message = {
|
|
|
|
from: identity,
|
2017-01-07 14:32:10 +00:00
|
|
|
topics: topics,
|
2016-10-12 11:54:40 +00:00
|
|
|
payload: web3.fromAscii(payload),
|
|
|
|
ttl: ttl,
|
|
|
|
priority: priority
|
|
|
|
};
|
|
|
|
|
2017-01-26 11:31:29 +00:00
|
|
|
return web3.shh.post(message, function() {});
|
2016-08-21 14:42:42 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
EmbarkJS.Messages.Whisper.listenTo = function(options) {
|
2016-10-12 11:54:40 +00:00
|
|
|
var topics = options.topic || options.topics;
|
2017-01-26 11:31:29 +00:00
|
|
|
var _topics = [];
|
2016-10-12 11:54:40 +00:00
|
|
|
|
|
|
|
if (typeof topics === 'string') {
|
2017-01-07 14:32:10 +00:00
|
|
|
_topics = [topics];
|
2016-10-12 11:54:40 +00:00
|
|
|
} else {
|
|
|
|
// TODO: replace with es6 + babel;
|
|
|
|
for (var i = 0; i < topics.length; i++) {
|
2017-01-07 14:32:10 +00:00
|
|
|
_topics.push(topics[i]);
|
2016-10-12 11:54:40 +00:00
|
|
|
}
|
|
|
|
}
|
2017-01-07 14:32:10 +00:00
|
|
|
topics = _topics;
|
2016-10-12 11:54:40 +00:00
|
|
|
|
|
|
|
var filterOptions = {
|
|
|
|
topics: topics
|
|
|
|
};
|
|
|
|
|
2016-10-12 11:59:50 +00:00
|
|
|
var messageEvents = function() {
|
2016-10-12 11:54:40 +00:00
|
|
|
this.cb = function() {};
|
|
|
|
};
|
|
|
|
|
2016-10-12 11:59:50 +00:00
|
|
|
messageEvents.prototype.then = function(cb) {
|
2016-10-12 11:54:40 +00:00
|
|
|
this.cb = cb;
|
|
|
|
};
|
|
|
|
|
2016-10-12 11:59:50 +00:00
|
|
|
messageEvents.prototype.error = function(err) {
|
2016-10-12 11:54:40 +00:00
|
|
|
return err;
|
|
|
|
};
|
|
|
|
|
2017-01-26 11:31:29 +00:00
|
|
|
messageEvents.prototype.stop = function() {
|
|
|
|
this.filter.stopWatching();
|
|
|
|
};
|
|
|
|
|
2016-10-12 11:59:50 +00:00
|
|
|
var promise = new messageEvents();
|
2016-10-12 11:54:40 +00:00
|
|
|
|
|
|
|
var filter = web3.shh.filter(filterOptions, function(err, result) {
|
|
|
|
var payload = JSON.parse(web3.toAscii(result.payload));
|
2017-01-07 13:38:34 +00:00
|
|
|
var data;
|
2016-10-12 11:54:40 +00:00
|
|
|
if (err) {
|
|
|
|
promise.error(err);
|
|
|
|
} else {
|
2017-01-07 13:38:34 +00:00
|
|
|
data = {
|
2017-01-07 14:32:10 +00:00
|
|
|
topic: topics,
|
2017-01-07 13:38:34 +00:00
|
|
|
data: payload,
|
|
|
|
from: result.from,
|
|
|
|
time: (new Date(result.sent * 1000))
|
|
|
|
};
|
|
|
|
promise.cb(payload, data, result);
|
2016-10-12 11:54:40 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2017-01-26 11:31:29 +00:00
|
|
|
promise.filter = filter;
|
|
|
|
|
2016-10-12 11:54:40 +00:00
|
|
|
return promise;
|
2016-08-21 14:42:42 +00:00
|
|
|
};
|
|
|
|
|
2017-01-07 05:03:03 +00:00
|
|
|
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(',');
|
|
|
|
}
|
|
|
|
|
2017-01-07 15:13:23 +00:00
|
|
|
this.orbit.join(topics);
|
|
|
|
|
2017-01-07 05:03:03 +00:00
|
|
|
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();
|
|
|
|
|
2017-01-07 15:13:23 +00:00
|
|
|
this.orbit.events.on('message', (channel, message) => {
|
|
|
|
// TODO: looks like sometimes it's receving messages from all topics
|
|
|
|
if (topics !== channel) return;
|
2017-01-07 05:03:03 +00:00
|
|
|
self.orbit.getPost(message.payload.value, true).then((post) => {
|
2017-01-07 13:38:34 +00:00
|
|
|
var data = {
|
2017-01-07 15:13:23 +00:00
|
|
|
topic: channel,
|
2017-01-07 13:38:34 +00:00
|
|
|
data: post.content,
|
|
|
|
from: post.meta.from.name,
|
|
|
|
time: (new Date(post.meta.ts))
|
|
|
|
};
|
|
|
|
promise.cb(post.content, data, post);
|
2017-01-07 05:03:03 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
return promise;
|
|
|
|
};
|
|
|
|
|
2016-08-26 11:01:22 +00:00
|
|
|
module.exports = EmbarkJS;
|