This commit is contained in:
Mark Nadal 2018-08-24 03:07:56 -07:00
parent aee20ebbc3
commit 80a9a67d0b
16 changed files with 71 additions and 51 deletions

2
gun.min.js vendored

File diff suppressed because one or more lines are too long

View File

@ -15,7 +15,7 @@
let err
// then attempt to log into each one until we find ours!
// (if two users have the same username AND the same password... that would be bad)
const [ user ] = await Promise.all(aliases.map(async ({ at: at, pub: pub }) => {
const users = await Promise.all(aliases.map(async ({ at: at, pub: pub }, i) => {
// attempt to PBKDF2 extend the password with the salt. (Verifying the signature gives us the plain text salt.)
const auth = parseProps(at.put.auth)
// NOTE: aliasquery uses `gun.get` which internally SEA.read verifies the data for us, so we do not need to re-verify it here.
@ -30,7 +30,7 @@
const salt = auth.salt
const sea = await SEA.decrypt(auth.ek, proof)
if (!sea) {
err = 'Failed to decrypt secret!'
err = 'Failed to decrypt secret! ' + i +'/'+aliases.length;
return
}
// now we have AES decrypted the private key, from when we encrypted it with the proof at registration.
@ -53,7 +53,7 @@
throw { err }
}
}))
var user = Gun.list.map(users, function(acc){ if(acc){ return acc } })
if (!user) {
throw { err: err || 'Public key does not exist!' }
}

View File

@ -14,7 +14,7 @@
var u;
// Well first we have to actually create a user. That is what this function does.
User.prototype.create = function(username, pass, cb){
User.prototype.create = function(username, pass, cb, opt){
// TODO: Needs to be cleaned up!!!
const gunRoot = this.back(-1)
var gun = this, cat = (gun._);
@ -24,12 +24,13 @@
return gun;
}
cat.ing = true;
opt = opt || {};
var resolve = function(){}, reject = resolve;
// Because more than 1 user might have the same username, we treat the alias as a list of those users.
if(cb){ resolve = reject = cb }
gunRoot.get('~@'+username).get(async (at, ev) => {
ev.off()
if (at.put) {
if (at.put && !opt.already) {
// If we can enforce that a user name is already taken, it might be nice to try, but this is not guaranteed.
const err = 'User already created!'
Gun.log(err)
@ -165,6 +166,17 @@
return user._.sea;
}
User.prototype.leave = async function(){
var gun = this, user = (gun.back(-1)._).user;
if(user){
delete user.is;
delete user._.is;
delete user._.sea;
}
if(typeof window !== 'undefined'){
var tmp = window.sessionStorage;
delete tmp.alias;
delete tmp.tmp;
}
return await authLeave(this.back(-1))
}
// If authenticated user wants to delete his/her account, let's support it!

View File

@ -51,7 +51,8 @@
const r = { pub: sa.pub, priv: sa.priv, /* pubId, */ epub: dh.epub, epriv: dh.epriv }
if(cb){ try{ cb(r) }catch(e){console.log(e)} }
return r;
} catch(e) {
} catch(e) {
console.log(e);
SEA.err = e;
if(cb){ cb() }
return;

View File

@ -4,7 +4,7 @@
// THIS IS AN EARLY ALPHA!
function SEA(){}
if(typeof window !== "undefined"){ SEA.window = window }
if(typeof window !== "undefined"){ (SEA.window = window).SEA = SEA }
module.exports = SEA;

View File

@ -1,14 +1,12 @@
const {
subtle, ossl = subtle, random: getRandomBytes, TextEncoder, TextDecoder
} = require('./shim')
const shim = require('./shim');
const Buffer = require('./buffer')
const parse = require('./parse')
const { pbkdf2 } = require('./settings')
// This internal func returns SHA-256 hashed data for signing
const sha256hash = async (mm) => {
const m = parse(mm)
const hash = await ossl.digest({name: pbkdf2.hash}, new TextEncoder().encode(m))
const hash = await shim.subtle.digest({name: pbkdf2.hash}, new shim.TextEncoder().encode(m))
return Buffer.from(hash)
}
module.exports = sha256hash

View File

@ -28,7 +28,7 @@
});
try{
const WebCrypto = require('node-webcrypto-ossl')
api.ossl = new WebCrypto({directory: 'key_storage'}).subtle // ECDH
api.ossl = new WebCrypto({directory: 'ossl'}).subtle // ECDH
}catch(e){
console.log("node-webcrypto-ossl is optionally needed for ECDH, please install if needed.");
}

View File

@ -25,7 +25,8 @@
if(cb){ try{ cb(r) }catch(e){console.log(e)} }
return r;
} catch(e) {
} catch(e) {
console.log(e);
SEA.err = e;
if(cb){ cb() }
return;

View File

@ -3,9 +3,8 @@
var Gun = SEA.Gun;
var then = require('./then');
function User(){
this._ = {$: this}
Gun.call()
function User(root){
this._ = {$: this};
}
User.prototype = (function(){ function F(){}; F.prototype = Gun.chain; return new F() }()) // Object.create polyfill
User.prototype.constructor = User;

View File

@ -26,7 +26,8 @@
if(cb){ try{ cb(r) }catch(e){console.log(e)} }
return r;
} catch(e) {
} catch(e) {
console.log(e);
SEA.err = e;
if(cb){ cb() }
return;

View File

@ -15,24 +15,21 @@ Gun.on('create', function(root){
// See the next 'opt' code below for actual saving of data.
var ev = this.to, opt = root.opt;
if(root.once){ return ev.next(root) }
if(false === opt.localStorage){ return ev.next(root) }
opt.file = opt.file || 'gun/';
var gap = Gun.obj.ify(store.getItem('gap/'+opt.file)) || {};
//if(false === opt.localStorage){ return ev.next(root) } // we want offline resynce queue regardless!
opt.prefix = opt.file || 'gun/';
var gap = Gun.obj.ify(store.getItem('gap/'+opt.prefix)) || {};
var empty = Gun.obj.empty, id, to, go;
// add re-sync command.
if(!empty(gap)){
root.on('localStorage', function(disk){
this.off();
var send = {}
Gun.obj.map(gap, function(node, soul){
Gun.obj.map(node, function(val, key){
send[soul] = Gun.state.to(disk[soul], key, send[soul]);
});
var disk = Gun.obj.ify(store.getItem(opt.prefix)) || {}, send = {};
Gun.obj.map(gap, function(node, soul){
Gun.obj.map(node, function(val, key){
send[soul] = Gun.state.to(disk[soul], key, send[soul]);
});
setTimeout(function(){
root.on('out', {put: send, '#': root.ask(ack), I: root.$});
},10);
});
setTimeout(function(){
root.on('out', {put: send, '#': root.ask(ack), I: root.$});
},10);
}
root.on('out', function(msg){
@ -70,7 +67,7 @@ Gun.on('create', function(root){
var flush = function(){
clearTimeout(to);
to = false;
try{store.setItem('gap/'+opt.file, JSON.stringify(gap));
try{store.setItem('gap/'+opt.prefix, JSON.stringify(gap));
}catch(e){ Gun.log(err = e || "localStorage failure") }
}
});
@ -80,9 +77,9 @@ Gun.on('create', function(root){
var opt = root.opt;
if(root.once){ return }
if(false === opt.localStorage){ return }
opt.file = opt.file || opt.prefix || 'gun/'; // support old option name.
opt.prefix = opt.file || 'gun/';
var graph = root.graph, acks = {}, count = 0, to;
var disk = Gun.obj.ify(store.getItem(opt.file)) || {};
var disk = Gun.obj.ify(store.getItem(opt.prefix)) || {};
var lS = function(){}, u;
root.on('localStorage', disk); // NON-STANDARD EVENT!
@ -130,10 +127,10 @@ Gun.on('create', function(root){
var ack = acks;
acks = {};
if(data){ disk = data }
try{store.setItem(opt.file, JSON.stringify(disk));
try{store.setItem(opt.prefix, JSON.stringify(disk));
}catch(e){
Gun.log(err = e || "localStorage failure");
root.on('localStorage:error', {err: err, file: opt.file, flush: disk, retry: flush});
root.on('localStorage:error', {err: err, file: opt.prefix, flush: disk, retry: flush});
}
if(!err && !Gun.obj.empty(opt.peers)){ return } // only ack if there are no peers.
Gun.obj.map(ack, function(yes, id){

View File

@ -99,11 +99,7 @@ function Mesh(ctx){
var wire = peer.wire;
try{
if(wire.send){
if(wire.readyState === wire.OPEN){
wire.send(raw);
} else {
(peer.queue = peer.queue || []).push(raw);
}
} else
if(peer.say){
peer.say(raw);

View File

@ -24,7 +24,6 @@ function output(msg){
at.on('in', at);
return;
}*/
//console.log("out!", at.get, get);
if(get['#'] || at.soul){
get['#'] = get['#'] || at.soul;
msg['#'] || (msg['#'] = text_rand(9));
@ -164,13 +163,12 @@ function relate(at, msg, from, rel){
not(at, msg);
}
tmp = from.id? ((at.map || (at.map = {}))[from.id] = at.map[from.id] || {at: from}) : {};
//console.log("REL?", at.id, at.get, rel === tmp.link, tmp.pass || at.pass);
if(rel === tmp.link){
if(!(tmp.pass || at.pass)){
return;
}
}
if(at.pass){
if(at.pass){
Gun.obj.map(at.map, function(tmp){ tmp.pass = true })
obj_del(at, 'pass');
}

View File

@ -33,6 +33,7 @@ var Graph = {};
if(env.soul){
at.rel = Val.rel.ify(env.soul);
}
env.shell = (as||{}).shell;
env.graph = env.graph || {};
env.seen = env.seen || [];
env.as = env.as || as;
@ -45,8 +46,10 @@ var Graph = {};
at.env = env;
at.soul = soul;
if(Node.ify(at.obj, map, at)){
//at.rel = at.rel || Val.rel.ify(Node.soul(at.node));
env.graph[Val.rel.is(at.rel)] = at.node;
at.rel = at.rel || Val.rel.ify(Node.soul(at.node));
if(at.obj !== env.shell){
env.graph[Val.rel.is(at.rel)] = at.node;
}
}
return at;
}

View File

@ -14,10 +14,8 @@ Gun.chain.map = function(cb, opt, t){
gun.map().on(function(data, key, at, ev){
var next = (cb||noop).call(this, data, key, at, ev);
if(u === next){ return }
if(data === next || Gun.is(next)){
chain._.on('in', next._);
return;
}
if(data === next){ return chain._.on('in', at) }
if(Gun.is(next)){ return chain._.on('in', next._) }
chain._.on('in', {get: key, put: next});
});
return chain;

View File

@ -151,8 +151,24 @@ Gun.dup = require('./dup');
}
Gun.on.get = function(msg, gun){
var root = gun._, soul = msg.get[_soul], node = root.graph[soul], has = msg.get[_has], tmp;
var root = gun._, get = msg.get, soul = get[_soul], node = root.graph[soul], has = get[_has], tmp;
var next = root.next || (root.next = {}), at = next[soul];
if(get['*']){ // TEMPORARY HACK FOR MARTTI, TESTING
var graph = {};
Gun.obj.map(root.graph, function(node, soul){
if(Gun.text.match(soul, get)){
graph[soul] = Gun.obj.copy(node);
}
});
if(!Gun.obj.empty(graph)){
root.on('in', {
'@': msg['#'],
how: '*',
put: graph,
$: gun
});
}
} // TEMPORARY HACK FOR MARTTI, TESTING
if(!node || !at){ return root.on('get', msg) }
if(has){
if(!obj_has(node, has)){ return root.on('get', msg) }
@ -213,7 +229,7 @@ Gun.log.once = function(w,s,o){ return (o = Gun.log.once)[w] = o[w] || 0, o[w]++
Gun.log.once("welcome", "Hello wonderful person! :) Thanks for using GUN, feel free to ask for help on https://gitter.im/amark/gun and ask StackOverflow questions tagged with 'gun'!");
;"Please do not remove these messages unless you are paying for a monthly sponsorship, thanks!";
if(typeof window !== "undefined"){ window.Gun = Gun }
if(typeof window !== "undefined"){ (window.Gun = Gun).window = window }
try{ if(typeof common !== "undefined"){ common.exports = Gun } }catch(e){}
module.exports = Gun;