add support for whisper v5; make get url ipfs get configurable

This commit is contained in:
Iuri Matias 2017-07-24 07:27:52 -04:00
parent 2bf91b6d9a
commit 29c343463b
5 changed files with 343 additions and 121 deletions

View File

@ -290,8 +290,10 @@ EmbarkJS.Storage.setProvider = function(provider, options) {
try {
if (options === undefined) {
self.ipfsConnection = IpfsApi('localhost', '5001');
self.getUrl = "http://localhost:8080/ipfs/";
} else {
self.ipfsConnection = IpfsApi(options.server, options.port);
self.getUrl = options.getUrl || "http://localhost:8080/ipfs/";
}
resolve(self);
} catch (err) {
@ -383,7 +385,8 @@ EmbarkJS.Storage.IPFS.uploadFile = function(inputSelector) {
EmbarkJS.Storage.IPFS.getUrl = function(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.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) {
var self = this;
var ipfs;
if (provider === 'whisper') {
this.currentMessages = EmbarkJS.Messages.Whisper;
if (typeof variable === 'undefined' && typeof(web3) === 'undefined') {
let provider;
if (options === undefined) {
web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:8545"));
provider = "localhost:8546";
} 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) {
console.log("whisper not available");
} else if (web3.version.whisper >= 5) {
console.log("this version of whisper is not supported yet; try a version of geth bellow 1.6.1");
} else if (version >= 5) {
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 {
self.currentMessages.identity = web3.shh.newIdentity();
console.log("this version of whisper is not supported yet; try a version of geth bellow 1.6.1");
}
} else {
self.currentMessages.identity = self.currentMessages.web3.shh.newIdentity();
}
self.currentMessages.whisperVersion = self.currentMessages.web3.version.whisper;
});
} else if (provider === 'orbit') {
this.currentMessages = EmbarkJS.Messages.Orbit;
@ -444,7 +491,10 @@ EmbarkJS.Messages.Whisper = {};
EmbarkJS.Messages.Whisper.sendMessage = function(options) {
var topics = options.topic || options.topics;
var data = options.data || options.payload;
var identity = options.identity || this.identity || web3.shh.newIdentity();
var identity;
if (!EmbarkJS.Messages.isNewWeb3()) {
identity = options.identity || this.identity || web3.shh.newIdentity();
}
var ttl = options.ttl || 100;
var priority = options.priority || 1000;
var _topics;
@ -457,48 +507,50 @@ EmbarkJS.Messages.Whisper.sendMessage = function(options) {
throw new Error("missing option: data");
}
// do fromAscii to each topics unless it's already a string
if (EmbarkJS.Messages.isNewWeb3()) {
topics = this.web3.utils.toHex(topics).slice(0, 10);
} else {
if (typeof topics === 'string') {
_topics = [web3.fromAscii(topics)];
_topics = [EmbarkJS.Utils.fromAscii(topics)];
} else {
// TODO: replace with es6 + babel;
for (var i = 0; i < topics.length; i++) {
_topics.push(web3.fromAscii(topics[i]));
_topics.push(EmbarkJS.Utils.fromAscii(topics[i]));
}
}
topics = _topics;
}
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,
topics: topics,
payload: web3.fromAscii(payload),
payload: EmbarkJS.Utils.fromAscii(payload),
ttl: ttl,
priority: priority
};
}
return web3.shh.post(message, function() {});
return this.web3.shh.post(message, function() {});
};
EmbarkJS.Messages.Whisper.listenTo = function(options) {
var topics = options.topic || options.topics;
var _topics = [];
if (typeof topics === 'string') {
_topics = [topics];
} else {
// TODO: replace with es6 + babel;
for (var i = 0; i < topics.length; i++) {
_topics.push(topics[i]);
}
}
topics = _topics;
var filterOptions = {
topics: topics
};
var messageEvents = function() {
this.cb = function() {};
};
@ -515,10 +567,51 @@ EmbarkJS.Messages.Whisper.listenTo = function(options) {
this.filter.stopWatching();
};
var promise = new messageEvents();
if (EmbarkJS.Messages.isNewWeb3()) {
topics = [this.web3.utils.toHex(topics).slice(0, 10)];
} else {
if (typeof topics === 'string') {
_topics = [topics];
} else {
// TODO: replace with es6 + babel;
for (var i = 0; i < topics.length; i++) {
_topics.push(topics[i]);
}
}
topics = _topics;
}
var filter = web3.shh.filter(filterOptions, function(err, result) {
var payload = JSON.parse(web3.toAscii(result.payload));
if (EmbarkJS.Messages.isNewWeb3()) {
let promise = new messageEvents();
let filter = this.web3.shh.subscribe("messages", {
symKeyID: this.symKeyID,
topics: topics
}).on('data', function(result) {
var payload = JSON.parse(EmbarkJS.Utils.toAscii(result.payload));
var data;
data = {
topic: result.topic,
data: payload,
//from: result.from,
time: result.timestamp
};
promise.cb(payload, data, result);
});
promise.filter = filter;
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);
@ -536,6 +629,7 @@ EmbarkJS.Messages.Whisper.listenTo = function(options) {
promise.filter = filter;
return promise;
}
};
EmbarkJS.Messages.Orbit = {};
@ -609,6 +703,13 @@ EmbarkJS.Messages.Orbit.listenTo = function(options) {
return promise;
};
EmbarkJS.Utils = {
fromAscii: function(str) {
var _web3 = new Web3();
return _web3.utils ? _web3.utils.fromAscii(str) : _web3.fromAscii(str);
}
};
module.exports = EmbarkJS;

View File

@ -313,26 +313,70 @@ EmbarkJS.Storage.IPFS.getUrl = function(hash) {
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) {
var self = this;
var ipfs;
if (provider === 'whisper') {
this.currentMessages = EmbarkJS.Messages.Whisper;
if (typeof variable === 'undefined' && typeof(web3) === 'undefined') {
let provider;
if (options === undefined) {
web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:8545"));
provider = "localhost:8546";
} 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) {
console.log("whisper not available");
} else if (web3.version.whisper >= 5) {
console.log("this version of whisper is not supported yet; try a version of geth bellow 1.6.1");
} else if (version >= 5) {
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 {
self.currentMessages.identity = web3.shh.newIdentity();
console.log("this version of whisper is not supported yet; try a version of geth bellow 1.6.1");
}
} else {
self.currentMessages.identity = self.currentMessages.web3.shh.newIdentity();
}
self.currentMessages.whisperVersion = self.currentMessages.web3.version.whisper;
});
} else if (provider === 'orbit') {
this.currentMessages = EmbarkJS.Messages.Orbit;
@ -365,7 +409,10 @@ EmbarkJS.Messages.Whisper = {};
EmbarkJS.Messages.Whisper.sendMessage = function(options) {
var topics = options.topic || options.topics;
var data = options.data || options.payload;
var identity = options.identity || this.identity || web3.shh.newIdentity();
var identity;
if (!EmbarkJS.Messages.isNewWeb3()) {
identity = options.identity || this.identity || web3.shh.newIdentity();
}
var ttl = options.ttl || 100;
var priority = options.priority || 1000;
var _topics;
@ -378,48 +425,50 @@ EmbarkJS.Messages.Whisper.sendMessage = function(options) {
throw new Error("missing option: data");
}
// do fromAscii to each topics unless it's already a string
if (EmbarkJS.Messages.isNewWeb3()) {
topics = this.web3.utils.toHex(topics).slice(0, 10);
} else {
if (typeof topics === 'string') {
_topics = [web3.fromAscii(topics)];
_topics = [EmbarkJS.Utils.fromAscii(topics)];
} else {
// TODO: replace with es6 + babel;
for (var i = 0; i < topics.length; i++) {
_topics.push(web3.fromAscii(topics[i]));
_topics.push(EmbarkJS.Utils.fromAscii(topics[i]));
}
}
topics = _topics;
}
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,
topics: topics,
payload: web3.fromAscii(payload),
payload: EmbarkJS.Utils.fromAscii(payload),
ttl: ttl,
priority: priority
};
}
return web3.shh.post(message, function() {});
return this.web3.shh.post(message, function() {});
};
EmbarkJS.Messages.Whisper.listenTo = function(options) {
var topics = options.topic || options.topics;
var _topics = [];
if (typeof topics === 'string') {
_topics = [topics];
} else {
// TODO: replace with es6 + babel;
for (var i = 0; i < topics.length; i++) {
_topics.push(topics[i]);
}
}
topics = _topics;
var filterOptions = {
topics: topics
};
var messageEvents = function() {
this.cb = function() {};
};
@ -436,10 +485,51 @@ EmbarkJS.Messages.Whisper.listenTo = function(options) {
this.filter.stopWatching();
};
var promise = new messageEvents();
if (EmbarkJS.Messages.isNewWeb3()) {
topics = [this.web3.utils.toHex(topics).slice(0, 10)];
} else {
if (typeof topics === 'string') {
_topics = [topics];
} else {
// TODO: replace with es6 + babel;
for (var i = 0; i < topics.length; i++) {
_topics.push(topics[i]);
}
}
topics = _topics;
}
var filter = web3.shh.filter(filterOptions, function(err, result) {
var payload = JSON.parse(web3.toAscii(result.payload));
if (EmbarkJS.Messages.isNewWeb3()) {
let promise = new messageEvents();
let filter = this.web3.shh.subscribe("messages", {
symKeyID: this.symKeyID,
topics: topics
}).on('data', function(result) {
var payload = JSON.parse(EmbarkJS.Utils.toAscii(result.payload));
var data;
data = {
topic: result.topic,
data: payload,
//from: result.from,
time: result.timestamp
};
promise.cb(payload, data, result);
});
promise.filter = filter;
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);
@ -457,6 +547,7 @@ EmbarkJS.Messages.Whisper.listenTo = function(options) {
promise.filter = filter;
return promise;
}
};
EmbarkJS.Messages.Orbit = {};
@ -530,4 +621,11 @@ EmbarkJS.Messages.Orbit.listenTo = function(options) {
return promise;
};
EmbarkJS.Utils = {
fromAscii: function(str) {
var _web3 = new Web3();
return _web3.utils ? _web3.utils.fromAscii(str) : _web3.fromAscii(str);
}
};
module.exports = EmbarkJS;

View File

@ -83,14 +83,14 @@ class ABIGenerator {
code += '\n\tweb3 = new Web3(web3.currentProvider);';
code += '\n}';
} 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}';
}
return code;
});
result += connectionCode.join(' else ');
result += connectionCode.join(' ');
}
@ -159,7 +159,18 @@ class ABIGenerator {
if (self.storageConfig.provider === 'ipfs' && self.storageConfig.enabled === true) {
// 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 += '\n})';
}
return result;
@ -171,14 +182,26 @@ class ABIGenerator {
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) {
result += "\nwhenEnvIsLoaded(function() {\n";
result += "\nEmbarkJS.Messages.setProvider('" + self.communicationConfig.provider + "');";
result += '\n})';
} else if (self.communicationConfig.provider === 'orbit' && self.communicationConfig.enabled === true) {
result += "\nwhenEnvIsLoaded(function() {\n";
if (self.communicationConfig.host === undefined && self.communicationConfig.port === undefined) {
result += "\nEmbarkJS.Messages.setProvider('" + self.communicationConfig.provider + "');";
} else {
result += "\nEmbarkJS.Messages.setProvider('" + self.communicationConfig.provider + "', {server: '" + self.communicationConfig.host + "', port: '" + self.communicationConfig.port + "'});";
}
result += '\n})';
}
return result;

View File

@ -10,7 +10,7 @@ describe('embark.ABIGenerator', function() {
let generator = new ABIGenerator({contractsConfig: {"dappConnection": [ "$WEB3", "http://somehost:1234" ] }, contractsManager: {}});
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);
});

View File

@ -1,7 +1,7 @@
{
"default": {
"versions": {
"web3.js": "0.19.1",
"web3.js": "1.0.0",
"solc": "0.4.11"
},
"deployment": {