@substrate-system/keys 0.5.0 → 0.5.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/_base.min.js +2 -2
- package/dist/_base.min.js.map +4 -4
- package/dist/aes/index.min.js +2 -2
- package/dist/aes/index.min.js.map +3 -3
- package/dist/browser/index.js +118 -34
- package/dist/browser/index.min.js +2 -2
- package/dist/crypto.min.js +2 -2
- package/dist/crypto.min.js.map +4 -4
- package/dist/ecc/index.min.js +2 -2
- package/dist/ecc/index.min.js.map +4 -4
- package/dist/index.min.js +2 -2
- package/dist/index.min.js.map +4 -4
- package/dist/rsa/index.min.js +2 -2
- package/dist/rsa/index.min.js.map +4 -4
- package/dist/util.d.ts +1 -1
- package/dist/util.d.ts.map +1 -1
- package/dist/util.min.js +1 -1
- package/dist/util.min.js.map +3 -3
- package/package.json +2 -2
package/dist/index.min.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
var tr=Object.defineProperty;var Rt=(t,e,r)=>e in t?tr(t,e,{enumerable:!0,configurable:!0,writable:!0,value:r}):t[e]=r;var s=(t,e)=>tr(t,"name",{value:e,configurable:!0});var D=(t,e)=>{for(var r in e)tr(t,r,{get:e[r],enumerable:!0})};var c=(t,e,r)=>Rt(t,typeof e!="symbol"?e+"":e,r);var qr=(n=>(n[n.B1024=1024]="B1024",n[n.B2048=2048]="B2048",n[n.B4096=4096]="B4096",n))(qr||{}),Jr=(n=>(n[n.B128=128]="B128",n[n.B192=192]="B192",n[n.B256=256]="B256",n))(Jr||{}),Zr=(r=>(r.ECC="ecc",r.RSA="rsa",r))(Zr||{}),nr=(i=>(i.SHA_1="SHA-1",i.SHA_256="SHA-256",i.SHA_384="SHA-384",i.SHA_512="SHA-512",i))(nr||{}),ir=(i=>(i.P_256="P-256",i.P_384="P-384",i.P_521="P-521",i.X25519="X25519",i))(ir||{}),se=(n=>(n.Sign="signing",n.Exchange="exchange",n.Write="write",n))(se||{}),sr=(r=>(r[r.B8=8]="B8",r[r.B16=16]="B16",r))(sr||{});var _t={};D(_t,{AES_GCM:()=>V,AES_GCM_TAG_LENGTH:()=>Lr,BASE58_DID_PREFIX:()=>X,BLS_DID_PREFIX:()=>Xe,DEFAULT_CHAR_SIZE:()=>F,DEFAULT_ECC_EXCHANGE:()=>Rr,DEFAULT_ECC_EXCHANGE_CURVE:()=>qn,DEFAULT_ECC_WRITE:()=>Or,DEFAULT_HASH_ALGORITHM:()=>ae,DEFAULT_RSA_EXCHANGE:()=>kr,DEFAULT_RSA_SIZE:()=>je,DEFAULT_RSA_WRITE:()=>Mr,DEFAULT_SYMM_ALGORITHM:()=>L,DEFAULT_SYMM_LENGTH:()=>Q,ECC_EXCHANGE_ALGORITHM:()=>De,ECC_WRITE_ALGORITHM:()=>oe,EDWARDS_DID_PREFIX:()=>Ye,IV_LENGTH:()=>I,KEY_TYPE:()=>Ie,KEY_USE:()=>xt,RSA_ALGORITHM:()=>J,RSA_DID_PREFIX:()=>We,RSA_HASHING_ALGORITHM:()=>Ve,RSA_SIGN_ALGORITHM:()=>Z,RsaKeys:()=>ue,SALT_LENGTH:()=>B,WRAP_FORMAT_VERSION:()=>$e,X25519_PUBLIC_KEY_LENGTH:()=>G,encrypt:()=>di,encryptKeyTo:()=>Ue,encryptTo:()=>It,getPublicKeyAsArrayBuffer:()=>re,getPublicKeyAsUint8Array:()=>Gr,publicKeyToDid:()=>ke,verify:()=>mi});function Se(t=0){return new Uint8Array(t)}s(Se,"allocUnsafe");function or(t,e){e==null&&(e=t.reduce((i,o)=>i+o.length,0));let r=Se(e),n=0;for(let i of t)r.set(i,n),n+=i.length;return r}s(or,"concat");var hr={};D(hr,{base10:()=>jt});var Ri=new Uint8Array(0);function Qr(t,e){if(t===e)return!0;if(t.byteLength!==e.byteLength)return!1;for(let r=0;r<t.byteLength;r++)if(t[r]!==e[r])return!1;return!0}s(Qr,"equals");function j(t){if(t instanceof Uint8Array&&t.constructor.name==="Uint8Array")return t;if(t instanceof ArrayBuffer)return new Uint8Array(t);if(ArrayBuffer.isView(t))return new Uint8Array(t.buffer,t.byteOffset,t.byteLength);throw new Error("Unknown type, must be binary type")}s(j,"coerce");function et(t){return new TextEncoder().encode(t)}s(et,"fromString");function rt(t){return new TextDecoder().decode(t)}s(rt,"toString");function zt(t,e){if(t.length>=255)throw new TypeError("Alphabet too long");for(var r=new Uint8Array(256),n=0;n<r.length;n++)r[n]=255;for(var i=0;i<t.length;i++){var o=t.charAt(i),a=o.charCodeAt(0);if(r[a]!==255)throw new TypeError(o+" is ambiguous");r[a]=i}var y=t.length,p=t.charAt(0),f=Math.log(y)/Math.log(256),h=Math.log(256)/Math.log(y);function w(u){if(u instanceof Uint8Array||(ArrayBuffer.isView(u)?u=new Uint8Array(u.buffer,u.byteOffset,u.byteLength):Array.isArray(u)&&(u=Uint8Array.from(u))),!(u instanceof Uint8Array))throw new TypeError("Expected Uint8Array");if(u.length===0)return"";for(var m=0,T=0,S=0,E=u.length;S!==E&&u[S]===0;)S++,m++;for(var U=(E-S)*h+1>>>0,v=new Uint8Array(U);S!==E;){for(var _=u[S],z=0,M=U-1;(_!==0||z<T)&&M!==-1;M--,z++)_+=256*v[M]>>>0,v[M]=_%y>>>0,_=_/y>>>0;if(_!==0)throw new Error("Non-zero carry");T=z,S++}for(var W=U-T;W!==U&&v[W]===0;)W++;for(var ze=p.repeat(m);W<U;++W)ze+=t.charAt(v[W]);return ze}s(w,"encode");function x(u){if(typeof u!="string")throw new TypeError("Expected String");if(u.length===0)return new Uint8Array;var m=0;if(u[m]!==" "){for(var T=0,S=0;u[m]===p;)T++,m++;for(var E=(u.length-m)*f+1>>>0,U=new Uint8Array(E);u[m];){var v=r[u.charCodeAt(m)];if(v===255)return;for(var _=0,z=E-1;(v!==0||_<S)&&z!==-1;z--,_++)v+=y*U[z]>>>0,U[z]=v%256>>>0,v=v/256>>>0;if(v!==0)throw new Error("Non-zero carry");S=_,m++}if(u[m]!==" "){for(var M=E-S;M!==E&&U[M]===0;)M++;for(var W=new Uint8Array(T+(E-M)),ze=T;M!==E;)W[ze++]=U[M++];return W}}}s(x,"decodeUnsafe");function O(u){var m=x(u);if(m)return m;throw new Error(`Non-${e} character`)}return s(O,"decode"),{encode:w,decodeUnsafe:x,decode:O}}s(zt,"base");var Ht=zt,Gt=Ht,nt=Gt;var ur=class ur{constructor(e,r,n){c(this,"name");c(this,"prefix");c(this,"baseEncode");this.name=e,this.prefix=r,this.baseEncode=n}encode(e){if(e instanceof Uint8Array)return`${this.prefix}${this.baseEncode(e)}`;throw Error("Unknown type, must be binary type")}};s(ur,"Encoder");var ar=ur,fr=class fr{constructor(e,r,n){c(this,"name");c(this,"prefix");c(this,"baseDecode");c(this,"prefixCodePoint");this.name=e,this.prefix=r;let i=r.codePointAt(0);if(i===void 0)throw new Error("Invalid prefix character");this.prefixCodePoint=i,this.baseDecode=n}decode(e){if(typeof e=="string"){if(e.codePointAt(0)!==this.prefixCodePoint)throw Error(`Unable to decode multibase string ${JSON.stringify(e)}, ${this.name} decoder only supports inputs prefixed with ${this.prefix}`);return this.baseDecode(e.slice(this.prefix.length))}else throw Error("Can only multibase decode strings")}or(e){return it(this,e)}};s(fr,"Decoder");var yr=fr,lr=class lr{constructor(e){c(this,"decoders");this.decoders=e}or(e){return it(this,e)}decode(e){let r=e[0],n=this.decoders[r];if(n!=null)return n.decode(e);throw RangeError(`Unable to decode multibase string ${JSON.stringify(e)}, only inputs prefixed with ${Object.keys(this.decoders)} are supported`)}};s(lr,"ComposedDecoder");var cr=lr;function it(t,e){return new cr({...t.decoders??{[t.prefix]:t},...e.decoders??{[e.prefix]:e}})}s(it,"or");var gr=class gr{constructor(e,r,n,i){c(this,"name");c(this,"prefix");c(this,"baseEncode");c(this,"baseDecode");c(this,"encoder");c(this,"decoder");this.name=e,this.prefix=r,this.baseEncode=n,this.baseDecode=i,this.encoder=new ar(e,r,n),this.decoder=new yr(e,r,i)}encode(e){return this.encoder.encode(e)}decode(e){return this.decoder.decode(e)}};s(gr,"Codec");var pr=gr;function le({name:t,prefix:e,encode:r,decode:n}){return new pr(t,e,r,n)}s(le,"from");function q({name:t,prefix:e,alphabet:r}){let{encode:n,decode:i}=nt(r,t);return le({prefix:e,name:t,encode:n,decode:s(o=>j(i(o)),"decode")})}s(q,"baseX");function Ft(t,e,r,n){let i=t.length;for(;t[i-1]==="=";)--i;let o=new Uint8Array(i*r/8|0),a=0,y=0,p=0;for(let f=0;f<i;++f){let h=e[t[f]];if(h===void 0)throw new SyntaxError(`Non-${n} character`);y=y<<r|h,a+=r,a>=8&&(a-=8,o[p++]=255&y>>a)}if(a>=r||(255&y<<8-a)!==0)throw new SyntaxError("Unexpected end of data");return o}s(Ft,"decode");function Vt(t,e,r){let n=e[e.length-1]==="=",i=(1<<r)-1,o="",a=0,y=0;for(let p=0;p<t.length;++p)for(y=y<<8|t[p],a+=8;a>r;)a-=r,o+=e[i&y>>a];if(a!==0&&(o+=e[i&y<<r-a]),n)for(;(o.length*r&7)!==0;)o+="=";return o}s(Vt,"encode");function Wt(t){let e={};for(let r=0;r<t.length;++r)e[t[r]]=r;return e}s(Wt,"createAlphabetIdx");function b({name:t,prefix:e,bitsPerChar:r,alphabet:n}){let i=Wt(n);return le({prefix:e,name:t,encode(o){return Vt(o,n,r)},decode(o){return Ft(o,i,r,t)}})}s(b,"rfc4648");var jt=q({prefix:"9",name:"base10",alphabet:"0123456789"});var mr={};D(mr,{base16:()=>Xt,base16upper:()=>Yt});var Xt=b({prefix:"f",name:"base16",alphabet:"0123456789abcdef",bitsPerChar:4}),Yt=b({prefix:"F",name:"base16upper",alphabet:"0123456789ABCDEF",bitsPerChar:4});var dr={};D(dr,{base2:()=>$t});var $t=b({prefix:"0",name:"base2",alphabet:"01",bitsPerChar:1});var Ar={};D(Ar,{base256emoji:()=>en});var st=Array.from("\u{1F680}\u{1FA90}\u2604\u{1F6F0}\u{1F30C}\u{1F311}\u{1F312}\u{1F313}\u{1F314}\u{1F315}\u{1F316}\u{1F317}\u{1F318}\u{1F30D}\u{1F30F}\u{1F30E}\u{1F409}\u2600\u{1F4BB}\u{1F5A5}\u{1F4BE}\u{1F4BF}\u{1F602}\u2764\u{1F60D}\u{1F923}\u{1F60A}\u{1F64F}\u{1F495}\u{1F62D}\u{1F618}\u{1F44D}\u{1F605}\u{1F44F}\u{1F601}\u{1F525}\u{1F970}\u{1F494}\u{1F496}\u{1F499}\u{1F622}\u{1F914}\u{1F606}\u{1F644}\u{1F4AA}\u{1F609}\u263A\u{1F44C}\u{1F917}\u{1F49C}\u{1F614}\u{1F60E}\u{1F607}\u{1F339}\u{1F926}\u{1F389}\u{1F49E}\u270C\u2728\u{1F937}\u{1F631}\u{1F60C}\u{1F338}\u{1F64C}\u{1F60B}\u{1F497}\u{1F49A}\u{1F60F}\u{1F49B}\u{1F642}\u{1F493}\u{1F929}\u{1F604}\u{1F600}\u{1F5A4}\u{1F603}\u{1F4AF}\u{1F648}\u{1F447}\u{1F3B6}\u{1F612}\u{1F92D}\u2763\u{1F61C}\u{1F48B}\u{1F440}\u{1F62A}\u{1F611}\u{1F4A5}\u{1F64B}\u{1F61E}\u{1F629}\u{1F621}\u{1F92A}\u{1F44A}\u{1F973}\u{1F625}\u{1F924}\u{1F449}\u{1F483}\u{1F633}\u270B\u{1F61A}\u{1F61D}\u{1F634}\u{1F31F}\u{1F62C}\u{1F643}\u{1F340}\u{1F337}\u{1F63B}\u{1F613}\u2B50\u2705\u{1F97A}\u{1F308}\u{1F608}\u{1F918}\u{1F4A6}\u2714\u{1F623}\u{1F3C3}\u{1F490}\u2639\u{1F38A}\u{1F498}\u{1F620}\u261D\u{1F615}\u{1F33A}\u{1F382}\u{1F33B}\u{1F610}\u{1F595}\u{1F49D}\u{1F64A}\u{1F639}\u{1F5E3}\u{1F4AB}\u{1F480}\u{1F451}\u{1F3B5}\u{1F91E}\u{1F61B}\u{1F534}\u{1F624}\u{1F33C}\u{1F62B}\u26BD\u{1F919}\u2615\u{1F3C6}\u{1F92B}\u{1F448}\u{1F62E}\u{1F646}\u{1F37B}\u{1F343}\u{1F436}\u{1F481}\u{1F632}\u{1F33F}\u{1F9E1}\u{1F381}\u26A1\u{1F31E}\u{1F388}\u274C\u270A\u{1F44B}\u{1F630}\u{1F928}\u{1F636}\u{1F91D}\u{1F6B6}\u{1F4B0}\u{1F353}\u{1F4A2}\u{1F91F}\u{1F641}\u{1F6A8}\u{1F4A8}\u{1F92C}\u2708\u{1F380}\u{1F37A}\u{1F913}\u{1F619}\u{1F49F}\u{1F331}\u{1F616}\u{1F476}\u{1F974}\u25B6\u27A1\u2753\u{1F48E}\u{1F4B8}\u2B07\u{1F628}\u{1F31A}\u{1F98B}\u{1F637}\u{1F57A}\u26A0\u{1F645}\u{1F61F}\u{1F635}\u{1F44E}\u{1F932}\u{1F920}\u{1F927}\u{1F4CC}\u{1F535}\u{1F485}\u{1F9D0}\u{1F43E}\u{1F352}\u{1F617}\u{1F911}\u{1F30A}\u{1F92F}\u{1F437}\u260E\u{1F4A7}\u{1F62F}\u{1F486}\u{1F446}\u{1F3A4}\u{1F647}\u{1F351}\u2744\u{1F334}\u{1F4A3}\u{1F438}\u{1F48C}\u{1F4CD}\u{1F940}\u{1F922}\u{1F445}\u{1F4A1}\u{1F4A9}\u{1F450}\u{1F4F8}\u{1F47B}\u{1F910}\u{1F92E}\u{1F3BC}\u{1F975}\u{1F6A9}\u{1F34E}\u{1F34A}\u{1F47C}\u{1F48D}\u{1F4E3}\u{1F942}"),qt=st.reduce((t,e,r)=>(t[r]=e,t),[]),Jt=st.reduce((t,e,r)=>{let n=e.codePointAt(0);if(n==null)throw new Error(`Invalid character: ${e}`);return t[n]=r,t},[]);function Zt(t){return t.reduce((e,r)=>(e+=qt[r],e),"")}s(Zt,"encode");function Qt(t){let e=[];for(let r of t){let n=r.codePointAt(0);if(n==null)throw new Error(`Invalid character: ${r}`);let i=Jt[n];if(i==null)throw new Error(`Non-base256emoji character: ${r}`);e.push(i)}return new Uint8Array(e)}s(Qt,"decode");var en=le({prefix:"\u{1F680}",name:"base256emoji",encode:Zt,decode:Qt});var br={};D(br,{base32:()=>ge,base32hex:()=>sn,base32hexpad:()=>an,base32hexpadupper:()=>yn,base32hexupper:()=>on,base32pad:()=>tn,base32padupper:()=>nn,base32upper:()=>rn,base32z:()=>cn});var ge=b({prefix:"b",name:"base32",alphabet:"abcdefghijklmnopqrstuvwxyz234567",bitsPerChar:5}),rn=b({prefix:"B",name:"base32upper",alphabet:"ABCDEFGHIJKLMNOPQRSTUVWXYZ234567",bitsPerChar:5}),tn=b({prefix:"c",name:"base32pad",alphabet:"abcdefghijklmnopqrstuvwxyz234567=",bitsPerChar:5}),nn=b({prefix:"C",name:"base32padupper",alphabet:"ABCDEFGHIJKLMNOPQRSTUVWXYZ234567=",bitsPerChar:5}),sn=b({prefix:"v",name:"base32hex",alphabet:"0123456789abcdefghijklmnopqrstuv",bitsPerChar:5}),on=b({prefix:"V",name:"base32hexupper",alphabet:"0123456789ABCDEFGHIJKLMNOPQRSTUV",bitsPerChar:5}),an=b({prefix:"t",name:"base32hexpad",alphabet:"0123456789abcdefghijklmnopqrstuv=",bitsPerChar:5}),yn=b({prefix:"T",name:"base32hexpadupper",alphabet:"0123456789ABCDEFGHIJKLMNOPQRSTUV=",bitsPerChar:5}),cn=b({prefix:"h",name:"base32z",alphabet:"ybndrfg8ejkmcpqxot1uwisza345h769",bitsPerChar:5});var Kr={};D(Kr,{base36:()=>Ce,base36upper:()=>pn});var Ce=q({prefix:"k",name:"base36",alphabet:"0123456789abcdefghijklmnopqrstuvwxyz"}),pn=q({prefix:"K",name:"base36upper",alphabet:"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"});var wr={};D(wr,{base58btc:()=>H,base58flickr:()=>un});var H=q({name:"base58btc",prefix:"z",alphabet:"123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"}),un=q({name:"base58flickr",prefix:"Z",alphabet:"123456789abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ"});var xr={};D(xr,{base64:()=>fn,base64pad:()=>ln,base64url:()=>gn,base64urlpad:()=>hn});var fn=b({prefix:"m",name:"base64",alphabet:"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/",bitsPerChar:6}),ln=b({prefix:"M",name:"base64pad",alphabet:"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",bitsPerChar:6}),gn=b({prefix:"u",name:"base64url",alphabet:"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_",bitsPerChar:6}),hn=b({prefix:"U",name:"base64urlpad",alphabet:"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_=",bitsPerChar:6});var Er={};D(Er,{base8:()=>mn});var mn=b({prefix:"7",name:"base8",alphabet:"01234567",bitsPerChar:3});var Ur={};D(Ur,{identity:()=>dn});var dn=le({prefix:"\0",name:"identity",encode:s(t=>rt(t),"encode"),decode:s(t=>et(t),"decode")});var ss=new TextEncoder,os=new TextDecoder;var Pr={};D(Pr,{identity:()=>zn});var Kn=yt,ot=128,wn=127,xn=~wn,En=Math.pow(2,31);function yt(t,e,r){e=e||[],r=r||0;for(var n=r;t>=En;)e[r++]=t&255|ot,t/=128;for(;t&xn;)e[r++]=t&255|ot,t>>>=7;return e[r]=t|0,yt.bytes=r-n+1,e}s(yt,"encode");var Un=Sr,Sn=128,at=127;function Sr(t,n){var r=0,n=n||0,i=0,o=n,a,y=t.length;do{if(o>=y)throw Sr.bytes=0,new RangeError("Could not decode varint");a=t[o++],r+=i<28?(a&at)<<i:(a&at)*Math.pow(2,i),i+=7}while(a>=Sn);return Sr.bytes=o-n,r}s(Sr,"read");var Cn=Math.pow(2,7),Pn=Math.pow(2,14),Bn=Math.pow(2,21),vn=Math.pow(2,28),Tn=Math.pow(2,35),Dn=Math.pow(2,42),In=Math.pow(2,49),_n=Math.pow(2,56),Nn=Math.pow(2,63),Ln=s(function(t){return t<Cn?1:t<Pn?2:t<Bn?3:t<vn?4:t<Tn?5:t<Dn?6:t<In?7:t<_n?8:t<Nn?9:10},"length"),kn={encode:Kn,decode:Un,encodingLength:Ln},Mn=kn,Pe=Mn;function Be(t,e=0){return[Pe.decode(t,e),Pe.decode.bytes]}s(Be,"decode");function he(t,e,r=0){return Pe.encode(t,e,r),e}s(he,"encodeTo");function me(t){return Pe.encodingLength(t)}s(me,"encodingLength");function Ae(t,e){let r=e.byteLength,n=me(t),i=n+me(r),o=new Uint8Array(i+r);return he(t,o,0),he(r,o,n),o.set(e,i),new de(t,r,e,o)}s(Ae,"create");function ct(t){let e=j(t),[r,n]=Be(e),[i,o]=Be(e.subarray(n)),a=e.subarray(n+o);if(a.byteLength!==i)throw new Error("Incorrect length");return new de(r,i,a,e)}s(ct,"decode");function pt(t,e){if(t===e)return!0;{let r=e;return t.code===r.code&&t.size===r.size&&r.bytes instanceof Uint8Array&&Qr(t.bytes,r.bytes)}}s(pt,"equals");var Cr=class Cr{constructor(e,r,n,i){c(this,"code");c(this,"size");c(this,"digest");c(this,"bytes");this.code=e,this.size=r,this.digest=n,this.bytes=i}};s(Cr,"Digest");var de=Cr;var ut=0,Rn="identity",ft=j;function On(t,e){if(e?.truncate!=null&&e.truncate!==t.byteLength){if(e.truncate<0||e.truncate>t.byteLength)throw new Error(`Invalid truncate option, must be less than or equal to ${t.byteLength}`);t=t.subarray(0,e.truncate)}return Ae(ut,ft(t))}s(On,"digest");var zn={code:ut,name:Rn,encode:ft,digest:On};var Dr={};D(Dr,{sha256:()=>Gn,sha512:()=>Fn});var Hn=20;function vr({name:t,code:e,encode:r,minDigestLength:n,maxDigestLength:i}){return new Br(t,e,r,n,i)}s(vr,"from");var Tr=class Tr{constructor(e,r,n,i,o){c(this,"name");c(this,"code");c(this,"encode");c(this,"minDigestLength");c(this,"maxDigestLength");this.name=e,this.code=r,this.encode=n,this.minDigestLength=i??Hn,this.maxDigestLength=o}digest(e,r){if(r?.truncate!=null){if(r.truncate<this.minDigestLength)throw new Error(`Invalid truncate option, must be greater than or equal to ${this.minDigestLength}`);if(this.maxDigestLength!=null&&r.truncate>this.maxDigestLength)throw new Error(`Invalid truncate option, must be less than or equal to ${this.maxDigestLength}`)}if(e instanceof Uint8Array){let n=this.encode(e);return n instanceof Uint8Array?lt(n,this.code,r?.truncate):n.then(i=>lt(i,this.code,r?.truncate))}else throw Error("Unknown type, must be binary type")}};s(Tr,"Hasher");var Br=Tr;function lt(t,e,r){if(r!=null&&r!==t.byteLength){if(r>t.byteLength)throw new Error(`Invalid truncate option, must be less than or equal to ${t.byteLength}`);t=t.subarray(0,r)}return Ae(e,t)}s(lt,"createDigest");function ht(t){return async e=>new Uint8Array(await crypto.subtle.digest(t,e))}s(ht,"sha");var Gn=vr({name:"sha2-256",code:18,encode:ht("SHA-256")}),Fn=vr({name:"sha2-512",code:19,encode:ht("SHA-512")});function mt(t,e){let{bytes:r,version:n}=t;return n===0?Wn(r,Ir(t),e??H.encoder):jn(r,Ir(t),e??ge.encoder)}s(mt,"format");var dt=new WeakMap;function Ir(t){let e=dt.get(t);if(e==null){let r=new Map;return dt.set(t,r),r}return e}s(Ir,"baseCache");var bt,C=class C{constructor(e,r,n,i){c(this,"code");c(this,"version");c(this,"multihash");c(this,"bytes");c(this,"/");c(this,bt,"CID");this.code=r,this.version=e,this.multihash=n,this.bytes=i,this["/"]=i}get asCID(){return this}get byteOffset(){return this.bytes.byteOffset}get byteLength(){return this.bytes.byteLength}toV0(){switch(this.version){case 0:return this;case 1:{let{code:e,multihash:r}=this;if(e!==Te)throw new Error("Cannot convert a non dag-pb CID to CIDv0");if(r.code!==Xn)throw new Error("Cannot convert non sha2-256 multihash CID to CIDv0");return C.createV0(r)}default:throw Error(`Can not convert CID version ${this.version} to version 0. This is a bug please report`)}}toV1(){switch(this.version){case 0:{let{code:e,digest:r}=this.multihash,n=Ae(e,r);return C.createV1(this.code,n)}case 1:return this;default:throw Error(`Can not convert CID version ${this.version} to version 1. This is a bug please report`)}}equals(e){return C.equals(this,e)}static equals(e,r){let n=r;return n!=null&&e.code===n.code&&e.version===n.version&&pt(e.multihash,n.multihash)}toString(e){return mt(this,e)}toJSON(){return{"/":mt(this)}}link(){return this}[(bt=Symbol.toStringTag,Symbol.for("nodejs.util.inspect.custom"))](){return`CID(${this.toString()})`}static asCID(e){if(e==null)return null;let r=e;if(r instanceof C)return r;if(r["/"]!=null&&r["/"]===r.bytes||r.asCID===r){let{version:n,code:i,multihash:o,bytes:a}=r;return new C(n,i,o,a??At(n,i,o.bytes))}else if(r[Yn]===!0){let{version:n,multihash:i,code:o}=r,a=ct(i);return C.create(n,o,a)}else return null}static create(e,r,n){if(typeof r!="number")throw new Error("String codecs are no longer supported");if(!(n.bytes instanceof Uint8Array))throw new Error("Invalid digest");switch(e){case 0:{if(r!==Te)throw new Error(`Version 0 CID must use dag-pb (code: ${Te}) block encoding`);return new C(e,r,n,n.bytes)}case 1:{let i=At(e,r,n.bytes);return new C(e,r,n,i)}default:throw new Error("Invalid version")}}static createV0(e){return C.create(0,Te,e)}static createV1(e,r){return C.create(1,e,r)}static decode(e){let[r,n]=C.decodeFirst(e);if(n.length!==0)throw new Error("Incorrect length");return r}static decodeFirst(e){let r=C.inspectBytes(e),n=r.size-r.multihashSize,i=j(e.subarray(n,n+r.multihashSize));if(i.byteLength!==r.multihashSize)throw new Error("Incorrect length");let o=i.subarray(r.multihashSize-r.digestSize),a=new de(r.multihashCode,r.digestSize,o,i);return[r.version===0?C.createV0(a):C.createV1(r.codec,a),e.subarray(r.size)]}static inspectBytes(e){let r=0,n=s(()=>{let[w,x]=Be(e.subarray(r));return r+=x,w},"next"),i=n(),o=Te;if(i===18?(i=0,r=0):o=n(),i!==0&&i!==1)throw new RangeError(`Invalid CID version ${i}`);let a=r,y=n(),p=n(),f=r+p,h=f-a;return{version:i,codec:o,multihashCode:y,digestSize:p,multihashSize:h,size:f}}static parse(e,r){let[n,i]=Vn(e,r),o=C.decode(i);if(o.version===0&&e[0]!=="Q")throw Error("Version 0 CID string must not include multibase prefix");return Ir(o).set(n,e),o}};s(C,"CID");var Ge=C;function Vn(t,e){switch(t[0]){case"Q":{let r=e??H;return[H.prefix,r.decode(`${H.prefix}${t}`)]}case H.prefix:{let r=e??H;return[H.prefix,r.decode(t)]}case ge.prefix:{let r=e??ge;return[ge.prefix,r.decode(t)]}case Ce.prefix:{let r=e??Ce;return[Ce.prefix,r.decode(t)]}default:{if(e==null)throw Error("To parse non base32, base36 or base58btc encoded CID multibase decoder must be provided");return[t[0],e.decode(t)]}}}s(Vn,"parseCIDtoBytes");function Wn(t,e,r){let{prefix:n}=r;if(n!==H.prefix)throw Error(`Cannot string encode V0 in ${r.name} encoding`);let i=e.get(n);if(i==null){let o=r.encode(t).slice(1);return e.set(n,o),o}else return i}s(Wn,"toStringV0");function jn(t,e,r){let{prefix:n}=r,i=e.get(n);if(i==null){let o=r.encode(t);return e.set(n,o),o}else return i}s(jn,"toStringV1");var Te=112,Xn=18;function At(t,e,r){let n=me(t),i=n+me(e),o=new Uint8Array(i+r.byteLength);return he(t,o,0),he(e,o,n),o.set(r,i),o}s(At,"encodeCID");var Yn=Symbol.for("@ipld/js-cid/CID");var _r={...Ur,...dr,...Er,...hr,...mr,...br,...Kr,...wr,...xr,...Ar},Ms={...Dr,...Pr};function wt(t,e,r,n){return{name:t,prefix:e,encoder:{name:t,prefix:e,encode:r},decoder:{decode:n}}}s(wt,"createCodec");var Kt=wt("utf8","u",t=>"u"+new TextDecoder("utf8").decode(t),t=>new TextEncoder().encode(t.substring(1))),Nr=wt("ascii","a",t=>{let e="a";for(let r=0;r<t.length;r++)e+=String.fromCharCode(t[r]);return e},t=>{t=t.substring(1);let e=Se(t.length);for(let r=0;r<t.length;r++)e[r]=t.charCodeAt(r);return e}),$n={utf8:Kt,"utf-8":Kt,hex:_r.base16,latin1:Nr,ascii:Nr,binary:Nr,..._r},Fe=$n;function K(t,e="utf8"){let r=Fe[e];if(r==null)throw new Error(`Unsupported encoding "${e}"`);return r.decoder.decode(`${r.prefix}${t}`)}s(K,"fromString");function l(t,e="utf8"){let r=Fe[e];if(r==null)throw new Error(`Unsupported encoding "${e}"`);return r.encoder.encode(t).substring(1)}s(l,"toString");var Bt={};D(Bt,{AES:()=>d,decryptBytes:()=>Pt,importAesKey:()=>we});var X="did:key:z",oe="Ed25519",De="X25519",qn="X25519",G=32,xt={sign:["sign","verify"],encrypt:["deriveKey","deriveBits"]},J="RSA-OAEP",Z="RSASSA-PKCS1-v1_5",Ve="SHA-256",We=new Uint8Array([0,245,2]),je=2048,ae="SHA-256",F=8,V="AES-GCM",L=V,Q=256,I=12,Lr=16,Xe=new Uint8Array([234,1]),Ye=new Uint8Array([237,1]),Ie={RSA:"rsa",Edwards:"ed25519",BLS:"bls12-381"},kr="rsa-exchange-key",Mr="rsa-write-key",Rr="ecc-exchange",Or="ecc-write",B=16,$e=1;var _e=globalThis.crypto,Et=new Error("Invalid key use. Please use 'encryption' or 'signing"),Jn=new Error("Max must be less than 256 and greater than 0");function g(t){return new Uint8Array(t).buffer}s(g,"toArrayBuffer");async function Ut(t){let e=t.buffer.slice(t.byteOffset,t.byteOffset+t.byteLength);return new Uint8Array(await _e.subtle.digest("sha-256",e))}s(Ut,"sha256");function N(t){let e=K(t,"base64pad");return g(e)}s(N,"base64ToArrBuf");var ye=s((t,e)=>{if(typeof t=="string")return e(t);if(typeof t=="object"&&t.byteLength!==void 0)return new Uint8Array(t).buffer;throw new Error("Improper value. Must be a string, ArrayBuffer, Uint8Array")},"normalizeToBuf");function qe(t){return ye(t,N)}s(qe,"normalizeBase64ToBuf");var Zn=s(t=>ye(t,e=>g(new TextEncoder().encode(e))),"normalizeUtf8ToBuf");function Qn(t,e){let r=e===8?new Uint8Array(t.length):new Uint16Array(t.length);for(let n=0,i=t.length;n<i;n++)r[n]=t.charCodeAt(n);return r.buffer}s(Qn,"strToArrBuf");var ei=s(t=>ye(t,e=>Qn(e,16)),"normalizeUtf16ToBuf");function ee(t,e){return e===8?Zn(t):ei(t)}s(ee,"normalizeUnicodeToBuf");function Je(t){if(!t.startsWith(X))throw new Error("Please use a base58-encoded DID formatted `did:key:z...`");let e=""+t.substring(X.length),r=K(e,"base58btc"),{keyBuffer:n,type:i}=ri(g(r));return{publicKey:new Uint8Array(n),type:i}}s(Je,"didToPublicKey");function ri(t){if(be(t,g(We)))return{keyBuffer:t.slice(We.byteLength),type:Ie.RSA};if(be(t,g(Ye)))return{keyBuffer:t.slice(Ye.byteLength),type:Ie.Edwards};if(be(t,g(Xe)))return{keyBuffer:t.slice(Xe.byteLength),type:Ie.BLS};throw new Error("Unsupported key algorithm. Try using RSA.")}s(ri,"parseMagicBytes");var ti={equal:s((t,e)=>{let r=new Uint8Array(t),n=new Uint8Array(e);if(r.length!==n.length)return!1;for(let i=0;i<r.length;i++)if(r[i]!==n[i])return!1;return!0},"equal")};function be(t,e){return ti.equal(e,t.slice(0,e.byteLength))}s(be,"hasPrefix");function P(t){return l(t instanceof ArrayBuffer?new Uint8Array(t):t,"base64pad")}s(P,"toBase64");async function Ne(t,e){let r=typeof t=="string"?N(t):g(t);return _e.subtle.importKey("raw",r,{name:e?.alg||L,length:e?.length||Q},e?.extractable??!0,["encrypt","decrypt"])}s(Ne,"importKey");function Ze(t,{max:e}={max:255}){if(e<1||e>255)throw Jn;let r=new Uint8Array(t);if(e===255)return _e.getRandomValues(r),r.buffer;let n=0,i=e+1,o=Math.floor(256/i)*i,a=new Uint8Array(1);for(;n<r.length;)_e.getRandomValues(a),a[0]<o&&(r[n]=a[0]%i,n++);return r.buffer}s(Ze,"randomBuf");function Y(t,e){let r=t instanceof ArrayBuffer?new Uint8Array(t):new Uint8Array(g(t)),n=e instanceof ArrayBuffer?new Uint8Array(e):new Uint8Array(g(e)),i=new Uint8Array(r.length+n.length);return i.set(r),i.set(n,r.length),i.buffer}s(Y,"joinBufs");async function zr(t,e,r,n=!1){let i=n;if(!Object.values(se).includes(r))throw new Error("invalid key use");let o=r==="exchange"?J:Z,a=r==="exchange"?["encrypt","decrypt"]:["sign","verify"];return _e.subtle.generateKey({name:o,modulusLength:t,publicExponent:ni(),hash:{name:e}},i,a)}s(zr,"makeRSAKeypair");function ni(){return new Uint8Array([1,0,1])}s(ni,"publicExponent");var R=globalThis.crypto,Qe={keyTypes:{"bls12-381":{magicBytes:new Uint8Array([234,1]),verify:s(()=>{throw new Error("Not implemented")},"verify")},rsa:{magicBytes:new Uint8Array([0,245,2]),verify:Ct},ed25519:{magicBytes:new Uint8Array([237,1]),verify:St}}};async function mo({message:t,did:e,signature:r}){let n=ii(e),i=er(e).publicKey;if(n==="ed25519")return St({message:t,publicKey:i,signature:r});if(n==="rsa")return Ct({message:t,publicKey:i,signature:r});throw new Error("Unknown key format")}s(mo,"verify");function ii(t){try{let{type:e}=er(t);return e}catch{return"unknown"}}s(ii,"getAlgorithm");async function St({message:t,publicKey:e,signature:r}){let n=t;typeof t=="string"&&(n=K(t,"utf8"));let i=r;typeof r=="string"&&(i=K(r,"base64pad"));let o=e;if(typeof e=="string"&&(o=er(e).publicKey),o.length!==32)throw new Error(`Invalid Ed25519 public key size: ${o.length} bytes, expected 32`);if(i.length!==64)throw new Error(`Invalid Ed25519 signature size: ${i.length} bytes, expected 64`);let a=await R.subtle.importKey("raw",g(o),{name:"Ed25519"},!1,["verify"]);return R.subtle.verify({name:"Ed25519"},a,g(i),g(n))}s(St,"ed25519Verify");async function Ct({message:t,publicKey:e,signature:r}){let n=e;return typeof e=="string"&&(n=er(e).publicKey),$.verify(t,r,await R.subtle.importKey("spki",g(n),{name:Z,hash:Ve},!1,["verify"]),8)}s(Ct,"rsaVerify");var $={verify:s(async function(e,r,n,i=F,o=ae){return R.subtle.verify({name:Z},typeof n=="string"?await Le(n,o,"signing"):n,qe(r),ee(e,i))},"rsaVerify"),sign:s(async function(e,r,n=F){return R.subtle.sign({name:Z},r,ee(e,n))},"sign"),encrypt:s(async function(e,r,n=F,i=ae){let o;return typeof r=="string"?o=await Le(r,i,"exchange"):o=r instanceof Uint8Array?await Le(g(r),i,"exchange"):r,R.subtle.encrypt({name:J},o,ee(e,n))},"rsaEncrypt"),decrypt:s(async function(e,r){let n=Ke(r)?r:await yi(r,["decrypt"]),i;typeof e=="string"?i=K(e,"base64pad"):i=e;let o=await R.subtle.decrypt({name:J},n,i instanceof Uint8Array?g(i):i);return new Uint8Array(o)},"rsaDecrypt")};async function Le(t,e,r){si(r);let n=r==="exchange"?J:Z,i=r==="exchange"?["encrypt"]:["verify"],o=typeof t=="string"?N(ai(t)):t;return R.subtle.importKey("spki",o,{name:n,hash:{name:e}},!0,i)}s(Le,"rsaImportPublicKey");function si(t){oi(t,["signing","exchange"],Et)}s(si,"checkValidKeyUse");function oi(t,e,r){if(!e.some(i=>i===t))throw r}s(oi,"checkValid");function ai(t){return t.replace(`-----BEGIN PUBLIC KEY-----
|
|
1
|
+
var or=Object.defineProperty;var Wt=(t,e,r)=>e in t?or(t,e,{enumerable:!0,configurable:!0,writable:!0,value:r}):t[e]=r;var s=(t,e)=>or(t,"name",{value:e,configurable:!0});var v=(t,e)=>{for(var r in e)or(t,r,{get:e[r],enumerable:!0})};var c=(t,e,r)=>Wt(t,typeof e!="symbol"?e+"":e,r);var rt=(n=>(n[n.B1024=1024]="B1024",n[n.B2048=2048]="B2048",n[n.B4096=4096]="B4096",n))(rt||{}),tt=(n=>(n[n.B128=128]="B128",n[n.B192=192]="B192",n[n.B256=256]="B256",n))(tt||{}),nt=(r=>(r.ECC="ecc",r.RSA="rsa",r))(nt||{}),ar=(i=>(i.SHA_1="SHA-1",i.SHA_256="SHA-256",i.SHA_384="SHA-384",i.SHA_512="SHA-512",i))(ar||{}),yr=(i=>(i.P_256="P-256",i.P_384="P-384",i.P_521="P-521",i.X25519="X25519",i))(yr||{}),ye=(n=>(n.Sign="signing",n.Exchange="exchange",n.Write="write",n))(ye||{}),cr=(r=>(r[r.B8=8]="B8",r[r.B16=16]="B16",r))(cr||{});var Ot={};v(Ot,{AES_GCM:()=>W,AES_GCM_TAG_LENGTH:()=>zr,BASE58_DID_PREFIX:()=>Y,BLS_DID_PREFIX:()=>Je,DEFAULT_CHAR_SIZE:()=>V,DEFAULT_ECC_EXCHANGE:()=>Fr,DEFAULT_ECC_EXCHANGE_CURVE:()=>ii,DEFAULT_ECC_WRITE:()=>Vr,DEFAULT_HASH_ALGORITHM:()=>ue,DEFAULT_RSA_EXCHANGE:()=>Hr,DEFAULT_RSA_SIZE:()=>qe,DEFAULT_RSA_WRITE:()=>Gr,DEFAULT_SYMM_ALGORITHM:()=>L,DEFAULT_SYMM_LENGTH:()=>re,ECC_EXCHANGE_ALGORITHM:()=>ke,ECC_WRITE_ALGORITHM:()=>pe,EDWARDS_DID_PREFIX:()=>Ze,IV_LENGTH:()=>B,KEY_TYPE:()=>Me,KEY_USE:()=>vt,RSA_ALGORITHM:()=>Q,RSA_DID_PREFIX:()=>$e,RSA_HASHING_ALGORITHM:()=>Ye,RSA_SIGN_ALGORITHM:()=>ee,RsaKeys:()=>he,SALT_LENGTH:()=>C,WRAP_FORMAT_VERSION:()=>Qe,X25519_PUBLIC_KEY_LENGTH:()=>F,encrypt:()=>Si,encryptKeyTo:()=>Be,encryptTo:()=>Rt,getPublicKeyAsArrayBuffer:()=>ne,getPublicKeyAsUint8Array:()=>Xr,publicKeyToDid:()=>He,verify:()=>Ui});function de(t=0){return new Uint8Array(t)}s(de,"allocUnsafe");function jt(t){return t?.buffer instanceof ArrayBuffer}s(jt,"isByteArrayWithArrayBuffer");function pr(t){if(jt(t))return t;let e=t.slice();return new Uint8Array(e.buffer,0,e.byteLength)}s(pr,"asUint8Array");function ur(t,e){e==null&&(e=t.reduce((i,o)=>i+o.length,0));let r=de(e),n=0;for(let i of t)r.set(i,n),n+=i.length;return pr(r)}s(ur,"concat");var wr={};v(wr,{base10:()=>en});var qi=new Uint8Array(0);function st(t,e){if(t===e)return!0;if(t.byteLength!==e.byteLength)return!1;for(let r=0;r<t.byteLength;r++)if(t[r]!==e[r])return!1;return!0}s(st,"equals");function O(t){if(t instanceof Uint8Array&&t.constructor.name==="Uint8Array")return ce(t);if(t instanceof ArrayBuffer)return new Uint8Array(t);if(ArrayBuffer.isView(t))return ce(new Uint8Array(t.buffer,t.byteOffset,t.byteLength));throw new Error("Unknown type, must be binary type")}s(O,"coerce");function ot(t){let e=new Uint8Array(t.length);for(let r=0;r<t.length;r++)e[r]=t.charCodeAt(r);return e}s(ot,"fromString");var it=4096;function at(t){let e=t.length;if(e<=it)return String.fromCharCode.apply(String,t);let r="",n=0;for(;n<e;)r+=String.fromCharCode.apply(String,t.subarray(n,n+=it));return r}s(at,"toString");function Xt(t){return t?.buffer instanceof ArrayBuffer}s(Xt,"isByteArrayWithArrayBuffer");function ce(t){return Xt(t)?t:t.slice()}s(ce,"toArrayBufferBackedArray");function Yt(t,e,r){if(t.length>=255)throw new TypeError("Alphabet too long");for(var n=new Uint8Array(256),i=0;i<n.length;i++)n[i]=255;for(var o=0;o<t.length;o++){var a=t.charAt(o),y=a.charCodeAt(0);if(n[y]!==255)throw new TypeError(a+" is ambiguous");if(n[y]=o,r){var p=a.toLowerCase().charCodeAt(0),u=a.toUpperCase().charCodeAt(0);p!==y&&(n[p]=o),u!==y&&(n[u]=o)}}var l=t.length,b=t.charAt(0),x=Math.log(l)/Math.log(256),H=Math.log(256)/Math.log(l);function M(f){if(f instanceof Uint8Array||(ArrayBuffer.isView(f)?f=new Uint8Array(f.buffer,f.byteOffset,f.byteLength):Array.isArray(f)&&(f=Uint8Array.from(f))),!(f instanceof Uint8Array))throw new TypeError("Expected Uint8Array");if(f.length===0)return"";for(var m=0,S=0,P=0,T=f.length;P!==T&&f[P]===0;)P++,m++;for(var R=(T-P)*H+1>>>0,J=new Uint8Array(R);P!==T;){for(var I=f[P],ae=0,X=R-1;(I!==0||ae<S)&&X!==-1;X--,ae++)I+=256*J[X]>>>0,J[X]=I%l>>>0,I=I/l>>>0;if(I!==0)throw new Error("Non-zero carry");S=ae,P++}for(var N=R-S;N!==R&&J[N]===0;)N++;for(var Te=b.repeat(m);N<R;++N)Te+=t.charAt(J[N]);return Te}s(M,"encode");function _(f){if(typeof f!="string")throw new TypeError("Expected String");if(f.length===0)return new Uint8Array;var m=0;if(f[m]!==" "){for(var S=0,P=0;f[m]===b;)S++,m++;for(var T=(f.length-m)*x+1>>>0,R=new Uint8Array(T);f[m];){var J=f.charCodeAt(m);if(J>255)return;var I=n[J];if(I===255)return;for(var ae=0,X=T-1;(I!==0||ae<P)&&X!==-1;X--,ae++)I+=l*R[X]>>>0,R[X]=I%256>>>0,I=I/256>>>0;if(I!==0)throw new Error("Non-zero carry");P=ae,m++}if(f[m]!==" "){for(var N=T-P;N!==T&&R[N]===0;)N++;for(var Te=new Uint8Array(S+(T-N)),Vt=S;N!==T;)Te[Vt++]=R[N++];return Te}}}s(_,"decodeUnsafe");function j(f){var m=_(f);if(m)return m;throw new Error(`Non-${e} character`)}return s(j,"decode"),{encode:M,decodeUnsafe:_,decode:j}}s(Yt,"base");var $t=Yt,qt=$t,ct=qt;var mr=class mr{constructor(e,r,n){c(this,"name");c(this,"prefix");c(this,"baseEncode");this.name=e,this.prefix=r,this.baseEncode=n}encode(e){if(e instanceof Uint8Array)return`${this.prefix}${this.baseEncode(e)}`;throw Error("Unknown type, must be binary type")}};s(mr,"Encoder");var fr=mr,dr=class dr{constructor(e,r,n){c(this,"name");c(this,"prefix");c(this,"baseDecode");c(this,"prefixCodePoint");this.name=e,this.prefix=r;let i=r.codePointAt(0);if(i===void 0)throw new Error("Invalid prefix character");this.prefixCodePoint=i,this.baseDecode=n}decode(e){if(typeof e=="string"){if(e.codePointAt(0)!==this.prefixCodePoint)throw Error(`Unable to decode multibase string ${JSON.stringify(e)}, ${this.name} decoder only supports inputs prefixed with ${this.prefix}`);return this.baseDecode(e.slice(this.prefix.length))}else throw Error("Can only multibase decode strings")}or(e){return pt(this,e)}};s(dr,"Decoder");var lr=dr,Ar=class Ar{constructor(e){c(this,"decoders");this.decoders=e}or(e){return pt(this,e)}decode(e){let r=e[0],n=this.decoders[r];if(n!=null)return n.decode(e);throw RangeError(`Unable to decode multibase string ${JSON.stringify(e)}, only inputs prefixed with ${Object.keys(this.decoders)} are supported`)}};s(Ar,"ComposedDecoder");var gr=Ar;function pt(t,e){return new gr({...t.decoders??{[t.prefix]:t},...e.decoders??{[e.prefix]:e}})}s(pt,"or");var br=class br{constructor(e,r,n,i){c(this,"name");c(this,"prefix");c(this,"baseEncode");c(this,"baseDecode");c(this,"encoder");c(this,"decoder");this.name=e,this.prefix=r,this.baseEncode=n,this.baseDecode=i,this.encoder=new fr(e,r,n),this.decoder=new lr(e,r,i)}encode(e){return this.encoder.encode(e)}decode(e){return this.decoder.decode(e)}};s(br,"Codec");var hr=br;function Ae({name:t,prefix:e,encode:r,decode:n}){return new hr(t,e,r,n)}s(Ae,"from");function Z({name:t,prefix:e,alphabet:r,caseInsensitive:n=!1}){let{encode:i,decode:o}=ct(r,t,n);return Ae({prefix:e,name:t,encode:i,decode:s(a=>O(o(a)),"decode")})}s(Z,"baseX");function Jt(t,e,r,n){let i=t.length;for(;t[i-1]==="=";)--i;let o=new Uint8Array(i*r/8|0),a=0,y=0,p=0;for(let u=0;u<i;++u){let l=e[t[u]];if(l===void 0)throw new SyntaxError(`Non-${n} character`);y=y<<r|l,a+=r,a>=8&&(a-=8,o[p++]=255&y>>a)}if(a>=r||(255&y<<8-a)!==0)throw new SyntaxError("Unexpected end of data");return o}s(Jt,"decode");function Zt(t,e,r){let n=e[e.length-1]==="=",i=(1<<r)-1,o="",a=0,y=0;for(let p=0;p<t.length;++p)for(y=y<<8|t[p],a+=8;a>r;)a-=r,o+=e[i&y>>a];if(a!==0&&(o+=e[i&y<<r-a]),n)for(;(o.length*r&7)!==0;)o+="=";return o}s(Zt,"encode");function Qt(t,e){let r={};for(let n=0;n<t.length;++n)if(t[n]!=="="&&(r[t[n]]=n,e)){let i=t[n].toLowerCase(),o=t[n].toUpperCase();i!==t[n]&&(r[i]=n),o!==t[n]&&(r[o]=n)}return r}s(Qt,"createAlphabetIdx");function w({name:t,prefix:e,bitsPerChar:r,alphabet:n,caseInsensitive:i=!1}){let o=Qt(n,i);return Ae({prefix:e,name:t,encode(a){return Zt(a,n,r)},decode(a){return Jt(a,o,r,t)}})}s(w,"rfc4648");var en=Z({prefix:"9",name:"base10",alphabet:"0123456789"});var Kr={};v(Kr,{base16:()=>rn,base16upper:()=>tn});var rn=w({prefix:"f",name:"base16",alphabet:"0123456789abcdef",bitsPerChar:4,caseInsensitive:!0}),tn=w({prefix:"F",name:"base16upper",alphabet:"0123456789ABCDEF",bitsPerChar:4,caseInsensitive:!0});var xr={};v(xr,{base2:()=>nn});var nn=w({prefix:"0",name:"base2",alphabet:"01",bitsPerChar:1});var Er={};v(Er,{base256emoji:()=>cn});var ut=Array.from("\u{1F680}\u{1FA90}\u2604\u{1F6F0}\u{1F30C}\u{1F311}\u{1F312}\u{1F313}\u{1F314}\u{1F315}\u{1F316}\u{1F317}\u{1F318}\u{1F30D}\u{1F30F}\u{1F30E}\u{1F409}\u2600\u{1F4BB}\u{1F5A5}\u{1F4BE}\u{1F4BF}\u{1F602}\u2764\u{1F60D}\u{1F923}\u{1F60A}\u{1F64F}\u{1F495}\u{1F62D}\u{1F618}\u{1F44D}\u{1F605}\u{1F44F}\u{1F601}\u{1F525}\u{1F970}\u{1F494}\u{1F496}\u{1F499}\u{1F622}\u{1F914}\u{1F606}\u{1F644}\u{1F4AA}\u{1F609}\u263A\u{1F44C}\u{1F917}\u{1F49C}\u{1F614}\u{1F60E}\u{1F607}\u{1F339}\u{1F926}\u{1F389}\u{1F49E}\u270C\u2728\u{1F937}\u{1F631}\u{1F60C}\u{1F338}\u{1F64C}\u{1F60B}\u{1F497}\u{1F49A}\u{1F60F}\u{1F49B}\u{1F642}\u{1F493}\u{1F929}\u{1F604}\u{1F600}\u{1F5A4}\u{1F603}\u{1F4AF}\u{1F648}\u{1F447}\u{1F3B6}\u{1F612}\u{1F92D}\u2763\u{1F61C}\u{1F48B}\u{1F440}\u{1F62A}\u{1F611}\u{1F4A5}\u{1F64B}\u{1F61E}\u{1F629}\u{1F621}\u{1F92A}\u{1F44A}\u{1F973}\u{1F625}\u{1F924}\u{1F449}\u{1F483}\u{1F633}\u270B\u{1F61A}\u{1F61D}\u{1F634}\u{1F31F}\u{1F62C}\u{1F643}\u{1F340}\u{1F337}\u{1F63B}\u{1F613}\u2B50\u2705\u{1F97A}\u{1F308}\u{1F608}\u{1F918}\u{1F4A6}\u2714\u{1F623}\u{1F3C3}\u{1F490}\u2639\u{1F38A}\u{1F498}\u{1F620}\u261D\u{1F615}\u{1F33A}\u{1F382}\u{1F33B}\u{1F610}\u{1F595}\u{1F49D}\u{1F64A}\u{1F639}\u{1F5E3}\u{1F4AB}\u{1F480}\u{1F451}\u{1F3B5}\u{1F91E}\u{1F61B}\u{1F534}\u{1F624}\u{1F33C}\u{1F62B}\u26BD\u{1F919}\u2615\u{1F3C6}\u{1F92B}\u{1F448}\u{1F62E}\u{1F646}\u{1F37B}\u{1F343}\u{1F436}\u{1F481}\u{1F632}\u{1F33F}\u{1F9E1}\u{1F381}\u26A1\u{1F31E}\u{1F388}\u274C\u270A\u{1F44B}\u{1F630}\u{1F928}\u{1F636}\u{1F91D}\u{1F6B6}\u{1F4B0}\u{1F353}\u{1F4A2}\u{1F91F}\u{1F641}\u{1F6A8}\u{1F4A8}\u{1F92C}\u2708\u{1F380}\u{1F37A}\u{1F913}\u{1F619}\u{1F49F}\u{1F331}\u{1F616}\u{1F476}\u{1F974}\u25B6\u27A1\u2753\u{1F48E}\u{1F4B8}\u2B07\u{1F628}\u{1F31A}\u{1F98B}\u{1F637}\u{1F57A}\u26A0\u{1F645}\u{1F61F}\u{1F635}\u{1F44E}\u{1F932}\u{1F920}\u{1F927}\u{1F4CC}\u{1F535}\u{1F485}\u{1F9D0}\u{1F43E}\u{1F352}\u{1F617}\u{1F911}\u{1F30A}\u{1F92F}\u{1F437}\u260E\u{1F4A7}\u{1F62F}\u{1F486}\u{1F446}\u{1F3A4}\u{1F647}\u{1F351}\u2744\u{1F334}\u{1F4A3}\u{1F438}\u{1F48C}\u{1F4CD}\u{1F940}\u{1F922}\u{1F445}\u{1F4A1}\u{1F4A9}\u{1F450}\u{1F4F8}\u{1F47B}\u{1F910}\u{1F92E}\u{1F3BC}\u{1F975}\u{1F6A9}\u{1F34E}\u{1F34A}\u{1F47C}\u{1F48D}\u{1F4E3}\u{1F942}"),sn=ut.reduce((t,e,r)=>(t[r]=e,t),[]),on=ut.reduce((t,e,r)=>{let n=e.codePointAt(0);if(n==null)throw new Error(`Invalid character: ${e}`);return t[n]=r,t},[]);function an(t){return t.reduce((e,r)=>(e+=sn[r],e),"")}s(an,"encode");function yn(t){let e=[];for(let r of t){let n=r.codePointAt(0);if(n==null)throw new Error(`Invalid character: ${r}`);let i=on[n];if(i==null)throw new Error(`Non-base256emoji character: ${r}`);e.push(i)}return new Uint8Array(e)}s(yn,"decode");var cn=Ae({prefix:"\u{1F680}",name:"base256emoji",encode:an,decode:yn});var Ur={};v(Ur,{base32:()=>be,base32hex:()=>ln,base32hexpad:()=>hn,base32hexpadupper:()=>mn,base32hexupper:()=>gn,base32pad:()=>un,base32padupper:()=>fn,base32upper:()=>pn,base32z:()=>dn});var be=w({prefix:"b",name:"base32",alphabet:"abcdefghijklmnopqrstuvwxyz234567",bitsPerChar:5,caseInsensitive:!0}),pn=w({prefix:"B",name:"base32upper",alphabet:"ABCDEFGHIJKLMNOPQRSTUVWXYZ234567",bitsPerChar:5,caseInsensitive:!0}),un=w({prefix:"c",name:"base32pad",alphabet:"abcdefghijklmnopqrstuvwxyz234567=",bitsPerChar:5,caseInsensitive:!0}),fn=w({prefix:"C",name:"base32padupper",alphabet:"ABCDEFGHIJKLMNOPQRSTUVWXYZ234567=",bitsPerChar:5,caseInsensitive:!0}),ln=w({prefix:"v",name:"base32hex",alphabet:"0123456789abcdefghijklmnopqrstuv",bitsPerChar:5,caseInsensitive:!0}),gn=w({prefix:"V",name:"base32hexupper",alphabet:"0123456789ABCDEFGHIJKLMNOPQRSTUV",bitsPerChar:5,caseInsensitive:!0}),hn=w({prefix:"t",name:"base32hexpad",alphabet:"0123456789abcdefghijklmnopqrstuv=",bitsPerChar:5,caseInsensitive:!0}),mn=w({prefix:"T",name:"base32hexpadupper",alphabet:"0123456789ABCDEFGHIJKLMNOPQRSTUV=",bitsPerChar:5,caseInsensitive:!0}),dn=w({prefix:"h",name:"base32z",alphabet:"ybndrfg8ejkmcpqxot1uwisza345h769",bitsPerChar:5});var Sr={};v(Sr,{base36:()=>Ie,base36upper:()=>An});var Ie=Z({prefix:"k",name:"base36",alphabet:"0123456789abcdefghijklmnopqrstuvwxyz",caseInsensitive:!0}),An=Z({prefix:"K",name:"base36upper",alphabet:"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ",caseInsensitive:!0});var Cr={};v(Cr,{base58btc:()=>G,base58flickr:()=>bn});var G=Z({name:"base58btc",prefix:"z",alphabet:"123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"}),bn=Z({name:"base58flickr",prefix:"Z",alphabet:"123456789abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ"});var Pr={};v(Pr,{base64:()=>wn,base64pad:()=>Kn,base64url:()=>xn,base64urlpad:()=>En});var wn=w({prefix:"m",name:"base64",alphabet:"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/",bitsPerChar:6}),Kn=w({prefix:"M",name:"base64pad",alphabet:"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",bitsPerChar:6}),xn=w({prefix:"u",name:"base64url",alphabet:"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_",bitsPerChar:6}),En=w({prefix:"U",name:"base64urlpad",alphabet:"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_=",bitsPerChar:6});var vr={};v(vr,{base8:()=>Un});var Un=w({prefix:"7",name:"base8",alphabet:"01234567",bitsPerChar:3});var Br={};v(Br,{identity:()=>Sn});var Sn=Ae({prefix:"\0",name:"identity",encode:s(t=>at(O(t)),"encode"),decode:s(t=>O(ot(t)),"decode")});var ds=new TextEncoder,As=new TextDecoder;var Dr={};v(Dr,{identity:()=>Yn});var vn=gt,ft=128,Bn=127,Tn=~Bn,In=Math.pow(2,31);function gt(t,e,r){e=e||[],r=r||0;for(var n=r;t>=In;)e[r++]=t&255|ft,t/=128;for(;t&Tn;)e[r++]=t&255|ft,t>>>=7;return e[r]=t|0,gt.bytes=r-n+1,e}s(gt,"encode");var Dn=Tr,_n=128,lt=127;function Tr(t,n){var r=0,n=n||0,i=0,o=n,a,y=t.length;do{if(o>=y)throw Tr.bytes=0,new RangeError("Could not decode varint");a=t[o++],r+=i<28?(a<)<<i:(a<)*Math.pow(2,i),i+=7}while(a>=_n);return Tr.bytes=o-n,r}s(Tr,"read");var Nn=Math.pow(2,7),Ln=Math.pow(2,14),kn=Math.pow(2,21),Mn=Math.pow(2,28),Rn=Math.pow(2,35),On=Math.pow(2,42),zn=Math.pow(2,49),Hn=Math.pow(2,56),Gn=Math.pow(2,63),Fn=s(function(t){return t<Nn?1:t<Ln?2:t<kn?3:t<Mn?4:t<Rn?5:t<On?6:t<zn?7:t<Hn?8:t<Gn?9:10},"length"),Vn={encode:vn,decode:Dn,encodingLength:Fn},Wn=Vn,De=Wn;function _e(t,e=0){let r=De.decode(t,e),n=De.decode.bytes;if(n>9)throw new RangeError("Invalid varint: too long");if(n>1&&t[e+n-1]===0)throw new RangeError("Invalid varint: not minimally encoded");return[r,n]}s(_e,"decode");function we(t,e,r=0){return De.encode(t,e,r),e}s(we,"encodeTo");function Ke(t){return De.encodingLength(t)}s(Ke,"encodingLength");function Ee(t,e){let r=e.byteLength,n=Ke(t),i=n+Ke(r),o=new Uint8Array(i+r);return we(t,o,0),we(r,o,n),o.set(e,i),new xe(t,r,e,o)}s(Ee,"create");function ht(t){let e=O(t),[r,n]=_e(e),[i,o]=_e(e.subarray(n)),a=e.subarray(n+o);if(a.byteLength!==i)throw new Error("Incorrect length");return new xe(r,i,a,e)}s(ht,"decode");function mt(t,e){if(t===e)return!0;{let r=e;return t.code===r.code&&t.size===r.size&&r.bytes instanceof Uint8Array&&st(t.bytes,r.bytes)}}s(mt,"equals");var Ir=class Ir{constructor(e,r,n,i){c(this,"code");c(this,"size");c(this,"digest");c(this,"bytes");this.code=e,this.size=r,this.digest=ce(n),this.bytes=ce(i)}};s(Ir,"Digest");var xe=Ir;var dt=0,jn="identity",At=O;function Xn(t,e){if(e?.truncate!=null&&e.truncate!==t.byteLength){if(e.truncate<0||e.truncate>t.byteLength)throw new Error(`Invalid truncate option, must be less than or equal to ${t.byteLength}`);t=t.subarray(0,e.truncate)}return Ee(dt,At(t))}s(Xn,"digest");var Yn={code:dt,name:jn,encode:At,digest:Xn};var kr={};v(kr,{sha256:()=>qn,sha512:()=>Jn});var $n=20;function Nr({name:t,code:e,encode:r,minDigestLength:n,maxDigestLength:i}){return new _r(t,e,r,n,i)}s(Nr,"from");var Lr=class Lr{constructor(e,r,n,i,o){c(this,"name");c(this,"code");c(this,"encode");c(this,"minDigestLength");c(this,"maxDigestLength");this.name=e,this.code=r,this.encode=n,this.minDigestLength=i??$n,this.maxDigestLength=o}digest(e,r){if(r?.truncate!=null){if(r.truncate<this.minDigestLength)throw new Error(`Invalid truncate option, must be greater than or equal to ${this.minDigestLength}`);if(this.maxDigestLength!=null&&r.truncate>this.maxDigestLength)throw new Error(`Invalid truncate option, must be less than or equal to ${this.maxDigestLength}`)}if(e instanceof Uint8Array){let n=this.encode(e);return n instanceof Uint8Array?bt(n,this.code,r?.truncate):n.then(i=>bt(i,this.code,r?.truncate))}else throw Error("Unknown type, must be binary type")}};s(Lr,"Hasher");var _r=Lr;function bt(t,e,r){if(r!=null&&r!==t.byteLength){if(r>t.byteLength)throw new Error(`Invalid truncate option, must be less than or equal to ${t.byteLength}`);t=t.subarray(0,r)}return Ee(e,t)}s(bt,"createDigest");function Kt(t){return async e=>new Uint8Array(await crypto.subtle.digest(t,e))}s(Kt,"sha");var qn=Nr({name:"sha2-256",code:18,encode:Kt("SHA-256")}),Jn=Nr({name:"sha2-512",code:19,encode:Kt("SHA-512")});function xt(t,e){let{bytes:r,version:n}=t;return n===0?Qn(r,Mr(t),e??G.encoder):ei(r,Mr(t),e??be.encoder)}s(xt,"format");var Et=new WeakMap;function Mr(t){let e=Et.get(t);if(e==null){let r=new Map;return Et.set(t,r),r}return e}s(Mr,"baseCache");var St,E=class E{constructor(e,r,n,i){c(this,"code");c(this,"version");c(this,"multihash");c(this,"bytes");c(this,"/");c(this,St,"CID");this.code=r,this.version=e,this.multihash=n,this.bytes=ce(i),this["/"]=this.bytes}get asCID(){return this}get byteOffset(){return this.bytes.byteOffset}get byteLength(){return this.bytes.byteLength}toV0(){switch(this.version){case 0:return this;case 1:{let{code:e,multihash:r}=this;if(e!==Le)throw new Error("Cannot convert a non dag-pb CID to CIDv0");if(r.code!==ri)throw new Error("Cannot convert non sha2-256 multihash CID to CIDv0");return E.createV0(r)}default:throw Error(`Can not convert CID version ${this.version} to version 0. This is a bug please report`)}}toV1(){switch(this.version){case 0:{let{code:e,digest:r}=this.multihash,n=Ee(e,r);return E.createV1(this.code,n)}case 1:return this;default:throw Error(`Can not convert CID version ${this.version} to version 1. This is a bug please report`)}}equals(e){return E.equals(this,e)}static equals(e,r){let n=r;return n!=null&&e.code===n.code&&e.version===n.version&&mt(e.multihash,n.multihash)}toString(e){return xt(this,e)}toJSON(){return{"/":xt(this)}}link(){return this}[(St=Symbol.toStringTag,Symbol.for("nodejs.util.inspect.custom"))](){return`CID(${this.toString()})`}static asCID(e){if(e==null)return null;let r=e;if(r instanceof E)return r;if(r["/"]!=null&&r["/"]===r.bytes||r.asCID===r){let{version:n,code:i,multihash:o,bytes:a}=r;return new E(n,i,o,a??Ut(n,i,o.bytes))}else if(r[ti]===!0){let{version:n,multihash:i,code:o}=r,a=ht(i);return E.create(n,o,a)}else return null}static create(e,r,n){if(typeof r!="number")throw new Error("String codecs are no longer supported");if(!(n.bytes instanceof Uint8Array))throw new Error("Invalid digest");switch(e){case 0:{if(r!==Le)throw new Error(`Version 0 CID must use dag-pb (code: ${Le}) block encoding`);return new E(e,r,n,n.bytes)}case 1:{let i=Ut(e,r,n.bytes);return new E(e,r,n,i)}default:throw new Error("Invalid version")}}static createV0(e){return E.create(0,Le,e)}static createV1(e,r){return E.create(1,e,r)}static decode(e){let[r,n]=E.decodeFirst(e);if(n.length!==0)throw new Error("Incorrect length");return r}static decodeFirst(e){let r=E.inspectBytes(e),n=r.size-r.multihashSize,i=O(e.subarray(n,n+r.multihashSize));if(i.byteLength!==r.multihashSize)throw new Error("Incorrect length");let o=i.subarray(r.multihashSize-r.digestSize),a=new xe(r.multihashCode,r.digestSize,o,i);return[r.version===0?E.createV0(a):E.createV1(r.codec,a),e.subarray(r.size)]}static inspectBytes(e){let r=0,n=s(()=>{let[b,x]=_e(e.subarray(r));return r+=x,b},"next"),i=n(),o=Le;if(i===18)i=0,r=0;else if(i===1)o=n();else throw i===0?new RangeError("Invalid CID version, v0 is always implicit"):new RangeError(`Invalid CID version ${i}`);let a=r,y=n(),p=n(),u=r+p,l=u-a;return{version:i,codec:o,multihashCode:y,digestSize:p,multihashSize:l,size:u}}static parse(e,r){let[n,i]=Zn(e,r),o=E.decode(i);if(o.version===0&&e[0]!=="Q")throw Error("Version 0 CID string must not include multibase prefix");return Mr(o).set(n,e),o}};s(E,"CID");var je=E;function Zn(t,e){switch(t[0]){case"Q":{let r=e??G;return[G.prefix,r.decode(`${G.prefix}${t}`)]}case G.prefix:{let r=e??G;return[G.prefix,r.decode(t)]}case be.prefix:{let r=e??be;return[be.prefix,r.decode(t)]}case Ie.prefix:{let r=e??Ie;return[Ie.prefix,r.decode(t)]}default:{if(e==null)throw Error("To parse non base32, base36 or base58btc encoded CID multibase decoder must be provided");return[t[0],e.decode(t)]}}}s(Zn,"parseCIDtoBytes");function Qn(t,e,r){let{prefix:n}=r;if(n!==G.prefix)throw Error(`Cannot string encode V0 in ${r.name} encoding`);let i=e.get(n);if(i==null){let o=r.encode(t).slice(1);return e.set(n,o),o}else return i}s(Qn,"toStringV0");function ei(t,e,r){let{prefix:n}=r,i=e.get(n);if(i==null){let o=r.encode(t);return e.set(n,o),o}else return i}s(ei,"toStringV1");var Le=112,ri=18;function Ut(t,e,r){let n=Ke(t),i=n+Ke(e),o=new Uint8Array(i+r.byteLength);return we(t,o,0),we(e,o,n),o.set(r,i),o}s(Ut,"encodeCID");var ti=Symbol.for("@ipld/js-cid/CID");var Rr={...Br,...xr,...vr,...wr,...Kr,...Ur,...Sr,...Cr,...Pr,...Er},$s={...kr,...Dr};function Pt(t,e,r,n){return{name:t,prefix:e,encoder:{name:t,prefix:e,encode:r},decoder:{decode:n}}}s(Pt,"createCodec");var Ct=Pt("utf8","u",t=>"u"+new TextDecoder("utf8").decode(t),t=>new TextEncoder().encode(t.substring(1))),Or=Pt("ascii","a",t=>{let e="a";for(let r=0;r<t.length;r++)e+=String.fromCharCode(t[r]);return e},t=>{t=t.substring(1);let e=de(t.length);for(let r=0;r<t.length;r++)e[r]=t.charCodeAt(r);return e}),ni={utf8:Ct,"utf-8":Ct,hex:Rr.base16,latin1:Or,ascii:Or,binary:Or,...Rr},Xe=ni;function K(t,e="utf8"){let r=Xe[e];if(r==null)throw new Error(`Unsupported encoding "${e}"`);return r.decoder.decode(`${r.prefix}${t}`)}s(K,"fromString");function g(t,e="utf8"){let r=Xe[e];if(r==null)throw new Error(`Unsupported encoding "${e}"`);return r.encoder.encode(t).substring(1)}s(g,"toString");var Nt={};v(Nt,{AES:()=>d,decryptBytes:()=>_t,importAesKey:()=>Ce});var Y="did:key:z",pe="Ed25519",ke="X25519",ii="X25519",F=32,vt={sign:["sign","verify"],encrypt:["deriveKey","deriveBits"]},Q="RSA-OAEP",ee="RSASSA-PKCS1-v1_5",Ye="SHA-256",$e=new Uint8Array([0,245,2]),qe=2048,ue="SHA-256",V=8,W="AES-GCM",L=W,re=256,B=12,zr=16,Je=new Uint8Array([234,1]),Ze=new Uint8Array([237,1]),Me={RSA:"rsa",Edwards:"ed25519",BLS:"bls12-381"},Hr="rsa-exchange-key",Gr="rsa-write-key",Fr="ecc-exchange",Vr="ecc-write",C=16,Qe=1;var Re=globalThis.crypto,Bt=new Error("Invalid key use. Please use 'encryption' or 'signing"),si=new Error("Max must be less than 256 and greater than 0");function h(t){return new Uint8Array(t).buffer}s(h,"toArrayBuffer");async function Tt(t){let e=t.buffer.slice(t.byteOffset,t.byteOffset+t.byteLength);return new Uint8Array(await Re.subtle.digest("sha-256",e))}s(Tt,"sha256");function D(t){let e=K(t,"base64pad");return h(e)}s(D,"base64ToArrBuf");var fe=s((t,e)=>{if(typeof t=="string")return e(t);if(typeof t=="object"&&t.byteLength!==void 0)return new Uint8Array(t).buffer;throw new Error("Improper value. Must be a string, ArrayBuffer, Uint8Array")},"normalizeToBuf");function er(t){return fe(t,D)}s(er,"normalizeBase64ToBuf");var oi=s(t=>fe(t,e=>h(new TextEncoder().encode(e))),"normalizeUtf8ToBuf");function ai(t,e){let r=e===8?new Uint8Array(t.length):new Uint16Array(t.length);for(let n=0,i=t.length;n<i;n++)r[n]=t.charCodeAt(n);return r.buffer}s(ai,"strToArrBuf");var yi=s(t=>fe(t,e=>ai(e,16)),"normalizeUtf16ToBuf");function te(t,e){return e===8?oi(t):yi(t)}s(te,"normalizeUnicodeToBuf");function rr(t){if(!t.startsWith(Y))throw new Error("Please use a base58-encoded DID formatted `did:key:z...`");let e=""+t.substring(Y.length),r=K(e,"base58btc"),{keyBuffer:n,type:i}=ci(h(r));return{publicKey:new Uint8Array(n),type:i}}s(rr,"didToPublicKey");function ci(t){if(Ue(t,h($e)))return{keyBuffer:t.slice($e.byteLength),type:Me.RSA};if(Ue(t,h(Ze)))return{keyBuffer:t.slice(Ze.byteLength),type:Me.Edwards};if(Ue(t,h(Je)))return{keyBuffer:t.slice(Je.byteLength),type:Me.BLS};throw new Error("Unsupported key algorithm. Try using RSA.")}s(ci,"parseMagicBytes");var pi={equal:s((t,e)=>{let r=new Uint8Array(t),n=new Uint8Array(e);if(r.length!==n.length)return!1;for(let i=0;i<r.length;i++)if(r[i]!==n[i])return!1;return!0},"equal")};function Ue(t,e){return pi.equal(e,t.slice(0,e.byteLength))}s(Ue,"hasPrefix");function U(t){return g(t instanceof ArrayBuffer?new Uint8Array(t):t,"base64pad")}s(U,"toBase64");async function Oe(t,e){let r=typeof t=="string"?D(t):h(t);return Re.subtle.importKey("raw",r,{name:e?.alg||L,length:e?.length||re},e?.extractable??!0,["encrypt","decrypt"])}s(Oe,"importKey");function tr(t,{max:e}={max:255}){if(e<1||e>255)throw si;let r=new Uint8Array(t);if(e===255)return Re.getRandomValues(r),r.buffer;let n=0,i=e+1,o=Math.floor(256/i)*i,a=new Uint8Array(1);for(;n<r.length;)Re.getRandomValues(a),a[0]<o&&(r[n]=a[0]%i,n++);return r.buffer}s(tr,"randomBuf");function $(t,e){let r=t instanceof ArrayBuffer?new Uint8Array(t):new Uint8Array(h(t)),n=e instanceof ArrayBuffer?new Uint8Array(e):new Uint8Array(h(e)),i=new Uint8Array(r.length+n.length);return i.set(r),i.set(n,r.length),i.buffer}s($,"joinBufs");async function Wr(t,e,r,n=!1){let i=n;if(!Object.values(ye).includes(r))throw new Error("invalid key use");let o=r==="exchange"?Q:ee,a=r==="exchange"?["encrypt","decrypt"]:["sign","verify"];return Re.subtle.generateKey({name:o,modulusLength:t,publicExponent:ui(),hash:{name:e}},i,a)}s(Wr,"makeRSAKeypair");function ui(){return new Uint8Array([1,0,1])}s(ui,"publicExponent");var z=globalThis.crypto,nr={keyTypes:{"bls12-381":{magicBytes:new Uint8Array([234,1]),verify:s(()=>{throw new Error("Not implemented")},"verify")},rsa:{magicBytes:new Uint8Array([0,245,2]),verify:Dt},ed25519:{magicBytes:new Uint8Array([237,1]),verify:It}}};async function Lo({message:t,did:e,signature:r}){let n=fi(e),i=ir(e).publicKey;if(n==="ed25519")return It({message:t,publicKey:i,signature:r});if(n==="rsa")return Dt({message:t,publicKey:i,signature:r});throw new Error("Unknown key format")}s(Lo,"verify");function fi(t){try{let{type:e}=ir(t);return e}catch{return"unknown"}}s(fi,"getAlgorithm");async function It({message:t,publicKey:e,signature:r}){let n=t;typeof t=="string"&&(n=K(t,"utf8"));let i=r;typeof r=="string"&&(i=K(r,"base64pad"));let o=e;if(typeof e=="string"&&(o=ir(e).publicKey),o.length!==32)throw new Error(`Invalid Ed25519 public key size: ${o.length} bytes, expected 32`);if(i.length!==64)throw new Error(`Invalid Ed25519 signature size: ${i.length} bytes, expected 64`);let a=await z.subtle.importKey("raw",h(o),{name:"Ed25519"},!1,["verify"]);return z.subtle.verify({name:"Ed25519"},a,h(i),h(n))}s(It,"ed25519Verify");async function Dt({message:t,publicKey:e,signature:r}){let n=e;return typeof e=="string"&&(n=ir(e).publicKey),q.verify(t,r,await z.subtle.importKey("spki",h(n),{name:ee,hash:Ye},!1,["verify"]),8)}s(Dt,"rsaVerify");var q={verify:s(async function(e,r,n,i=V,o=ue){return z.subtle.verify({name:ee},typeof n=="string"?await ze(n,o,"signing"):n,er(r),te(e,i))},"rsaVerify"),sign:s(async function(e,r,n=V){return z.subtle.sign({name:ee},r,te(e,n))},"sign"),encrypt:s(async function(e,r,n=V,i=ue){let o;return typeof r=="string"?o=await ze(r,i,"exchange"):o=r instanceof Uint8Array?await ze(h(r),i,"exchange"):r,z.subtle.encrypt({name:Q},o,te(e,n))},"rsaEncrypt"),decrypt:s(async function(e,r){let n=Se(r)?r:await mi(r,["decrypt"]),i;typeof e=="string"?i=K(e,"base64pad"):i=e;let o=await z.subtle.decrypt({name:Q},n,i instanceof Uint8Array?h(i):i);return new Uint8Array(o)},"rsaDecrypt")};async function ze(t,e,r){li(r);let n=r==="exchange"?Q:ee,i=r==="exchange"?["encrypt"]:["verify"],o=typeof t=="string"?D(hi(t)):t;return z.subtle.importKey("spki",o,{name:n,hash:{name:e}},!0,i)}s(ze,"rsaImportPublicKey");function li(t){gi(t,["signing","exchange"],Bt)}s(li,"checkValidKeyUse");function gi(t,e,r){if(!e.some(i=>i===t))throw r}s(gi,"checkValid");function hi(t){return t.replace(`-----BEGIN PUBLIC KEY-----
|
|
2
2
|
`,"").replace(`
|
|
3
|
-
-----END PUBLIC KEY-----`,"")}s(ai,"stripKeyHeader");function Ke(t){return typeof CryptoKey<"u"?t instanceof CryptoKey:Hr(t,"algorithm")&&Hr(t,"extractable")&&Hr(t,"type")}s(Ke,"isCryptoKey");function Hr(t,e){return typeof t=="object"&&t!=null&&e in t}s(Hr,"hasProp");function yi(t,e){return R.subtle.importKey("spki",t instanceof Uint8Array?g(t):t,{name:J,hash:Ve},!1,e)}s(yi,"importRsaKey");async function ke(t,e="rsa"){let r;if(t instanceof CryptoKey){e=t.algorithm.name.includes("RSA")?"rsa":"ed25519";let o=e==="ed25519"?"raw":"spki",a=await R.subtle.exportKey(o,t);r=new Uint8Array(a)}else r=t;let n=Qe.keyTypes[e]?.magicBytes;if(!n)throw new Error(`Key type '${e}' not supported, available types: ${Object.keys(Qe.keyTypes).join(", ")}`);let i=or([n,r]);return X+l(i,"base58btc")}s(ke,"publicKeyToDid");function Ao(t){let e=t.substring(X.length),r=K(e,"base58btc"),n=Object.entries(Qe.keyTypes).find(([o,a])=>be(g(r),g(a.magicBytes)));if(!n)throw new Error("Invalid key type");let i=n[0];if(i!=="ed25519"&&i!=="rsa")throw new Error(`Unsupported key type: ${i}`);return i}s(Ao,"keyTypeFromDid");async function re(t){return t instanceof CryptoKey?await R.subtle.exportKey("spki",t):await R.subtle.exportKey("spki",t.publicKey)}s(re,"getPublicKeyAsArrayBuffer");async function Gr(t){let e=await re(t);return new Uint8Array(e)}s(Gr,"getPublicKeyAsUint8Array");function er(t){if(!t.startsWith(X))throw new Error("Please use a base58-encoded DID formatted `did:key:z...`");let e=t.substring(X.length),r=K(e,"base58btc"),n=Object.entries(Qe.keyTypes).find(([o,a])=>be(g(r),g(a.magicBytes)));if(!n)throw new Error("Unsupported key algorithm.");let i=r.slice(n[1].magicBytes.length);return n[0]==="ed25519"&&i.length===44?{publicKey:i.slice(-32),type:n[0]}:{publicKey:i,type:n[0]}}s(er,"didToPublicKey");var ce=globalThis.crypto,d={create(t={alg:L,length:Q}){let e=t.length||Q,r=t.alg||L,n=t.extractable??!0;return ce.subtle.generateKey({name:r,length:e},n,["encrypt","decrypt"])},export:Object.assign(async t=>{let e=await ce.subtle.exportKey("raw",t);return new Uint8Array(e)},{asString:s(async(t,e)=>{let r=await d.export(t);return e?l(r,e):P(r)},"asString")}),import(t,e){return we(typeof t=="string"?N(t):t,void 0,e)},async exportAsString(t){let e=await d.export(t);return P(e)},encrypt:ci,async decrypt(t,e,r){let n=Ke(e)?e:await we(e),i=r?await ce.subtle.decrypt({name:V,iv:g(r)},n,typeof t=="string"?g(K(t,"base64pad")):t instanceof Uint8Array?g(t):t):await Pt(t,n);return new Uint8Array(i)}};function we(t,e,r=!0){return ce.subtle.importKey("raw",t instanceof Uint8Array?g(t):t,{name:V,length:e||256},r,["encrypt","decrypt"])}s(we,"importAesKey");async function ci(t,e,r,n){let i=Ke(e)?e:await we(e),o=n?await ce.subtle.encrypt({name:V,iv:g(n)},i,t instanceof ArrayBuffer?t:g(typeof t=="string"?K(t):t)):await pi(t,i);return r&&r==="arraybuffer"?o:new Uint8Array(o)}s(ci,"encrypt");async function pi(t,e,r){let n=ee(t,r?.charsize??F),i=typeof e=="string"?await Ne(e,r):e,o=r?.iv||Ze(I),a=await ce.subtle.encrypt({name:V,iv:o},i,n);return Y(o,a)}s(pi,"encryptBytes");async function Pt(t,e,r){let n=qe(t),i=typeof e=="string"?await Ne(e,r):e,o=n.slice(0,I),a=n.slice(I);return await ce.subtle.decrypt({name:L,iv:o},i,a)}s(Pt,"decryptBytes");function rr(t){return new Promise((e,r)=>{t.oncomplete=t.onsuccess=()=>e(t.result),t.onabort=t.onerror=()=>r(t.error)})}s(rr,"promisifyRequest");function ui(t,e){let r,n=s(()=>{if(r)return r;let i=indexedDB.open(t);return i.onupgradeneeded=()=>i.result.createObjectStore(e),r=rr(i),r.then(o=>{o.onclose=()=>r=void 0},()=>{r=void 0}),r},"getDB");return(i,o)=>n().then(a=>o(a.transaction(e,i).objectStore(e)))}s(ui,"createStore");var Fr;function Vr(){return Fr||(Fr=ui("keyval-store","keyval")),Fr}s(Vr,"defaultGetStore");function Me(t,e=Vr()){return e("readonly",r=>rr(r.get(t)))}s(Me,"get");function Wr(t,e,r=Vr()){return r("readwrite",n=>(n.put(e,t),rr(n.transaction)))}s(Wr,"set");function jr(t,e=Vr()){return e("readwrite",r=>(t.forEach(n=>r.delete(n)),rr(r.transaction)))}s(jr,"delMany");var To=new Error("Key does not exist. Make sure you properly instantiated the keystore."),Do=new Error("Retrieved a symmetric key when an asymmetric keypair was expected. Please use a different key name."),Io=new Error("Retrieved an asymmetric keypair when an symmetric key was expected. Please use a different key name."),_o=new Error("ECC is not enabled for this browser. Please use RSA instead."),No=new Error("Cryptosystem not supported. Please use ECC or RSA"),fi=new Error("Invalid key use. Please use 'exchange' or 'write'"),Lo=new Error("Max must be less than 256 and greater than 0"),Xr=class Xr extends Error{constructor(e){super(e||"These keys were deleted"),this.name="KeysDeletedError"}};s(Xr,"KeysDeletedError");var xe=Xr;function vt(t){li(t,["exchange","write","signing"],fi)}s(vt,"checkValidKeyUse");function li(t,e,r){if(!e.some(i=>i===t))throw r}s(li,"checkValid");var te=class te{constructor(e){c(this,"DID");c(this,"hasPersisted");c(this,"isSessionOnly");c(this,"destroyed");c(this,"exchangeKeyName");c(this,"writeKeyName");c(this,"_exchangeKey");c(this,"_writeKey");let{keys:r}=e,n=this.constructor;this.DID=e.did,this._exchangeKey=r.exchange,this._writeKey=r.write,this.hasPersisted=e.hasPersisted,this.isSessionOnly=!!e.isSessionOnly,this.destroyed=!1,this.exchangeKeyName=e.exchangeKeyName||n.EXCHANGE_KEY_NAME,this.writeKeyName=e.writeKeyName||n.WRITE_KEY_NAME}get exchangeKey(){if(this.destroyed||!this._exchangeKey)throw new xe;return this._exchangeKey}get writeKey(){if(this.destroyed||!this._writeKey)throw new xe;return this._writeKey}publicExchangeKeyAsString(e){return this.publicExchangeKey.asString(e)}publicWriteKeyAsString(e){return this.publicWriteKey.asString(e)}get publicWriteKey(){let e=this.writeKey.publicKey;return Object.assign(e,{asString:s(async r=>{let n=await re(this.writeKey),i=new Uint8Array(n);return r?l(i,r):P(i)},"asString")})}get publicExchangeKey(){let e=this.exchangeKey.publicKey;return Object.assign(e,{asString:s(async r=>{let n=await re(this.exchangeKey),i=new Uint8Array(n);return r?l(i,r):P(i)},"asString")})}get privateWriteKey(){return this.writeKey.privateKey}get privateExchangeKey(){return this.exchangeKey.privateKey}get deviceName(){return te.deviceName(this.DID)}static deviceName(e){return gi(e)}async persist(){if(this.destroyed)throw new xe;this.isSessionOnly||(await Promise.all([Wr(this.exchangeKeyName,this.exchangeKey),Wr(this.writeKeyName,this.writeKey)]),this.hasPersisted=!0)}async delete(){let e=this.exchangeKeyName,r=this.writeKeyName;await jr([e,r]),this.constructor._invalidate(e,r),this._destroy()}_destroy(){this.destroyed=!0,this.hasPersisted=!1,this._exchangeKey=null,this._writeKey=null}static _invalidate(e,r){let n=this._instance;n&&n.exchangeKeyName===e&&n.writeKeyName===r&&(n._destroy(),this._instance=null)}static async delete(e={}){let r=e.encryptionKeyName||this.EXCHANGE_KEY_NAME,n=e.writeKeyName||this.WRITE_KEY_NAME;await jr([r,n]),this._invalidate(r,n)}async getDeviceName(){return te.deviceName(this.DID)}static _createExchangeKeys(e){throw new Error("The child should implement this")}static _createWriteKeys(e){throw new Error("The child should implement this")}static _keyNames(e){return e?{exchangeKeyName:`${e}.${this.EXCHANGE_KEY_NAME}`,writeKeyName:`${e}.${this.WRITE_KEY_NAME}`}:{exchangeKeyName:this.EXCHANGE_KEY_NAME,writeKeyName:this.WRITE_KEY_NAME}}static async create(e,r,n){let i=n?.exchangeKeys||await this._createExchangeKeys(r),o=n?.writeKeys||await this._createWriteKeys(r),a=await re(o),y=await ke(new Uint8Array(a),this.TYPE==="ecc"?"ed25519":"rsa"),{exchangeKeyName:p,writeKeyName:f}=this._keyNames(n?.dbKey),h=new this({keys:{exchange:i,write:o},did:y,hasPersisted:!1,isSessionOnly:!!e,exchangeKeyName:p,writeKeyName:f});return this._instance=h,h}static async exist(e={}){let r=await Me(e.writeKeyName||this.WRITE_KEY_NAME),n=await Me(e.encryptionKeyName||this.EXCHANGE_KEY_NAME);return!(!r||!n)}static async load(e={session:!1}){let r=e.persist!==!1,n=this._keyNames(e.dbKey),i=e.encryptionKeyName||n.exchangeKeyName,o=e.writeKeyName||n.writeKeyName,a=this._instance;if(a&&!a.destroyed&&a.exchangeKeyName===i&&a.writeKeyName===o)return r&&!a.hasPersisted&&await a.persist(),a;let y=!0,p=await Me(i),f=await Me(o);p||(y=!1,p=await this._createExchangeKeys(e.extractable)),f||(y=!1,f=await this._createWriteKeys(e.extractable));let h=await re(f),w=await ke(new Uint8Array(h),this.TYPE==="ecc"?"ed25519":"rsa"),x=new this({keys:{exchange:p,write:f},did:w,hasPersisted:y,isSessionOnly:!!e.session,exchangeKeyName:i,writeKeyName:o});return this._instance=x,r&&!y&&await x.persist(),x}};s(te,"AbstractKeys"),c(te,"EXCHANGE_KEY_NAME"),c(te,"WRITE_KEY_NAME"),c(te,"_instance");var Ee=te;async function Tt(t,e){let{content:r,publicKey:n}=t,i=e||await d.create(),o=await d.encrypt(typeof r=="string"?K(r):r,typeof i=="string"?await d.import(i):i),a=await Yr({key:i,publicKey:n});return Y(a,o)}s(Tt,"encryptTo");Tt.asString=async function(t,e){let r=await Tt(t,e);return l(new Uint8Array(r),"base64pad")};async function Yr({key:t,publicKey:e},r){let n;t instanceof CryptoKey?n=await d.export(t):n=t;let i=await $.encrypt(n,e);return r&&r==="arraybuffer"?i:new Uint8Array(i)}s(Yr,"encryptKeyTo");Yr.asString=async function({key:t,publicKey:e},r){let n=await Yr({key:t,publicKey:e});return r?l(n,r):P(n)};async function gi(t){let e=await Ut(new TextEncoder().encode(t.normalize("NFD")));return l(e,"base32").slice(0,32)}s(gi,"getDeviceName");var $r=globalThis.crypto;function Dt(t){return new Uint8Array(t).buffer}s(Dt,"toArrayBuffer");function hi(t){return t.algorithm.modulusLength/8}s(hi,"rsaCiphertextByteLength");var pe=class pe extends Ee{static async create(e,r,n){return await super.create(e,r,n)}static async load(e={session:!1}){return super.load(e)}static async _createExchangeKeys(e=!1){return await zr(je,ae,"exchange",e)}static async _createWriteKeys(e=!1){return await zr(je,ae,"write",e)}async toJson(e){let r=this.publicExchangeKey,n=this.DID,i=await $r.subtle.exportKey("spki",r);return{publicExchangeKey:e?l(new Uint8Array(i),e):P(i),DID:n}}async sign(e,r=F){let n=this.writeKey,i=await $.sign(e,n.privateKey,r);return new Uint8Array(i)}async signAsString(e,r){let n=await this.sign(e,r);return P(n)}async encrypt(e,r,n,i,o){let a=r||this.exchangeKey.publicKey,y=n,p=i,f=y||await d.create({length:p||o}),h=await d.encrypt(typeof e=="string"?K(e):e,typeof f=="string"?await d.import(f):f),w=await Ue({key:f,publicKey:a}),x=Y(w,h);return new Uint8Array(x)}async encryptAsString(e,r,n,i,o){let a=await this.encrypt(e,r,n,i,o);return l(a,"base64pad")}async decrypt(e,r,n){let i=ye(e,N),o=hi(this.exchangeKey.privateKey),a=i.slice(0,o),y=i.slice(o),p=await this.decryptKey(a);return await d.decrypt(y,p)}async decryptAsString(e,r,n){let i=await this.decrypt(e,r,n);return l(i)}async decryptKey(e){return await $.decrypt(e,this.exchangeKey.privateKey)}async decryptKeyAsString(e,r){let n=await $.decrypt(e,this.exchangeKey.privateKey);return l(n,r)}async getAesKey(e,r){if(!e)throw new Error("RSA requires an encrypted AES key to decrypt");if(typeof e=="string"){let n=await this.decryptKey(e);return await d.import(n)}else return e}};s(pe,"RsaKeys"),c(pe,"TYPE","rsa"),c(pe,"INFO","keys"),c(pe,"EXCHANGE_KEY_NAME",kr),c(pe,"WRITE_KEY_NAME",Mr);var ue=pe;Object.assign(ue.prototype.sign,{asString:s(async function(t,e){return this.signAsString(t,e)},"asString")});Object.assign(ue.prototype.encrypt,{asString:s(async function(t,e,r,n){return this.encryptAsString(t,e,r,n)},"asString")});Object.assign(ue.prototype.decrypt,{asString:s(async function(t,e){return this.decryptAsString(t,e)},"asString")});async function mi(t,e,r){let n=Je(r),i=await Le(n.publicKey.buffer,"SHA-256","signing");try{return $.verify(t,e,i)}catch{return!1}}s(mi,"verify");Object.assign(ue.prototype.decryptKey,{asString:s(async function(t,e){return this.decryptKeyAsString(t,e)},"asString")});async function It(t,e){let{content:r,publicKey:n}=t,i=e||await d.create(),o=await d.encrypt(typeof r=="string"?K(r):r,typeof i=="string"?await d.import(i):i),a=await Ue({key:i,publicKey:n});return Y(a,o)}s(It,"encryptTo");It.asString=async function(t,e){let{content:r,publicKey:n}=t,i=e||await d.create(),o=await d.encrypt(typeof r=="string"?K(r):r,typeof i=="string"?await d.import(i):i,"arraybuffer"),a=await Ue({key:i,publicKey:n}),y=Y(a,o);return l(new Uint8Array(y),"base64pad")};async function Ue({key:t,publicKey:e},r){let n;t instanceof CryptoKey?n=await d.export(t):n=t;let i=await $.encrypt(n,e);return r&&r==="arraybuffer"?i:new Uint8Array(i)}s(Ue,"encryptKeyTo");Ue.asString=async function({key:t,publicKey:e},r){let n=await Ue({key:t,publicKey:e});return r?l(n,r):P(n)};async function di(t,e,r,n){let i=Ke(e)?e:await we(e),o=n?await $r.subtle.encrypt({name:V,iv:Dt(n)},i,Dt(t)):await Ai(t,i);return r&&r==="arraybuffer"?o:new Uint8Array(o)}s(di,"encrypt");async function Ai(t,e,r){let n=ee(t,r?.charsize??F),i=typeof e=="string"?await Ne(e,r):e,o=r?.iv||Ze(I),a=await $r.subtle.encrypt({name:V,iv:o},i,n);return Y(o,a)}s(Ai,"encryptBytes");var Mt={};D(Mt,{EccKeys:()=>ie,default:()=>bi,exportPublicKey:()=>fe,importPublicKey:()=>Oe,verify:()=>kt});var A=globalThis.crypto;function ne(t){return new Uint8Array(t).buffer}s(ne,"toArrayBuffer");var k=class k extends Ee{static async create(e,r,n){return await super.create(e,r,n)}get publicExchangeKey(){let e=this.exchangeKey.publicKey;return Object.assign(e,{asString:s(async r=>{let n=await fe(this.exchangeKey),i=new Uint8Array(n);return r?l(i,r):P(n)},"asString")})}get publicWriteKey(){let e=this.writeKey.publicKey;return Object.assign(e,{asString:s(async r=>{let n=await fe(this.writeKey),i=new Uint8Array(n);return r?l(i,r):P(n)},"asString")})}static async _createExchangeKeys(e=!1){return await A.subtle.generateKey({name:De},e,["deriveKey","deriveBits"])}static async _createWriteKeys(e=!1){return await A.subtle.generateKey({name:oe},e,["sign","verify"])}static async load(e={session:!1}){return super.load(e)}async toJson(e){let r=this.DID,n=await fe(this.exchangeKey);return{publicExchangeKey:e?l(new Uint8Array(n),e):P(n),DID:r}}async wrap(e,r,n){let i=await k._createExchangeKeys(!1),o=await fe(i),a=new Uint8Array(o),y=new Uint8Array(await A.subtle.exportKey("raw",r)),p=n||"key-wrap",f=A.getRandomValues(new Uint8Array(B)),h=await Re(i.privateKey,r,f,Nt(a,y,p)),w,x=!1;if(e instanceof CryptoKey){let _=await A.subtle.exportKey("raw",e);w=new Uint8Array(_),x=!0}else typeof e=="string"?(w=K(e,"base64pad"),x=!0):w=e;let O=A.getRandomValues(new Uint8Array(I)),u=await A.subtle.encrypt({name:L,iv:O},h,ne(w));x&&w.fill(0);let m=new Uint8Array(O),T=new Uint8Array(u),S=1+B+m.length+T.length,E=new Uint8Array(S);E[0]=$e,E.set(f,1),E.set(m,1+B),E.set(T,1+B+m.length);let U=Lt(a,y,E,p),v=await this.sign(U);return{enc:P(a),wrappedKey:P(E),signature:l(v,"base64pad")}}async unwrap(e,r,n={}){let{enc:i,wrappedKey:o,signature:a}=e,y=n.info,p=n.extractable??!0;if(!r)throw new Error("`unwrap` requires the DID of the sender");if(!a)throw new Error("The wrapped key is not signed");let f=K(i,"base64pad");if(f.length!==G)throw new Error(`\`enc\` must be a ${G}-byte X25519 public key, got ${f.length} bytes`);let h=new Uint8Array(N(o)),w=y||"key-wrap",x=1+B+I+Lr;if(h.length<x)throw new Error(`Wrapped key must be at least ${x} bytes, got ${h.length} bytes`);let O=h[0];if(O!==$e)throw new Error(`Unsupported wrapped key format version: ${O}`);let u=new Uint8Array(await fe(this.exchangeKey)),m=Lt(f,u,h,w);if(!await kt(m,a,r))throw new Error("Invalid signature for the wrapped key");let S=await Oe(i,"X25519","exchange"),E=h.slice(1,1+B),U=h.slice(1+B,1+B+I),v=h.slice(1+B+I),_=await Re(this.exchangeKey.privateKey,S,E,Nt(f,u,w)),z=await A.subtle.decrypt({name:L,iv:U},_,ne(v));return await A.subtle.importKey("raw",z,{name:L},p,["encrypt","decrypt"])}async encrypt(e,r,n,i,o){let a=A.getRandomValues(new Uint8Array(B)),y=r||this.exchangeKey.publicKey,p;typeof y=="string"?p=await Oe(y,"X25519","exchange"):p=y;let f=await k._createExchangeKeys(!1),h=i?i instanceof CryptoKey?i:await d.import(i):await Re(f.privateKey,p,a,n||k.INFO,o),w=A.getRandomValues(new Uint8Array(I)),x=typeof e=="string"?new TextEncoder().encode(e):e,O=await A.subtle.encrypt({name:L,iv:w},h,ne(x)),u=new Uint8Array(await fe(f)),m=new Uint8Array(a),T=new Uint8Array(w),S=new Uint8Array(O),E=u.length+m.length+T.length+S.length,U=new Uint8Array(E);return U.set(u,0),U.set(m,u.length),U.set(T,u.length+m.length),U.set(S,u.length+m.length+T.length),U}async encryptAsString(e,r,n,i,o){let a=await this.encrypt(e,r,n||k.INFO,i,o);return l(new Uint8Array(a),"base64pad")}async decrypt(e,r,n,i){let o=ye(e,N),a=o.slice(0,G),y=await A.subtle.importKey("raw",a,{name:De},!0,[]),p=o.slice(G,G+B),f=o.slice(G+B,G+B+I),h=o.slice(G+B+I),{privateKey:w}=this.exchangeKey,x=i?i instanceof CryptoKey?i:await d.import(i):await Re(w,y,p,n||k.INFO);return await A.subtle.decrypt({name:r||L,iv:f},x,h)}async decryptAsString(e,r,n,i){let o=await this.decrypt(e,r,n,i);return l(new Uint8Array(o))}async getAesKey(e,r,n){e||(e=this.publicExchangeKey);let i;typeof e=="string"?i=await Oe(e,"X25519","exchange"):i=e;let o=n?new Uint8Array(typeof n=="string"?N(n):n):A.getRandomValues(new Uint8Array(B)),a=await Re(this.exchangeKey.privateKey,i,o,r||k.INFO);return Object.assign(a,{salt:P(o)})}async sign(e,r){let n=new TextEncoder,i;typeof e=="string"?i=n.encode(e):e instanceof ArrayBuffer?i=new Uint8Array(e):i=e;let o=await A.subtle.sign({name:oe},this.writeKey.privateKey,ne(i));return new Uint8Array(o)}async signAsString(e,r){let n=await this.sign(e,r);return l(n,"base64pad")}};s(k,"EccKeys"),c(k,"TYPE","ecc"),c(k,"EXCHANGE_KEY_NAME",Rr),c(k,"WRITE_KEY_NAME",Or),c(k,"INFO","keys");var ie=k;Object.assign(ie.prototype.sign,{asString:s(async function(t,e){let r=await this.sign(t,e);return l(r,"base64pad")},"asString")});async function fe(t,e="raw"){return await A.subtle.exportKey(e,t.publicKey)}s(fe,"exportPublicKey");async function Oe(t,e,r){vt(r);let n=r==="exchange"?De:oe,i=r==="exchange"?[]:["verify"],o=N(t),a=e==="X25519"?{name:n}:{name:n,namedCurve:e};return A.subtle.importKey("raw",o,a,!0,i)}s(Oe,"importPublicKey");var bi={EccKeys:ie,importPublicKey:Oe};Object.assign(ie.prototype.encrypt,{asString:s(async function(t,e,r,n,i){return this.encryptAsString(t,e,r,n,i)},"asString")});Object.assign(ie.prototype.decrypt,{asString:s(async function(t,e){return this.decryptAsString(t,e)},"asString")});Object.assign(ie.prototype.sign,{asString:s(async function(t,e){return this.signAsString(t,e)},"asString")});async function kt(t,e,r){try{let n=Je(r);if(n.type!=="ed25519")throw new Error(`Expected Ed25519 key for ECC verification, got ${n.type}`);let i=n.publicKey.slice(-32),o=await A.subtle.importKey("raw",i,{name:oe},!0,["verify"]),a=new TextEncoder,y;typeof t=="string"?y=a.encode(t):t instanceof ArrayBuffer?y=new Uint8Array(t):y=t;let p;return typeof e=="string"?p=new Uint8Array(N(e)):p=e,await A.subtle.verify({name:oe},o,ne(p),ne(y))}catch{return!1}}s(kt,"verify");function Nt(t,e,r){let n=new TextEncoder().encode(r),i=new Uint8Array(t.length+e.length+n.length);return i.set(t,0),i.set(e,t.length),i.set(n,t.length+e.length),i}s(Nt,"wrapKekInfo");function Lt(t,e,r,n){let i=new TextEncoder().encode(n),o=new Uint8Array(4);new DataView(o.buffer).setUint32(0,r.length);let a=new Uint8Array(t.length+e.length+o.length+r.length+i.length),y=0;return a.set(t,y),y+=t.length,a.set(e,y),y+=e.length,a.set(o,y),y+=o.length,a.set(r,y),y+=r.length,a.set(i,y),a}s(Lt,"wrapSignData");async function Re(t,e,r,n,i){let o=new TextEncoder,a=typeof n=="string"?o.encode(n):n,y=await A.subtle.deriveBits({name:"X25519",public:e},t,256),p=await A.subtle.importKey("raw",y,"HKDF",!1,["deriveKey"]);return A.subtle.deriveKey({name:"HKDF",hash:"SHA-256",salt:r instanceof Uint8Array?ne(r):r,info:ne(a)},p,{name:"AES-GCM",length:i||Q},!1,["encrypt","decrypt"])}s(Re,"deriveKey");export{V as AES_GCM,Lr as AES_GCM_TAG_LENGTH,Ee as AbstractKeys,X as BASE58_DID_PREFIX,Xe as BLS_DID_PREFIX,sr as CharSize,Zr as CryptoSystem,F as DEFAULT_CHAR_SIZE,Rr as DEFAULT_ECC_EXCHANGE,qn as DEFAULT_ECC_EXCHANGE_CURVE,Or as DEFAULT_ECC_WRITE,ae as DEFAULT_HASH_ALGORITHM,kr as DEFAULT_RSA_EXCHANGE,je as DEFAULT_RSA_SIZE,Mr as DEFAULT_RSA_WRITE,L as DEFAULT_SYMM_ALGORITHM,Q as DEFAULT_SYMM_LENGTH,De as ECC_EXCHANGE_ALGORITHM,oe as ECC_WRITE_ALGORITHM,Ye as EDWARDS_DID_PREFIX,ir as EccCurve,nr as HashAlg,I as IV_LENGTH,Ie as KEY_TYPE,xt as KEY_USE,se as KeyUse,xe as KeysDeletedError,J as RSA_ALGORITHM,We as RSA_DID_PREFIX,Ve as RSA_HASHING_ALGORITHM,Z as RSA_SIGN_ALGORITHM,qr as RsaSize,B as SALT_LENGTH,Jr as SymmKeyLength,$e as WRAP_FORMAT_VERSION,G as X25519_PUBLIC_KEY_LENGTH,Bt as aes,si as checkValidKeyUse,Qe as did,er as didToPublicKey,Mt as ecc,St as ed25519Verify,Yr as encryptKeyTo,Tt as encryptTo,ii as getAlgorithm,gi as getDeviceName,re as getPublicKeyAsArrayBuffer,Gr as getPublicKeyAsUint8Array,yi as importRsaKey,Ke as isCryptoKey,Ao as keyTypeFromDid,ke as publicKeyToDid,_t as rsa,Le as rsaImportPublicKey,$ as rsaOperations,Ct as rsaVerify,mo as verify};
|
|
3
|
+
-----END PUBLIC KEY-----`,"")}s(hi,"stripKeyHeader");function Se(t){return typeof CryptoKey<"u"?t instanceof CryptoKey:jr(t,"algorithm")&&jr(t,"extractable")&&jr(t,"type")}s(Se,"isCryptoKey");function jr(t,e){return typeof t=="object"&&t!=null&&e in t}s(jr,"hasProp");function mi(t,e){return z.subtle.importKey("spki",t instanceof Uint8Array?h(t):t,{name:Q,hash:Ye},!1,e)}s(mi,"importRsaKey");async function He(t,e="rsa"){let r;if(t instanceof CryptoKey){e=t.algorithm.name.includes("RSA")?"rsa":"ed25519";let o=e==="ed25519"?"raw":"spki",a=await z.subtle.exportKey(o,t);r=new Uint8Array(a)}else r=t;let n=nr.keyTypes[e]?.magicBytes;if(!n)throw new Error(`Key type '${e}' not supported, available types: ${Object.keys(nr.keyTypes).join(", ")}`);let i=ur([n,r]);return Y+g(i,"base58btc")}s(He,"publicKeyToDid");function ko(t){let e=t.substring(Y.length),r=K(e,"base58btc"),n=Object.entries(nr.keyTypes).find(([o,a])=>Ue(h(r),h(a.magicBytes)));if(!n)throw new Error("Invalid key type");let i=n[0];if(i!=="ed25519"&&i!=="rsa")throw new Error(`Unsupported key type: ${i}`);return i}s(ko,"keyTypeFromDid");async function ne(t){return t instanceof CryptoKey?await z.subtle.exportKey("spki",t):await z.subtle.exportKey("spki",t.publicKey)}s(ne,"getPublicKeyAsArrayBuffer");async function Xr(t){let e=await ne(t);return new Uint8Array(e)}s(Xr,"getPublicKeyAsUint8Array");function ir(t){if(!t.startsWith(Y))throw new Error("Please use a base58-encoded DID formatted `did:key:z...`");let e=t.substring(Y.length),r=K(e,"base58btc"),n=Object.entries(nr.keyTypes).find(([o,a])=>Ue(h(r),h(a.magicBytes)));if(!n)throw new Error("Unsupported key algorithm.");let i=r.slice(n[1].magicBytes.length);return n[0]==="ed25519"&&i.length===44?{publicKey:i.slice(-32),type:n[0]}:{publicKey:i,type:n[0]}}s(ir,"didToPublicKey");var le=globalThis.crypto,d={create(t={alg:L,length:re}){let e=t.length||re,r=t.alg||L,n=t.extractable??!0;return le.subtle.generateKey({name:r,length:e},n,["encrypt","decrypt"])},export:Object.assign(async t=>{let e=await le.subtle.exportKey("raw",t);return new Uint8Array(e)},{asString:s(async(t,e)=>{let r=await d.export(t);return e?g(r,e):U(r)},"asString")}),import(t,e){return Ce(typeof t=="string"?D(t):t,void 0,e)},async exportAsString(t){let e=await d.export(t);return U(e)},encrypt:di,async decrypt(t,e,r){let n=Se(e)?e:await Ce(e),i=r?await le.subtle.decrypt({name:W,iv:h(r)},n,typeof t=="string"?h(K(t,"base64pad")):t instanceof Uint8Array?h(t):t):await _t(t,n);return new Uint8Array(i)}};function Ce(t,e,r=!0){return le.subtle.importKey("raw",t instanceof Uint8Array?h(t):t,{name:W,length:e||256},r,["encrypt","decrypt"])}s(Ce,"importAesKey");async function di(t,e,r,n){let i=Se(e)?e:await Ce(e),o=n?await le.subtle.encrypt({name:W,iv:h(n)},i,t instanceof ArrayBuffer?t:h(typeof t=="string"?K(t):t)):await Ai(t,i);return r&&r==="arraybuffer"?o:new Uint8Array(o)}s(di,"encrypt");async function Ai(t,e,r){let n=te(t,r?.charsize??V),i=typeof e=="string"?await Oe(e,r):e,o=r?.iv||tr(B),a=await le.subtle.encrypt({name:W,iv:o},i,n);return $(o,a)}s(Ai,"encryptBytes");async function _t(t,e,r){let n=er(t),i=typeof e=="string"?await Oe(e,r):e,o=n.slice(0,B),a=n.slice(B);return await le.subtle.decrypt({name:L,iv:o},i,a)}s(_t,"decryptBytes");function sr(t){return new Promise((e,r)=>{t.oncomplete=t.onsuccess=()=>e(t.result),t.onabort=t.onerror=()=>r(t.error)})}s(sr,"promisifyRequest");function bi(t,e){let r,n=s(()=>{if(r)return r;let i=indexedDB.open(t);return i.onupgradeneeded=()=>i.result.createObjectStore(e),r=sr(i),r.then(o=>{o.onclose=()=>r=void 0},()=>{r=void 0}),r},"getDB");return(i,o)=>n().then(a=>o(a.transaction(e,i).objectStore(e)))}s(bi,"createStore");var Yr;function $r(){return Yr||(Yr=bi("keyval-store","keyval")),Yr}s($r,"defaultGetStore");function Ge(t,e=$r()){return e("readonly",r=>sr(r.get(t)))}s(Ge,"get");function qr(t,e,r=$r()){return r("readwrite",n=>(n.put(e,t),sr(n.transaction)))}s(qr,"set");function Jr(t,e=$r()){return e("readwrite",r=>(t.forEach(n=>r.delete(n)),sr(r.transaction)))}s(Jr,"delMany");var Yo=new Error("Key does not exist. Make sure you properly instantiated the keystore."),$o=new Error("Retrieved a symmetric key when an asymmetric keypair was expected. Please use a different key name."),qo=new Error("Retrieved an asymmetric keypair when an symmetric key was expected. Please use a different key name."),Jo=new Error("ECC is not enabled for this browser. Please use RSA instead."),Zo=new Error("Cryptosystem not supported. Please use ECC or RSA"),wi=new Error("Invalid key use. Please use 'exchange' or 'write'"),Qo=new Error("Max must be less than 256 and greater than 0"),Zr=class Zr extends Error{constructor(e){super(e||"These keys were deleted"),this.name="KeysDeletedError"}};s(Zr,"KeysDeletedError");var Pe=Zr;function Lt(t){Ki(t,["exchange","write","signing"],wi)}s(Lt,"checkValidKeyUse");function Ki(t,e,r){if(!e.some(i=>i===t))throw r}s(Ki,"checkValid");var ie=class ie{constructor(e){c(this,"DID");c(this,"hasPersisted");c(this,"isSessionOnly");c(this,"destroyed");c(this,"exchangeKeyName");c(this,"writeKeyName");c(this,"_exchangeKey");c(this,"_writeKey");let{keys:r}=e,n=this.constructor;this.DID=e.did,this._exchangeKey=r.exchange,this._writeKey=r.write,this.hasPersisted=e.hasPersisted,this.isSessionOnly=!!e.isSessionOnly,this.destroyed=!1,this.exchangeKeyName=e.exchangeKeyName||n.EXCHANGE_KEY_NAME,this.writeKeyName=e.writeKeyName||n.WRITE_KEY_NAME}get exchangeKey(){if(this.destroyed||!this._exchangeKey)throw new Pe;return this._exchangeKey}get writeKey(){if(this.destroyed||!this._writeKey)throw new Pe;return this._writeKey}publicExchangeKeyAsString(e){return this.publicExchangeKey.asString(e)}publicWriteKeyAsString(e){return this.publicWriteKey.asString(e)}get publicWriteKey(){let e=this.writeKey.publicKey;return Object.assign(e,{asString:s(async r=>{let n=await ne(this.writeKey),i=new Uint8Array(n);return r?g(i,r):U(i)},"asString")})}get publicExchangeKey(){let e=this.exchangeKey.publicKey;return Object.assign(e,{asString:s(async r=>{let n=await ne(this.exchangeKey),i=new Uint8Array(n);return r?g(i,r):U(i)},"asString")})}get privateWriteKey(){return this.writeKey.privateKey}get privateExchangeKey(){return this.exchangeKey.privateKey}get deviceName(){return ie.deviceName(this.DID)}static deviceName(e){return xi(e)}async persist(){if(this.destroyed)throw new Pe;this.isSessionOnly||(await Promise.all([qr(this.exchangeKeyName,this.exchangeKey),qr(this.writeKeyName,this.writeKey)]),this.hasPersisted=!0)}async delete(){let e=this.exchangeKeyName,r=this.writeKeyName;await Jr([e,r]),this.constructor._invalidate(e,r),this._destroy()}_destroy(){this.destroyed=!0,this.hasPersisted=!1,this._exchangeKey=null,this._writeKey=null}static _invalidate(e,r){let n=this._instance;n&&n.exchangeKeyName===e&&n.writeKeyName===r&&(n._destroy(),this._instance=null)}static async delete(e={}){let r=e.encryptionKeyName||this.EXCHANGE_KEY_NAME,n=e.writeKeyName||this.WRITE_KEY_NAME;await Jr([r,n]),this._invalidate(r,n)}async getDeviceName(){return ie.deviceName(this.DID)}static _createExchangeKeys(e){throw new Error("The child should implement this")}static _createWriteKeys(e){throw new Error("The child should implement this")}static _keyNames(e){return e?{exchangeKeyName:`${e}.${this.EXCHANGE_KEY_NAME}`,writeKeyName:`${e}.${this.WRITE_KEY_NAME}`}:{exchangeKeyName:this.EXCHANGE_KEY_NAME,writeKeyName:this.WRITE_KEY_NAME}}static async create(e,r,n){let i=n?.exchangeKeys||await this._createExchangeKeys(r),o=n?.writeKeys||await this._createWriteKeys(r),a=await ne(o),y=await He(new Uint8Array(a),this.TYPE==="ecc"?"ed25519":"rsa"),{exchangeKeyName:p,writeKeyName:u}=this._keyNames(n?.dbKey),l=new this({keys:{exchange:i,write:o},did:y,hasPersisted:!1,isSessionOnly:!!e,exchangeKeyName:p,writeKeyName:u});return this._instance=l,l}static async exist(e={}){let r=await Ge(e.writeKeyName||this.WRITE_KEY_NAME),n=await Ge(e.encryptionKeyName||this.EXCHANGE_KEY_NAME);return!(!r||!n)}static async load(e={session:!1}){let r=e.persist!==!1,n=this._keyNames(e.dbKey),i=e.encryptionKeyName||n.exchangeKeyName,o=e.writeKeyName||n.writeKeyName,a=this._instance;if(a&&!a.destroyed&&a.exchangeKeyName===i&&a.writeKeyName===o)return r&&!a.hasPersisted&&await a.persist(),a;let y=!0,p=await Ge(i),u=await Ge(o);p||(y=!1,p=await this._createExchangeKeys(e.extractable)),u||(y=!1,u=await this._createWriteKeys(e.extractable));let l=await ne(u),b=await He(new Uint8Array(l),this.TYPE==="ecc"?"ed25519":"rsa"),x=new this({keys:{exchange:p,write:u},did:b,hasPersisted:y,isSessionOnly:!!e.session,exchangeKeyName:i,writeKeyName:o});return this._instance=x,r&&!y&&await x.persist(),x}};s(ie,"AbstractKeys"),c(ie,"EXCHANGE_KEY_NAME"),c(ie,"WRITE_KEY_NAME"),c(ie,"_instance");var ve=ie;async function kt(t,e){let{content:r,publicKey:n}=t,i=e||await d.create(),o=await d.encrypt(typeof r=="string"?K(r):r,typeof i=="string"?await d.import(i):i),a=await Qr({key:i,publicKey:n});return $(a,o)}s(kt,"encryptTo");kt.asString=async function(t,e){let r=await kt(t,e);return g(new Uint8Array(r),"base64pad")};async function Qr({key:t,publicKey:e},r){let n;t instanceof CryptoKey?n=await d.export(t):n=t;let i=await q.encrypt(n,e);return r&&r==="arraybuffer"?i:new Uint8Array(i)}s(Qr,"encryptKeyTo");Qr.asString=async function({key:t,publicKey:e},r){let n=await Qr({key:t,publicKey:e});return r?g(n,r):U(n)};async function xi(t){let e=await Tt(new TextEncoder().encode(t.normalize("NFD")));return g(e,"base32").slice(0,32)}s(xi,"getDeviceName");var et=globalThis.crypto;function Mt(t){return new Uint8Array(t).buffer}s(Mt,"toArrayBuffer");function Ei(t){return t.algorithm.modulusLength/8}s(Ei,"rsaCiphertextByteLength");var ge=class ge extends ve{static async create(e,r,n){return await super.create(e,r,n)}static async load(e={session:!1}){return super.load(e)}static async _createExchangeKeys(e=!1){return await Wr(qe,ue,"exchange",e)}static async _createWriteKeys(e=!1){return await Wr(qe,ue,"write",e)}async toJson(e){let r=this.publicExchangeKey,n=this.DID,i=await et.subtle.exportKey("spki",r);return{publicExchangeKey:e?g(new Uint8Array(i),e):U(i),DID:n}}async sign(e,r=V){let n=this.writeKey,i=await q.sign(e,n.privateKey,r);return new Uint8Array(i)}async signAsString(e,r){let n=await this.sign(e,r);return U(n)}async encrypt(e,r,n,i,o){let a=r||this.exchangeKey.publicKey,y=n,p=i,u=y||await d.create({length:p||o}),l=await d.encrypt(typeof e=="string"?K(e):e,typeof u=="string"?await d.import(u):u),b=await Be({key:u,publicKey:a}),x=$(b,l);return new Uint8Array(x)}async encryptAsString(e,r,n,i,o){let a=await this.encrypt(e,r,n,i,o);return g(a,"base64pad")}async decrypt(e,r,n){let i=fe(e,D),o=Ei(this.exchangeKey.privateKey),a=i.slice(0,o),y=i.slice(o),p=await this.decryptKey(a);return await d.decrypt(y,p)}async decryptAsString(e,r,n){let i=await this.decrypt(e,r,n);return g(i)}async decryptKey(e){return await q.decrypt(e,this.exchangeKey.privateKey)}async decryptKeyAsString(e,r){let n=await q.decrypt(e,this.exchangeKey.privateKey);return g(n,r)}async getAesKey(e,r){if(!e)throw new Error("RSA requires an encrypted AES key to decrypt");if(typeof e=="string"){let n=await this.decryptKey(e);return await d.import(n)}else return e}};s(ge,"RsaKeys"),c(ge,"TYPE","rsa"),c(ge,"INFO","keys"),c(ge,"EXCHANGE_KEY_NAME",Hr),c(ge,"WRITE_KEY_NAME",Gr);var he=ge;Object.assign(he.prototype.sign,{asString:s(async function(t,e){return this.signAsString(t,e)},"asString")});Object.assign(he.prototype.encrypt,{asString:s(async function(t,e,r,n){return this.encryptAsString(t,e,r,n)},"asString")});Object.assign(he.prototype.decrypt,{asString:s(async function(t,e){return this.decryptAsString(t,e)},"asString")});async function Ui(t,e,r){let n=rr(r),i=await ze(n.publicKey.buffer,"SHA-256","signing");try{return q.verify(t,e,i)}catch{return!1}}s(Ui,"verify");Object.assign(he.prototype.decryptKey,{asString:s(async function(t,e){return this.decryptKeyAsString(t,e)},"asString")});async function Rt(t,e){let{content:r,publicKey:n}=t,i=e||await d.create(),o=await d.encrypt(typeof r=="string"?K(r):r,typeof i=="string"?await d.import(i):i),a=await Be({key:i,publicKey:n});return $(a,o)}s(Rt,"encryptTo");Rt.asString=async function(t,e){let{content:r,publicKey:n}=t,i=e||await d.create(),o=await d.encrypt(typeof r=="string"?K(r):r,typeof i=="string"?await d.import(i):i,"arraybuffer"),a=await Be({key:i,publicKey:n}),y=$(a,o);return g(new Uint8Array(y),"base64pad")};async function Be({key:t,publicKey:e},r){let n;t instanceof CryptoKey?n=await d.export(t):n=t;let i=await q.encrypt(n,e);return r&&r==="arraybuffer"?i:new Uint8Array(i)}s(Be,"encryptKeyTo");Be.asString=async function({key:t,publicKey:e},r){let n=await Be({key:t,publicKey:e});return r?g(n,r):U(n)};async function Si(t,e,r,n){let i=Se(e)?e:await Ce(e),o=n?await et.subtle.encrypt({name:W,iv:Mt(n)},i,Mt(t)):await Ci(t,i);return r&&r==="arraybuffer"?o:new Uint8Array(o)}s(Si,"encrypt");async function Ci(t,e,r){let n=te(t,r?.charsize??V),i=typeof e=="string"?await Oe(e,r):e,o=r?.iv||tr(B),a=await et.subtle.encrypt({name:W,iv:o},i,n);return $(o,a)}s(Ci,"encryptBytes");var Ft={};v(Ft,{EccKeys:()=>oe,default:()=>Pi,exportPublicKey:()=>me,importPublicKey:()=>Ve,verify:()=>Gt});var A=globalThis.crypto;function se(t){return new Uint8Array(t).buffer}s(se,"toArrayBuffer");var k=class k extends ve{static async create(e,r,n){return await super.create(e,r,n)}get publicExchangeKey(){let e=this.exchangeKey.publicKey;return Object.assign(e,{asString:s(async r=>{let n=await me(this.exchangeKey),i=new Uint8Array(n);return r?g(i,r):U(n)},"asString")})}get publicWriteKey(){let e=this.writeKey.publicKey;return Object.assign(e,{asString:s(async r=>{let n=await me(this.writeKey),i=new Uint8Array(n);return r?g(i,r):U(n)},"asString")})}static async _createExchangeKeys(e=!1){return await A.subtle.generateKey({name:ke},e,["deriveKey","deriveBits"])}static async _createWriteKeys(e=!1){return await A.subtle.generateKey({name:pe},e,["sign","verify"])}static async load(e={session:!1}){return super.load(e)}async toJson(e){let r=this.DID,n=await me(this.exchangeKey);return{publicExchangeKey:e?g(new Uint8Array(n),e):U(n),DID:r}}async wrap(e,r,n){let i=await k._createExchangeKeys(!1),o=await me(i),a=new Uint8Array(o),y=new Uint8Array(await A.subtle.exportKey("raw",r)),p=n||"key-wrap",u=A.getRandomValues(new Uint8Array(C)),l=await Fe(i.privateKey,r,u,zt(a,y,p)),b,x=!1;if(e instanceof CryptoKey){let T=await A.subtle.exportKey("raw",e);b=new Uint8Array(T),x=!0}else typeof e=="string"?(b=K(e,"base64pad"),x=!0):b=e;let H=A.getRandomValues(new Uint8Array(B)),M=await A.subtle.encrypt({name:L,iv:H},l,se(b));x&&b.fill(0);let _=new Uint8Array(H),j=new Uint8Array(M),f=1+C+_.length+j.length,m=new Uint8Array(f);m[0]=Qe,m.set(u,1),m.set(_,1+C),m.set(j,1+C+_.length);let S=Ht(a,y,m,p),P=await this.sign(S);return{enc:U(a),wrappedKey:U(m),signature:g(P,"base64pad")}}async unwrap(e,r,n={}){let{enc:i,wrappedKey:o,signature:a}=e,y=n.info,p=n.extractable??!0;if(!r)throw new Error("`unwrap` requires the DID of the sender");if(!a)throw new Error("The wrapped key is not signed");let u=K(i,"base64pad");if(u.length!==F)throw new Error(`\`enc\` must be a ${F}-byte X25519 public key, got ${u.length} bytes`);let l=new Uint8Array(D(o)),b=y||"key-wrap",x=1+C+B+zr;if(l.length<x)throw new Error(`Wrapped key must be at least ${x} bytes, got ${l.length} bytes`);let H=l[0];if(H!==Qe)throw new Error(`Unsupported wrapped key format version: ${H}`);let M=new Uint8Array(await me(this.exchangeKey)),_=Ht(u,M,l,b);if(!await Gt(_,a,r))throw new Error("Invalid signature for the wrapped key");let f=await Ve(i,"X25519","exchange"),m=l.slice(1,1+C),S=l.slice(1+C,1+C+B),P=l.slice(1+C+B),T=await Fe(this.exchangeKey.privateKey,f,m,zt(u,M,b)),R=await A.subtle.decrypt({name:L,iv:S},T,se(P));return await A.subtle.importKey("raw",R,{name:L},p,["encrypt","decrypt"])}async encrypt(e,r,n,i,o){let a=A.getRandomValues(new Uint8Array(C)),y=r||this.exchangeKey.publicKey,p;typeof y=="string"?p=await Ve(y,"X25519","exchange"):p=y;let u=await k._createExchangeKeys(!1),l=i?i instanceof CryptoKey?i:await d.import(i):await Fe(u.privateKey,p,a,n||k.INFO,o),b=A.getRandomValues(new Uint8Array(B)),x=typeof e=="string"?new TextEncoder().encode(e):e,H=await A.subtle.encrypt({name:L,iv:b},l,se(x)),M=new Uint8Array(await me(u)),_=new Uint8Array(a),j=new Uint8Array(b),f=new Uint8Array(H),m=M.length+_.length+j.length+f.length,S=new Uint8Array(m);return S.set(M,0),S.set(_,M.length),S.set(j,M.length+_.length),S.set(f,M.length+_.length+j.length),S}async encryptAsString(e,r,n,i,o){let a=await this.encrypt(e,r,n||k.INFO,i,o);return g(new Uint8Array(a),"base64pad")}async decrypt(e,r,n,i){let o=fe(e,D),a=o.slice(0,F),y=await A.subtle.importKey("raw",a,{name:ke},!0,[]),p=o.slice(F,F+C),u=o.slice(F+C,F+C+B),l=o.slice(F+C+B),{privateKey:b}=this.exchangeKey,x=i?i instanceof CryptoKey?i:await d.import(i):await Fe(b,y,p,n||k.INFO);return await A.subtle.decrypt({name:r||L,iv:u},x,l)}async decryptAsString(e,r,n,i){let o=await this.decrypt(e,r,n,i);return g(new Uint8Array(o))}async getAesKey(e,r,n){e||(e=this.publicExchangeKey);let i;typeof e=="string"?i=await Ve(e,"X25519","exchange"):i=e;let o=n?new Uint8Array(typeof n=="string"?D(n):n):A.getRandomValues(new Uint8Array(C)),a=await Fe(this.exchangeKey.privateKey,i,o,r||k.INFO);return Object.assign(a,{salt:U(o)})}async sign(e,r){let n=new TextEncoder,i;typeof e=="string"?i=n.encode(e):e instanceof ArrayBuffer?i=new Uint8Array(e):i=e;let o=await A.subtle.sign({name:pe},this.writeKey.privateKey,se(i));return new Uint8Array(o)}async signAsString(e,r){let n=await this.sign(e,r);return g(n,"base64pad")}};s(k,"EccKeys"),c(k,"TYPE","ecc"),c(k,"EXCHANGE_KEY_NAME",Fr),c(k,"WRITE_KEY_NAME",Vr),c(k,"INFO","keys");var oe=k;Object.assign(oe.prototype.sign,{asString:s(async function(t,e){let r=await this.sign(t,e);return g(r,"base64pad")},"asString")});async function me(t,e="raw"){return await A.subtle.exportKey(e,t.publicKey)}s(me,"exportPublicKey");async function Ve(t,e,r){Lt(r);let n=r==="exchange"?ke:pe,i=r==="exchange"?[]:["verify"],o=D(t),a=e==="X25519"?{name:n}:{name:n,namedCurve:e};return A.subtle.importKey("raw",o,a,!0,i)}s(Ve,"importPublicKey");var Pi={EccKeys:oe,importPublicKey:Ve};Object.assign(oe.prototype.encrypt,{asString:s(async function(t,e,r,n,i){return this.encryptAsString(t,e,r,n,i)},"asString")});Object.assign(oe.prototype.decrypt,{asString:s(async function(t,e){return this.decryptAsString(t,e)},"asString")});Object.assign(oe.prototype.sign,{asString:s(async function(t,e){return this.signAsString(t,e)},"asString")});async function Gt(t,e,r){try{let n=rr(r);if(n.type!=="ed25519")throw new Error(`Expected Ed25519 key for ECC verification, got ${n.type}`);let i=n.publicKey.slice(-32),o=await A.subtle.importKey("raw",i,{name:pe},!0,["verify"]),a=new TextEncoder,y;typeof t=="string"?y=a.encode(t):t instanceof ArrayBuffer?y=new Uint8Array(t):y=t;let p;return typeof e=="string"?p=new Uint8Array(D(e)):p=e,await A.subtle.verify({name:pe},o,se(p),se(y))}catch{return!1}}s(Gt,"verify");function zt(t,e,r){let n=new TextEncoder().encode(r),i=new Uint8Array(t.length+e.length+n.length);return i.set(t,0),i.set(e,t.length),i.set(n,t.length+e.length),i}s(zt,"wrapKekInfo");function Ht(t,e,r,n){let i=new TextEncoder().encode(n),o=new Uint8Array(4);new DataView(o.buffer).setUint32(0,r.length);let a=new Uint8Array(t.length+e.length+o.length+r.length+i.length),y=0;return a.set(t,y),y+=t.length,a.set(e,y),y+=e.length,a.set(o,y),y+=o.length,a.set(r,y),y+=r.length,a.set(i,y),a}s(Ht,"wrapSignData");async function Fe(t,e,r,n,i){let o=new TextEncoder,a=typeof n=="string"?o.encode(n):n,y=await A.subtle.deriveBits({name:"X25519",public:e},t,256),p=await A.subtle.importKey("raw",y,"HKDF",!1,["deriveKey"]);return A.subtle.deriveKey({name:"HKDF",hash:"SHA-256",salt:r instanceof Uint8Array?se(r):r,info:se(a)},p,{name:"AES-GCM",length:i||re},!1,["encrypt","decrypt"])}s(Fe,"deriveKey");export{W as AES_GCM,zr as AES_GCM_TAG_LENGTH,ve as AbstractKeys,Y as BASE58_DID_PREFIX,Je as BLS_DID_PREFIX,cr as CharSize,nt as CryptoSystem,V as DEFAULT_CHAR_SIZE,Fr as DEFAULT_ECC_EXCHANGE,ii as DEFAULT_ECC_EXCHANGE_CURVE,Vr as DEFAULT_ECC_WRITE,ue as DEFAULT_HASH_ALGORITHM,Hr as DEFAULT_RSA_EXCHANGE,qe as DEFAULT_RSA_SIZE,Gr as DEFAULT_RSA_WRITE,L as DEFAULT_SYMM_ALGORITHM,re as DEFAULT_SYMM_LENGTH,ke as ECC_EXCHANGE_ALGORITHM,pe as ECC_WRITE_ALGORITHM,Ze as EDWARDS_DID_PREFIX,yr as EccCurve,ar as HashAlg,B as IV_LENGTH,Me as KEY_TYPE,vt as KEY_USE,ye as KeyUse,Pe as KeysDeletedError,Q as RSA_ALGORITHM,$e as RSA_DID_PREFIX,Ye as RSA_HASHING_ALGORITHM,ee as RSA_SIGN_ALGORITHM,rt as RsaSize,C as SALT_LENGTH,tt as SymmKeyLength,Qe as WRAP_FORMAT_VERSION,F as X25519_PUBLIC_KEY_LENGTH,Nt as aes,li as checkValidKeyUse,nr as did,ir as didToPublicKey,Ft as ecc,It as ed25519Verify,Qr as encryptKeyTo,kt as encryptTo,fi as getAlgorithm,xi as getDeviceName,ne as getPublicKeyAsArrayBuffer,Xr as getPublicKeyAsUint8Array,mi as importRsaKey,Se as isCryptoKey,ko as keyTypeFromDid,He as publicKeyToDid,Ot as rsa,ze as rsaImportPublicKey,q as rsaOperations,Dt as rsaVerify,Lo as verify};
|
|
4
4
|
//# sourceMappingURL=index.min.js.map
|