move old code to a separate file
This commit is contained in:
parent
f4e81da630
commit
37a126a6b8
|
@ -1,128 +1,5 @@
|
|||
/*global EmbarkJS, Web3 */
|
||||
|
||||
// for the old version of whisper and web3.js
|
||||
let __embarkWhisperOld = {};
|
||||
|
||||
__embarkWhisperOld.setProvider = function(options) {
|
||||
const self = this;
|
||||
let provider;
|
||||
if (options === undefined) {
|
||||
provider = "localhost:8546";
|
||||
} else {
|
||||
provider = options.server + ':' + options.port;
|
||||
}
|
||||
self.web3 = new Web3(new Web3.providers.HttpProvider("http://" + provider));
|
||||
self.getWhisperVersion(function(err, version) {
|
||||
if (err) {
|
||||
console.log("whisper not available");
|
||||
} else if (version >= 5) {
|
||||
throw new Error("whisper 5 not supported with this version of web3.js");
|
||||
} else {
|
||||
self.identity = self.web3.shh.newIdentity();
|
||||
}
|
||||
self.whisperVersion = self.web3.version.whisper;
|
||||
});
|
||||
};
|
||||
|
||||
__embarkWhisperOld.sendMessage = function(options) {
|
||||
var topics, data, ttl, priority, payload;
|
||||
topics = options.topic || options.topics;
|
||||
data = options.data || options.payload;
|
||||
ttl = options.ttl || 100;
|
||||
priority = options.priority || 1000;
|
||||
var identity = options.identity || this.identity || this.web3.shh.newIdentity();
|
||||
var _topics;
|
||||
|
||||
if (topics === undefined) {
|
||||
throw new Error("missing option: topic");
|
||||
}
|
||||
|
||||
if (data === undefined) {
|
||||
throw new Error("missing option: data");
|
||||
}
|
||||
|
||||
if (typeof topics === 'string') {
|
||||
_topics = [EmbarkJS.Utils.fromAscii(topics)];
|
||||
} else {
|
||||
_topics = topics.map((t) => EmbarkJS.Utils.fromAscii(t));
|
||||
}
|
||||
topics = _topics;
|
||||
|
||||
payload = JSON.stringify(data);
|
||||
|
||||
var message;
|
||||
message = {
|
||||
from: identity,
|
||||
topics: topics,
|
||||
payload: EmbarkJS.Utils.fromAscii(payload),
|
||||
ttl: ttl,
|
||||
priority: priority
|
||||
};
|
||||
|
||||
return this.web3.shh.post(message, function() { });
|
||||
};
|
||||
|
||||
__embarkWhisperOld.listenTo = function(options) {
|
||||
var topics, _topics, messageEvents;
|
||||
topics = options.topic || options.topics;
|
||||
_topics = [];
|
||||
|
||||
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 (typeof topics === 'string') {
|
||||
_topics = [topics];
|
||||
} else {
|
||||
_topics = topics.map((t) => EmbarkJS.Utils.fromAscii(t));
|
||||
}
|
||||
topics = _topics;
|
||||
|
||||
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;
|
||||
};
|
||||
|
||||
__embarkWhisperOld.getWhisperVersion = function(cb) {
|
||||
this.web3.version.getWhisper(function(err, _res) {
|
||||
cb(err, self.web3.version.whisper);
|
||||
});
|
||||
};
|
||||
|
||||
// for the whisper v5 and web3.js 1.0
|
||||
let __embarkWhisperNewWeb3 = {};
|
||||
|
||||
|
@ -237,9 +114,3 @@ __embarkWhisperNewWeb3.getWhisperVersion = function(cb) {
|
|||
});
|
||||
};
|
||||
|
||||
if (EmbarkJS.isNewWeb3()) {
|
||||
EmbarkJS.Messages.registerProvider('whisper', __embarkWhisperNewWeb3);
|
||||
} else {
|
||||
EmbarkJS.Messages.registerProvider('whisper', __embarkWhisperOld);
|
||||
}
|
||||
|
||||
|
|
|
@ -0,0 +1,125 @@
|
|||
/*global EmbarkJS, Web3 */
|
||||
|
||||
// for the old version of whisper and web3.js
|
||||
let __embarkWhisperOld = {};
|
||||
|
||||
__embarkWhisperOld.setProvider = function(options) {
|
||||
const self = this;
|
||||
let provider;
|
||||
if (options === undefined) {
|
||||
provider = "localhost:8546";
|
||||
} else {
|
||||
provider = options.server + ':' + options.port;
|
||||
}
|
||||
self.web3 = new Web3(new Web3.providers.HttpProvider("http://" + provider));
|
||||
self.getWhisperVersion(function(err, version) {
|
||||
if (err) {
|
||||
console.log("whisper not available");
|
||||
} else if (version >= 5) {
|
||||
throw new Error("whisper 5 not supported with this version of web3.js");
|
||||
} else {
|
||||
self.identity = self.web3.shh.newIdentity();
|
||||
}
|
||||
self.whisperVersion = self.web3.version.whisper;
|
||||
});
|
||||
};
|
||||
|
||||
__embarkWhisperOld.sendMessage = function(options) {
|
||||
var topics, data, ttl, priority, payload;
|
||||
topics = options.topic || options.topics;
|
||||
data = options.data || options.payload;
|
||||
ttl = options.ttl || 100;
|
||||
priority = options.priority || 1000;
|
||||
var identity = options.identity || this.identity || this.web3.shh.newIdentity();
|
||||
var _topics;
|
||||
|
||||
if (topics === undefined) {
|
||||
throw new Error("missing option: topic");
|
||||
}
|
||||
|
||||
if (data === undefined) {
|
||||
throw new Error("missing option: data");
|
||||
}
|
||||
|
||||
if (typeof topics === 'string') {
|
||||
_topics = [EmbarkJS.Utils.fromAscii(topics)];
|
||||
} else {
|
||||
_topics = topics.map((t) => EmbarkJS.Utils.fromAscii(t));
|
||||
}
|
||||
topics = _topics;
|
||||
|
||||
payload = JSON.stringify(data);
|
||||
|
||||
var message;
|
||||
message = {
|
||||
from: identity,
|
||||
topics: topics,
|
||||
payload: EmbarkJS.Utils.fromAscii(payload),
|
||||
ttl: ttl,
|
||||
priority: priority
|
||||
};
|
||||
|
||||
return this.web3.shh.post(message, function() { });
|
||||
};
|
||||
|
||||
__embarkWhisperOld.listenTo = function(options) {
|
||||
var topics, _topics, messageEvents;
|
||||
topics = options.topic || options.topics;
|
||||
_topics = [];
|
||||
|
||||
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 (typeof topics === 'string') {
|
||||
_topics = [topics];
|
||||
} else {
|
||||
_topics = topics.map((t) => EmbarkJS.Utils.fromAscii(t));
|
||||
}
|
||||
topics = _topics;
|
||||
|
||||
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;
|
||||
};
|
||||
|
||||
__embarkWhisperOld.getWhisperVersion = function(cb) {
|
||||
this.web3.version.getWhisper(function(err, _res) {
|
||||
cb(err, self.web3.version.whisper);
|
||||
});
|
||||
};
|
||||
|
|
@ -30,6 +30,7 @@ class Whisper {
|
|||
}
|
||||
|
||||
addWhisperToEmbarkJS() {
|
||||
const self = this;
|
||||
// TODO: make this a shouldAdd condition
|
||||
if (this.communicationConfig === {}) {
|
||||
return;
|
||||
|
@ -38,12 +39,19 @@ class Whisper {
|
|||
return;
|
||||
}
|
||||
|
||||
// TODO: possible race condition could be a concern
|
||||
this.events.request("version:get:web3", function(web3Version) {
|
||||
let code = "";
|
||||
|
||||
if (web3Version[0] === "0") {
|
||||
code += "\n" + fs.readFileSync(utils.joinPath(__dirname, 'embarkjs_old_web3.js')).toString();
|
||||
code += "\nEmbarkJS.Messages.registerProvider('whisper', __embarkWhisperOld);";
|
||||
} else {
|
||||
code += "\n" + fs.readFileSync(utils.joinPath(__dirname, 'embarkjs.js')).toString();
|
||||
//code += "\nEmbarkJS.Storage.registerProvider('whisper', __embarkWhisper);";
|
||||
|
||||
this.embark.addCodeToEmbarkJS(code);
|
||||
code += "\nEmbarkJS.Messages.registerProvider('whisper', __embarkWhisperNewWeb3);";
|
||||
}
|
||||
self.embark.addCodeToEmbarkJS(code);
|
||||
});
|
||||
}
|
||||
|
||||
addSetProvider() {
|
||||
|
|
Loading…
Reference in New Issue