mirror of https://github.com/embarklabs/embark.git
add support for whisper v5; make get url ipfs get configurable
This commit is contained in:
parent
2bf91b6d9a
commit
29c343463b
|
@ -290,8 +290,10 @@ EmbarkJS.Storage.setProvider = function(provider, options) {
|
||||||
try {
|
try {
|
||||||
if (options === undefined) {
|
if (options === undefined) {
|
||||||
self.ipfsConnection = IpfsApi('localhost', '5001');
|
self.ipfsConnection = IpfsApi('localhost', '5001');
|
||||||
|
self.getUrl = "http://localhost:8080/ipfs/";
|
||||||
} else {
|
} else {
|
||||||
self.ipfsConnection = IpfsApi(options.server, options.port);
|
self.ipfsConnection = IpfsApi(options.server, options.port);
|
||||||
|
self.getUrl = options.getUrl || "http://localhost:8080/ipfs/";
|
||||||
}
|
}
|
||||||
resolve(self);
|
resolve(self);
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
|
@ -383,7 +385,8 @@ EmbarkJS.Storage.IPFS.uploadFile = function(inputSelector) {
|
||||||
EmbarkJS.Storage.IPFS.getUrl = function(hash) {
|
EmbarkJS.Storage.IPFS.getUrl = function(hash) {
|
||||||
//var ipfsHash = web3.toAscii(hash);
|
//var ipfsHash = web3.toAscii(hash);
|
||||||
|
|
||||||
return 'http://localhost:8080/ipfs/' + hash;
|
//return 'http://localhost:8080/ipfs/' + hash;
|
||||||
|
return (self.getUrl || "http://localhost:8080/ipfs/") + hash;
|
||||||
};
|
};
|
||||||
|
|
||||||
//=========================================================
|
//=========================================================
|
||||||
|
@ -392,26 +395,70 @@ EmbarkJS.Storage.IPFS.getUrl = function(hash) {
|
||||||
|
|
||||||
EmbarkJS.Messages = {};
|
EmbarkJS.Messages = {};
|
||||||
|
|
||||||
|
EmbarkJS.Messages.web3CompatibleWithV5 = function() {
|
||||||
|
var _web3 = new Web3();
|
||||||
|
if (typeof(_web3.version) === "string") {
|
||||||
|
return true;
|
||||||
|
} else {
|
||||||
|
return parseInt(_web3.version.api.split('.')[1], 10) >= 20;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
EmbarkJS.Messages.isNewWeb3 = function() {
|
||||||
|
var _web3 = new Web3();
|
||||||
|
if (typeof(_web3.version) === "string") {
|
||||||
|
return true;
|
||||||
|
} else {
|
||||||
|
return parseInt(_web3.version.api.split('.')[0], 10) >= 1;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
EmbarkJS.Messages.getWhisperVersion = function(cb) {
|
||||||
|
if (this.isNewWeb3()) {
|
||||||
|
this.currentMessages.web3.shh.getVersion(function(err, version) {
|
||||||
|
cb(err, version);
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
this.currentMessages.web3.version.getWhisper(function(err, res) {
|
||||||
|
cb(err, web3.version.whisper);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
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' && typeof(web3) === 'undefined') {
|
if (typeof variable === 'undefined' && typeof(web3) === 'undefined') {
|
||||||
|
let provider;
|
||||||
if (options === undefined) {
|
if (options === undefined) {
|
||||||
web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:8545"));
|
provider = "localhost:8546";
|
||||||
} else {
|
} else {
|
||||||
web3 = new Web3(new Web3.providers.HttpProvider("http://" + options.server + ':' + options.port));
|
provider = options.server + ':' + options.port;
|
||||||
|
}
|
||||||
|
if (this.isNewWeb3()) {
|
||||||
|
// TODO: add current Provider
|
||||||
|
self.currentMessages.web3 = new Web3(new Web3.providers.WebsocketProvider("ws://" + provider));
|
||||||
|
} else {
|
||||||
|
self.currentMessages.web3 = new Web3(new Web3.providers.HttpProvider("http://" + provider));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
web3.version.getWhisper(function(err, res) {
|
console.log("getting whisper version");
|
||||||
|
self.getWhisperVersion(function(err, version) {
|
||||||
if (err) {
|
if (err) {
|
||||||
console.log("whisper not available");
|
console.log("whisper not available");
|
||||||
} else if (web3.version.whisper >= 5) {
|
} else if (version >= 5) {
|
||||||
console.log("this version of whisper is not supported yet; try a version of geth bellow 1.6.1");
|
if (self.web3CompatibleWithV5()) {
|
||||||
|
self.currentMessages.web3.shh.newSymKey().then((id) => {self.currentMessages.symKeyID = id;});
|
||||||
|
self.currentMessages.web3.shh.newKeyPair().then((id) => {self.currentMessages.sig = id;});
|
||||||
|
} else {
|
||||||
|
console.log("this version of whisper is not supported yet; try a version of geth bellow 1.6.1");
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
self.currentMessages.identity = web3.shh.newIdentity();
|
self.currentMessages.identity = self.currentMessages.web3.shh.newIdentity();
|
||||||
}
|
}
|
||||||
|
self.currentMessages.whisperVersion = self.currentMessages.web3.version.whisper;
|
||||||
});
|
});
|
||||||
} else if (provider === 'orbit') {
|
} else if (provider === 'orbit') {
|
||||||
this.currentMessages = EmbarkJS.Messages.Orbit;
|
this.currentMessages = EmbarkJS.Messages.Orbit;
|
||||||
|
@ -444,7 +491,10 @@ 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;
|
||||||
|
if (!EmbarkJS.Messages.isNewWeb3()) {
|
||||||
|
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;
|
||||||
|
@ -457,85 +507,129 @@ EmbarkJS.Messages.Whisper.sendMessage = function(options) {
|
||||||
throw new Error("missing option: data");
|
throw new Error("missing option: data");
|
||||||
}
|
}
|
||||||
|
|
||||||
// do fromAscii to each topics unless it's already a string
|
if (EmbarkJS.Messages.isNewWeb3()) {
|
||||||
if (typeof topics === 'string') {
|
topics = this.web3.utils.toHex(topics).slice(0, 10);
|
||||||
_topics = [web3.fromAscii(topics)];
|
|
||||||
} else {
|
} else {
|
||||||
|
if (typeof topics === 'string') {
|
||||||
|
_topics = [EmbarkJS.Utils.fromAscii(topics)];
|
||||||
|
} else {
|
||||||
// TODO: replace with es6 + babel;
|
// TODO: replace with es6 + babel;
|
||||||
for (var i = 0; i < topics.length; i++) {
|
for (var i = 0; i < topics.length; i++) {
|
||||||
_topics.push(web3.fromAscii(topics[i]));
|
_topics.push(EmbarkJS.Utils.fromAscii(topics[i]));
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
topics = _topics;
|
||||||
}
|
}
|
||||||
topics = _topics;
|
|
||||||
|
|
||||||
var payload = JSON.stringify(data);
|
var payload = JSON.stringify(data);
|
||||||
|
|
||||||
var message = {
|
var message;
|
||||||
|
if (EmbarkJS.Messages.isNewWeb3()) {
|
||||||
|
message = {
|
||||||
|
symKeyID: this.symKeyID, // encrypts using the sym key ID
|
||||||
|
sig: this.sig, // signs the message using the keyPair ID
|
||||||
|
ttl: 10,
|
||||||
|
topic: topics,
|
||||||
|
payload: EmbarkJS.Utils.fromAscii('hello'),
|
||||||
|
powTime: 3,
|
||||||
|
powTarget: 0.5
|
||||||
|
};
|
||||||
|
} else {
|
||||||
|
message = {
|
||||||
from: identity,
|
from: identity,
|
||||||
topics: topics,
|
topics: topics,
|
||||||
payload: web3.fromAscii(payload),
|
payload: EmbarkJS.Utils.fromAscii(payload),
|
||||||
ttl: ttl,
|
ttl: ttl,
|
||||||
priority: priority
|
priority: priority
|
||||||
};
|
};
|
||||||
|
}
|
||||||
|
|
||||||
return web3.shh.post(message, function() {});
|
return this.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 = [];
|
||||||
|
|
||||||
|
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();
|
||||||
|
};
|
||||||
|
|
||||||
|
if (EmbarkJS.Messages.isNewWeb3()) {
|
||||||
|
topics = [this.web3.utils.toHex(topics).slice(0, 10)];
|
||||||
|
} else {
|
||||||
if (typeof topics === 'string') {
|
if (typeof topics === 'string') {
|
||||||
_topics = [topics];
|
_topics = [topics];
|
||||||
} else {
|
} else {
|
||||||
// TODO: replace with es6 + babel;
|
// TODO: replace with es6 + babel;
|
||||||
for (var i = 0; i < topics.length; i++) {
|
for (var i = 0; i < topics.length; i++) {
|
||||||
_topics.push(topics[i]);
|
_topics.push(topics[i]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
topics = _topics;
|
topics = _topics;
|
||||||
|
}
|
||||||
|
|
||||||
var filterOptions = {
|
if (EmbarkJS.Messages.isNewWeb3()) {
|
||||||
|
let promise = new messageEvents();
|
||||||
|
|
||||||
|
let filter = this.web3.shh.subscribe("messages", {
|
||||||
|
symKeyID: this.symKeyID,
|
||||||
topics: topics
|
topics: topics
|
||||||
};
|
}).on('data', function(result) {
|
||||||
|
var payload = JSON.parse(EmbarkJS.Utils.toAscii(result.payload));
|
||||||
var messageEvents = function() {
|
var data;
|
||||||
this.cb = function() {};
|
data = {
|
||||||
};
|
topic: result.topic,
|
||||||
|
data: payload,
|
||||||
messageEvents.prototype.then = function(cb) {
|
//from: result.from,
|
||||||
this.cb = cb;
|
time: result.timestamp
|
||||||
};
|
};
|
||||||
|
promise.cb(payload, data, result);
|
||||||
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;
|
promise.filter = filter;
|
||||||
|
|
||||||
return promise;
|
return promise;
|
||||||
|
|
||||||
|
} else {
|
||||||
|
var filterOptions = {
|
||||||
|
topics: topics
|
||||||
|
};
|
||||||
|
|
||||||
|
let promise = new messageEvents();
|
||||||
|
|
||||||
|
let filter = this.web3.shh.filter(filterOptions, function(err, result) {
|
||||||
|
var payload = JSON.parse(EmbarkJS.Utils.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 = {};
|
||||||
|
@ -609,6 +703,13 @@ EmbarkJS.Messages.Orbit.listenTo = function(options) {
|
||||||
return promise;
|
return promise;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
EmbarkJS.Utils = {
|
||||||
|
fromAscii: function(str) {
|
||||||
|
var _web3 = new Web3();
|
||||||
|
return _web3.utils ? _web3.utils.fromAscii(str) : _web3.fromAscii(str);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
module.exports = EmbarkJS;
|
module.exports = EmbarkJS;
|
||||||
|
|
||||||
|
|
||||||
|
|
214
js/embark.js
214
js/embark.js
|
@ -313,26 +313,70 @@ EmbarkJS.Storage.IPFS.getUrl = function(hash) {
|
||||||
|
|
||||||
EmbarkJS.Messages = {};
|
EmbarkJS.Messages = {};
|
||||||
|
|
||||||
|
EmbarkJS.Messages.web3CompatibleWithV5 = function() {
|
||||||
|
var _web3 = new Web3();
|
||||||
|
if (typeof(_web3.version) === "string") {
|
||||||
|
return true;
|
||||||
|
} else {
|
||||||
|
return parseInt(_web3.version.api.split('.')[1], 10) >= 20;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
EmbarkJS.Messages.isNewWeb3 = function() {
|
||||||
|
var _web3 = new Web3();
|
||||||
|
if (typeof(_web3.version) === "string") {
|
||||||
|
return true;
|
||||||
|
} else {
|
||||||
|
return parseInt(_web3.version.api.split('.')[0], 10) >= 1;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
EmbarkJS.Messages.getWhisperVersion = function(cb) {
|
||||||
|
if (this.isNewWeb3()) {
|
||||||
|
this.currentMessages.web3.shh.getVersion(function(err, version) {
|
||||||
|
cb(err, version);
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
this.currentMessages.web3.version.getWhisper(function(err, res) {
|
||||||
|
cb(err, web3.version.whisper);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
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' && typeof(web3) === 'undefined') {
|
if (typeof variable === 'undefined' && typeof(web3) === 'undefined') {
|
||||||
|
let provider;
|
||||||
if (options === undefined) {
|
if (options === undefined) {
|
||||||
web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:8545"));
|
provider = "localhost:8546";
|
||||||
} else {
|
} else {
|
||||||
web3 = new Web3(new Web3.providers.HttpProvider("http://" + options.server + ':' + options.port));
|
provider = options.server + ':' + options.port;
|
||||||
|
}
|
||||||
|
if (this.isNewWeb3()) {
|
||||||
|
// TODO: add current Provider
|
||||||
|
self.currentMessages.web3 = new Web3(new Web3.providers.WebsocketProvider("ws://" + provider));
|
||||||
|
} else {
|
||||||
|
self.currentMessages.web3 = new Web3(new Web3.providers.HttpProvider("http://" + provider));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
web3.version.getWhisper(function(err, res) {
|
console.log("getting whisper version");
|
||||||
|
self.getWhisperVersion(function(err, version) {
|
||||||
if (err) {
|
if (err) {
|
||||||
console.log("whisper not available");
|
console.log("whisper not available");
|
||||||
} else if (web3.version.whisper >= 5) {
|
} else if (version >= 5) {
|
||||||
console.log("this version of whisper is not supported yet; try a version of geth bellow 1.6.1");
|
if (self.web3CompatibleWithV5()) {
|
||||||
|
self.currentMessages.web3.shh.newSymKey().then((id) => {self.currentMessages.symKeyID = id;});
|
||||||
|
self.currentMessages.web3.shh.newKeyPair().then((id) => {self.currentMessages.sig = id;});
|
||||||
|
} else {
|
||||||
|
console.log("this version of whisper is not supported yet; try a version of geth bellow 1.6.1");
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
self.currentMessages.identity = web3.shh.newIdentity();
|
self.currentMessages.identity = self.currentMessages.web3.shh.newIdentity();
|
||||||
}
|
}
|
||||||
|
self.currentMessages.whisperVersion = self.currentMessages.web3.version.whisper;
|
||||||
});
|
});
|
||||||
} else if (provider === 'orbit') {
|
} else if (provider === 'orbit') {
|
||||||
this.currentMessages = EmbarkJS.Messages.Orbit;
|
this.currentMessages = EmbarkJS.Messages.Orbit;
|
||||||
|
@ -365,7 +409,10 @@ 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;
|
||||||
|
if (!EmbarkJS.Messages.isNewWeb3()) {
|
||||||
|
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;
|
||||||
|
@ -378,85 +425,129 @@ EmbarkJS.Messages.Whisper.sendMessage = function(options) {
|
||||||
throw new Error("missing option: data");
|
throw new Error("missing option: data");
|
||||||
}
|
}
|
||||||
|
|
||||||
// do fromAscii to each topics unless it's already a string
|
if (EmbarkJS.Messages.isNewWeb3()) {
|
||||||
if (typeof topics === 'string') {
|
topics = this.web3.utils.toHex(topics).slice(0, 10);
|
||||||
_topics = [web3.fromAscii(topics)];
|
|
||||||
} else {
|
} else {
|
||||||
|
if (typeof topics === 'string') {
|
||||||
|
_topics = [EmbarkJS.Utils.fromAscii(topics)];
|
||||||
|
} else {
|
||||||
// TODO: replace with es6 + babel;
|
// TODO: replace with es6 + babel;
|
||||||
for (var i = 0; i < topics.length; i++) {
|
for (var i = 0; i < topics.length; i++) {
|
||||||
_topics.push(web3.fromAscii(topics[i]));
|
_topics.push(EmbarkJS.Utils.fromAscii(topics[i]));
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
topics = _topics;
|
||||||
}
|
}
|
||||||
topics = _topics;
|
|
||||||
|
|
||||||
var payload = JSON.stringify(data);
|
var payload = JSON.stringify(data);
|
||||||
|
|
||||||
var message = {
|
var message;
|
||||||
|
if (EmbarkJS.Messages.isNewWeb3()) {
|
||||||
|
message = {
|
||||||
|
symKeyID: this.symKeyID, // encrypts using the sym key ID
|
||||||
|
sig: this.sig, // signs the message using the keyPair ID
|
||||||
|
ttl: 10,
|
||||||
|
topic: topics,
|
||||||
|
payload: EmbarkJS.Utils.fromAscii('hello'),
|
||||||
|
powTime: 3,
|
||||||
|
powTarget: 0.5
|
||||||
|
};
|
||||||
|
} else {
|
||||||
|
message = {
|
||||||
from: identity,
|
from: identity,
|
||||||
topics: topics,
|
topics: topics,
|
||||||
payload: web3.fromAscii(payload),
|
payload: EmbarkJS.Utils.fromAscii(payload),
|
||||||
ttl: ttl,
|
ttl: ttl,
|
||||||
priority: priority
|
priority: priority
|
||||||
};
|
};
|
||||||
|
}
|
||||||
|
|
||||||
return web3.shh.post(message, function() {});
|
return this.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 = [];
|
||||||
|
|
||||||
|
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();
|
||||||
|
};
|
||||||
|
|
||||||
|
if (EmbarkJS.Messages.isNewWeb3()) {
|
||||||
|
topics = [this.web3.utils.toHex(topics).slice(0, 10)];
|
||||||
|
} else {
|
||||||
if (typeof topics === 'string') {
|
if (typeof topics === 'string') {
|
||||||
_topics = [topics];
|
_topics = [topics];
|
||||||
} else {
|
} else {
|
||||||
// TODO: replace with es6 + babel;
|
// TODO: replace with es6 + babel;
|
||||||
for (var i = 0; i < topics.length; i++) {
|
for (var i = 0; i < topics.length; i++) {
|
||||||
_topics.push(topics[i]);
|
_topics.push(topics[i]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
topics = _topics;
|
topics = _topics;
|
||||||
|
}
|
||||||
|
|
||||||
var filterOptions = {
|
if (EmbarkJS.Messages.isNewWeb3()) {
|
||||||
|
let promise = new messageEvents();
|
||||||
|
|
||||||
|
let filter = this.web3.shh.subscribe("messages", {
|
||||||
|
symKeyID: this.symKeyID,
|
||||||
topics: topics
|
topics: topics
|
||||||
};
|
}).on('data', function(result) {
|
||||||
|
var payload = JSON.parse(EmbarkJS.Utils.toAscii(result.payload));
|
||||||
var messageEvents = function() {
|
var data;
|
||||||
this.cb = function() {};
|
data = {
|
||||||
};
|
topic: result.topic,
|
||||||
|
data: payload,
|
||||||
messageEvents.prototype.then = function(cb) {
|
//from: result.from,
|
||||||
this.cb = cb;
|
time: result.timestamp
|
||||||
};
|
};
|
||||||
|
promise.cb(payload, data, result);
|
||||||
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;
|
promise.filter = filter;
|
||||||
|
|
||||||
return promise;
|
return promise;
|
||||||
|
|
||||||
|
} else {
|
||||||
|
var filterOptions = {
|
||||||
|
topics: topics
|
||||||
|
};
|
||||||
|
|
||||||
|
let promise = new messageEvents();
|
||||||
|
|
||||||
|
let filter = this.web3.shh.filter(filterOptions, function(err, result) {
|
||||||
|
var payload = JSON.parse(EmbarkJS.Utils.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 = {};
|
||||||
|
@ -530,4 +621,11 @@ EmbarkJS.Messages.Orbit.listenTo = function(options) {
|
||||||
return promise;
|
return promise;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
EmbarkJS.Utils = {
|
||||||
|
fromAscii: function(str) {
|
||||||
|
var _web3 = new Web3();
|
||||||
|
return _web3.utils ? _web3.utils.fromAscii(str) : _web3.fromAscii(str);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
module.exports = EmbarkJS;
|
module.exports = EmbarkJS;
|
||||||
|
|
|
@ -83,14 +83,14 @@ class ABIGenerator {
|
||||||
code += '\n\tweb3 = new Web3(web3.currentProvider);';
|
code += '\n\tweb3 = new Web3(web3.currentProvider);';
|
||||||
code += '\n}';
|
code += '\n}';
|
||||||
} else {
|
} else {
|
||||||
code += "if (typeof Web3 !== 'undefined' && !web3.isConnected()) {";
|
code += "if (typeof Web3 !== 'undefined' && ((typeof web3 === 'undefined') || (typeof web3 !== 'undefined' && !web3.isConnected()))) {";
|
||||||
code += '\n\tweb3 = new Web3(new Web3.providers.HttpProvider("' + connection + '"));';
|
code += '\n\tweb3 = new Web3(new Web3.providers.HttpProvider("' + connection + '"));';
|
||||||
code += '\n}';
|
code += '\n}';
|
||||||
}
|
}
|
||||||
return code;
|
return code;
|
||||||
});
|
});
|
||||||
|
|
||||||
result += connectionCode.join(' else ');
|
result += connectionCode.join(' ');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -159,7 +159,18 @@ class ABIGenerator {
|
||||||
|
|
||||||
if (self.storageConfig.provider === 'ipfs' && self.storageConfig.enabled === true) {
|
if (self.storageConfig.provider === 'ipfs' && self.storageConfig.enabled === true) {
|
||||||
// TODO: make this more readable
|
// TODO: make this more readable
|
||||||
|
|
||||||
|
result += "\nvar whenEnvIsLoaded = function(cb) {";
|
||||||
|
result += "\n if (typeof document !== 'undefined' && document !== null) {";
|
||||||
|
result += "\n document.addEventListener('DOMContentLoaded', cb);";
|
||||||
|
result += "\n } else {";
|
||||||
|
result += "\n cb();";
|
||||||
|
result += "\n }";
|
||||||
|
result += "\n}";
|
||||||
|
|
||||||
|
result += "\nwhenEnvIsLoaded(function() {\n";
|
||||||
result += "\nEmbarkJS.Storage.setProvider('" + self.storageConfig.provider + "', {server: '" + self.storageConfig.host + "', port: '" + self.storageConfig.port + "', getUrl: '" + self.storageConfig.getUrl + "'});";
|
result += "\nEmbarkJS.Storage.setProvider('" + self.storageConfig.provider + "', {server: '" + self.storageConfig.host + "', port: '" + self.storageConfig.port + "', getUrl: '" + self.storageConfig.getUrl + "'});";
|
||||||
|
result += '\n})';
|
||||||
}
|
}
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
|
@ -171,14 +182,26 @@ class ABIGenerator {
|
||||||
|
|
||||||
if (!useEmbarkJS || self.communicationConfig === {}) return "";
|
if (!useEmbarkJS || self.communicationConfig === {}) return "";
|
||||||
|
|
||||||
|
result += "\nvar whenEnvIsLoaded = function(cb) {";
|
||||||
|
result += "\n if (typeof document !== 'undefined' && document !== null) {";
|
||||||
|
result += "\n document.addEventListener('DOMContentLoaded', cb);";
|
||||||
|
result += "\n } else {";
|
||||||
|
result += "\n cb();";
|
||||||
|
result += "\n }";
|
||||||
|
result += "\n}";
|
||||||
|
|
||||||
if (self.communicationConfig.provider === 'whisper' && self.communicationConfig.enabled === true) {
|
if (self.communicationConfig.provider === 'whisper' && self.communicationConfig.enabled === true) {
|
||||||
|
result += "\nwhenEnvIsLoaded(function() {\n";
|
||||||
result += "\nEmbarkJS.Messages.setProvider('" + self.communicationConfig.provider + "');";
|
result += "\nEmbarkJS.Messages.setProvider('" + self.communicationConfig.provider + "');";
|
||||||
|
result += '\n})';
|
||||||
} else if (self.communicationConfig.provider === 'orbit' && self.communicationConfig.enabled === true) {
|
} else if (self.communicationConfig.provider === 'orbit' && self.communicationConfig.enabled === true) {
|
||||||
|
result += "\nwhenEnvIsLoaded(function() {\n";
|
||||||
if (self.communicationConfig.host === undefined && self.communicationConfig.port === undefined) {
|
if (self.communicationConfig.host === undefined && self.communicationConfig.port === undefined) {
|
||||||
result += "\nEmbarkJS.Messages.setProvider('" + self.communicationConfig.provider + "');";
|
result += "\nEmbarkJS.Messages.setProvider('" + self.communicationConfig.provider + "');";
|
||||||
} else {
|
} else {
|
||||||
result += "\nEmbarkJS.Messages.setProvider('" + self.communicationConfig.provider + "', {server: '" + self.communicationConfig.host + "', port: '" + self.communicationConfig.port + "'});";
|
result += "\nEmbarkJS.Messages.setProvider('" + self.communicationConfig.provider + "', {server: '" + self.communicationConfig.host + "', port: '" + self.communicationConfig.port + "'});";
|
||||||
}
|
}
|
||||||
|
result += '\n})';
|
||||||
}
|
}
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
|
|
|
@ -10,7 +10,7 @@ describe('embark.ABIGenerator', function() {
|
||||||
let generator = new ABIGenerator({contractsConfig: {"dappConnection": [ "$WEB3", "http://somehost:1234" ] }, contractsManager: {}});
|
let generator = new ABIGenerator({contractsConfig: {"dappConnection": [ "$WEB3", "http://somehost:1234" ] }, contractsManager: {}});
|
||||||
|
|
||||||
it('should generate code to connect to a provider', function() {
|
it('should generate code to connect to a provider', function() {
|
||||||
var providerCode = "\nvar whenEnvIsLoaded = function(cb) {\n if (typeof document !== 'undefined' && document !== null) {\n document.addEventListener('DOMContentLoaded', cb);\n } else {\n cb();\n }\n}\nwhenEnvIsLoaded(function() {\nif (typeof web3 !== 'undefined' && typeof Web3 !== 'undefined') {\n\tweb3 = new Web3(web3.currentProvider);\n} else if (typeof Web3 !== 'undefined' && !web3.isConnected()) {\n\tweb3 = new Web3(new Web3.providers.HttpProvider(\"http://somehost:1234\"));\n}\nweb3.eth.defaultAccount = web3.eth.accounts[0];\n})";
|
var providerCode = "\nvar whenEnvIsLoaded = function(cb) {\n if (typeof document !== 'undefined' && document !== null) {\n document.addEventListener('DOMContentLoaded', cb);\n } else {\n cb();\n }\n}\nwhenEnvIsLoaded(function() {\nif (typeof web3 !== 'undefined' && typeof Web3 !== 'undefined') {\n\tweb3 = new Web3(web3.currentProvider);\n} if (typeof Web3 !== 'undefined' && ((typeof web3 === 'undefined') || (typeof web3 !== 'undefined' && !web3.isConnected()))) {\n\tweb3 = new Web3(new Web3.providers.HttpProvider(\"http://somehost:1234\"));\n}\nweb3.eth.defaultAccount = web3.eth.accounts[0];\n})";
|
||||||
|
|
||||||
assert.equal(generator.generateProvider(), providerCode);
|
assert.equal(generator.generateProvider(), providerCode);
|
||||||
});
|
});
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
{
|
{
|
||||||
"default": {
|
"default": {
|
||||||
"versions": {
|
"versions": {
|
||||||
"web3.js": "0.19.1",
|
"web3.js": "1.0.0",
|
||||||
"solc": "0.4.11"
|
"solc": "0.4.11"
|
||||||
},
|
},
|
||||||
"deployment": {
|
"deployment": {
|
||||||
|
|
Loading…
Reference in New Issue