Skip to content

Commit

Permalink
hello-world
Browse files Browse the repository at this point in the history
  • Loading branch information
alex2006hw committed Dec 11, 2018
1 parent 5686823 commit f758fbb
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 22 deletions.
56 changes: 35 additions & 21 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,14 @@ var Gun = require('gun/gun');
require('gun/nts');
require('gun/lib/path.js');

var PEERS = ['https://troposphere.usertoken.com/gun', 'https://alex.us-east.mybluemix.net/gun', 'https://haley.mybluemix.net/gun'];
var SEEDS = ['https://troposphere.usertoken.com/gun', 'https://alex.us-east.mybluemix.net/gun', 'https://haley.mybluemix.net/gun'];
var ID = UUIDv4().replace(/\//g, '').replace(/\-/g, '').replace(/\ /g, '').toLowerCase();

var ROOT = "/";
var BROADCAST = "BROADCAST";

var GENESIS = "USERTOKEN";
var GENESIS_ATTRIBUTES = "USERTOKEN/ATTRIBUTES";
var GENESIS_CONTRACTS = "USERTOKEN/CONTRACTS";
var GENESIS_CHANNELS = "USERTOKEN/CHANNELS";

var ROOT_ATTRIBUTES = "ROOT/ATTRIBUTES";
var ROOT_CONTRACTS = "ROOT/CONTRACTS";
var ROOT_CHANNELS = "ROOT/CHANNELS";
var GENESIS = 'GENESIS';

/**
* Create a new token or connect to existing by providing OPTIONS = { id: id, peers: peer || [peers] }
Expand All @@ -24,12 +22,12 @@ var GENESIS_CHANNELS = "USERTOKEN/CHANNELS";
*/

module.exports = function(CB, OPTIONS) {

var id = (OPTIONS && OPTIONS.id)? OPTIONS.id : UUIDv4().replace(/\//g, '').replace(/\-/g, '').replace(/\ /g, '').toLowerCase();
var BROADCAST_REPLY = `USERTOKEN/CHANNELS/${id}`;
var id = (OPTIONS && OPTIONS.id)? OPTIONS.id : ID;
var BROADCAST = "BROADCAST";
var BROADCAST_REPLY = `ROOT/CHANNELS/${id}`;

var options = {
peers: (OPTIONS && OPTIONS.peers)? OPTIONS.peers : PEERS,
peers: (OPTIONS && OPTIONS.peers)? OPTIONS.peers : SEEDS,
file: false
};

Expand All @@ -44,50 +42,66 @@ module.exports = function(CB, OPTIONS) {
var token = tokenEngine.get(id);

var tokenGenesis = tokenEngine.get(GENESIS);
var attributesGenesis = tokenEngine.get(GENESIS_ATTRIBUTES);
var contractsGenesis = tokenEngine.get(GENESIS_CONTRACTS);
var channelsGenesis = tokenEngine.get(GENESIS_CHANNELS);
var attributesGenesis = tokenEngine.get(ROOT_ATTRIBUTES);
var contractsGenesis = tokenEngine.get(ROOT_CONTRACTS);
var channelsGenesis = tokenEngine.get(ROOT_CHANNELS);

// starts new roots
root.path(ROOT).set(tokenGenesis);
root.path('ROOT').set(tokenGenesis);
root.path(GENESIS).set(tokenGenesis);
root.path('TOKEN').set(token);

root.path('ATTRIBUTES').set(attributesGenesis);
root.path('CONTRACTS').set(contractsGenesis);
root.path('ATTRIBUTES').set(attributesGenesis);
root.path('CHANNELS').set(channelsGenesis);
root.path('TOKEN').set(token);

// link to root
tokenGenesis.path(ROOT).set(root);
tokenGenesis.path('ROOT').set(root);
tokenGenesis.path(GENESIS).set(tokenGenesis);
tokenGenesis.path('TOKEN').set(token);

tokenGenesis.path('ATTRIBUTES').set(attributesGenesis);
tokenGenesis.path('CONTRACTS').set(contractsGenesis);
tokenGenesis.path('ATTRIBUTES').set(attributesGenesis);
tokenGenesis.path('CHANNELS').set(channelsGenesis);
tokenGenesis.path('TOKEN').set(token);

attributesGenesis.path(ROOT).set(root);
attributesGenesis.path('ROOT').set(root);
attributesGenesis.path(GENESIS).set(tokenGenesis);
attributesGenesis.path('TOKEN').set(token);

contractsGenesis.path(ROOT).set(root);
contractsGenesis.path('ROOT').set(root);
contractsGenesis.path(GENESIS).set(tokenGenesis);
contractsGenesis.path('TOKEN').set(token);

channelsGenesis.path(ROOT).set(root);
channelsGenesis.path('ROOT').set(root);
channelsGenesis.path(GENESIS).set(tokenGenesis);
channelsGenesis.path('TOKEN').set(token);

token.get('ROOT').put(ROOT);
token.get('id').put(id);

// set paths
token.path(ROOT).set(root);
token.path('ROOT').set(root)
token.path('GENESIS').set(tokenGenesis);
token.path(GENESIS).set(tokenGenesis);

token.path('ATTRIBUTES').set(attributesGenesis);
token.path('CONTRACTS').set(contractsGenesis);
token.path('CHANNELS').set(channelsGenesis);

token.get('BROADCAST').put(BROADCAST);
token.get('LISTEN').put(BROADCAST_REPLY);
token.get('ROOT').put(ROOT);
token.get('id').put(id);

broadcast.get('PING').put(id)
broadcast.get('PONG').on((peer) => {
if (peer === id) return
token.get('PEERS').put(peer);
});

CB({id: id, root: root, token: token, broadcast: broadcast, listen: listen});
};
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "token-memory",
"version": "0.1.9",
"version": "0.2.0",
"description": "Durable distributed memory token",
"main": "index.js",
"scripts": {
Expand Down

0 comments on commit f758fbb

Please sign in to comment.