import{b as ot,c as it}from"./chunk-46XHGFCR.min.js";import{a as dt}from"./chunk-W2UANUF5.min.js";import{a as ie,b as ce,c as ct,d as at,e as ft}from"./chunk-JBWXLFTW.min.js";import"./chunk-6W6YJVWD.min.js";import{a as ae,b as lt}from"./chunk-T3V5ENTO.min.js";import"./chunk-I5LEGGLB.min.js";import{a as et,b as O,c as _,d as nt,e as st,f as U,g as rt,h as se,i as re,j as K,k as oe,l as I}from"./chunk-ZX2MET3W.min.js";import"./chunk-KCJWWN4G.min.js";import{a as F}from"./chunk-33Z4575M.min.js";import"./chunk-WSQOG2X5.min.js";import"./chunk-KHOUPFOL.min.js";import"./chunk-4PJ2LHHI.min.js";import"./chunk-UNSZMVZB.min.js";var bt={p:0x7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffedn,n:0x1000000000000000000000000000000014def9dea2f79cd65812631a5cf5d3edn,h:8n,a:0x7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffecn,d:0x52036cee2b6ffe738cc740797779e89800700a4d4141d8ab75eb4dca135978a3n,Gx:0x216936d3cd6e53fec0a4e231fdd6dc5c692cc7609525a7b2c9562d608f25d51an,Gy:0x6666666666666666666666666666666666666666666666666666666666666658n},{p:h,n:C,Gx:ut,Gy:pt,a:V,d:L,h:Yt}=bt,P=32,$=64,kt=(...e)=>{"captureStackTrace"in Error&&typeof Error.captureStackTrace=="function"&&Error.captureStackTrace(...e)},y=(e="")=>{let t=new Error(e);throw kt(t,y),t},qt=e=>typeof e=="bigint",Ft=e=>typeof e=="string",Ot=e=>e instanceof Uint8Array||ArrayBuffer.isView(e)&&e.constructor.name==="Uint8Array",D=(e,t,n="")=>{let s=Ot(e),r=e?.length,o=t!==void 0;if(!s||o&&r!==t){let c=n&&`"${n}" `,a=o?` of length ${t}`:"",f=s?`length=${r}`:`type=${typeof e}`;y(c+"expected Uint8Array"+a+", got "+f)}return e},R=e=>new Uint8Array(e),wt=e=>Uint8Array.from(e),xt=(e,t)=>e.toString(16).padStart(t,"0"),St=e=>Array.from(D(e)).map(t=>xt(t,2)).join(""),w={_0:48,_9:57,A:65,F:70,a:97,f:102},yt=e=>{if(e>=w._0&&e<=w._9)return e-w._0;if(e>=w.A&&e<=w.F)return e-(w.A-10);if(e>=w.a&&e<=w.f)return e-(w.a-10)},Bt=e=>{let t="hex invalid";if(!Ft(e))return y(t);let n=e.length,s=n/2;if(n%2)return y(t);let r=R(s);for(let o=0,c=0;o<s;o++,c+=2){let a=yt(e.charCodeAt(c)),f=yt(e.charCodeAt(c+1));if(a===void 0||f===void 0)return y(t);r[o]=a*16+f}return r},Dt=()=>globalThis?.crypto,_t=()=>Dt()?.subtle??y("crypto.subtle must be defined, consider polyfill"),Z=(...e)=>{let t=R(e.reduce((s,r)=>s+D(r).length,0)),n=0;return e.forEach(s=>{t.set(s,n),n+=s.length}),t},Vt=(e=P)=>Dt().getRandomValues(R(e)),N=BigInt,x=(e,t,n,s="bad number: out of range")=>qt(e)&&t<=e&&e<n?e:y(s),i=(e,t=h)=>{let n=e%t;return n>=0n?n:t+n},vt=e=>i(e,C),Lt=(e,t)=>{(e===0n||t<=0n)&&y("no inverse n="+e+" mod="+t);let n=i(e,t),s=t,r=0n,o=1n,c=1n,a=0n;for(;n!==0n;){let f=s/n,l=s%n,d=r-c*f,u=o-a*f;s=n,n=l,r=c,o=a,c=d,a=u}return s===1n?i(r,t):y("no inverse")};var j=e=>e instanceof S?e:y("Point expected"),G=2n**256n,S=class e{static BASE;static ZERO;X;Y;Z;T;constructor(t,n,s,r){let o=G;this.X=x(t,0n,o),this.Y=x(n,0n,o),this.Z=x(s,1n,o),this.T=x(r,0n,o),Object.freeze(this)}static CURVE(){return bt}static fromAffine(t){return new e(t.x,t.y,1n,i(t.x*t.y))}static fromBytes(t,n=!1){let s=L,r=wt(D(t,P)),o=t[31];r[31]=o&-129;let c=Kt(r);x(c,0n,n?G:h);let f=i(c*c),l=i(f-1n),d=i(s*f+1n),{isValid:u,value:p}=Gt(l,d);u||y("bad point: y not sqrt");let m=(p&1n)===1n,g=(o&128)!==0;return!n&&p===0n&&g&&y("bad point: x==0, isLastByteOdd"),g!==m&&(p=i(-p)),new e(p,c,1n,i(p*c))}static fromHex(t,n){return e.fromBytes(Bt(t),n)}get x(){return this.toAffine().x}get y(){return this.toAffine().y}assertValidity(){let t=V,n=L,s=this;if(s.is0())return y("bad point: ZERO");let{X:r,Y:o,Z:c,T:a}=s,f=i(r*r),l=i(o*o),d=i(c*c),u=i(d*d),p=i(f*t),m=i(d*i(p+l)),g=i(u+i(n*i(f*l)));if(m!==g)return y("bad point: equation left != right (1)");let v=i(r*o),A=i(c*a);return v!==A?y("bad point: equation left != right (2)"):this}equals(t){let{X:n,Y:s,Z:r}=this,{X:o,Y:c,Z:a}=j(t),f=i(n*a),l=i(o*r),d=i(s*a),u=i(c*r);return f===l&&d===u}is0(){return this.equals(E)}negate(){return new e(i(-this.X),this.Y,this.Z,i(-this.T))}double(){let{X:t,Y:n,Z:s}=this,r=V,o=i(t*t),c=i(n*n),a=i(2n*i(s*s)),f=i(r*o),l=t+n,d=i(i(l*l)-o-c),u=f+c,p=u-a,m=f-c,g=i(d*p),v=i(u*m),A=i(d*m),T=i(p*u);return new e(g,v,T,A)}add(t){let{X:n,Y:s,Z:r,T:o}=this,{X:c,Y:a,Z:f,T:l}=j(t),d=V,u=L,p=i(n*c),m=i(s*a),g=i(o*u*l),v=i(r*f),A=i((n+s)*(c+a)-p-m),T=i(v-g),Q=i(v+g),tt=i(m-d*p),Zt=i(A*T),Nt=i(Q*tt),Xt=i(A*tt),Rt=i(T*Q);return new e(Zt,Nt,Rt,Xt)}subtract(t){return this.add(j(t).negate())}multiply(t,n=!0){if(!n&&(t===0n||this.is0()))return E;if(x(t,1n,C),t===1n)return this;if(this.equals(B))return te(t).p;let s=E,r=B;for(let o=this;t>0n;o=o.double(),t>>=1n)t&1n?s=s.add(o):n&&(r=r.add(o));return s}multiplyUnsafe(t){return this.multiply(t,!1)}toAffine(){let{X:t,Y:n,Z:s}=this;if(this.equals(E))return{x:0n,y:1n};let r=Lt(s,h);i(s*r)!==1n&&y("invalid inverse");let o=i(t*r),c=i(n*r);return{x:o,y:c}}toBytes(){let{x:t,y:n}=this.assertValidity().toAffine(),s=At(n);return s[31]|=t&1n?128:0,s}toHex(){return St(this.toBytes())}clearCofactor(){return this.multiply(N(Yt),!1)}isSmallOrder(){return this.clearCofactor().is0()}isTorsionFree(){let t=this.multiply(C/2n,!1).double();return C%2n&&(t=t.add(this)),t.is0()}},B=new S(ut,pt,1n,i(ut*pt)),E=new S(0n,1n,1n,0n);S.BASE=B;S.ZERO=E;var At=e=>Bt(xt(x(e,0n,G),$)).reverse(),Kt=e=>N("0x"+St(wt(D(e)).reverse())),b=(e,t)=>{let n=e;for(;t-- >0n;)n*=n,n%=h;return n},jt=e=>{let n=e*e%h*e%h,s=b(n,2n)*n%h,r=b(s,1n)*e%h,o=b(r,5n)*r%h,c=b(o,10n)*o%h,a=b(c,20n)*c%h,f=b(a,40n)*a%h,l=b(f,80n)*f%h,d=b(l,80n)*f%h,u=b(d,10n)*o%h;return{pow_p_5_8:b(u,2n)*e%h,b2:n}},ht=0x2b8324804fc1df0b2b4d00993dfbd7a72f431806ad2fe478c4ee1b274a0ea0b0n,Gt=(e,t)=>{let n=i(t*t*t),s=i(n*n*t),r=jt(e*s).pow_p_5_8,o=i(e*n*r),c=i(t*o*o),a=o,f=i(o*ht),l=c===e,d=c===i(-e),u=c===i(-e*ht);return l&&(o=a),(d||u)&&(o=f),(i(o)&1n)===1n&&(o=i(-o)),{isValid:l||d,value:o}},H=e=>vt(Kt(e)),z=(...e)=>zt.sha512Async(Z(...e));var Ht=e=>{let t=e.slice(0,P);t[0]&=248,t[31]&=127,t[31]|=64;let n=e.slice(P,$),s=H(t),r=B.multiply(s),o=r.toBytes();return{head:t,prefix:n,scalar:s,point:r,pointBytes:o}},It=e=>z(D(e,P)).then(Ht);var Y=e=>It(e).then(t=>t.pointBytes);var Mt=e=>z(e.hashable).then(e.finish);var $t=(e,t,n)=>{let{pointBytes:s,scalar:r}=e,o=H(t),c=B.multiply(o).toBytes();return{hashable:Z(c,s,n),finish:l=>{let d=vt(o+H(l)*r);return D(Z(c,At(d)),$)}}},Et=async(e,t)=>{let n=D(e),s=await It(t),r=await z(s.prefix,n);return Mt($t(s,r,n))};var zt={sha512Async:async e=>{let t=_t(),n=Z(e);return R(await t.digest("SHA-512",n.buffer))},sha512:void 0},Wt=(e=Vt(P))=>e;var Pt=async e=>{let t=Wt(e),n=await Y(t);return{secretKey:t,publicKey:n}};var X=8,Jt=256,Tt=Math.ceil(Jt/X)+1,M=2**(X-1),Qt=()=>{let e=[],t=B,n=t;for(let s=0;s<Tt;s++){n=t,e.push(n);for(let r=1;r<M;r++)n=n.add(t),e.push(n);t=n.double()}return e},mt,gt=(e,t)=>{let n=t.negate();return e?n:t},te=e=>{let t=mt||(mt=Qt()),n=E,s=B,r=2**X,o=r,c=N(r-1),a=N(X);for(let f=0;f<Tt;f++){let l=Number(e&c);e>>=a,l>M&&(l-=o,e+=1n);let d=f*M,u=d,p=d+Math.abs(l)-1,m=f%2!==0,g=l<0;l===0?s=s.add(gt(m,t[u])):n=n.add(gt(g,t[p]))}return e!==0n&&y("invalid wnaf"),{p:n,f:s}};var Ut=globalThis.crypto;function W(e,t,n){let s=U.fromPublicKey(e,t);if(n){if(!et(n.verifiableDid.publicKey,s.verifiableDid.publicKey))throw new Error("Public key mismatch");if(n.verifiableDid.type!==s.verifiableDid.type)throw new Error("Key type mismatch");s=n}return s}globalThis.crypto||(globalThis.crypto=Ut);function ee(e){if(e.verifiableDid.type!=="Ed25519")throw new TypeError(`Unsupported key type ${e.verifiableDid.type}`)}var k=class e extends rt{static code=4864;#t;signatureType;constructor(t,n){ee(t),super(t),this.#t=n,this.signatureType="Ed25519"}static async generate(t){let n,s;if(t)n=t,s=await Y(n);else{let r=await Pt();n=r.secretKey,s=r.publicKey}return new e(U.fromPublicKey("Ed25519",s),n)}static async import(t,n){let s=st(e.code,O.decode(t)),r=await Y(s);return new e(W("Ed25519",r,n),s)}static importJwk(t,n){let s=_.decode(t.d),r=_.decode(t.x);return new e(W("Ed25519",r,n),s)}sign(t){return Et(t,this.#t)}export(){return O.encode(nt(e.code,this.#t))}toString(){return this.didObject.didUrl}};var{WARN:Pe,LOG:ne,DEBUG:J,VERBOSE:Te,ERROR:q}=F.setup(F.INFO),Ct=class e{constructor(t,n,s,r=[],o=t,c){this.baseUrl=t;this.issuerKey=n;this.tmpKey=s;this.delegations=r;this.gatewayBaseUrl=o;this.nameBaseUrl=c}static async init({url:t,issuerKey:n,delegations:s,...r}){let o=I(t),c=await k.generate();ne("Init UcanStoreProxyConnector:",{url:t,serviceDID:o,issuerDID:n.did,tmpKeyDID:c.did});let a=s??[];if(!s){let f=["/storage/read","/storage/store","/storage/pin","/ipns/publish"];for(let l of f){let d=await K({issuer:n.did,audience:c.did,subject:o,command:l,expiration:1/0,signer:n});a.push(d),J("Created parent delegation",{cmd:l,cid:d.cid.toString()})}}return Object.assign(new e(t,n,c,a),r)}get isSetup(){return!0}get isValid(){return!0}async storeCar(t,n){let s=I(this.baseUrl),r=this.delegations.find(c=>c.payload.cmd==="/storage/store");if(!r)throw q("No parent delegation found for /storage/store");J("[storeCar] Creating request delegation:",{issuer:this.tmpKey.did,audience:s,subject:s,parentIss:r.payload.iss,parentAud:r.payload.aud,parentSub:r.payload.sub});let o=await K({issuer:this.tmpKey.did,audience:s,subject:s,command:"/storage/store",expiration:1/0,proofs:[r],signer:this.tmpKey});return lt(this.baseUrl,t,o,n)}async pushDag(t,n,s){let r=I(this.baseUrl),o=this.delegations.find(a=>a.payload.cmd==="/storage/store");if(!o)throw q("No parent delegation found for /storage/store");let c=await K({issuer:this.tmpKey.did,audience:r,subject:r,command:"/storage/store",expiration:1/0,proofs:[o],signer:this.tmpKey});return ot(this.baseUrl,t,n,{delegation:c,signal:s})}async pullDag(t,n,s){let r=I(this.baseUrl),o=this.delegations.find(a=>a.payload.cmd==="/storage/read");if(!o)throw q("No parent delegation found for /storage/read");let c=await K({issuer:this.tmpKey.did,audience:r,subject:r,command:"/storage/read",expiration:1/0,proofs:[o],signer:this.tmpKey});return it(this.baseUrl,t,n,{delegation:c,signal:s})}async retrieveCar(t){if(!this.gatewayBaseUrl)throw new Error("gatewayBaseUrl required for retrieveCar");return ft(this.gatewayBaseUrl,t)}async retrieveThread(...t){if(!this.gatewayBaseUrl)throw new Error("gatewayBaseUrl required for retrieveThread");return ct(this,...t)}async resolveIPNS(...t){if(!this.baseUrl)throw new Error("baseUrl required for resolveIPNS");return at(this,...t)}async publishIPNS(t,n){let s=this.nameBaseUrl??this.baseUrl,r=I(s),o=this.delegations.find(a=>a.payload.cmd==="/ipns/publish");if(!o)throw q("No parent delegation found for /ipns/publish");J("[publishIPNS] Creating request delegation:",{issuer:this.tmpKey.did,audience:r,subject:r,parentIss:o.payload.iss,parentAud:o.payload.aud,parentSub:o.payload.sub});let c=await K({issuer:this.tmpKey.did,audience:r,subject:r,command:"/ipns/publish",expiration:1/0,proofs:[o],signer:this.tmpKey});return dt(s,t,n,c)}asIPNSTarget(){return{name:`ucan-proxy:${this.baseUrl}`,publish:async(t,n)=>{let s=btoa(String.fromCharCode(...n));await this.publishIPNS(t,s)}}}};export{ae as DagIncompleteError,ie as IPNSNeverPublishedError,ce as IPNSResolutionError,Ct as UcanStoreProxyConnector,K as createStoreDelegation,oe as delegationToBase64,I as getServiceDID,se as normalizeCommand,dt as publishIPNS,at as resolveIPNS,ft as retrieveCar,ct as retrieveThread,re as serializeDelegation,lt as storeCar};
/*! Bundled license information:

@noble/ed25519/index.js:
  (*! noble-ed25519 - MIT License (c) 2019 Paul Miller (paulmillr.com) *)
*/
//# sourceMappingURL=index.min.js.map