12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325 |
- ;(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){
- if(typeof global !== "undefined"){
- var g = global;
- g.btoa = function (data) { return Buffer.from(data, "binary").toString("base64"); };
- g.atob = function (data) { return Buffer.from(data, "base64").toString("binary"); };
- }
- })(USE, './base64');
- ;USE(function(module){
- USE('./base64');
-
- 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){
- USE('./base64');
-
-
-
-
-
- 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 { Crypto: WebCrypto } = USE('@peculiar/webcrypto', 1);
- api.ossl = api.subtle = new WebCrypto({directory: 'ossl'}).subtle
-
-
-
- }catch(e){
- console.log("text-encoding and @peculiar/webcrypto may not be included by default, please add it to your package.json!");
- TEXT_ENCODING_OR_PECULIAR_WEBCRYPTO_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(9), iv: shim.random(15)};
- 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({ public: await ecdhSubtle.importKey(...pubKeyData, true, []) },S.ecdh);
- 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((typeof common == "undefined"?'.':'')+'./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){
- console.log("user.delete() IS DEPRECATED AND WILL BE MOVED TO A MODULE!!!");
- 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(){
- console.log("user.alive() IS DEPRECATED!!!");
- 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.get('trust').get(path).put(theirPubkey);
-
-
-
-
-
- }
- 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._.sea, path = '';
- gun.back(function(at){ if(at.is){ return } path += (at.get||'') });
- (async function(){
- var enc, sec = await user.get('grant').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('grant').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('grant').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._||noop).user){
- return this.to.next(msg);
- }
- var ctx = this.as;
- (msg._||(msg._=function(){})).user = ctx.user;
- security.call(this, msg);
- }
-
-
-
- function security(msg){
- var at = this.as, sea = at.sea, to = this.to;
- if(at.opt.faith && (msg._||noop).faith){
- this.to.next(msg);
- return;
- }
- 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._||noop).user); return;
- }
- each.any(val, key, node, soul, (msg._||noop).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(Gun.is(msg.$) && 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(!(pub = SEA.opt.pub(soul))){
- if(at.opt.secure){
- each.end({err: "Soul is missing public key at '" + key + "'."});
- 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(Gun.is(msg.$) && user && user.is && pub === user.is.pub){
-
- 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});
- return;
- }
- check['any'+soul+key] = 1;
- SEA.verify(SEA.opt.pack(val,key,node,soul), pub, 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});
- });
- }
- 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 || security;
- 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 = function(){}, u;
- var fl = Math.floor;
- var rel_is = Gun.val.rel.is;
-
- })(USE, './index');
- }());
|