Skip to content

Commit

Permalink
cross computer SEA changes
Browse files Browse the repository at this point in the history
  • Loading branch information
Mark Nadal authored and Mark Nadal committed Apr 3, 2018
1 parent 2ba3780 commit 0591539
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 84 deletions.
15 changes: 11 additions & 4 deletions as.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,13 +97,15 @@
}, 99));
var u;
window.as = as;
$.as = as;
}());

;(function(){
$('.page').not(':first').hide();
$(document).on('click', 'a, button', function(e){
var tmp = $(this).attr('href') || '';
if(0 === tmp.indexOf('http')){ return }
e.preventDefault();
r($(this).attr('href'));
r(tmp);
});
function r(href){
if(!href){ return }
Expand All @@ -130,11 +132,16 @@
});
return $data;
}
setTimeout(function(){ r(location.hash.slice(1)) },1);
window.onhashchange = function(){ r(location.hash.slice(1)) };
$.as && ($.as.route = r);
if(window.as){
as.route = r;
} else {
$.route = r;
}
}());
}());

;$(function(){
$('.page').not(':first').hide();
$.as.route(location.hash.slice(1));
});
33 changes: 22 additions & 11 deletions examples/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ button, input, textarea {
color: black;
}

a {
color: white;
}

input, textarea {
width: 100%;
}
Expand Down Expand Up @@ -66,6 +70,9 @@ ul, li {
margin-left: auto;
margin-right: auto;
}
.flush {
line-height: 0em;
}

.sit { margin-bottom: 0; }
.row { width: 100%; }
Expand All @@ -88,6 +95,10 @@ ul, li {
.gap {
padding: 3%;
}
.ditch {
margin-top: 2em;
margin-bottom: 2em;
}

.loud {
font-size: 150%;
Expand Down Expand Up @@ -145,17 +156,6 @@ ul, li {
color: white;
}

.pulse {
animation: pulse 2s infinite;
}

@keyframes pulse
{
0% {opacity: 1;}
50% {opacity: 0.5;}
100% {opacity: 1;}
}

.hue {
background: #4D79D8;
-webkit-animation: hue 900s infinite;
Expand Down Expand Up @@ -314,4 +314,15 @@ ul, li {
50% {color: #4D79D8;}
75% {color: #33cc33;}
100% {color: #f2b919;}
}

.pulse {
animation: pulse 2s infinite;
}

@keyframes pulse
{
0% {opacity: 1;}
50% {opacity: 0.5;}
100% {opacity: 1;}
}
1 change: 1 addition & 0 deletions gun.js
Original file line number Diff line number Diff line change
Expand Up @@ -1374,6 +1374,7 @@
var tmp = cat.root.now; obj.del(cat.root, 'now'); cat.root.PUT = true;
var tmp2 = cat.root.stop;
(as.ref._).now = true;
console.log("PUT!", as.env.graph);
(as.ref._).on('out', {
gun: as.ref, put: as.out = as.env.graph, opt: as.opt, '#': ask
});
Expand Down
98 changes: 30 additions & 68 deletions sea.js
Original file line number Diff line number Diff line change
Expand Up @@ -1290,12 +1290,11 @@
at.sea = {own: {}};
var uuid = at.opt.uuid || Gun.state.lex;
at.opt.uuid = function(cb){ // TODO: consider async/await and drop callback pattern...
if(!cb){ return }
var id = uuid(), pair = at.user && (at.user._).sea;
if(!pair){ return id }
SEA.sign(id, pair).then(function(sig){
cb(null, id + '~' + sig);
}).catch(function(e){cb(e)});
var id = uuid(), pub = at.user;
if(!pub || !(pub = at.user._.sea) || !(pub = pub.pub)){ return id }
id = id + '~' + pub;
if(cb){ cb(null, id) }
return id;
}
at.on('in', security, at); // now listen to all input data, acting as a firewall.
at.on('out', signature, at); // and output listeners, to encrypt outgoing data.
Expand Down Expand Up @@ -1331,19 +1330,6 @@
d = true;
if(d && !c){ to.next(msg) }
return;
/*var to = this.to, ctx = this.as;
var own = ctx.sea.own, soul = msg.get, c = 0;
var pub = own[soul] || soul.slice(4), vertex = (msg.gun._).put;
Gun.node.is(msg.put, function(val, key, node){ c++; // for each property on the node.
SEA.read(val, pub).then(function(data){ c--;
vertex[key] = node[key] = val = data; // verify signature and get plain value.
if(val && val['#'] && (key = Gun.val.rel.is(val))){ // if it is a relation / edge
if('alias/' !== soul.slice(0,6)){ own[key] = pub; } // associate the public key with a node if it is itself
}
if(!c && (c = -1)){ to.next(msg) }
});
});
if(!c){ to.next(msg) }*/
}

// signature handles data output, it is a proxy to the security function.
Expand Down Expand Up @@ -1377,7 +1363,7 @@
}
if(msg.put){
// potentially parallel async operations!!!
var check = {}, on = Gun.on(), each = {}, u;
var check = {}, each = {}, u;
each.node = function(node, soul){
if(Gun.obj.empty(node, '_')){ return check['node'+soul] = 0 } // ignore empty updates, don't reject them.
Gun.obj.map(node, each.way, {soul: soul, node: node});
Expand All @@ -1396,15 +1382,6 @@
}
each.any(val, key, node, soul, msg.user); return;
return each.end({err: "No other data allowed!"});
/*if(!(tmp = at.user)){ return }
if(soul.slice(4) === (tmp = tmp._).pub){ // not a special case, if we are logged in and have outbound data on us.
each.user(val, key, node, soul, {
pub: tmp.pub, priv: tmp.sea.priv, epub: tmp.sea.epub, epriv: tmp.sea.epriv
});
}
if((tmp = sea.own[soul])){ // not special case, if we receive an update on an ID associated with a public key, then
each.own(val, key, node, soul, tmp);
}*/
};
each.alias = function(val, key, node, soul){ // Example: {_:#alias, alias/alice: {#alias/alice}}
if(!val){ return each.end({err: "Data must exist!"}) } // data MUST exist
Expand All @@ -1423,15 +1400,16 @@
}
check['user'+soul+key] = 1;
if(user && (user = user._) && user.sea && pub === user.pub){
var id = Gun.text.random(3);
SEA.write(val, Gun.obj.to(user.sea, {pub: user.pub, epub: user.epub})).then(function(data){ var rel;
//var id = Gun.text.random(3);
SEA.write(val, user.sea).then(function(data){ var rel;
if(rel = Gun.val.rel.is(val)){
(at.sea.own[rel] = at.sea.own[rel] || {})[pub] = true;
}
node[key] = data;
check['user'+soul+key] = 0;
each.end({ok: 1});
});
// TODO: Handle error!!!!
return;
}
// TODO: consider async/await and drop callback pattern...
Expand All @@ -1440,47 +1418,31 @@
return each.end({err: "Unverified data."}); // reject any updates that are signed with a mismatched account.
}
if((rel = Gun.val.rel.is(data)) && (tmp = rel.split('~')) && 2 === tmp.length){
SEA.verify(tmp[0], pub, tmp[1]).then(function(ok){
if(!ok){ return each.end({err: "Signature did not match account."}) }
if(pub === tmp[1]){
(at.sea.own[rel] = at.sea.own[rel] || {})[pub] = true;
check['user'+soul+key] = 0;
each.end({ok: 1});
});
return;
}
}
check['user'+soul+key] = 0;
each.end({ok: 1});
});
};
each.any = function(val, key, node, soul, user){ var tmp;
each.any = function(val, key, node, soul, user){ var tmp, pub;
if(!user || !(user = user._) || !(user = user.sea)){
if(user = at.sea.own[soul]){
if((tmp = soul.split('~')) && 2 == tmp.length){
check['any'+soul+key] = 1;
user = Gun.obj.map(user, function(a,b){ return b });
// TODO: consider async/await and drop callback pattern...
SEA.read(val, user).then(function(data){ var rel;
if(!data){ return each.end({err: "Mismatched owner on '" + key + "'.", }) }
SEA.read(val, (pub = tmp[1])).then(function(data){ var rel;
if(!data){ return each.end({err: "Mismatched owner on '" + key + "'."}) }
if((rel = Gun.val.rel.is(data)) && (tmp = rel.split('~')) && 2 === tmp.length){
SEA.verify(tmp[0], user, tmp[1]).then(function(ok){
if(!ok){ return each.end({err: "Signature did not match account."}) }
(at.sea.own[rel] = at.sea.own[rel] || {})[user] = true;
check['any'+soul+key] = 0;
each.end({ok: 1});
});
return;
if(pub === tmp[1]){
(at.sea.own[rel] = at.sea.own[rel] || {})[pub] = true;
}
}
check['any'+soul+key] = 0;
each.end({ok: 1});
});
return;
}
check['any'+soul+key] = 1;
if((tmp = soul.split('~')) && 2 == tmp.length){
setTimeout(function(){ // hacky idea, what would be better?
each.any(val, key, node, soul);
},1);
return;
}
at.on('secure', function(msg){ this.off();
check['any'+soul+key] = 0;
each.end(msg || {err: "Data cannot be modified."});
Expand All @@ -1489,26 +1451,26 @@
return;
}
if(!(tmp = soul.split('~')) || 2 !== tmp.length){
each.end({err: "Soul is not signed at '" + key + "'."});
each.end({err: "Soul is missing public key at '" + key + "'."});
return;
}
var pub = tmp[1];
if(pub !== user.pub){
each.any(val, key, node, soul);
return;
}
var other = Gun.obj.map(at.sea.own[soul], function(v, p){
/*var other = Gun.obj.map(at.sea.own[soul], function(v, p){
if(user.pub !== p){ return p }
});
if(other){
each.any(val, key, node, soul);
return;
}
}*/
check['any'+soul+key] = 1;
// TODO: consider async/await and drop callback pattern...
SEA.verify(tmp[0], user.pub, tmp[1]).then(function(ok){
if(!ok){ return each.end({err: "Signature did not match account at '" + key + "'."}) }
(at.sea.own[soul] = at.sea.own[soul] || {})[user.pub] = true;
SEA.write(val, user).then(function(data){
node[key] = data;
check['any'+soul+key] = 0;
each.end({ok: 1});
});
SEA.write(val, user).then(function(data){
node[key] = data;
check['any'+soul+key] = 0;
each.end({ok: 1});
});
}
each.end = function(ctx){ // TODO: Can't you just switch this to each.end = cb?
Expand Down
2 changes: 1 addition & 1 deletion test/user.html
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ <h1>User</h1>

$('#said').on('submit', function(e){
e.preventDefault();
if(!user.is){ return }
if(!user.is){ return }
user.get('said').set($('#say').val());
$('#say').val("");
});
Expand Down

0 comments on commit 0591539

Please sign in to comment.