2016-08-26 11:01:22 +00:00
|
|
|
|
var Promise = require('bluebird');
|
|
|
|
|
//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;
|
|
|
|
|
|
|
|
|
|
this.abi = options.abi;
|
|
|
|
|
this.address = options.address;
|
|
|
|
|
this.code = options.code;
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
this._originalContractObject = ContractClass.at(this.address);
|
|
|
|
|
this._methods = Object.getOwnPropertyNames(this._originalContractObject).filter(function (p) {
|
|
|
|
|
// TODO: check for forbidden properties
|
|
|
|
|
if (typeof self._originalContractObject[p] === 'function') {
|
|
|
|
|
self[p] = Promise.promisify(self._originalContractObject[p]);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
EmbarkJS.Contract.prototype.deploy = function(args) {
|
|
|
|
|
var self = this;
|
|
|
|
|
var contractParams;
|
|
|
|
|
|
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,
|
2016-09-13 10:58:52 +00:00
|
|
|
|
gas: 500000,
|
2016-08-18 11:45:08 +00:00
|
|
|
|
gasPrice: 10000000000000
|
|
|
|
|
});
|
|
|
|
|
|
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) {
|
|
|
|
|
console.log("callback");
|
|
|
|
|
if (err) {
|
|
|
|
|
console.log("error");
|
|
|
|
|
reject(err);
|
|
|
|
|
} else if (transaction.address !== undefined) {
|
|
|
|
|
console.log("address contract: " + transaction.address);
|
|
|
|
|
resolve(new EmbarkJS.Contract({abi: self.abi, code: self.code, address: transaction.address}));
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
console.log(contractParams);
|
|
|
|
|
|
|
|
|
|
// 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('ipfs',{server: 'localhost', port: '5001'})<F37>
|
|
|
|
|
//{server: ‘localhost’, port: ‘5001’};
|
|
|
|
|
|
|
|
|
|
EmbarkJS.Storage.setProvider = function(provider, options) {
|
|
|
|
|
if (provider === 'ipfs') {
|
|
|
|
|
this.currentStorage = EmbarkJS.Storage.IPFS;
|
2016-10-12 12:20:20 +00:00
|
|
|
|
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;
|
|
|
|
|
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-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-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 = {
|
|
|
|
|
};
|
|
|
|
|
|
2016-10-12 11:54:40 +00:00
|
|
|
|
EmbarkJS.Messages.setProvider = function(provider) {
|
|
|
|
|
if (provider === 'whisper') {
|
|
|
|
|
this.currentMessages = EmbarkJS.Messages.Whisper;
|
|
|
|
|
} else {
|
|
|
|
|
throw Error('unknown provider');
|
|
|
|
|
}
|
2016-08-21 14:42:42 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
EmbarkJS.Messages.sendMessage = function(options) {
|
2016-10-12 11:54:40 +00:00
|
|
|
|
return EmbarkJS.Messages.Whisper.sendMessage(options);
|
2016-08-21 14:42:42 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
EmbarkJS.Messages.listenTo = function(options) {
|
2016-10-12 11:54:40 +00:00
|
|
|
|
return EmbarkJS.Messages.Whisper.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;
|
|
|
|
|
var identity = options.identity || web3.shh.newIdentity();
|
|
|
|
|
var ttl = options.ttl || 100;
|
|
|
|
|
var priority = options.priority || 1000;
|
|
|
|
|
|
|
|
|
|
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 = topics;
|
|
|
|
|
} else {
|
|
|
|
|
// TODO: replace with es6 + babel;
|
|
|
|
|
var _topics = [];
|
|
|
|
|
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: [web3.fromAscii(topics)],
|
|
|
|
|
payload: web3.fromAscii(payload),
|
|
|
|
|
ttl: ttl,
|
|
|
|
|
priority: priority
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return web3.shh.post(message);
|
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;
|
|
|
|
|
|
|
|
|
|
if (typeof topics === 'string') {
|
|
|
|
|
topics = [topics];
|
|
|
|
|
} else {
|
|
|
|
|
// TODO: replace with es6 + babel;
|
|
|
|
|
var _topics = [];
|
|
|
|
|
for (var i = 0; i < topics.length; i++) {
|
|
|
|
|
_topics.push(web3.fromAscii(topics[i]));
|
|
|
|
|
}
|
|
|
|
|
topics = _topics;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
};
|
|
|
|
|
|
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));
|
|
|
|
|
if (err) {
|
|
|
|
|
promise.error(err);
|
|
|
|
|
} else {
|
|
|
|
|
promise.cb(payload);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return promise;
|
2016-08-21 14:42:42 +00:00
|
|
|
|
};
|
|
|
|
|
|
2016-08-26 11:01:22 +00:00
|
|
|
|
module.exports = EmbarkJS;
|