1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315 |
- ;(function(){
-
- var root;
- if(typeof window !== "undefined"){ root = window }
- if(typeof global !== "undefined"){ root = global }
- root = root || {};
- var console = root.console || {log: function(){}};
- function USE(arg, req){
- return req? require(arg) : arg.slice? USE[R(arg)] : function(mod, path){
- arg(mod = {exports: {}});
- USE[R(path)] = mod.exports;
- }
- function R(p){
- return p.split('/').slice(-1).toString().replace('.js','');
- }
- }
- if(typeof module !== "undefined"){ var common = module }
-
- ;USE(function(module){
-
-
-
-
- if(typeof window !== "undefined"){ module.window = window }
- var tmp = module.window || module;
- var SEA = tmp.SEA || {};
- if(SEA.window = module.window){ SEA.window.SEA = SEA }
- try{ if(typeof common !== "undefined"){ common.exports = SEA } }catch(e){}
- module.exports = SEA;
- })(USE, './root');
- ;USE(function(module){
- var SEA = USE('./root');
- try{ if(SEA.window){
- if(location.protocol.indexOf('s') < 0
- && location.host.indexOf('localhost') < 0
- && location.protocol.indexOf('file:') < 0){
- location.protocol = 'https:';
- }
- } }catch(e){}
- })(USE, './https');
- ;USE(function(module){
-
- function SeaArray() {}
- Object.assign(SeaArray, { from: Array.from })
- SeaArray.prototype = Object.create(Array.prototype)
- SeaArray.prototype.toString = function(enc, start, end) { enc = enc || 'utf8'; start = start || 0;
- const length = this.length
- if (enc === 'hex') {
- const buf = new Uint8Array(this)
- return [ ...Array(((end && (end + 1)) || length) - start).keys()]
- .map((i) => buf[ i + start ].toString(16).padStart(2, '0')).join('')
- }
- if (enc === 'utf8') {
- return Array.from(
- { length: (end || length) - start },
- (_, i) => String.fromCharCode(this[ i + start])
- ).join('')
- }
- if (enc === 'base64') {
- return btoa(this)
- }
- }
- module.exports = SeaArray;
- })(USE, './array');
- ;USE(function(module){
-
-
-
-
-
- var SeaArray = USE('./array');
- function SafeBuffer(...props) {
- console.warn('new SafeBuffer() is depreciated, please use SafeBuffer.from()')
- return SafeBuffer.from(...props)
- }
- SafeBuffer.prototype = Object.create(Array.prototype)
- Object.assign(SafeBuffer, {
-
- from() {
- if (!Object.keys(arguments).length) {
- throw new TypeError('First argument must be a string, Buffer, ArrayBuffer, Array, or array-like object.')
- }
- const input = arguments[0]
- let buf
- if (typeof input === 'string') {
- const enc = arguments[1] || 'utf8'
- if (enc === 'hex') {
- const bytes = input.match(/([\da-fA-F]{2})/g)
- .map((byte) => parseInt(byte, 16))
- if (!bytes || !bytes.length) {
- throw new TypeError('Invalid first argument for type \'hex\'.')
- }
- buf = SeaArray.from(bytes)
- } else if (enc === 'utf8') {
- const length = input.length
- const words = new Uint16Array(length)
- Array.from({ length: length }, (_, i) => words[i] = input.charCodeAt(i))
- buf = SeaArray.from(words)
- } else if (enc === 'base64') {
- const dec = atob(input)
- const length = dec.length
- const bytes = new Uint8Array(length)
- Array.from({ length: length }, (_, i) => bytes[i] = dec.charCodeAt(i))
- buf = SeaArray.from(bytes)
- } else if (enc === 'binary') {
- buf = SeaArray.from(input)
- } else {
- console.info('SafeBuffer.from unknown encoding: '+enc)
- }
- return buf
- }
- const byteLength = input.byteLength
- const length = input.byteLength ? input.byteLength : input.length
- if (length) {
- let buf
- if (input instanceof ArrayBuffer) {
- buf = new Uint8Array(input)
- }
- return SeaArray.from(buf || input)
- }
- },
-
- alloc(length, fill = 0 ) {
- return SeaArray.from(new Uint8Array(Array.from({ length: length }, () => fill)))
- },
-
- allocUnsafe(length) {
- return SeaArray.from(new Uint8Array(Array.from({ length : length })))
- },
-
- concat(arr) {
- if (!Array.isArray(arr)) {
- throw new TypeError('First argument must be Array containing ArrayBuffer or Uint8Array instances.')
- }
- return SeaArray.from(arr.reduce((ret, item) => ret.concat(Array.from(item)), []))
- }
- })
- SafeBuffer.prototype.from = SafeBuffer.from
- SafeBuffer.prototype.toString = SeaArray.prototype.toString
- module.exports = SafeBuffer;
- })(USE, './buffer');
- ;USE(function(module){
- const SEA = USE('./root')
- const Buffer = USE('./buffer')
- const api = {Buffer: Buffer}
- var o = {};
- if(SEA.window){
- api.crypto = window.crypto || window.msCrypto;
- api.subtle = (api.crypto||o).subtle || (api.crypto||o).webkitSubtle;
- api.TextEncoder = window.TextEncoder;
- api.TextDecoder = window.TextDecoder;
- api.random = (len) => Buffer.from(api.crypto.getRandomValues(new Uint8Array(Buffer.alloc(len))))
- }
- if(!api.crypto){try{
- var crypto = USE('crypto', 1);
- const { TextEncoder, TextDecoder } = USE('text-encoding', 1)
- Object.assign(api, {
- crypto,
-
- TextEncoder,
- TextDecoder,
- random: (len) => Buffer.from(crypto.randomBytes(len))
- });
-
- const WebCrypto = USE('node-webcrypto-ossl', 1);
- api.ossl = api.subtle = new WebCrypto({directory: 'ossl'}).subtle
-
-
-
- }catch(e){
- console.log("node-webcrypto-ossl and text-encoding may not be included by default, please add it to your package.json!");
- OSSL_WEBCRYPTO_OR_TEXT_ENCODING_NOT_INSTALLED;
- }}
- module.exports = api
- })(USE, './shim');
- ;USE(function(module){
- var SEA = USE('./root');
- var Buffer = USE('./buffer');
- var s = {};
- s.pbkdf2 = {hash: 'SHA-256', iter: 100000, ks: 64};
- s.ecdsa = {
- pair: {name: 'ECDSA', namedCurve: 'P-256'},
- sign: {name: 'ECDSA', hash: {name: 'SHA-256'}}
- };
- s.ecdh = {name: 'ECDH', namedCurve: 'P-256'};
-
- s.jwk = function(pub, d){
- pub = pub.split('.');
- var x = pub[0], y = pub[1];
- var jwk = {kty: "EC", crv: "P-256", x: x, y: y, ext: true};
- jwk.key_ops = d ? ['sign'] : ['verify'];
- if(d){ jwk.d = d }
- return jwk;
- };
- s.recall = {
- validity: 12 * 60 * 60,
- hook: function(props){ return props }
- };
- s.check = function(t){ return (typeof t == 'string') && ('SEA{' === t.slice(0,4)) }
- s.parse = function p(t){ try {
- var yes = (typeof t == 'string');
- if(yes && 'SEA{' === t.slice(0,4)){ t = t.slice(3) }
- return yes ? JSON.parse(t) : t;
- } catch (e) {}
- return t;
- }
- SEA.opt = s;
- module.exports = s
- })(USE, './settings');
- ;USE(function(module){
- var shim = USE('./shim');
- module.exports = async function(d, o){
- var t = (typeof d == 'string')? d : JSON.stringify(d);
- var hash = await shim.subtle.digest({name: o||'SHA-256'}, new shim.TextEncoder().encode(t));
- return shim.Buffer.from(hash);
- }
- })(USE, './sha256');
- ;USE(function(module){
-
- const __shim = USE('./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
- })(USE, './sha1');
- ;USE(function(module){
- var SEA = USE('./root');
- var shim = USE('./shim');
- var S = USE('./settings');
- var sha = USE('./sha256');
- var u;
- SEA.work = SEA.work || (async (data, pair, cb, opt) => { try {
- var salt = (pair||{}).epub || pair;
- var opt = opt || {};
- if(salt instanceof Function){
- cb = salt;
- salt = u;
- }
- salt = salt || shim.random(9);
- data = (typeof data == 'string')? data : JSON.stringify(data);
- if('sha' === (opt.name||'').toLowerCase().slice(0,3)){
- var rsha = shim.Buffer.from(await sha(data, opt.name), 'binary').toString(opt.encode || 'base64')
- if(cb){ try{ cb(rsha) }catch(e){console.log(e)} }
- return rsha;
- }
- var key = await (shim.ossl || shim.subtle).importKey('raw', new shim.TextEncoder().encode(data), {name: opt.name || 'PBKDF2'}, false, ['deriveBits']);
- var work = await (shim.ossl || shim.subtle).deriveBits({
- name: opt.name || 'PBKDF2',
- iterations: opt.iterations || S.pbkdf2.iter,
- salt: new shim.TextEncoder().encode(opt.salt || salt),
- hash: opt.hash || S.pbkdf2.hash,
- }, key, opt.length || (S.pbkdf2.ks * 8))
- data = shim.random(data.length)
- var r = shim.Buffer.from(work, 'binary').toString(opt.encode || 'base64')
- if(cb){ try{ cb(r) }catch(e){console.log(e)} }
- return r;
- } catch(e) {
- console.log(e);
- SEA.err = e;
- if(SEA.throw){ throw e }
- if(cb){ cb() }
- return;
- }});
- module.exports = SEA.work;
- })(USE, './work');
- ;USE(function(module){
- var SEA = USE('./root');
- var shim = USE('./shim');
- var S = USE('./settings');
- SEA.name = SEA.name || (async (cb, opt) => { try {
- if(cb){ try{ cb() }catch(e){console.log(e)} }
- return;
- } catch(e) {
- console.log(e);
- SEA.err = e;
- if(SEA.throw){ throw e }
- if(cb){ cb() }
- return;
- }});
-
- SEA.pair = SEA.pair || (async (cb, opt) => { try {
- var ecdhSubtle = shim.ossl || shim.subtle;
-
- var sa = await shim.subtle.generateKey(S.ecdsa.pair, true, [ 'sign', 'verify' ])
- .then(async (keys) => {
-
-
- var key = {};
- key.priv = (await shim.subtle.exportKey('jwk', keys.privateKey)).d;
- var pub = await shim.subtle.exportKey('jwk', keys.publicKey);
-
- key.pub = pub.x+'.'+pub.y;
-
-
-
- return key;
- })
-
-
-
-
- try{
- var dh = await ecdhSubtle.generateKey(S.ecdh, true, ['deriveKey'])
- .then(async (keys) => {
-
- var key = {};
- key.epriv = (await ecdhSubtle.exportKey('jwk', keys.privateKey)).d;
- var pub = await ecdhSubtle.exportKey('jwk', keys.publicKey);
-
- key.epub = pub.x+'.'+pub.y;
-
-
-
- return key;
- })
- }catch(e){
- if(SEA.window){ throw e }
- if(e == 'Error: ECDH is not a supported algorithm'){ console.log('Ignoring ECDH...') }
- else { throw e }
- } dh = dh || {};
- var r = { pub: sa.pub, priv: sa.priv, epub: dh.epub, epriv: dh.epriv }
- if(cb){ try{ cb(r) }catch(e){console.log(e)} }
- return r;
- } catch(e) {
- console.log(e);
- SEA.err = e;
- if(SEA.throw){ throw e }
- if(cb){ cb() }
- return;
- }});
- module.exports = SEA.pair;
- })(USE, './pair');
- ;USE(function(module){
- var SEA = USE('./root');
- var shim = USE('./shim');
- var S = USE('./settings');
- var sha = USE('./sha256');
- var u;
- SEA.sign = SEA.sign || (async (data, pair, cb, opt) => { try {
- opt = opt || {};
- if(!(pair||opt).priv){
- pair = await SEA.I(null, {what: data, how: 'sign', why: opt.why});
- }
- if(u === data){ throw '`undefined` not allowed.' }
- var json = S.parse(data);
- var check = opt.check = opt.check || json;
- if(SEA.verify && (SEA.opt.check(check) || (check && check.s && check.m))
- && u !== await SEA.verify(check, pair)){
- var r = S.parse(check);
- if(!opt.raw){ r = 'SEA'+JSON.stringify(r) }
- if(cb){ try{ cb(r) }catch(e){console.log(e)} }
- return r;
- }
- var pub = pair.pub;
- var priv = pair.priv;
- var jwk = S.jwk(pub, priv);
- var hash = await sha(json);
- var sig = await (shim.ossl || shim.subtle).importKey('jwk', jwk, S.ecdsa.pair, false, ['sign'])
- .then((key) => (shim.ossl || shim.subtle).sign(S.ecdsa.sign, key, new Uint8Array(hash)))
- var r = {m: json, s: shim.Buffer.from(sig, 'binary').toString(opt.encode || 'base64')}
- if(!opt.raw){ r = 'SEA'+JSON.stringify(r) }
- if(cb){ try{ cb(r) }catch(e){console.log(e)} }
- return r;
- } catch(e) {
- console.log(e);
- SEA.err = e;
- if(SEA.throw){ throw e }
- if(cb){ cb() }
- return;
- }});
- module.exports = SEA.sign;
- })(USE, './sign');
- ;USE(function(module){
- var SEA = USE('./root');
- var shim = USE('./shim');
- var S = USE('./settings');
- var sha = USE('./sha256');
- var u;
- SEA.verify = SEA.verify || (async (data, pair, cb, opt) => { try {
- var json = S.parse(data);
- if(false === pair){
- var raw = S.parse(json.m);
- if(cb){ try{ cb(raw) }catch(e){console.log(e)} }
- return raw;
- }
- opt = opt || {};
-
- var pub = pair.pub || pair;
- var key = SEA.opt.slow_leak? await SEA.opt.slow_leak(pub) : await (shim.ossl || shim.subtle).importKey('jwk', jwk, S.ecdsa.pair, false, ['verify']);
- var hash = await sha(json.m);
- var buf, sig, check, tmp; try{
- buf = shim.Buffer.from(json.s, opt.encode || 'base64');
- sig = new Uint8Array(buf);
- check = await (shim.ossl || shim.subtle).verify(S.ecdsa.sign, key, sig, new Uint8Array(hash));
- if(!check){ throw "Signature did not match." }
- }catch(e){
- if(SEA.opt.fallback){
- return await SEA.opt.fall_verify(data, pair, cb, opt);
- }
- }
- var r = check? S.parse(json.m) : u;
- if(cb){ try{ cb(r) }catch(e){console.log(e)} }
- return r;
- } catch(e) {
- console.log(e);
- SEA.err = e;
- if(SEA.throw){ throw e }
- if(cb){ cb() }
- return;
- }});
- module.exports = SEA.verify;
-
- var knownKeys = {};
- var keyForPair = SEA.opt.slow_leak = pair => {
- if (knownKeys[pair]) return knownKeys[pair];
- var jwk = S.jwk(pair);
- knownKeys[pair] = (shim.ossl || shim.subtle).importKey("jwk", jwk, S.ecdsa.pair, false, ["verify"]);
- return knownKeys[pair];
- };
- SEA.opt.fall_verify = async function(data, pair, cb, opt, f){
- if(f === SEA.opt.fallback){ throw "Signature did not match" } f = f || 1;
- var json = S.parse(data), pub = pair.pub || pair, key = await SEA.opt.slow_leak(pub);
- var hash = (f <= SEA.opt.fallback)? shim.Buffer.from(await shim.subtle.digest({name: 'SHA-256'}, new shim.TextEncoder().encode(S.parse(json.m)))) : await sha(json.m);
- var buf; var sig; var check; try{
- buf = shim.Buffer.from(json.s, opt.encode || 'base64')
- sig = new Uint8Array(buf)
- check = await (shim.ossl || shim.subtle).verify(S.ecdsa.sign, key, sig, new Uint8Array(hash))
- if(!check){ throw "Signature did not match." }
- }catch(e){
- buf = shim.Buffer.from(json.s, 'utf8')
- sig = new Uint8Array(buf)
- check = await (shim.ossl || shim.subtle).verify(S.ecdsa.sign, key, sig, new Uint8Array(hash))
- if(!check){ throw "Signature did not match." }
- }
- var r = check? S.parse(json.m) : u;
- if(cb){ try{ cb(r) }catch(e){console.log(e)} }
- return r;
- }
- SEA.opt.fallback = 2;
- })(USE, './verify');
- ;USE(function(module){
- var shim = USE('./shim');
- var sha256hash = USE('./sha256');
- const importGen = async (key, salt, opt) => {
-
- var opt = opt || {};
- const combo = key + (salt || shim.random(8)).toString('utf8');
- const hash = shim.Buffer.from(await sha256hash(combo), 'binary')
- return await shim.subtle.importKey('raw', new Uint8Array(hash), opt.name || 'AES-GCM', false, ['encrypt', 'decrypt'])
- }
- module.exports = importGen;
- })(USE, './aeskey');
- ;USE(function(module){
- var SEA = USE('./root');
- var shim = USE('./shim');
- var S = USE('./settings');
- var aeskey = USE('./aeskey');
- var u;
- SEA.encrypt = SEA.encrypt || (async (data, pair, cb, opt) => { try {
- opt = opt || {};
- var key = (pair||opt).epriv || pair;
- if(u === data){ throw '`undefined` not allowed.' }
- if(!key){
- pair = await SEA.I(null, {what: data, how: 'encrypt', why: opt.why});
- key = pair.epriv || pair;
- }
- var msg = (typeof data == 'string')? data : JSON.stringify(data);
- var rand = {s: shim.random(8), iv: shim.random(16)};
- var ct = await aeskey(key, rand.s, opt).then((aes) => ( shim.subtle).encrypt({
- name: opt.name || 'AES-GCM', iv: new Uint8Array(rand.iv)
- }, aes, new shim.TextEncoder().encode(msg)));
- var r = {
- ct: shim.Buffer.from(ct, 'binary').toString(opt.encode || 'base64'),
- iv: rand.iv.toString(opt.encode || 'base64'),
- s: rand.s.toString(opt.encode || 'base64')
- }
- if(!opt.raw){ r = 'SEA'+JSON.stringify(r) }
- if(cb){ try{ cb(r) }catch(e){console.log(e)} }
- return r;
- } catch(e) {
- console.log(e);
- SEA.err = e;
- if(SEA.throw){ throw e }
- if(cb){ cb() }
- return;
- }});
- module.exports = SEA.encrypt;
- })(USE, './encrypt');
- ;USE(function(module){
- var SEA = USE('./root');
- var shim = USE('./shim');
- var S = USE('./settings');
- var aeskey = USE('./aeskey');
- SEA.decrypt = SEA.decrypt || (async (data, pair, cb, opt) => { try {
- opt = opt || {};
- var key = (pair||opt).epriv || pair;
- if(!key){
- pair = await SEA.I(null, {what: data, how: 'decrypt', why: opt.why});
- key = pair.epriv || pair;
- }
- var json = S.parse(data);
- var buf, bufiv, bufct; try{
- buf = shim.Buffer.from(json.s, opt.encode || 'base64');
- bufiv = shim.Buffer.from(json.iv, opt.encode || 'base64');
- bufct = shim.Buffer.from(json.ct, opt.encode || 'base64');
- var ct = await aeskey(key, buf, opt).then((aes) => ( shim.subtle).decrypt({
- name: opt.name || 'AES-GCM', iv: new Uint8Array(bufiv)
- }, aes, new Uint8Array(bufct)));
- }catch(e){
- if('utf8' === opt.encode){ throw "Could not decrypt" }
- if(SEA.opt.fallback){
- opt.encode = 'utf8';
- return await SEA.decrypt(data, pair, cb, opt);
- }
- }
- var r = S.parse(new shim.TextDecoder('utf8').decode(ct));
- if(cb){ try{ cb(r) }catch(e){console.log(e)} }
- return r;
- } catch(e) {
- console.log(e);
- SEA.err = e;
- if(SEA.throw){ throw e }
- if(cb){ cb() }
- return;
- }});
- module.exports = SEA.decrypt;
- })(USE, './decrypt');
- ;USE(function(module){
- var SEA = USE('./root');
- var shim = USE('./shim');
- var S = USE('./settings');
-
- SEA.secret = SEA.secret || (async (key, pair, cb, opt) => { try {
- opt = opt || {};
- if(!pair || !pair.epriv || !pair.epub){
- pair = await SEA.I(null, {what: key, how: 'secret', why: opt.why});
- }
- var pub = key.epub || key;
- var epub = pair.epub;
- var epriv = pair.epriv;
- var ecdhSubtle = shim.ossl || shim.subtle;
- var pubKeyData = keysToEcdhJwk(pub);
- var props = Object.assign(S.ecdh, { public: await ecdhSubtle.importKey(...pubKeyData, true, []) });
- var privKeyData = keysToEcdhJwk(epub, epriv);
- var derived = await ecdhSubtle.importKey(...privKeyData, false, ['deriveKey']).then(async (privKey) => {
-
- var derivedKey = await ecdhSubtle.deriveKey(props, privKey, { name: 'AES-GCM', length: 256 }, true, [ 'encrypt', 'decrypt' ]);
- return ecdhSubtle.exportKey('jwk', derivedKey).then(({ k }) => k);
- })
- var r = derived;
- if(cb){ try{ cb(r) }catch(e){console.log(e)} }
- return r;
- } catch(e) {
- console.log(e);
- SEA.err = e;
- if(SEA.throw){ throw e }
- if(cb){ cb() }
- return;
- }});
-
- var keysToEcdhJwk = (pub, d) => {
-
- var [ x, y ] = pub.split('.')
- var jwk = d ? { d: d } : {}
- return [
- 'jwk',
- Object.assign(
- jwk,
- { x: x, y: y, kty: 'EC', crv: 'P-256', ext: true }
- ),
- S.ecdh
- ]
- }
- module.exports = SEA.secret;
- })(USE, './secret');
- ;USE(function(module){
- var shim = USE('./shim');
-
- var SEA = USE('./root');
- SEA.work = USE('./work');
- SEA.sign = USE('./sign');
- SEA.verify = USE('./verify');
- SEA.encrypt = USE('./encrypt');
- SEA.decrypt = USE('./decrypt');
- SEA.random = SEA.random || shim.random;
-
-
- SEA.Buffer = SEA.Buffer || USE('./buffer');
-
-
-
-
-
-
- SEA.keyid = SEA.keyid || (async (pub) => {
- try {
-
- const pb = Buffer.concat(
- pub.replace(/-/g, '+').replace(/_/g, '/').split('.')
- .map((t) => Buffer.from(t, 'base64'))
- )
-
- const id = Buffer.concat([
- Buffer.from([0x99, pb.length / 0x100, pb.length % 0x100]), pb
- ])
- const sha1 = await sha1hash(id)
- const hash = Buffer.from(sha1, 'binary')
- return hash.toString('hex', hash.length - 8)
- } catch (e) {
- console.log(e)
- throw e
- }
- });
-
-
-
-
-
-
-
-
-
- var Gun = (SEA.window||{}).Gun || USE('./gun', 1);
- Gun.SEA = SEA;
- SEA.GUN = SEA.Gun = Gun;
- module.exports = SEA
- })(USE, './sea');
- ;USE(function(module){
- var Gun = USE('./sea').Gun;
- Gun.chain.then = function(cb){
- var gun = this, p = (new Promise(function(res, rej){
- gun.once(res);
- }));
- return cb? p.then(cb) : p;
- }
- })(USE, './then');
- ;USE(function(module){
- var SEA = USE('./sea');
- var Gun = SEA.Gun;
- var then = USE('./then');
- function User(root){
- this._ = {$: this};
- }
- User.prototype = (function(){ function F(){}; F.prototype = Gun.chain; return new F() }())
- User.prototype.constructor = User;
-
-
- Gun.chain.user = function(pub){
- var gun = this, root = gun.back(-1), user;
- if(pub){ return root.get('~'+pub) }
- if(user = root.back('user')){ return user }
- var root = (root._), at = root, uuid = at.opt.uuid || Gun.state.lex;
- (at = (user = at.user = gun.chain(new User))._).opt = {};
- at.opt.uuid = function(cb){
- var id = uuid(), pub = root.user;
- if(!pub || !(pub = pub.is) || !(pub = pub.pub)){ return id }
- id = id + '~' + pub + '.';
- if(cb && cb.call){ cb(null, id) }
- return id;
- }
- return user;
- }
- Gun.User = User;
- module.exports = User;
- })(USE, './user');
- ;USE(function(module){
-
-
- var SEA = USE('./sea');
- var User = USE('./user');
- var authsettings = USE('./settings');
- var Gun = SEA.Gun;
- var noop = function(){};
-
- User.prototype.create = function(alias, pass, cb, opt){
- var gun = this, cat = (gun._), root = gun.back(-1);
- cb = cb || noop;
- if(cat.ing){
- cb({err: Gun.log("User is already being created or authenticated!"), wait: true});
- return gun;
- }
- cat.ing = true;
- opt = opt || {};
- var act = {}, u;
- act.a = function(pubs){
- act.pubs = pubs;
- if(pubs && !opt.already){
-
- var ack = {err: Gun.log('User already created!')};
- cat.ing = false;
- cb(ack);
- gun.leave();
- return;
- }
- act.salt = Gun.text.random(64);
- SEA.work(pass, act.salt, act.b);
- }
- act.b = function(proof){
- act.proof = proof;
- SEA.pair(act.c);
- }
- act.c = function(pair){ var tmp;
- act.pair = pair || {};
- if(tmp = cat.root.user){
- tmp._.sea = pair;
- tmp.is = {pub: pair.pub, epub: pair.epub, alias: alias};
- }
-
- act.data = {pub: pair.pub};
- act.d();
- }
- act.d = function(){
- act.data.alias = alias;
- act.e();
- }
- act.e = function(){
- act.data.epub = act.pair.epub;
- SEA.encrypt({priv: act.pair.priv, epriv: act.pair.epriv}, act.proof, act.f, {raw:1});
- }
- act.f = function(auth){
- act.data.auth = JSON.stringify({ek: auth, s: act.salt});
- act.g(act.data.auth);
- }
- act.g = function(auth){ var tmp;
- act.data.auth = act.data.auth || auth;
- root.get(tmp = '~'+act.pair.pub).put(act.data);
- root.get('~@'+alias).put(Gun.obj.put({}, tmp, Gun.val.link.ify(tmp)));
- setTimeout(function(){
- cat.ing = false;
- cb({ok: 0, pub: act.pair.pub});
- if(noop === cb){ gun.auth(alias, pass) }
- },10);
- }
- root.get('~@'+alias).once(act.a);
- return gun;
- }
-
- User.prototype.auth = function(alias, pass, cb, opt){
- var gun = this, cat = (gun._), root = gun.back(-1);
- cb = cb || function(){};
- if(cat.ing){
- cb({err: Gun.log("User is already being created or authenticated!"), wait: true});
- return gun;
- }
- cat.ing = true;
- opt = opt || {};
- var pair = (alias && (alias.pub || alias.epub))? alias : (pass && (pass.pub || pass.epub))? pass : null;
- var act = {}, u;
- act.a = function(data){
- if(!data){ return act.b() }
- if(!data.pub){
- var tmp = [];
- Gun.node.is(data, function(v){ tmp.push(v) })
- return act.b(tmp);
- }
- if(act.name){ return act.f(data) }
- act.c((act.data = data).auth);
- }
- act.b = function(list){
- 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.') }
- return act.err('Wrong user or password.')
- }
- root.get(get).once(act.a);
- }
- act.c = function(auth){
- if(u === auth){ return act.b() }
- if(Gun.text.is(auth)){ return act.c(Gun.obj.ify(auth)) }
- SEA.work(pass, (act.auth = auth).s, act.d, act.enc);
- }
- act.d = function(proof){
- SEA.decrypt(act.auth.ek, proof, act.e, act.enc);
- }
- act.e = function(half){
- if(u === half){
- if(!act.enc){
- act.enc = {encode: 'utf8'};
- return act.c(act.auth);
- } act.enc = null;
- return act.b();
- }
- act.half = half;
- act.f(act.data);
- }
- act.f = function(data){
- if(!data || !data.pub){ return act.b() }
- var tmp = act.half || {};
- act.g({pub: data.pub, epub: data.epub, priv: tmp.priv, epriv: tmp.epriv});
- }
- act.g = function(pair){
- act.pair = pair;
- var user = (root._).user, at = (user._);
- var tmp = at.tag;
- var upt = at.opt;
- at = user._ = root.get('~'+pair.pub)._;
- at.opt = upt;
-
- user.is = {pub: pair.pub, epub: pair.epub, alias: alias};
- at.sea = act.pair;
- cat.ing = false;
- try{if(pass && !Gun.obj.has(Gun.obj.ify(cat.root.graph['~'+pair.pub].auth), ':')){ opt.shuffle = opt.change = pass; } }catch(e){}
- opt.change? act.z() : cb(at);
- if(SEA.window && ((gun.back('user')._).opt||opt).remember){
-
- try{var sS = {};
- sS = window.sessionStorage;
- sS.recall = true;
- sS.alias = alias;
- sS.tmp = pass;
- }catch(e){}
- }
- try{
- (root._).on('auth', at)
-
- }catch(e){
- Gun.log("Your 'auth' callback crashed with:", e);
- }
- }
- act.z = function(){
-
- act.salt = Gun.text.random(64);
- SEA.work(opt.change, act.salt, act.y);
- }
- act.y = function(proof){
- SEA.encrypt({priv: act.pair.priv, epriv: act.pair.epriv}, proof, act.x, {raw:1});
- }
- act.x = function(auth){
- act.w(JSON.stringify({ek: auth, s: act.salt}));
- }
- act.w = function(auth){
- if(opt.shuffle){
- console.log('migrate core account from UTF8 & shuffle');
- var tmp = Gun.obj.to(act.data);
- Gun.obj.del(tmp, '_');
- tmp.auth = auth;
- root.get('~'+act.pair.pub).put(tmp);
- }
- root.get('~'+act.pair.pub).get('auth').put(auth, cb);
- }
- act.err = function(e){
- var ack = {err: Gun.log(e || 'User cannot be found!')};
- cat.ing = false;
- cb(ack);
- }
- act.plugin = function(name){
- if(!(act.name = name)){ return act.err() }
- var tmp = [name];
- if('~' !== name[0]){
- tmp[1] = '~'+name;
- tmp[2] = '~@'+name;
- }
- act.b(tmp);
- }
- if(pair){
- act.g(pair);
- } else
- if(alias){
- root.get('~@'+alias).once(act.a);
- } else
- if(!alias && !pass){
- SEA.name(act.plugin);
- }
- return gun;
- }
- User.prototype.pair = function(){
- console.log("user.pair() IS DEPRECATED AND WILL BE DELETED!!!");
- var user = this;
- if(!user.is){ return false }
- return user._.sea;
- }
- User.prototype.leave = function(opt, cb){
- var gun = this, user = (gun.back(-1)._).user;
- if(user){
- delete user.is;
- delete user._.is;
- delete user._.sea;
- }
- if(SEA.window){
- try{var sS = {};
- sS = window.sessionStorage;
- delete sS.alias;
- delete sS.tmp;
- delete sS.recall;
- }catch(e){};
- }
- return gun;
- }
-
- User.prototype.delete = async function(alias, pass, cb){
- var gun = this, root = gun.back(-1), user = gun.back('user');
- try {
- user.auth(alias, pass, function(ack){
- var pub = (user.is||{}).pub;
-
- user.map().once(function(){ this.put(null) });
-
- user.leave();
- (cb || noop)({ok: 0});
- });
- } catch (e) {
- Gun.log('User.delete failed! Error:', e);
- }
- return gun;
- }
- User.prototype.recall = function(opt, cb){
- var gun = this, root = gun.back(-1), tmp;
- opt = opt || {};
- if(opt && opt.sessionStorage){
- if(SEA.window){
- try{var sS = {};
- sS = window.sessionStorage;
- if(sS){
- (root._).opt.remember = true;
- ((gun.back('user')._).opt||opt).remember = true;
- if(sS.recall || (sS.alias && sS.tmp)){
- root.user().auth(sS.alias, sS.tmp, cb);
- }
- }
- }catch(e){}
- }
- return gun;
- }
-
- return gun;
- }
- User.prototype.alive = async function(){
- const gunRoot = this.back(-1)
- try {
-
- await authRecall(gunRoot)
- return gunRoot._.user._
- } catch (e) {
- const err = 'No session!'
- Gun.log(err)
- throw { err }
- }
- }
- User.prototype.trust = async function(user){
-
-
- if (Gun.is(user)) {
- user.get('pub').get((ctx, ev) => {
- console.log(ctx, ev)
- })
- }
- }
- User.prototype.grant = function(to, cb){
- console.log("`.grant` API MAY BE DELETED OR CHANGED OR RENAMED, DO NOT USE!");
- var gun = this, user = gun.back(-1).user(), pair = user.pair(), path = '';
- gun.back(function(at){ if(at.is){ return } path += (at.get||'') });
- (async function(){
- var enc, sec = await user.get('trust').get(pair.pub).get(path).then();
- sec = await SEA.decrypt(sec, pair);
- if(!sec){
- sec = SEA.random(16).toString();
- enc = await SEA.encrypt(sec, pair);
- user.get('trust').get(pair.pub).get(path).put(enc);
- }
- var pub = to.get('pub').then();
- var epub = to.get('epub').then();
- pub = await pub; epub = await epub;
- var dh = await SEA.secret(epub, pair);
- enc = await SEA.encrypt(sec, dh);
- user.get('trust').get(pub).get(path).put(enc, cb);
- }());
- return gun;
- }
- User.prototype.secret = function(data, cb){
- console.log("`.secret` API MAY BE DELETED OR CHANGED OR RENAMED, DO NOT USE!");
- var gun = this, user = gun.back(-1).user(), pair = user.pair(), path = '';
- gun.back(function(at){ if(at.is){ return } path += (at.get||'') });
- (async function(){
- var enc, sec = await user.get('trust').get(pair.pub).get(path).then();
- sec = await SEA.decrypt(sec, pair);
- if(!sec){
- sec = SEA.random(16).toString();
- enc = await SEA.encrypt(sec, pair);
- user.get('trust').get(pair.pub).get(path).put(enc);
- }
- enc = await SEA.encrypt(data, sec);
- gun.put(enc, cb);
- }());
- return gun;
- }
- module.exports = User
- })(USE, './create');
- ;USE(function(module){
- const SEA = USE('./sea')
- const Gun = SEA.Gun;
-
-
- Gun.on('opt', function(at){
- if(!at.sea){
- at.sea = {own: {}};
- at.on('in', security, at);
- at.on('out', signature, at);
- at.on('node', each, at);
- }
- this.to.next(at);
- });
-
-
-
-
-
-
-
-
-
-
-
-
-
- function each(msg){
-
-
- var to = this.to, vertex = (msg.$._).put, c = 0, d;
- Gun.node.is(msg.put, function(val, key, node){
-
- var tmp = Gun.obj.ify(val) || noop;
- if(u !== tmp[':']){
- node[key] = SEA.opt.unpack(tmp);
- return;
- }
- if(!SEA.opt.check(val)){ return }
- c++;
- SEA.verify(val, false, function(data){ c--;
- node[key] = SEA.opt.unpack(data, key, node);;
- if(d && !c && (c = -1)){ to.next(msg) }
- });
- });
- if((d = true) && !c){ to.next(msg) }
- }
-
- function signature(msg){
- if(msg.user){
- return this.to.next(msg);
- }
- var ctx = this.as;
- msg.user = ctx.user;
- security.call(this, msg);
- }
-
-
-
- function security(msg){
- var at = this.as, sea = at.sea, to = this.to;
- if(msg.get){
-
- var soul = msg.get['#'];
- if(soul){
- if(typeof soul !== 'string'){ return to.next(msg) }
- if('alias' === soul){
- return to.next(msg);
- } else
- if('~@' === soul.slice(0,2)){
- return to.next(msg);
- } else {
- return to.next(msg);
- }
- }
- }
- if(msg.put){
-
- var check = {}, each = {}, u;
- each.node = function(node, soul){
- if(Gun.obj.empty(node, '_')){ return check['node'+soul] = 0 }
- Gun.obj.map(node, each.way, {soul: soul, node: node});
- };
- each.way = function(val, key){
- var soul = this.soul, node = this.node, tmp;
- if('_' === key){ return }
- if('~@' === soul){
- each.alias(val, key, node, soul); return;
- }
- if('~@' === soul.slice(0,2)){
- each.pubs(val, key, node, soul); return;
- }
- if('~' === soul.slice(0,1) && 2 === (tmp = soul.slice(1)).split('.').length){
- each.pub(val, key, node, soul, tmp, msg.user); return;
- }
- each.any(val, key, node, soul, msg.user); return;
- return each.end({err: "No other data allowed!"});
- };
- each.alias = function(val, key, node, soul){
- if(!val){ return each.end({err: "Data must exist!"}) }
- if('~@'+key === Gun.val.link.is(val)){ return check['alias'+key] = 0 }
- each.end({err: "Mismatching alias."});
- };
- each.pubs = function(val, key, node, soul){
- if(!val){ return each.end({err: "Alias must exist!"}) }
- if(key === Gun.val.link.is(val)){ return check['pubs'+soul+key] = 0 }
- each.end({err: "Alias must match!"});
- };
- each.pub = function(val, key, node, soul, pub, user){ var tmp;
- if('pub' === key){
- if(val === pub){ return (check['pub'+soul+key] = 0) }
- return each.end({err: "Account must match!"});
- }
- check['user'+soul+key] = 1;
- if(msg.I && user && user.is && pub === user.is.pub){
- SEA.sign(SEA.opt.prep(tmp = SEA.opt.parse(val), key, node, soul), (user._).sea, function(data){ var rel;
- if(u === data){ return each.end({err: SEA.err || 'Pub signature fail.'}) }
- if(rel = Gun.val.link.is(val)){
- (at.sea.own[rel] = at.sea.own[rel] || {})[pub] = true;
- }
- node[key] = JSON.stringify({':': SEA.opt.unpack(data.m), '~': data.s});
- check['user'+soul+key] = 0;
- each.end({ok: 1});
- }, {check: SEA.opt.pack(tmp, key, node, soul), raw: 1});
- return;
- }
- SEA.verify(SEA.opt.pack(val,key,node,soul), pub, function(data){ var rel, tmp;
- data = SEA.opt.unpack(data, key, node);
- if(u === data){
- return each.end({err: "Unverified data."});
- }
- if((rel = Gun.val.link.is(data)) && pub === SEA.opt.pub(rel)){
- (at.sea.own[rel] = at.sea.own[rel] || {})[pub] = true;
- }
- check['user'+soul+key] = 0;
- each.end({ok: 1});
- });
- };
- each.any = function(val, key, node, soul, user){ var tmp, pub;
- if(!user || !user.is){
- if(tmp = SEA.opt.pub(soul)){
- check['any'+soul+key] = 1;
- SEA.verify(SEA.opt.pack(val,key,node,soul), pub = tmp, function(data){ var rel;
- data = SEA.opt.unpack(data, key, node);
- if(u === data){ return each.end({err: "Mismatched owner on '" + key + "'."}) }
- if((rel = Gun.val.link.is(data)) && pub === SEA.opt.pub(rel)){
- (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;
- at.on('secure', function(msg){ this.off();
- check['any'+soul+key] = 0;
- if(at.opt.secure){ msg = null }
- each.end(msg || {err: "Data cannot be modified."});
- }).on.on('secure', msg);
-
- return;
- }
- if(!(tmp = SEA.opt.pub(soul))){
- if(at.opt.secure){
- each.end({err: "Soul is missing public key at '" + key + "'."});
- return;
- }
-
- check['any'+soul+key] = 0;
- each.end({ok: 1});
- return;
- }
- if(!msg.I){
- each.any(val, key, node, soul);
- return;
- }
- if((pub = tmp) !== (user.is||noop).pub){
- each.any(val, key, node, soul);
- return;
- }
-
- check['any'+soul+key] = 1;
- SEA.sign(SEA.opt.prep(tmp = SEA.opt.parse(val), key, node, soul), (user._).sea, function(data){
- if(u === data){ return each.end({err: 'My signature fail.'}) }
- node[key] = JSON.stringify({':': SEA.opt.unpack(data.m), '~': data.s});
- check['any'+soul+key] = 0;
- each.end({ok: 1});
- }, {check: SEA.opt.pack(tmp, key, node, soul), raw: 1});
- }
- each.end = function(ctx){
- if(each.err){ return }
- if((each.err = ctx.err) || ctx.no){
- console.log('NO!', each.err, msg.put);
- return;
- }
- if(!each.end.ed){ return }
- if(Gun.obj.map(check, function(no){
- if(no){ return true }
- })){ return }
- msg.user = at.user;
- to.next(msg);
- };
- Gun.obj.map(msg.put, each.node);
- each.end({end: each.end.ed = true});
- return;
- }
- to.next(msg);
- }
- SEA.opt.pub = function(s){
- if(!s){ return }
- s = s.split('~');
- if(!s || !(s = s[1])){ return }
- s = s.split('.');
- if(!s || 2 > s.length){ return }
- s = s.slice(0,2).join('.');
- return s;
- }
- SEA.opt.prep = function(d,k, n,s){
- return {'#':s,'.':k,':':SEA.opt.parse(d),'>':Gun.state.is(n, k)};
- }
- SEA.opt.pack = function(d,k, n,s){
- if(SEA.opt.check(d)){ return d }
- var meta = (Gun.obj.ify(d)||noop), sig = meta['~'];
- return sig? {m: {'#':s,'.':k,':':meta[':'],'>':Gun.state.is(n, k)}, s: sig} : d;
- }
- SEA.opt.unpack = function(d, k, n){ var tmp;
- if(u === d){ return }
- if(d && (u !== (tmp = d[':']))){ return tmp }
- if(!k || !n){ return }
- if(d === n[k]){ return d }
- if(!SEA.opt.check(n[k])){ return d }
- var soul = Gun.node.soul(n), s = Gun.state.is(n, k);
- if(d && 4 === d.length && soul === d[0] && k === d[1] && fl(s) === fl(d[3])){
- return d[2];
- }
- if(s < SEA.opt.shuffle_attack){
- return d;
- }
- }
- SEA.opt.shuffle_attack = 1546329600000;
- var noop = {}, u;
- var fl = Math.floor;
- var rel_is = Gun.val.rel.is;
-
- })(USE, './index');
- }());
|