Merge branch 'orbit' into develop
This commit is contained in:
commit
04bb61bb12
22
README.md
22
README.md
|
@ -28,7 +28,7 @@ Table of Contents
|
||||||
* [Using and Configuring Contracts](#dapp-structure)
|
* [Using and Configuring Contracts](#dapp-structure)
|
||||||
* [EmbarkJS](#embarkjs)
|
* [EmbarkJS](#embarkjs)
|
||||||
* [EmbarkJS - Storage (IPFS)](#embarkjs---storage)
|
* [EmbarkJS - Storage (IPFS)](#embarkjs---storage)
|
||||||
* [EmbarkJS - Communication (Whisper)](#embarkjs---communication)
|
* [EmbarkJS - Communication (Whisper/Orbit)](#embarkjs---communication)
|
||||||
* [Testing Contracts](#tests)
|
* [Testing Contracts](#tests)
|
||||||
* [Working with different chains](#working-with-different-chains)
|
* [Working with different chains](#working-with-different-chains)
|
||||||
* [Custom Application Structure](#structuring-application)
|
* [Custom Application Structure](#structuring-application)
|
||||||
|
@ -351,12 +351,22 @@ EmbarkJS - Communication
|
||||||
|
|
||||||
**initialization**
|
**initialization**
|
||||||
|
|
||||||
The current available communication is Whisper.
|
For Whisper:
|
||||||
|
|
||||||
|
```EmbarkJS.Messages.setProvider('whisper')```
|
||||||
|
|
||||||
|
For Orbit:
|
||||||
|
|
||||||
|
You'll need to use IPFS from master and run it as: ```ipfs daemon --enable-pubsub-experiment```.
|
||||||
|
|
||||||
|
then set the provider:
|
||||||
|
|
||||||
|
```EmbarkJS.Messages.setProvider('orbit', {server: 'localhost', port: 5001})```
|
||||||
|
|
||||||
**listening to messages**
|
**listening to messages**
|
||||||
|
|
||||||
```Javascript
|
```Javascript
|
||||||
EmbarkJS.Messages.listenTo({topic: ["achannel", "anotherchannel"]}).then(function(message) { console.log("received: " + message); })
|
EmbarkJS.Messages.listenTo({topic: ["topic1", "topic2"]}).then(function(message) { console.log("received: " + message); })
|
||||||
```
|
```
|
||||||
|
|
||||||
**sending messages**
|
**sending messages**
|
||||||
|
@ -364,15 +374,17 @@ The current available communication is Whisper.
|
||||||
you can send plain text
|
you can send plain text
|
||||||
|
|
||||||
```Javascript
|
```Javascript
|
||||||
EmbarkJS.Messages.sendMessage({topic: "achannel", data: 'hello world'})
|
EmbarkJS.Messages.sendMessage({topic: "sometopic", data: 'hello world'})
|
||||||
```
|
```
|
||||||
|
|
||||||
or an object
|
or an object
|
||||||
|
|
||||||
```Javascript
|
```Javascript
|
||||||
EmbarkJS.Messages.sendMessage({topic: "achannel", data: {msg: 'hello world'}})
|
EmbarkJS.Messages.sendMessage({topic: "sometopic", data: {msg: 'hello world'}})
|
||||||
```
|
```
|
||||||
|
|
||||||
|
note: array of topics are considered an AND. In Whisper you can use another array for OR combinations of several topics e.g ```["topic1", ["topic2", "topic3"]]``` => ```topic1 AND (topic2 OR topic 3)```
|
||||||
|
|
||||||
Tests
|
Tests
|
||||||
======
|
======
|
||||||
|
|
||||||
|
|
|
@ -246,20 +246,31 @@ var EmbarkJS =
|
||||||
EmbarkJS.Messages = {
|
EmbarkJS.Messages = {
|
||||||
};
|
};
|
||||||
|
|
||||||
EmbarkJS.Messages.setProvider = function(provider) {
|
EmbarkJS.Messages.setProvider = function(provider, options) {
|
||||||
|
var ipfs;
|
||||||
if (provider === 'whisper') {
|
if (provider === 'whisper') {
|
||||||
this.currentMessages = EmbarkJS.Messages.Whisper;
|
this.currentMessages = EmbarkJS.Messages.Whisper;
|
||||||
|
this.currentMessages.identity = web3.shh.newIdentity();
|
||||||
|
} 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]);
|
||||||
} else {
|
} else {
|
||||||
throw Error('unknown provider');
|
throw Error('unknown provider');
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
EmbarkJS.Messages.sendMessage = function(options) {
|
EmbarkJS.Messages.sendMessage = function(options) {
|
||||||
return EmbarkJS.Messages.Whisper.sendMessage(options);
|
return this.currentMessages.sendMessage(options);
|
||||||
};
|
};
|
||||||
|
|
||||||
EmbarkJS.Messages.listenTo = function(options) {
|
EmbarkJS.Messages.listenTo = function(options) {
|
||||||
return EmbarkJS.Messages.Whisper.listenTo(options);
|
return this.currentMessages.listenTo(options);
|
||||||
};
|
};
|
||||||
|
|
||||||
EmbarkJS.Messages.Whisper = {
|
EmbarkJS.Messages.Whisper = {
|
||||||
|
@ -268,7 +279,7 @@ var EmbarkJS =
|
||||||
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 || web3.shh.newIdentity();
|
var 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;
|
||||||
|
|
||||||
|
@ -282,21 +293,21 @@ var EmbarkJS =
|
||||||
|
|
||||||
// do fromAscii to each topics unless it's already a string
|
// do fromAscii to each topics unless it's already a string
|
||||||
if (typeof topics === 'string') {
|
if (typeof topics === 'string') {
|
||||||
topics = topics;
|
_topics = [web3.fromAscii(topics)];
|
||||||
} else {
|
} else {
|
||||||
// TODO: replace with es6 + babel;
|
// TODO: replace with es6 + babel;
|
||||||
var _topics = [];
|
var _topics = [];
|
||||||
for (var i = 0; i < topics.length; i++) {
|
for (var i = 0; i < topics.length; i++) {
|
||||||
_topics.push(web3.fromAscii(topics[i]));
|
_topics.push(web3.fromAscii(topics[i]));
|
||||||
}
|
}
|
||||||
topics = _topics;
|
|
||||||
}
|
}
|
||||||
|
topics = _topics;
|
||||||
|
|
||||||
var payload = JSON.stringify(data);
|
var payload = JSON.stringify(data);
|
||||||
|
|
||||||
var message = {
|
var message = {
|
||||||
from: identity,
|
from: identity,
|
||||||
topics: [web3.fromAscii(topics)],
|
topics: topics,
|
||||||
payload: web3.fromAscii(payload),
|
payload: web3.fromAscii(payload),
|
||||||
ttl: ttl,
|
ttl: ttl,
|
||||||
priority: priority
|
priority: priority
|
||||||
|
@ -309,15 +320,15 @@ var EmbarkJS =
|
||||||
var topics = options.topic || options.topics;
|
var topics = options.topic || options.topics;
|
||||||
|
|
||||||
if (typeof topics === 'string') {
|
if (typeof topics === 'string') {
|
||||||
topics = [topics];
|
_topics = [topics];
|
||||||
} else {
|
} else {
|
||||||
// TODO: replace with es6 + babel;
|
// TODO: replace with es6 + babel;
|
||||||
var _topics = [];
|
var _topics = [];
|
||||||
for (var i = 0; i < topics.length; i++) {
|
for (var i = 0; i < topics.length; i++) {
|
||||||
_topics.push(web3.fromAscii(topics[i]));
|
_topics.push(topics[i]);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
topics = _topics;
|
topics = _topics;
|
||||||
}
|
|
||||||
|
|
||||||
var filterOptions = {
|
var filterOptions = {
|
||||||
topics: topics
|
topics: topics
|
||||||
|
@ -339,16 +350,95 @@ var EmbarkJS =
|
||||||
|
|
||||||
var filter = web3.shh.filter(filterOptions, function(err, result) {
|
var filter = web3.shh.filter(filterOptions, function(err, result) {
|
||||||
var payload = JSON.parse(web3.toAscii(result.payload));
|
var payload = JSON.parse(web3.toAscii(result.payload));
|
||||||
|
var data;
|
||||||
if (err) {
|
if (err) {
|
||||||
promise.error(err);
|
promise.error(err);
|
||||||
} else {
|
} else {
|
||||||
promise.cb(payload);
|
data = {
|
||||||
|
topic: topics,
|
||||||
|
data: payload,
|
||||||
|
from: result.from,
|
||||||
|
time: (new Date(result.sent * 1000))
|
||||||
|
};
|
||||||
|
promise.cb(payload, data, result);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
return promise;
|
return promise;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
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(',');
|
||||||
|
}
|
||||||
|
|
||||||
|
this.orbit.join(topics);
|
||||||
|
|
||||||
|
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();
|
||||||
|
|
||||||
|
this.orbit.events.on('message', (channel, message) => {
|
||||||
|
// TODO: looks like sometimes it's receving messages from all topics
|
||||||
|
if (topics !== channel) return;
|
||||||
|
self.orbit.getPost(message.payload.value, true).then((post) => {
|
||||||
|
var data = {
|
||||||
|
topic: channel,
|
||||||
|
data: post.content,
|
||||||
|
from: post.meta.from.name,
|
||||||
|
time: (new Date(post.meta.ts))
|
||||||
|
};
|
||||||
|
promise.cb(post.content, data, post);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
return promise;
|
||||||
|
};
|
||||||
|
|
||||||
module.exports = EmbarkJS;
|
module.exports = EmbarkJS;
|
||||||
|
|
||||||
|
|
||||||
|
|
112
js/embark.js
112
js/embark.js
|
@ -199,20 +199,31 @@ EmbarkJS.Storage.getUrl = function(hash) {
|
||||||
EmbarkJS.Messages = {
|
EmbarkJS.Messages = {
|
||||||
};
|
};
|
||||||
|
|
||||||
EmbarkJS.Messages.setProvider = function(provider) {
|
EmbarkJS.Messages.setProvider = function(provider, options) {
|
||||||
|
var ipfs;
|
||||||
if (provider === 'whisper') {
|
if (provider === 'whisper') {
|
||||||
this.currentMessages = EmbarkJS.Messages.Whisper;
|
this.currentMessages = EmbarkJS.Messages.Whisper;
|
||||||
|
this.currentMessages.identity = web3.shh.newIdentity();
|
||||||
|
} 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]);
|
||||||
} else {
|
} else {
|
||||||
throw Error('unknown provider');
|
throw Error('unknown provider');
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
EmbarkJS.Messages.sendMessage = function(options) {
|
EmbarkJS.Messages.sendMessage = function(options) {
|
||||||
return EmbarkJS.Messages.Whisper.sendMessage(options);
|
return this.currentMessages.sendMessage(options);
|
||||||
};
|
};
|
||||||
|
|
||||||
EmbarkJS.Messages.listenTo = function(options) {
|
EmbarkJS.Messages.listenTo = function(options) {
|
||||||
return EmbarkJS.Messages.Whisper.listenTo(options);
|
return this.currentMessages.listenTo(options);
|
||||||
};
|
};
|
||||||
|
|
||||||
EmbarkJS.Messages.Whisper = {
|
EmbarkJS.Messages.Whisper = {
|
||||||
|
@ -221,7 +232,7 @@ 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 || web3.shh.newIdentity();
|
var 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;
|
||||||
|
|
||||||
|
@ -235,21 +246,21 @@ EmbarkJS.Messages.Whisper.sendMessage = function(options) {
|
||||||
|
|
||||||
// do fromAscii to each topics unless it's already a string
|
// do fromAscii to each topics unless it's already a string
|
||||||
if (typeof topics === 'string') {
|
if (typeof topics === 'string') {
|
||||||
topics = topics;
|
_topics = [web3.fromAscii(topics)];
|
||||||
} else {
|
} else {
|
||||||
// TODO: replace with es6 + babel;
|
// TODO: replace with es6 + babel;
|
||||||
var _topics = [];
|
var _topics = [];
|
||||||
for (var i = 0; i < topics.length; i++) {
|
for (var i = 0; i < topics.length; i++) {
|
||||||
_topics.push(web3.fromAscii(topics[i]));
|
_topics.push(web3.fromAscii(topics[i]));
|
||||||
}
|
}
|
||||||
topics = _topics;
|
|
||||||
}
|
}
|
||||||
|
topics = _topics;
|
||||||
|
|
||||||
var payload = JSON.stringify(data);
|
var payload = JSON.stringify(data);
|
||||||
|
|
||||||
var message = {
|
var message = {
|
||||||
from: identity,
|
from: identity,
|
||||||
topics: [web3.fromAscii(topics)],
|
topics: topics,
|
||||||
payload: web3.fromAscii(payload),
|
payload: web3.fromAscii(payload),
|
||||||
ttl: ttl,
|
ttl: ttl,
|
||||||
priority: priority
|
priority: priority
|
||||||
|
@ -262,15 +273,15 @@ EmbarkJS.Messages.Whisper.listenTo = function(options) {
|
||||||
var topics = options.topic || options.topics;
|
var topics = options.topic || options.topics;
|
||||||
|
|
||||||
if (typeof topics === 'string') {
|
if (typeof topics === 'string') {
|
||||||
topics = [topics];
|
_topics = [topics];
|
||||||
} else {
|
} else {
|
||||||
// TODO: replace with es6 + babel;
|
// TODO: replace with es6 + babel;
|
||||||
var _topics = [];
|
var _topics = [];
|
||||||
for (var i = 0; i < topics.length; i++) {
|
for (var i = 0; i < topics.length; i++) {
|
||||||
_topics.push(web3.fromAscii(topics[i]));
|
_topics.push(topics[i]);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
topics = _topics;
|
topics = _topics;
|
||||||
}
|
|
||||||
|
|
||||||
var filterOptions = {
|
var filterOptions = {
|
||||||
topics: topics
|
topics: topics
|
||||||
|
@ -292,14 +303,93 @@ EmbarkJS.Messages.Whisper.listenTo = function(options) {
|
||||||
|
|
||||||
var filter = web3.shh.filter(filterOptions, function(err, result) {
|
var filter = web3.shh.filter(filterOptions, function(err, result) {
|
||||||
var payload = JSON.parse(web3.toAscii(result.payload));
|
var payload = JSON.parse(web3.toAscii(result.payload));
|
||||||
|
var data;
|
||||||
if (err) {
|
if (err) {
|
||||||
promise.error(err);
|
promise.error(err);
|
||||||
} else {
|
} else {
|
||||||
promise.cb(payload);
|
data = {
|
||||||
|
topic: topics,
|
||||||
|
data: payload,
|
||||||
|
from: result.from,
|
||||||
|
time: (new Date(result.sent * 1000))
|
||||||
|
};
|
||||||
|
promise.cb(payload, data, result);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
return promise;
|
return promise;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
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(',');
|
||||||
|
}
|
||||||
|
|
||||||
|
this.orbit.join(topics);
|
||||||
|
|
||||||
|
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();
|
||||||
|
|
||||||
|
this.orbit.events.on('message', (channel, message) => {
|
||||||
|
// TODO: looks like sometimes it's receving messages from all topics
|
||||||
|
if (topics !== channel) return;
|
||||||
|
self.orbit.getPost(message.payload.value, true).then((post) => {
|
||||||
|
var data = {
|
||||||
|
topic: channel,
|
||||||
|
data: post.content,
|
||||||
|
from: post.meta.from.name,
|
||||||
|
time: (new Date(post.meta.ts))
|
||||||
|
};
|
||||||
|
promise.cb(post.content, data, post);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
return promise;
|
||||||
|
};
|
||||||
|
|
||||||
module.exports = EmbarkJS;
|
module.exports = EmbarkJS;
|
||||||
|
|
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
|
@ -84,6 +84,9 @@ Config.prototype.loadFiles = function(files) {
|
||||||
if (file === 'embark.js') {
|
if (file === 'embark.js') {
|
||||||
readFiles.push({filename: 'web3.js', content: fs.readFileSync(path.join(__dirname, "/../js/web3.js")).toString()});
|
readFiles.push({filename: 'web3.js', content: fs.readFileSync(path.join(__dirname, "/../js/web3.js")).toString()});
|
||||||
readFiles.push({filename: 'ipfs.js', content: fs.readFileSync(path.join(__dirname, "/../js/ipfs.js")).toString()});
|
readFiles.push({filename: 'ipfs.js', content: fs.readFileSync(path.join(__dirname, "/../js/ipfs.js")).toString()});
|
||||||
|
// TODO: remove duplicated files if funcitonality is the same for storage and orbit
|
||||||
|
readFiles.push({filename: 'ipfs-api.js', content: fs.readFileSync(path.join(__dirname, "/../js/ipfs-api.min.js")).toString()});
|
||||||
|
readFiles.push({filename: 'orbit.js', content: fs.readFileSync(path.join(__dirname, "/../js/orbit.min.js")).toString()});
|
||||||
readFiles.push({filename: 'embark.js', content: fs.readFileSync(path.join(__dirname, "/../js/build/embark.bundle.js")).toString()});
|
readFiles.push({filename: 'embark.js', content: fs.readFileSync(path.join(__dirname, "/../js/build/embark.bundle.js")).toString()});
|
||||||
} else {
|
} else {
|
||||||
readFiles.push({filename: file, content: fs.readFileSync(file).toString()});
|
readFiles.push({filename: file, content: fs.readFileSync(file).toString()});
|
||||||
|
|
Loading…
Reference in New Issue