Skip to content

Commit

Permalink
have unbuild function wrap to prevent scope leaks & allow RETURN hehe…
Browse files Browse the repository at this point in the history
…he so I can reject @bmatusiak 's lS change O:) O:) I love you you're a hero!

later with @bmatusiak check sea.then for '../gun.js' vs '../' vs ...
note: src/index -> core.js
TODO: something about WebRTC candidates hitting ack decrement limits?
  • Loading branch information
amark committed Jan 17, 2024
1 parent 5c52df2 commit 638c2c3
Show file tree
Hide file tree
Showing 48 changed files with 216 additions and 78 deletions.
6 changes: 3 additions & 3 deletions gun.js
Expand Up @@ -136,7 +136,7 @@
};
})(USE, './onto');

;USE(function(module){ ;(function(){
;USE(function(module){
// TODO: BUG! Unbuild will make these globals... CHANGE unbuild to wrap files in a function.
// Book is a replacement for JS objects, maps, dictionaries.
var sT = setTimeout, B = sT.Book || (sT.Book = function(text){
Expand Down Expand Up @@ -348,7 +348,6 @@
}

try{module.exports=B}catch(e){}
}());//delete later.
})(USE, './book');

;USE(function(module){
Expand Down Expand Up @@ -716,7 +715,7 @@
tmp = keys.length;
console.STAT && console.STAT(S, -(S - (S = +new Date)), 'got copied some');
DBG && (DBG.ga = +new Date);
root.on('in', {'@': to, '#': id, put: put, '%': (tmp? (id = text_rand(9)) : u), $: root.$, _: faith, DBG: DBG, FOO: 1});
root.on('in', {'@': to, '#': id, put: put, '%': (tmp? (id = text_rand(9)) : u), $: root.$, _: faith, DBG: DBG});
console.STAT && console.STAT(S, +new Date - S, 'got in');
if(!tmp){ return }
setTimeout.turn(go);
Expand Down Expand Up @@ -1388,6 +1387,7 @@
var Gun = USE('./root');
USE('./shim');
USE('./onto');
USE('./book');
USE('./valid');
USE('./state');
USE('./dup');
Expand Down
1 change: 1 addition & 0 deletions lib/unbuild.js
Expand Up @@ -110,6 +110,7 @@ var undent = function(code, n){
if(rcode != code){
console.log("unbuild:","update",file);
}
code = ";(function(){\n"+code+"\n}());";
write(file, code);
recurse();
}());
Expand Down
4 changes: 3 additions & 1 deletion sea/aeskey.js
@@ -1,3 +1,4 @@
;(function(){

var shim = require('./shim');
var S = require('./settings');
Expand All @@ -13,4 +14,5 @@
return await shim.subtle.importKey('jwk', jwkKey, {name:'AES-GCM'}, false, ['encrypt', 'decrypt'])
}
module.exports = importGen;


}());
4 changes: 3 additions & 1 deletion sea/array.js
@@ -1,3 +1,4 @@
;(function(){

require('./base64');
// This is Array extended to have .toString(['utf8'|'hex'|'base64'])
Expand All @@ -22,4 +23,5 @@
}
}
module.exports = SeaArray;


}());
13 changes: 10 additions & 3 deletions sea/auth.js
@@ -1,3 +1,4 @@
;(function(){

var User = require('./user'), SEA = User.SEA, Gun = User.GUN, noop = function(){};
// now that we have created a user, we want to authenticate them!
Expand All @@ -7,7 +8,8 @@
var pass = (alias || (pair && !(pair.priv && pair.epriv))) && typeof args[1] === 'string' ? args[1] : null;
var cb = args.filter(arg => typeof arg === 'function')[0] || null; // cb now can stand anywhere, after alias/pass or pair
var opt = args && args.length > 1 && typeof args[args.length-1] === 'object' ? args[args.length-1] : {}; // opt is always the last parameter which typeof === 'object' and stands after cb

var retries = typeof opt.retries === 'number' ? opt.retries : 9;

var gun = this, cat = (gun._), root = gun.back(-1);

if(cat.ing){
Expand All @@ -30,6 +32,10 @@
var get = (act.list = (act.list||[]).concat(list||[])).shift();
if(u === get){
if(act.name){ return act.err('Your user account is not published for dApps to access, please consider syncing it online, or allowing local access by adding your device as a peer.') }
if(alias && retries--){
root.get('~@'+alias).once(act.a);
return;
}
return act.err('Wrong user or password.')
}
root.get(get).once(act.a);
Expand Down Expand Up @@ -74,7 +80,7 @@
if(SEA.window && ((gun.back('user')._).opt||opt).remember){
// TODO: this needs to be modular.
try{var sS = {};
sS = window.sessionStorage; // TODO: FIX BUG putting on `.is`!
sS = SEA.window.sessionStorage; // TODO: FIX BUG putting on `.is`!
sS.recall = true;
sS.pair = JSON.stringify(pair); // auth using pair is more reliable than alias/pass
}catch(e){}
Expand Down Expand Up @@ -154,4 +160,5 @@
}catch(e){o={}};
return o;
}


}());
4 changes: 3 additions & 1 deletion sea/base64.js
@@ -1,3 +1,4 @@
;(function(){

var u;
if(u+''== typeof btoa){
Expand All @@ -7,4 +8,5 @@
global.btoa = function(data){ return Buffer.from(data, "binary").toString("base64") };
global.atob = function(data){ return Buffer.from(data, "base64").toString("binary") };
}


}());
4 changes: 3 additions & 1 deletion sea/buffer.js
@@ -1,3 +1,4 @@
;(function(){

require('./base64');
// This is Buffer implementation used in SEA. Functionality is mostly
Expand Down Expand Up @@ -76,4 +77,5 @@
SafeBuffer.prototype.toString = SeaArray.prototype.toString

module.exports = SafeBuffer;


}());
4 changes: 3 additions & 1 deletion sea/certify.js
@@ -1,3 +1,4 @@
;(function(){

var SEA = require('./root');
// This is to certify that a group of "certificants" can "put" anything at a group of matched "paths" to the certificate authority's graph
Expand Down Expand Up @@ -69,4 +70,5 @@
}});

module.exports = SEA.certify;


}());
6 changes: 4 additions & 2 deletions sea/create.js
@@ -1,3 +1,4 @@
;(function(){

var User = require('./user'), SEA = User.SEA, Gun = User.GUN, noop = function(){};

Expand Down Expand Up @@ -94,11 +95,12 @@
}
if(SEA.window){
try{var sS = {};
sS = window.sessionStorage;
sS = SEA.window.sessionStorage;
delete sS.recall;
delete sS.pair;
}catch(e){};
}
return gun;
}


}());
4 changes: 3 additions & 1 deletion sea/decrypt.js
@@ -1,3 +1,4 @@
;(function(){

var SEA = require('./root');
var shim = require('./shim');
Expand Down Expand Up @@ -39,4 +40,5 @@
}});

module.exports = SEA.decrypt;


}());
4 changes: 3 additions & 1 deletion sea/encrypt.js
@@ -1,3 +1,4 @@
;(function(){

var SEA = require('./root');
var shim = require('./shim');
Expand Down Expand Up @@ -37,4 +38,5 @@
}});

module.exports = SEA.encrypt;


}());
8 changes: 6 additions & 2 deletions sea/https.js
@@ -1,12 +1,16 @@
;(function(){

var SEA = require('./root');
try{ if(SEA.window){
if(location.protocol.indexOf('s') < 0
&& location.host.indexOf('localhost') < 0
&& ! /^127\.\d+\.\d+\.\d+$/.test(location.hostname)
&& location.protocol.indexOf('file:') < 0){
&& location.protocol.indexOf('blob:') < 0
&& location.protocol.indexOf('file:') < 0
&& location.origin != 'null'){
console.warn('HTTPS needed for WebCrypto in SEA, redirecting...');
location.protocol = 'https:'; // WebCrypto does NOT work without HTTPS!
}
} }catch(e){}


}());
6 changes: 4 additions & 2 deletions sea/index.js
@@ -1,6 +1,7 @@
;(function(){

var SEA = require('./sea'), S = require('./settings'), noop = function() {}, u;
var Gun = (''+u != typeof window)? (window.Gun||{on:noop}) : require((''+u === typeof MODULE?'.':'')+'./gun', 1);
var Gun = (SEA.window||'').GUN || require((''+u === typeof MODULE?'.':'')+'./gun', 1);
// After we have a GUN extension to make user registration/login easy, we then need to handle everything else.

// We do this with a GUN adapter, we first listen to when a gun instance is created (and when its options change)
Expand Down Expand Up @@ -66,7 +67,7 @@
check.any(eve, msg, val, key, soul, at, no, at.user||''); return;
eve.to.next(msg); // not handled
}
check.hash = function(eve, msg, val, key, soul, at, no){
check.hash = function(eve, msg, val, key, soul, at, no){ // mark unbuilt @i001962 's epic hex contrib!
SEA.work(val, null, function(data){
function hexToBase64(hexStr) {
let base64 = "";
Expand Down Expand Up @@ -246,3 +247,4 @@
// TODO: Potential bug? If pub/priv key starts with `-`? IDK how possible.


}());
4 changes: 3 additions & 1 deletion sea/pair.js
@@ -1,3 +1,4 @@
;(function(){

var SEA = require('./root');
var shim = require('./shim');
Expand Down Expand Up @@ -70,4 +71,5 @@
}});

module.exports = SEA.pair;


}());
6 changes: 4 additions & 2 deletions sea/recall.js
@@ -1,3 +1,4 @@
;(function(){

var User = require('./user'), SEA = User.SEA, Gun = User.GUN;
User.prototype.recall = function(opt, cb){
Expand All @@ -7,7 +8,7 @@
if(SEA.window){
try{
var sS = {};
sS = window.sessionStorage; // TODO: FIX BUG putting on `.is`!
sS = SEA.window.sessionStorage; // TODO: FIX BUG putting on `.is`!
if(sS){
(root._).opt.remember = true;
((gun.back('user')._).opt||opt).remember = true;
Expand All @@ -24,4 +25,5 @@
*/
return gun;
}


}());
5 changes: 4 additions & 1 deletion sea/root.js
@@ -1,9 +1,11 @@
;(function(){

// Security, Encryption, and Authorization: SEA.js
// MANDATORY READING: https://gun.eco/explainers/data/security.html
// IT IS IMPLEMENTED IN A POLYFILL/SHIM APPROACH.
// THIS IS AN EARLY ALPHA!

if(typeof self !== "undefined"){ module.window = self } // should be safe for at least browser/worker/nodejs, need to check other envs like RN etc.
if(typeof window !== "undefined"){ module.window = window }

var tmp = module.window || module, u;
Expand All @@ -13,4 +15,5 @@

try{ if(u+'' !== typeof MODULE){ MODULE.exports = SEA } }catch(e){}
module.exports = SEA;


}());
8 changes: 5 additions & 3 deletions sea/sea.js
@@ -1,3 +1,4 @@
;(function(){

var shim = require('./shim');
// Practical examples about usage found in tests.
Expand All @@ -16,7 +17,7 @@
// For documentation see https://nodejs.org/api/buffer.html
SEA.Buffer = SEA.Buffer || require('./buffer');

// These SEA functions support now only Promises or
// These SEA functions support now ony Promises or
// async/await (compatible) code, use those like Promises.
//
// Creates a wrapper library around Web Crypto API
Expand Down Expand Up @@ -45,7 +46,7 @@
// Obviously it is missing MANY necessary features. This is only an alpha release.
// Please experiment with it, audit what I've done so far, and complain about what needs to be added.
// SEA should be a full suite that is easy and seamless to use.
// Again, scroll near the top, where I provide an EXAMPLE of how to create a user and sign in.
// Again, scroll naer the top, where I provide an EXAMPLE of how to create a user and sign in.
// Once logged in, the rest of the code you just read handled automatically signing/validating data.
// But all other behavior needs to be equally easy, like opinionated ways of
// Adding friends (trusted public keys), sending private messages, etc.
Expand All @@ -55,4 +56,5 @@
module.exports = SEA
// -------------- END SEA MODULES --------------------
// -- BEGIN SEA+GUN MODULES: BUNDLED BY DEFAULT UNTIL OTHERS USE SEA ON OWN -------


}());
4 changes: 3 additions & 1 deletion sea/secret.js
@@ -1,3 +1,4 @@
;(function(){

var SEA = require('./root');
var shim = require('./shim');
Expand Down Expand Up @@ -50,4 +51,5 @@
}

module.exports = SEA.secret;


}());
4 changes: 3 additions & 1 deletion sea/settings.js
@@ -1,3 +1,4 @@
;(function(){

var SEA = require('./root');
var shim = require('./shim');
Expand Down Expand Up @@ -41,4 +42,5 @@

SEA.opt = s;
module.exports = s


}());
4 changes: 3 additions & 1 deletion sea/sha1.js
@@ -1,8 +1,10 @@
;(function(){

// This internal func returns SHA-1 hashed data for KeyID generation
const __shim = require('./shim')
const subtle = __shim.subtle
const ossl = __shim.ossl ? __shim.ossl : subtle
const sha1hash = (b) => ossl.digest({name: 'SHA-1'}, new ArrayBuffer(b))
module.exports = sha1hash


}());
4 changes: 3 additions & 1 deletion sea/sha256.js
@@ -1,8 +1,10 @@
;(function(){

var shim = require('./shim');
module.exports = async function(d, o){
var t = (typeof d == 'string')? d : await shim.stringify(d);
var hash = await shim.subtle.digest({name: o||'SHA-256'}, new shim.TextEncoder().encode(t));
return shim.Buffer.from(hash);
}


}());

0 comments on commit 638c2c3

Please sign in to comment.