@calimero-network/mero-js 22.2.0 → 22.4.0
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/README.md +5 -0
- package/dist/index.browser.mjs +2 -2
- package/dist/index.browser.mjs.map +4 -4
- package/dist/index.cjs +295 -21
- package/dist/index.cjs.map +4 -4
- package/dist/index.d.ts +2 -2
- package/dist/index.js +1 -1
- package/dist/index.mjs +295 -21
- package/dist/index.mjs.map +4 -4
- package/dist/relay/index.d.ts +1 -1
- package/dist/relay/relay-client.d.ts +76 -0
- package/dist/relay/relay-client.js +77 -0
- package/dist/sealed/release.d.ts +16 -4
- package/dist/sealed/release.js +35 -5
- package/dist/warrant/creation-warrant.d.ts +145 -0
- package/dist/warrant/creation-warrant.js +189 -0
- package/dist/warrant/index.d.ts +2 -0
- package/dist/warrant/index.js +1 -0
- package/dist/warrant/warrant.d.ts +5 -0
- package/dist/warrant/warrant.js +5 -2
- package/package.json +1 -1
package/dist/index.browser.mjs
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
function _e(n){let e=n.filter(Boolean);if(e.length===0)return;let t=AbortSignal;if(typeof t.any=="function")try{return t.any(e)}catch{}let r=new AbortController,s=i=>{r.abort(i.target.reason);for(let a of e)a.removeEventListener("abort",s)};for(let i of e){if(i.aborted)return AbortSignal.abort(i.reason);i.addEventListener("abort",s,{once:!0})}return r.signal}function qe(n){if(typeof AbortSignal.timeout=="function")return AbortSignal.timeout(n);let e=new AbortController;return setTimeout(()=>{e.abort(new DOMException("Timeout","TimeoutError"))},n),e.signal}var qn=new Set(["token_reuse","token_revoked"]);function Hn(n){if(n)try{let e=JSON.parse(n),t=e?.error;if(typeof t=="string"&&t.trim()!=="")return t;let r=t?.message;if(typeof r=="string"&&r.trim()!=="")return r;let s=e?.detail;if(typeof s=="string"&&s.trim()!=="")return s}catch{}}var v=class extends Error{constructor(t,r,s,i,a){let o=Hn(a)??(t===0&&a?.trim()?a.trim():void 0);super(o?`HTTP ${t} ${r}: ${o}`:`HTTP ${t} ${r}`);this.status=t;this.statusText=r;this.url=s;this.headers=i;this.bodyText=a;this.name="HTTPError";this.explanation=o}toJSON(){return{status:this.status,statusText:this.statusText,url:this.url,headers:Rt(this.headers),bodyText:this.bodyText}}},_=class extends v{constructor(t,r,s,i,a,o){super(r,s,i,a,o);this.reason=t;this.name="AuthRevokedError";this.message=`Authentication revoked (${t}): HTTP ${r} ${s}`}};function Rt(n){let e={};return n.forEach((t,r)=>{e[r]=t}),e}function Gn(n){try{return new URL(n).pathname}catch{let e=n.indexOf("?");return e===-1?n:n.slice(0,e)}}var ee=class{constructor(e){this.transport=e;this.refreshTokenPromise=null;this.onTokenRefreshPromise=null}async get(e,t){return this.request(e,{...t,method:"GET"})}async post(e,t,r){return this.request(e,{...r,method:"POST",body:t?JSON.stringify(t):void 0,headers:{"Content-Type":"application/json",...r?.headers}})}async put(e,t,r){return this.request(e,{...r,method:"PUT",body:t?JSON.stringify(t):void 0,headers:{"Content-Type":"application/json",...r?.headers}})}async delete(e,t){return this.request(e,{...t,method:"DELETE"})}async patch(e,t,r){return this.request(e,{...r,method:"PATCH",body:t?JSON.stringify(t):void 0,headers:{"Content-Type":"application/json",...r?.headers}})}async head(e,t){let r=await this.makeRequest(e,{...t,method:"HEAD",parse:"response"});return{headers:Rt(r.headers),status:r.status}}async request(e,t){return this.makeRequest(e,t)}async makeRequest(e,t,r=0,s){let a=this.buildUrl(e),o=s??Date.now(),c=this.createAbortSignal(t),l=await this.buildHeaders(t?.headers),d;l instanceof Headers?(d={},l.forEach((g,R)=>{d[R]=g})):d=l;let p={method:t?.method||"GET",headers:d},m=t?.body instanceof ReadableStream||typeof t?.body=="object"&&t?.body!==null&&"getReader"in t.body&&!(t.body instanceof Blob);(t?.body!==void 0&&!m||t?.body!==void 0&&m&&r===0)&&(p.body=t.body);let f;if(r>0&&s!==void 0){let g=t?.timeoutMs||this.transport.timeoutMs;if(g){let R=Date.now()-o,C=Math.max(0,g-R);f=this.createAbortSignal({...t,timeoutMs:C})}else f=this.createAbortSignal(t)}else f=c;if(f&&(p.signal=f),this.transport.credentials!==void 0&&(p.credentials=this.transport.credentials),t?.mode!==void 0&&(p.mode=t.mode),t?.cache!==void 0&&(p.cache=t.cache),t?.redirect!==void 0&&(p.redirect=t.redirect),t?.referrer!==void 0&&(p.referrer=t.referrer),t?.referrerPolicy!==void 0&&(p.referrerPolicy=t.referrerPolicy),t?.integrity!==void 0&&(p.integrity=t.integrity),t?.keepalive!==void 0&&(p.keepalive=t.keepalive),this.transport.getProof){let g=await this.transport.getProof({method:p.method??"GET",path:Gn(a),body:p.body});g&&(d["X-Calimero-Proof"]=g)}try{let g=await this.transport.fetch(a,p);if(!g.ok){let R=await this.getBodyText(g),C=new v(g.status,g.statusText,a,g.headers,R),A=g.headers.get("x-auth-error");if((g.status===401||g.status===403)&&A&&qn.has(A)){if(this.refreshTokenPromise=null,this.onTokenRefreshPromise=null,this.transport.onAuthRevoked)try{await this.transport.onAuthRevoked()}catch{}throw new _(A,g.status,g.statusText,a,g.headers,R)}let x=t?.signal?.aborted===!0;if(g.status===401&&this.transport.refreshToken&&A==="token_expired"&&r<1&&!m&&!x)try{let T=this.refreshTokenPromise;T||(T=this.transport.refreshToken(),this.refreshTokenPromise=T);let H=await T;if(!H||H.trim()==="")throw this.refreshTokenPromise=null,this.onTokenRefreshPromise=null,new Error("Refresh token returned empty token");if(!this.transport.onTokenRefresh)throw this.refreshTokenPromise=null,this.onTokenRefreshPromise=null,new Error("onTokenRefresh callback is required when refreshToken is provided. The callback must update the token storage so getAuthToken() returns the new token.");let E=this.onTokenRefreshPromise;E||(E=this.transport.onTokenRefresh(H),this.onTokenRefreshPromise=E),await E,this.refreshTokenPromise=null,this.onTokenRefreshPromise=null;let N=t?.timeoutMs||this.transport.timeoutMs;if(N&&s!==void 0){let B=Date.now()-o;if(N-B<=0)throw C}return this.makeRequest(e,t,r+1,o)}catch(T){throw this.refreshTokenPromise=null,this.onTokenRefreshPromise=null,T instanceof Error&&T.message.includes("onTokenRefresh")?T:C}throw C}return this.parseResponse(g,t?.parse)}catch(g){throw g instanceof v||g instanceof Error&&g.message.includes("onTokenRefresh")?g:new v(0,"Network Error",a,new Headers,g instanceof Error?g.message:"Unknown error")}}buildUrl(e){if(e.startsWith("http://")||e.startsWith("https://"))return e;let t=this.transport.baseUrl;return e.startsWith("/")?`${t.endsWith("/")?t.slice(0,-1):t}${e}`:`${t.endsWith("/")?t:`${t}/`}${e}`}createAbortSignal(e){let t=[];this.transport.defaultAbortSignal&&t.push(this.transport.defaultAbortSignal),e?.signal&&t.push(e.signal);let r=e?.timeoutMs||this.transport.timeoutMs;return r&&t.push(qe(r)),t.length>0?_e(t):void 0}async buildHeaders(e){let t={...this.transport.defaultHeaders};if(this.transport.getAuthToken)try{let r=await this.transport.getAuthToken();r&&r.trim()!==""&&(t.Authorization=`Bearer ${r}`)}catch{}return e&&(e instanceof Headers?e.forEach((r,s)=>{t[s]=r}):Array.isArray(e)?e.forEach(([r,s])=>{t[r]=s}):Object.assign(t,e)),t}async parseResponse(e,t){switch(t){case"text":return await e.text();case"blob":return await e.blob();case"arrayBuffer":return await e.arrayBuffer();case"response":return e;default:{let r=await e.text();if(!r)return null;try{return JSON.parse(r)}catch{throw new Error(`Expected a JSON response (status ${e.status}) but body was not JSON: ${r.slice(0,200)}`)}}}}async getBodyText(e){try{let t=await e.text();return t.length>65536?t.slice(0,65536)+"...":t}catch{return""}}};function He(n){return new ee(n)}function F(n){let e={fetch:(t,r)=>n.fetch?n.fetch(t,r):globalThis.fetch(t,r),baseUrl:n.baseUrl,getAuthToken:n.getAuthToken,getProof:n.getProof,onTokenRefresh:n.onTokenRefresh,refreshToken:n.refreshToken,onAuthRevoked:n.onAuthRevoked,defaultHeaders:n.defaultHeaders,timeoutMs:n.timeoutMs,credentials:n.credentials,defaultAbortSignal:n.defaultAbortSignal};return He(e)}function ge(n){let e=n.fetch??globalThis.fetch;if(!e)throw new Error("No fetch implementation available. Please provide a fetch implementation (e.g., undici.fetch) or use Node.js 18+ which has native fetch support.");let r={fetch:e===globalThis.fetch?(s,i)=>globalThis.fetch(s,i):(s,i)=>e(s,i),baseUrl:n.baseUrl,getAuthToken:n.getAuthToken,getProof:n.getProof,onTokenRefresh:n.onTokenRefresh,refreshToken:n.refreshToken,onAuthRevoked:n.onAuthRevoked,defaultHeaders:n.defaultHeaders,timeoutMs:n.timeoutMs,credentials:n.credentials,defaultAbortSignal:n.defaultAbortSignal};return He(r)}function Ge(n){return typeof window<"u"?F(n):ge(n)}function On(n,e){if(e<=0)return!1;let r=n?.name;if(r==="TimeoutError")return!0;if(r==="AbortError")return!1;let s=n;if("status"in s&&typeof s.status=="number"){let i=s.status;return i>=500||i===429}return r==="TypeError"}function Ln(n){let t=250*Math.pow(2,n-1),r=(Math.random()-.5)*.4*t;return Math.max(0,t+r)}async function me(n,e={}){let{attempts:t=3}=e,r;for(let s=1;s<=t;s++)try{return await n(s)}catch(i){if(r=i,!On(r,t-s))throw r;let a=Ln(s),l=r.headers?.get?.("Retry-After");if(l){let d=parseInt(l,10);if(!isNaN(d))a=Math.max(a,d*1e3);else{let p=new Date(l);if(!isNaN(p.getTime())){let m=Math.max(0,p.getTime()-Date.now());a=Math.max(a,Math.min(m,6e4))}}}await new Promise(d=>setTimeout(d,a))}throw r||new Error("Retry failed without error")}function $n(n,e={}){return async(...t)=>me(()=>n(...t),e)}var G=class{constructor(e){this.httpClient=e}async getHealth(){let e=await this.httpClient.get("/auth/health");if(!e.data)throw new Error("Health response data is null");return e.data}async getIdentity(){let e=await this.httpClient.get("/admin/identity");if(!e.data)throw new Error("Identity response data is null");return e.data}async getProviders(){let e=await this.httpClient.get("/auth/providers");if(!e.data)throw new Error("Providers response data is null");return e.data}async getLoginPage(){return this.httpClient.get("/auth/login",{parse:"text"})}async generateTokens(e){return this.httpClient.post("/auth/token",e)}async refreshToken(e){return this.httpClient.post("/auth/refresh",e)}async generateMockTokens(e){return this.httpClient.post("/auth/mock-token",e)}async validateToken(e){try{let t=await this.validateTokenGet(e);return{valid:t.status===200,headers:t.headers,status:t.status}}catch{return{valid:!1,headers:{},status:401}}}async validateTokenGet(e){let t=await this.httpClient.head("/auth/validate",{headers:{Authorization:`Bearer ${e}`}});return{status:t.status,headers:t.headers}}async revokeTokens(e){let t=await this.httpClient.post("/admin/revoke",e);if(!t.data)throw new Error("Revoke tokens response data is null");return t.data}async listRootKeys(){let e=await this.httpClient.get("/admin/keys");if(!e.data)throw new Error("Root keys response data is null");return e.data}async createRootKey(e){let t=await this.httpClient.post("/admin/keys",e);if(!t.data)throw new Error("Create root key response data is null");return t.data}async deleteRootKey(e){return this.httpClient.delete(`/admin/keys/${e}`)}async listClientKeys(){let e=await this.httpClient.get("/admin/keys/clients");if(!e.data)throw new Error("Client keys response data is null");return e.data}async generateClientKey(e){return this.httpClient.post("/admin/client-key",e)}async deleteClientKey(e,t){return this.httpClient.delete(`/admin/keys/${e}/clients/${t}`)}async getKeyPermissions(e){return this.httpClient.get(`/admin/keys/${e}/permissions`)}async updateKeyPermissions(e,t){return this.httpClient.put(`/admin/keys/${e}/permissions`,{add:t.add,remove:t.remove})}};var te=class{async get(){throw new Error("HTTP client not implemented - use createAuthApiClientFromHttpClient with a real HTTP client")}async post(){throw new Error("HTTP client not implemented - use createAuthApiClientFromHttpClient with a real HTTP client")}async put(){throw new Error("HTTP client not implemented - use createAuthApiClientFromHttpClient with a real HTTP client")}async delete(){throw new Error("HTTP client not implemented - use createAuthApiClientFromHttpClient with a real HTTP client")}async patch(){throw new Error("HTTP client not implemented - use createAuthApiClientFromHttpClient with a real HTTP client")}async head(){throw new Error("HTTP client not implemented - use createAuthApiClientFromHttpClient with a real HTTP client")}async request(){throw new Error("HTTP client not implemented - use createAuthApiClientFromHttpClient with a real HTTP client")}};function Ys(n){let e=new te;return new G(e)}function Xs(n){let e=new te;return new G(e)}function Zs(n){let e=new te;return new G(e)}function At(n,e){return new G(n)}var ne={CAN_CREATE_CONTEXT:1,CAN_INVITE_MEMBERS:2,CAN_JOIN_OPEN_SUBGROUPS:4,MANAGE_MEMBERS:8,MANAGE_APPLICATION:16,CAN_CREATE_SUBGROUP:32,CAN_DELETE_SUBGROUP:64,CAN_MANAGE_VISIBILITY:128,CAN_MANAGE_METADATA:256,CAN_AUTHOR_ON_BEHALF:512};function Oe(n,e){let t=e>>>0;return(n&t)>>>0===t}function Le(n,e){return(n|e)>>>0}function ti(n,e){return(n&~e)>>>0}var Bn={invalid:!1,unauthorized:!0,forbidden:!1,"not-found":!0,conflict:!1,gone:!1,"too-large":!1,"rate-limited":!0,unavailable:!0,server:!0,unreachable:!0,unknown:!0};function Kn(n){switch(n){case 0:return"unreachable";case 400:return"invalid";case 401:return"unauthorized";case 403:return"forbidden";case 404:return"not-found";case 409:return"conflict";case 410:return"gone";case 413:return"too-large";case 429:return"rate-limited";case 503:case 504:return"unavailable"}return n>=500&&n<600?"server":n>=400&&n<500?"invalid":"unknown"}function jn(n){if(!n||typeof n!="object")return;let e=n;for(let t of[e.status,e.response?.status,e.statusCode])if(typeof t=="number"&&Number.isFinite(t))return t}function $e(n,e="Request failed"){if(typeof n=="string")return n.trim()||e;if(!n||typeof n!="object")return e;let t=n;for(let r of[t.explanation,t.response?.data?.error,t.response?.data?.message,t.data?.message,t.error,t.message])if(typeof r=="string"&&r.trim())return r;return e}function Be(n){let e=jn(n),t;return e!==void 0?t=Kn(e):n instanceof TypeError?t="unreachable":t="unknown",{kind:t,status:e,retryable:Bn[t],message:$e(n)}}function Vn(n){return n.status!=="failed"||!n.retryable}function ii(n){return!Vn(n)}function Fn(n){let e=n.toLowerCase();return e.includes("invitation has expired")||e.includes("invitation expired")||/\binvitation for group .+? expired at \d+/.test(e)?"expired":e.includes("malformed")||e.includes("does not match namespace_id")||e.includes("invalid invitation")||/\binvitation for group .+? is invalid:/.test(e)?"invalid":null}function Wn(n,e){let{kind:t,status:r}=Be(n),s=Fn(e);return r===401?{reason:"signed-out",retryable:!0}:r===400?{reason:s??"invalid",retryable:!1}:r===410?{reason:"expired",retryable:!1}:r===403?{reason:"refused",retryable:!1}:r===409?{reason:s??"refused",retryable:!1}:t==="unavailable"?{reason:"no-one-online",retryable:!0}:t==="unreachable"?{reason:"node-unreachable",retryable:!0}:s?{reason:s,retryable:!1}:{reason:"unknown",retryable:!0}}async function xt(n,e){let{namespaceId:t,invitation:r,teamName:s}=n,i=null;try{await e.join(t,r)}catch(d){i=d}let a=null;try{a=(await e.memberships()).includes(t)}catch{a=null}if(i===null)return{status:"joined",namespaceId:t,teamName:s};if(a===!0)return{status:"already-member",namespaceId:t,teamName:s};let o=$e(i,"Could not join. Check the invitation."),{reason:c,retryable:l}=Wn(i,o);return{status:"failed",namespaceId:t,message:o,reason:c,retryable:l}}function u(n){return n?.data}function Jn(n,e){let[t,r]=Ct(n),[s,i]=Ct(e),a=t.split("."),o=s.split("."),c=Math.max(a.length,o.length);for(let m=0;m<c;m++){let f=vt(a[m]??"0",o[m]??"0");if(f!==0)return f}if(r===void 0&&i===void 0)return 0;if(r===void 0)return 1;if(i===void 0)return-1;let l=r.split("."),d=i.split("."),p=Math.max(l.length,d.length);for(let m=0;m<p;m++){if(m>=l.length)return-1;if(m>=d.length)return 1;let f=vt(l[m],d[m]);if(f!==0)return f}return 0}function Ct(n){let e=n.split("+",1)[0],t=e.indexOf("-");return t===-1?[e,void 0]:[e.slice(0,t),e.slice(t+1)]}function vt(n,e){let t=/^\d+$/.test(n),r=/^\d+$/.test(e);if(t&&r){let s=Number(n)-Number(e);return s<0?-1:s>0?1:0}return t?-1:r?1:n<e?-1:n>e?1:0}async function zn(n,e,t){return me(async()=>{let r=await fetch(n,{signal:AbortSignal.timeout(1e4)});if(!r.ok){let s=new Error(`registry ${e} fetch failed (${r.status}) for ${t}`);throw s.status=r.status,s}return r},{attempts:3})}var O=class{constructor(e){this.httpClient=e}async healthCheck(){return u(await this.httpClient.get("/admin-api/health"))}async readinessCheck(){return u(await this.httpClient.get("/admin-api/ready"))}async isAuthed(){return this.httpClient.get("/admin-api/is-authed")}async installApplication(e){return u(await this.httpClient.post("/admin-api/install-application",e))}async getRegistryVersions(e,t){let r=new URL("/api/v2/bundles",new URL(e).origin);r.searchParams.set("package",t);let i=await(await zn(r.toString(),"versions",t)).json();return(Array.isArray(i)?i:[]).map(a=>a.appVersion).filter(a=>typeof a=="string").sort((a,o)=>Jn(o,a))}async installDevApplication(e){return u(await this.httpClient.post("/admin-api/install-dev-application",e))}async uninstallApplication(e){return u(await this.httpClient.delete(`/admin-api/applications/${e}`))}async listApplications(){return u(await this.httpClient.get("/admin-api/applications"))}async getApplication(e){return u(await this.httpClient.get(`/admin-api/applications/${e}`))}async listApplicationVersions(e){return u(await this.httpClient.get(`/admin-api/applications/${e}/versions`))}async getApplicationAbi(e,t){let r=t?`?service_name=${encodeURIComponent(t)}`:"";return u(await this.httpClient.get(`/admin-api/applications/${e}/abi${r}`))}async listPackages(){let e=await this.httpClient.get("/admin-api/packages");return e?.data??e}async listPackageVersions(e){let t=await this.httpClient.get(`/admin-api/packages/${encodeURIComponent(e)}/versions`);return t?.data??t}async getLatestPackageVersion(e){return this.httpClient.get(`/admin-api/packages/${encodeURIComponent(e)}/latest`)}async createContext(e){let t={...e,initializationParams:e.initializationParams??[]};return u(await this.httpClient.post("/admin-api/contexts",t))}async deleteContext(e){return u(await this.httpClient.delete(`/admin-api/contexts/${e}`))}async getContexts(){return u(await this.httpClient.get("/admin-api/contexts"))}async getContext(e){return u(await this.httpClient.get(`/admin-api/contexts/${e}`))}async getContextsForApplication(e){return u(await this.httpClient.get(`/admin-api/contexts/for-application/${e}`))}async generateContextIdentity(){return u(await this.httpClient.post("/admin-api/identity/context",{}))}async getContextIdentities(e){return u(await this.httpClient.get(`/admin-api/contexts/${e}/identities`))}async getContextIdentitiesOwned(e){return u(await this.httpClient.get(`/admin-api/contexts/${e}/identities-owned`))}async joinContext(e){return u(await this.httpClient.post(`/admin-api/contexts/${e}/join`,{}))}async getContextGroup(e){return u(await this.httpClient.get(`/admin-api/contexts/${e}/group`))}async getContextStorage(e){return u(await this.httpClient.get(`/admin-api/contexts/${e}/storage`))}async syncContext(e){let t=e?`/admin-api/contexts/sync/${e}`:"/admin-api/contexts/sync";await this.httpClient.post(t,{})}async resyncContext(e,t={}){return await this.httpClient.post(`/admin-api/contexts/${e}/resync`,t)??{contextId:e,resyncStarted:!0}}async inviteSpecializedNode(e){return u(await this.httpClient.post("/admin-api/contexts/invite-specialized-node",e))}async updateContextApplication(e,t){await this.httpClient.post(`/admin-api/contexts/${e}/application`,t)}async getContextsWithExecutorsForApplication(e){let t=await this.httpClient.get(`/admin-api/contexts/with-executors/for-application/${e}`);return Array.isArray(t)?t:t?.contexts??t?.data??[]}async uploadBlob(e){let t=new URLSearchParams;e.hash&&t.set("hash",e.hash),e.contextId&&t.set("context_id",e.contextId);let r=t.toString(),s=r?`/admin-api/blobs?${r}`:"/admin-api/blobs",i=u(await this.httpClient.request(s,{method:"PUT",body:e.data,headers:{"Content-Type":"application/octet-stream"}}));return{blobId:i.blob_id,size:i.size}}async deleteBlob(e){let t=await this.httpClient.delete(`/admin-api/blobs/${e}`);return{blobId:t.blob_id,deleted:t.deleted}}async listBlobs(){return{blobs:u(await this.httpClient.get("/admin-api/blobs")).blobs.map(t=>({blobId:t.blob_id,size:t.size}))}}blobPath(e,t){if(!t?.contextId)return`/admin-api/blobs/${e}`;let r=new URLSearchParams({context_id:t.contextId});return`/admin-api/blobs/${e}?${r.toString()}`}async getBlob(e,t){return this.httpClient.get(this.blobPath(e,t),{parse:"arrayBuffer"})}async getBlobInfo(e,t){let{headers:r}=await this.httpClient.head(this.blobPath(e,t)),s=Number(r["content-length"]);return{blobId:r["x-blob-id"]??e,size:Number.isFinite(s)?s:0,hash:r["x-blob-hash"],mimeType:r["x-blob-mime-type"],source:r["x-blob-source"]}}async createContextAlias(e){return u(await this.httpClient.post("/admin-api/alias/create/context",{alias:e.alias,contextId:e.contextId}))}async createApplicationAlias(e){return u(await this.httpClient.post("/admin-api/alias/create/application",{alias:e.alias,applicationId:e.applicationId}))}async lookupContextAlias(e){return u(await this.httpClient.post(`/admin-api/alias/lookup/context/${encodeURIComponent(e)}`,{}))}async lookupApplicationAlias(e){return u(await this.httpClient.post(`/admin-api/alias/lookup/application/${encodeURIComponent(e)}`,{}))}async deleteContextAlias(e){return u(await this.httpClient.post(`/admin-api/alias/delete/context/${encodeURIComponent(e)}`,{}))}async deleteApplicationAlias(e){return u(await this.httpClient.post(`/admin-api/alias/delete/application/${encodeURIComponent(e)}`,{}))}async listContextAliases(){return u(await this.httpClient.get("/admin-api/alias/list/context"))}async listApplicationAliases(){return u(await this.httpClient.get("/admin-api/alias/list/application"))}async createDeviceAlias(e){return u(await this.httpClient.post("/admin-api/alias/create/device",{alias:e.alias,deviceId:e.deviceId}))}async lookupDeviceAlias(e){return u(await this.httpClient.post(`/admin-api/alias/lookup/device/${encodeURIComponent(e)}`,{}))}async deleteDeviceAlias(e){return u(await this.httpClient.post(`/admin-api/alias/delete/device/${encodeURIComponent(e)}`,{}))}async listDeviceAliases(){return u(await this.httpClient.get("/admin-api/alias/list/device"))}async listNamespaces(){return u(await this.httpClient.get("/admin-api/namespaces"))}async getNamespace(e){return u(await this.httpClient.get(`/admin-api/namespaces/${e}`))}async getNodeIdentity(){return u(await this.httpClient.get("/admin-api/identity"))}async getNamespaceIdentity(e){let t=await this.getNodeIdentity();return{namespaceId:e,publicKey:t.publicKey,account:t.accountId}}async performIntent(e,t){return u(await this.httpClient.post(`/admin-api/contexts/${e}/intents`,t))}async queryContext(e,t){return u(await this.httpClient.post(`/admin-api/contexts/${e}/query`,t))}async getIntentRelay(e){return u(await this.httpClient.get(`/admin-api/contexts/${e}/intents`))}async listNamespacesForApplication(e){return u(await this.httpClient.get(`/admin-api/namespaces/for-application/${e}`))}async createNamespace(e){return u(await this.httpClient.post("/admin-api/namespaces",e))}async deleteNamespace(e){return u(await this.httpClient.request(`/admin-api/namespaces/${e}`,{method:"DELETE",body:"{}",headers:{"Content-Type":"application/json"}}))}async createNamespaceInvitation(e,t){return u(await this.httpClient.post(`/admin-api/namespaces/${e}/invite`,t??{}))}async admitJoin(e,t){return u(await this.httpClient.post(`/admin-api/namespaces/${e}/admit`,t,{timeoutMs:65e3}))}async joinNamespace(e,t){let r=u(await this.httpClient.post(`/admin-api/namespaces/${e}/join`,t,{timeoutMs:65e3}));return r&&r.namespaceId===void 0&&r.groupId!==void 0?{...r,namespaceId:r.groupId}:r}async redeemInvitation(e,t,r={}){let{groupName:s,teamName:i}=r;return xt({namespaceId:e,invitation:t,teamName:i},{join:async()=>{await this.joinNamespace(e,s===void 0?{invitation:t}:{invitation:t,groupName:s})},memberships:async()=>(await this.listNamespaces()??[]).map(a=>a.namespaceId)})}async createGroupInNamespace(e,t){return u(await this.httpClient.post(`/admin-api/namespaces/${e}/groups`,t??{}))}async listNamespaceGroups(e){return u(await this.httpClient.get(`/admin-api/namespaces/${e}/groups`))}async initAccountPairing(e){return u(await this.httpClient.post("/admin-api/account/pair-init",e))}async completeAccountPairing(e){return u(await this.httpClient.post("/admin-api/account/pair-complete",e))}async relinkAccountDevice(e,t){return u(await this.httpClient.post(`/admin-api/account/devices/${e}/relink`,t??{}))}async rescopeAccountDevice(e,t){return u(await this.httpClient.put(`/admin-api/account/devices/${encodeURIComponent(e)}/scope`,t))}async signWithAccountRoot(e){return u(await this.httpClient.post("/admin-api/account/sign-with-root",e))}async labelAccountDevice(e,t){return u(await this.httpClient.put(`/admin-api/account/devices/${encodeURIComponent(e)}/label`,t))}async listAccountDevices(){return(await this.httpClient.get("/admin-api/account/devices")).devices}async listAccountApplications(){return(await this.httpClient.get("/admin-api/account/applications")).applications}async revokeAccountDevice(e,t){return u(await this.httpClient.post(`/admin-api/namespaces/${e}/account/revoke`,t))}async getGroupInfo(e){return u(await this.httpClient.get(`/admin-api/groups/${e}`))}async getDefaultCapabilities(e){return(await this.getGroupInfo(e)).defaultCapabilities}async getSubgroupVisibility(e){return(await this.getGroupInfo(e)).subgroupVisibility}async deleteGroup(e){return u(await this.httpClient.request(`/admin-api/groups/${e}`,{method:"DELETE",body:"{}",headers:{"Content-Type":"application/json"}}))}async listGroupMembers(e){let t=await this.httpClient.get(`/admin-api/groups/${e}/members`);if(!Array.isArray(t?.members)){let r=String(e).replace(/[\r\n\t\s]/g,"").slice(0,64);throw new Error(`Invalid listGroupMembers response for group ${r}: missing or non-array \`members\` field`)}return t}async listMemberDevices(e,t={}){let r=new URLSearchParams;t.offset!==void 0&&r.set("offset",String(t.offset)),t.limit!==void 0&&r.set("limit",String(t.limit));let s=r.toString(),i=`/admin-api/groups/${e}/member-devices${s?`?${s}`:""}`;return this.httpClient.get(i)}async sealToAccount(e,t,r){return u(await this.httpClient.post(`/admin-api/groups/${e}/accounts/${t}/seal`,r))}async listGroupContexts(e){return u(await this.httpClient.get(`/admin-api/groups/${e}/contexts`))}async addGroupMembers(e,t){await this.httpClient.post(`/admin-api/groups/${e}/members`,t)}async removeGroupMembers(e,t){await this.httpClient.post(`/admin-api/groups/${e}/members/remove`,t)}async updateMemberRole(e,t,r){await this.httpClient.put(`/admin-api/groups/${e}/members/${t}/role`,r)}async getMemberCapabilities(e,t){return u(await this.httpClient.get(`/admin-api/groups/${e}/members/${t}/capabilities`))}async setMemberCapabilities(e,t,r){await this.httpClient.put(`/admin-api/groups/${e}/members/${t}/capabilities`,r)}async setDefaultCapabilities(e,t){await this.httpClient.put(`/admin-api/groups/${e}/settings/default-capabilities`,t)}async openToDelegatedExecution(e){let t=await this.getDefaultCapabilities(e);if(Oe(t,ne.CAN_AUTHOR_ON_BEHALF))return{changed:!1,defaultCapabilities:t};let r=Le(t,ne.CAN_AUTHOR_ON_BEHALF);return await this.setDefaultCapabilities(e,{defaultCapabilities:r}),{changed:!0,defaultCapabilities:r}}async grantAuthorship(e,t){let{capabilities:r}=await this.getMemberCapabilities(e,t);if(Oe(r,ne.CAN_AUTHOR_ON_BEHALF))return{changed:!1,capabilities:r};let s=Le(r,ne.CAN_AUTHOR_ON_BEHALF);return await this.setMemberCapabilities(e,t,{capabilities:s}),{changed:!0,capabilities:s}}async setSubgroupVisibility(e,t){await this.httpClient.put(`/admin-api/groups/${e}/settings/subgroup-visibility`,t)}async setTeeAdmissionPolicy(e,t){await this.httpClient.put(`/admin-api/groups/${e}/settings/tee-admission-policy`,t)}async getTeeAdmissionPolicy(e){let t=await this.httpClient.get(`/admin-api/groups/${e}/settings/tee-admission-policy`);return t.data??t}async setTeeAuthoringPolicy(e,t){await this.httpClient.put(`/admin-api/groups/${e}/settings/tee-authoring-policy`,t)}async disableTeeAuthoringPolicy(e){await this.httpClient.delete(`/admin-api/groups/${e}/settings/tee-authoring-policy`)}async setGroupMetadata(e,t){await this.httpClient.put(`/admin-api/groups/${e}/metadata`,t)}async getGroupMetadata(e){return(await this.httpClient.get(`/admin-api/groups/${e}/metadata`))?.data??null}async setMemberMetadata(e,t,r){await this.httpClient.put(`/admin-api/groups/${e}/members/${t}/metadata`,r)}async getMemberMetadata(e,t){return(await this.httpClient.get(`/admin-api/groups/${e}/members/${t}/metadata`))?.data??null}async setContextMetadata(e,t,r){await this.httpClient.put(`/admin-api/groups/${e}/contexts/${t}/metadata`,r)}async getContextMetadata(e,t){return(await this.httpClient.get(`/admin-api/groups/${e}/contexts/${t}/metadata`))?.data??null}async syncGroup(e){return u(await this.httpClient.post(`/admin-api/groups/${e}/sync`,{}))}async upgradeGroup(e,t){return u(await this.httpClient.post(`/admin-api/groups/${e}/upgrade`,t))}async getGroupUpgradeStatus(e){return u(await this.httpClient.get(`/admin-api/groups/${e}/upgrade/status`))}async getMigrationStatus(e){let t=encodeURIComponent(e);return this.httpClient.get(`/admin-api/groups/${t}/migration-status`)}async getCascadeStatus(e){let t=encodeURIComponent(e);return u(await this.httpClient.get(`/admin-api/groups/${t}/cascade-status`))}async retryGroupUpgrade(e){return u(await this.httpClient.post(`/admin-api/groups/${e}/upgrade/retry`,{}))}async reparentGroup(e,t){let r=await this.httpClient.post(`/admin-api/groups/${e}/reparent`,t);return r?.data??r}async listSubgroups(e){let t=await this.httpClient.get(`/admin-api/groups/${e}/subgroups`);return t.subgroups??t.data??[]}async detachContextFromGroup(e,t){await this.httpClient.post(`/admin-api/groups/${e}/contexts/${t}/remove`,{})}async createGroupInvitation(e,t){return u(await this.httpClient.post(`/admin-api/groups/${e}/invite`,t??{}))}async joinGroup(e){return u(await this.httpClient.post("/admin-api/groups/join",e))}async joinSubgroupInheritance(e){return u(await this.httpClient.post(`/admin-api/groups/${e}/join-via-inheritance`,{}))}async getTeeInfo(){return u(await this.httpClient.get("/admin-api/tee/info"))}async teeAttest(e){return u(await this.httpClient.post("/admin-api/tee/attest",e))}async teeVerifyQuote(e){return u(await this.httpClient.post("/admin-api/tee/verify-quote",e))}async getPeersCount(){return this.httpClient.get("/admin-api/peers")}async getNetworkStatus(){return this.httpClient.get("/admin-api/network/status")}async getUsage(){return this.httpClient.get("/admin-api/usage")}async createGroup(e){return u(await this.httpClient.post("/admin-api/groups",e))}async leaveGroup(e){await this.httpClient.post(`/admin-api/groups/${e}/leave`,{})}async leaveContext(e){await this.httpClient.post(`/admin-api/contexts/${e}/leave`,{})}async leaveNamespace(e){await this.httpClient.post(`/admin-api/namespaces/${e}/leave`,{})}async issueOwnershipProof(e,t){return this.httpClient.post(`/admin-api/groups/${e}/issue-ownership-proof`,t)}async issueNamespaceOwnershipProof(e,t){return this.httpClient.post(`/admin-api/groups/${e}/issue-namespace-ownership-proof`,t)}async setMemberAutoFollow(e,t,r){await this.httpClient.put(`/admin-api/groups/${e}/members/${t}/auto-follow`,r)}async abortMigration(e){return this.httpClient.post(`/admin-api/groups/${e}/migration/abort`,{})}};function pi(n){return Array.from(new TextEncoder().encode(n),e=>e.toString(16).padStart(2,"0")).join("")}function yi(n){return new O(F(n))}function bi(n){return new O(ge(n))}function wi(n){return new O(Ge(n))}function kt(n,e){return new O(n)}function Ke(n){try{let e=n.indexOf("#");if(e===-1)return null;let t=n.substring(e+1),r=new URLSearchParams(t),s=r.get("access_token");return s?{accessToken:s,refreshToken:r.get("refresh_token")??"",applicationId:r.get("application_id")??"",contextId:r.get("context_id")??"",contextIdentity:r.get("context_identity")??"",nodeUrl:r.get("node_url")??""}:null}catch{return null}}function je(n,e){let t=new URLSearchParams;return t.set("callback-url",e.callbackUrl),e.permissions&&e.permissions.length>0&&t.set("permissions",e.permissions.join(",")),t.set("mode",e.mode),e.packageName&&(t.set("package-name",e.packageName),e.packageVersion&&t.set("package-version",e.packageVersion),e.registryUrl&&t.set("registry-url",e.registryUrl)),`${n.replace(/\/+$/,"")}/auth/login?${t.toString()}`}var St=new Uint8Array([48,46,2,1,0,48,5,6,3,43,101,112,4,34,4,32]);function k(n){let e=new Uint8Array(4);return new DataView(e.buffer).setUint32(0,n,!0),e}function b(n){return Array.from(n,e=>e.toString(16).padStart(2,"0")).join("")}function y(n,e,t){let r=n.trim();if(!/^[0-9a-fA-F]*$/.test(r)||r.length!==t*2)throw new Error(`${e} must be ${t*2} hex characters, got ${r.length}`);let s=new Uint8Array(t);for(let i=0;i<t;i+=1)s[i]=Number.parseInt(r.slice(i*2,i*2+2),16);return s}function he(n,e){let t=n.trim();if(!/^[0-9a-fA-F]*$/.test(t)||t.length%2!==0)throw new Error(`${e} must be an even number of hex characters`);let r=new Uint8Array(t.length/2);for(let s=0;s<r.length;s+=1)r[s]=Number.parseInt(t.slice(s*2,s*2+2),16);return r}function S(n){let e=new Uint8Array(8);return new DataView(e.buffer).setBigUint64(0,BigInt(n),!0),e}function h(...n){let e=new Uint8Array(n.reduce((r,s)=>r+s.length,0)),t=0;for(let r of n)e.set(r,t),t+=r.length;return e}async function U(n,e){let t=[S(n.length),n];for(let r of e)t.push(S(r.length),r);return new Uint8Array(await crypto.subtle.digest("SHA-256",h(...t)))}async function I(n){let e=y(n,"deviceSecret",32),t=h(St,e);try{return await crypto.subtle.importKey("pkcs8",t,{name:"Ed25519"},!1,["sign"])}catch(r){let s=r instanceof Error?`: ${r.message}`:"";throw new Error("this runtime has no WebCrypto Ed25519, so a warrant cannot be signed here. Node 18.4+, Safari 17+, Firefox 129+ and Chrome 137+ have it; otherwise mint the warrant with `merod account warrant`"+s)}}async function P(n){let e=y(n,"deviceSecret",32),t=h(St,e),r=await crypto.subtle.importKey("pkcs8",t,{name:"Ed25519"},!0,["sign"]),i=(await crypto.subtle.exportKey("jwk",r)).x;return Uint8Array.from(atob(i.replace(/-/g,"+").replace(/_/g,"/")),a=>a.charCodeAt(0))}var Qn=new TextEncoder().encode("calimero.auth.request.v1"),Yn=new TextEncoder().encode("calimero.auth.request.body.v1");function Tt(n){let e=new TextEncoder().encode(n);return h(k(e.length),e)}async function Ve(n=new Uint8Array(0)){let e=typeof n=="string"?new TextEncoder().encode(n):n;return U(Yn,[e])}async function fe(n){let e=await I(n.signerSecret),t=await Ve(n.body),r=S(n.issuedAt),s=S(n.expiresAt),i=new TextEncoder,a=await U(Qn,[i.encode(n.method),i.encode(n.path),t,r,s]),o=new Uint8Array(await crypto.subtle.sign({name:"Ed25519"},e,a));return b(h(Tt(n.method),Tt(n.path),t,r,s,o))}function ye(n){let e=he(n.credential,"credential"),t=he(n.request,"request signature"),r=n.session===void 0?new Uint8Array([0]):h(new Uint8Array([1]),he(n.session,"session statement"));return b(h(e,r,t))}function Xn(n){if(n==null)return new Uint8Array(0);if(typeof n=="string"||n instanceof Uint8Array)return n;if(n instanceof ArrayBuffer)return new Uint8Array(n);if(ArrayBuffer.isView(n))return new Uint8Array(n.buffer,n.byteOffset,n.byteLength);throw new Error(`a request-carried proof cannot commit to a ${n?.constructor?.name??typeof n} body: pass a string or bytes, or use a session for this call`)}function be(n){let e=n.ttlSeconds??120,t=n.now??(()=>Math.floor(Date.now()/1e3));return async r=>{let s=t();return ye({credential:n.credential,session:n.session,request:await fe({method:r.method,path:r.path,body:Xn(r.body),issuedAt:s,expiresAt:s+e,signerSecret:n.signerSecret})})}}var we=class extends Error{constructor(e,t,r,s){super(t),this.name="RpcError",this.code=e,this.data=r,this.type=s}};async function Fe(n,e,t){let r=await n.post("/jsonrpc",{jsonrpc:"2.0",id:1,method:e,params:t});if(r.error){let s=r.error;throw new we(s.code??-1,s.message??s.type??"RPC error",s.data,s.type)}return r.result}var re=class{constructor(e){this.httpClient=e.httpClient}async execute(e){let t=await Fe(this.httpClient,"execute",{contextId:e.contextId,method:e.method,argsJson:e.argsJson??{}});return t&&"output"in t?t.output:t}async migrateMyEntries(e){return this.execute({contextId:e,method:"migrate_my_entries"})}async countMyPending(e){return this.execute({contextId:e,method:"count_my_pending"})}};var Zn=new Set(["MigrationStarted","MigrationProgress","CascadeProgress","MigrationCompleted"]);function Et(n){return n.type!==void 0&&Zn.has(n.type)}var er=new Set(["token_reuse","token_revoked"]),tr=new Set([401,403]);function It(n){let e=n.headers.get("x-auth-error");return!e||!tr.has(n.status)?null:er.has(e)?e:null}var W=class{constructor(e){this.sessionId=null;this.abortController=null;this.reconnectTimer=null;this.subscribedContextIds=new Set;this.subscribedGroupIds=new Set;this.closed=!1;this.listeners={connect:[],event:[],error:[]};this.baseUrl=e.baseUrl.replace(/\/+$/,""),this.getAuthToken=e.getAuthToken,this.reconnectDelayMs=e.reconnectDelayMs??3e3,this.fetchImpl=(t,r)=>e.fetch?e.fetch(t,r):globalThis.fetch(t,r)}on(e,t){let r=e;if(r in this.listeners){let s=this.listeners[r];s.includes(t)||s.push(t)}}off(e,t){let r=e;if(r in this.listeners){let s=this.listeners[r],i=s.indexOf(t);i!==-1&&s.splice(i,1)}}onAppVersionChanged(e){let t=r=>{if(r.type!=="AppVersionChanged")return;let s=r.data;e({contextId:r.contextId,fromVersion:s?.fromVersion,toVersion:s?.toVersion})};return this.on("event",t),()=>this.off("event",t)}onMigrationEvent(e){let t=r=>{Et(r)&&e(r)};return this.on("event",t),()=>this.off("event",t)}emit(e,t){let r=e;if(r in this.listeners)for(let s of this.listeners[r])try{s(t)}catch{}}async connect(){if(!(this.abortController&&!this.closed)){this.closed=!1,this.abortController=new AbortController;try{let e=await this.getAuthToken(),t=`${this.baseUrl}/sse`,r=await this.fetchImpl(t,{headers:{Authorization:`Bearer ${e}`,Accept:"text/event-stream"},signal:this.abortController.signal});if(!r.ok){let s=It(r),i=await r.text().catch(()=>{});throw s?new _(s,r.status,r.statusText,t,r.headers,i):new v(r.status,r.statusText,t,r.headers,i)}if(!r.body)throw new Error("SSE response has no body");this.readStream(r.body).catch(s=>{if(this.closed)return;let i=s instanceof Error?s:new Error(String(s));this.emit("error",i),this.scheduleReconnect()})}catch(e){if(this.closed)return;let t=e instanceof Error?e:new Error(String(e));if(this.emit("error",t),t instanceof _)return;this.scheduleReconnect()}}}async readStream(e){let t=e.getReader(),r=new TextDecoder,s="";try{for(;;){let{done:i,value:a}=await t.read();if(i)break;s+=r.decode(a,{stream:!0});let o=s.split(`
|
|
2
|
-
`);s=o.pop()??"";for(let c of o)if(c.startsWith("data:")){let l=c.slice(5).trim();l&&this.handleMessage(l)}}if(s+=r.decode(),s.startsWith("data:")){let i=s.slice(5).trim();i&&this.handleMessage(i)}}catch(i){if(this.closed)return;let a=i instanceof Error?i:new Error(String(i));this.emit("error",a)}this.closed||this.scheduleReconnect()}handleMessage(e){try{let t=JSON.parse(e);if(t.type==="connect"&&t.session_id){this.sessionId=t.session_id,this.emit("connect",t.session_id),(this.subscribedContextIds.size>0||this.subscribedGroupIds.size>0)&&this.sendSubscription("subscribe",{contextIds:[...this.subscribedContextIds],groupIds:[...this.subscribedGroupIds]});return}if(!t.result)return;let r=t.result.data;if(Array.isArray(r))try{let s=new Uint8Array(r),i=new TextDecoder().decode(s);r=JSON.parse(i)}catch{}if(t.result.contextId){this.emit("event",{contextId:t.result.contextId,type:t.result.type,data:r});return}t.result.groupId&&this.emit("event",{groupId:t.result.groupId,type:t.result.type,data:r})}catch{}}async subscribe(e){let t=Array.isArray(e)?{contextIds:e}:e,r=(t.contextIds??[]).filter(i=>!this.subscribedContextIds.has(i)),s=(t.groupIds??[]).filter(i=>!this.subscribedGroupIds.has(i));for(let i of t.contextIds??[])this.subscribedContextIds.add(i);for(let i of t.groupIds??[])this.subscribedGroupIds.add(i);(r.length>0||s.length>0)&&this.sessionId&&await this.sendSubscription("subscribe",{contextIds:r,groupIds:s})}async unsubscribe(e){let t=Array.isArray(e)?{contextIds:e}:e,r=(t.contextIds??[]).filter(i=>this.subscribedContextIds.has(i)),s=(t.groupIds??[]).filter(i=>this.subscribedGroupIds.has(i));for(let i of t.contextIds??[])this.subscribedContextIds.delete(i);for(let i of t.groupIds??[])this.subscribedGroupIds.delete(i);(r.length>0||s.length>0)&&this.sessionId&&await this.sendSubscription("unsubscribe",{contextIds:r,groupIds:s})}async sendSubscription(e,t){try{let r=await this.getAuthToken(),s={};t.contextIds&&t.contextIds.length>0&&(s.contextIds=t.contextIds),t.groupIds&&t.groupIds.length>0&&(s.groupIds=t.groupIds);let i=await this.fetchImpl(`${this.baseUrl}/sse/subscription`,{method:"POST",headers:{Authorization:`Bearer ${r}`,"Content-Type":"application/json"},body:JSON.stringify({id:this.sessionId,method:e,params:s})});if(!i.ok){let a=It(i),o=await i.text().catch(()=>{});this.emit("error",a?new _(a,i.status,i.statusText,`${this.baseUrl}/sse/subscription`,i.headers,o):new v(i.status,i.statusText,`${this.baseUrl}/sse/subscription`,i.headers,o))}}catch(r){this.emit("error",r instanceof Error?r:new Error(`SSE ${e} failed`))}}forceReconnect(){this.abortController&&(this.abortController.abort(),this.abortController=null),this.sessionId=null,this.connect()}scheduleReconnect(){this.closed||(this.reconnectTimer&&clearTimeout(this.reconnectTimer),this.reconnectTimer=setTimeout(()=>{this.reconnectTimer=null,this.forceReconnect()},this.reconnectDelayMs))}close(){this.closed=!0,this.abortController&&(this.abortController.abort(),this.abortController=null),this.reconnectTimer&&(clearTimeout(this.reconnectTimer),this.reconnectTimer=null),this.sessionId=null,this.subscribedContextIds.clear(),this.subscribedGroupIds.clear()}};var Re=class Re{constructor(e){this.ws=null;this.closed=!1;this.reconnectAttempt=0;this.reconnectTimer=null;this.subscribedContextIds=new Set;this.subscribedGroupIds=new Set;this.listeners={connect:[],event:[],error:[]};this.baseUrl=e.baseUrl.replace(/\/+$/,""),this.getAuthToken=e.getAuthToken}on(e,t){let r=e;if(r in this.listeners){let s=this.listeners[r];s.includes(t)||s.push(t)}}off(e,t){let r=e;if(r in this.listeners){let s=this.listeners[r],i=s.indexOf(t);i!==-1&&s.splice(i,1)}}emit(e,t){let r=e;if(r in this.listeners)for(let s of this.listeners[r])try{s(t)}catch{}}async connect(){if(!(this.ws&&(this.ws.readyState===0||this.ws.readyState===1))){this.closed=!1;try{let e=await this.getAuthToken();if(!e)throw new Error("No authentication token available for WebSocket connection");let t=this.baseUrl.replace(/^http/,"ws");this.ws=new WebSocket(`${t}/ws?token=${encodeURIComponent(e)}`),this.ws.onopen=()=>{this.reconnectAttempt=0,this.emit("connect"),this.resubscribeAfterReconnect()},this.ws.onmessage=r=>{this.handleMessage(r.data)},this.ws.onerror=()=>{this.emit("error",new Error("WebSocket error"))},this.ws.onclose=()=>{this.closed||this.scheduleReconnect()}}catch(e){if(this.closed)return;let t=e instanceof Error?e:new Error(String(e));this.emit("error",t),this.scheduleReconnect()}}}handleMessage(e){if(typeof e=="string")try{let t=JSON.parse(e);if(!t.result)return;let r=t.result.data;if(Array.isArray(r))try{let s=new Uint8Array(r),i=new TextDecoder().decode(s);r=JSON.parse(i)}catch{}if(t.result.contextId){this.emit("event",{contextId:t.result.contextId,type:t.result.type,data:r});return}t.result.groupId&&this.emit("event",{groupId:t.result.groupId,type:t.result.type,data:r})}catch{}}subscribe(e){let t=Array.isArray(e)?{contextIds:e}:e;for(let r of t.contextIds??[])this.subscribedContextIds.add(r);for(let r of t.groupIds??[])this.subscribedGroupIds.add(r);this.ws&&this.ws.readyState===1&&this.sendMessage({id:null,method:"subscribe",params:this.buildSubscriptionParams(t.contextIds??[],t.groupIds??[])})}unsubscribe(e){let t=Array.isArray(e)?{contextIds:e}:e;for(let r of t.contextIds??[])this.subscribedContextIds.delete(r);for(let r of t.groupIds??[])this.subscribedGroupIds.delete(r);this.ws&&this.ws.readyState===1&&this.sendMessage({id:null,method:"unsubscribe",params:this.buildSubscriptionParams(t.contextIds??[],t.groupIds??[])})}resubscribeAfterReconnect(){this.subscribedContextIds.size===0&&this.subscribedGroupIds.size===0||this.sendMessage({id:null,method:"subscribe",params:this.buildSubscriptionParams([...this.subscribedContextIds],[...this.subscribedGroupIds])})}buildSubscriptionParams(e,t){let r={};return e.length>0&&(r.contextIds=e),t.length>0&&(r.groupIds=t),r}sendMessage(e){this.ws&&this.ws.readyState===1&&this.ws.send(JSON.stringify(e))}scheduleReconnect(){if(this.closed)return;this.reconnectTimer&&clearTimeout(this.reconnectTimer);let e=Math.min(1e3*Math.pow(2,this.reconnectAttempt),Re.MAX_BACKOFF_MS);this.reconnectAttempt++,this.reconnectTimer=setTimeout(()=>{this.reconnectTimer=null,this.connect()},e)}close(){this.closed=!0,this.reconnectTimer&&(clearTimeout(this.reconnectTimer),this.reconnectTimer=null),this.ws&&(this.ws.onclose=null,this.ws.close(),this.ws=null),this.subscribedContextIds.clear(),this.subscribedGroupIds.clear()}};Re.MAX_BACKOFF_MS=3e4;var J=Re;function Ae(){return{encode(n){return Array.from(new TextEncoder().encode(JSON.stringify(n)))},decode(n){return JSON.parse(new TextDecoder().decode(new Uint8Array(n)))}}}var se=class{constructor(e){this.httpClient=e.httpClient,this.sse=e.sse}async set(e,t,r=Ae()){await Fe(this.httpClient,"set_ephemeral",{contextId:e,state:r.encode(t)})}subscribe(e,t,r=Ae()){let s=i=>{let a=i;if(a.type!=="Ephemeral"||a.contextId!==e)return;let o=a.data;if(!o?.author)return;let c=!!o.removed,l={author:o.author,state:c||o.state===void 0?void 0:r.decode(o.state),removed:c};o.ageMs!==void 0&&(l.ageMs=o.ageMs),t(l)};return this.sse.on("event",s),this.sse.connect().catch(()=>{}),this.sse.subscribe([e]).catch(()=>{}),()=>{this.sse.off("event",s)}}};var nr=new TextEncoder().encode("calimero.device.cert.v1"),Pt=new TextEncoder().encode("calimero.account.genesis.v1"),rr=new TextEncoder().encode("calimero.device.id.v1"),We=2;async function Dt(n,e){if(e.length!==16)throw new Error(`nonce must be 16 bytes, got ${e.length}`);let t=y(n,"account",32);return b(await U(rr,[t,e]))}async function Je(n){return U(nr,[y(n.account,"account",32),y(n.device,"device",32),y(n.signPublicKey,"signPublicKey",32),y(n.kemPublicKey,"kemPublicKey",32),k(n.keyEpoch),k(n.deviceEpoch)])}function Ut(n){return h(new Uint8Array([We]),n.rootPublicKey,k(0),y(n.account,"account",32),y(n.device,"device",32),y(n.signPublicKey,"signPublicKey",32),y(n.kemPublicKey,"kemPublicKey",32),k(n.keyEpoch),k(n.deviceEpoch),n.signature)}async function Mt(n){let e=await P(n.rootSecret),t=b(await U(Pt,[h(new Uint8Array([We]),e)])),r=0,s=await Je({account:t,device:n.device,signPublicKey:n.signPublicKey,kemPublicKey:n.kemPublicKey,keyEpoch:r,deviceEpoch:n.deviceEpoch}),i=await I(n.rootSecret),a=new Uint8Array(await crypto.subtle.sign("Ed25519",i,s)),o=Ut({rootPublicKey:e,account:t,device:n.device,signPublicKey:n.signPublicKey,kemPublicKey:n.kemPublicKey,keyEpoch:r,deviceEpoch:n.deviceEpoch,signature:a});return b(o)}async function xe(n){let e=await P(n);return b(await U(Pt,[h(new Uint8Array([We]),e)]))}var ze=["abandon","ability","able","about","above","absent","absorb","abstract","absurd","abuse","access","accident","account","accuse","achieve","acid","acoustic","acquire","across","act","action","actor","actress","actual","adapt","add","addict","address","adjust","admit","adult","advance","advice","aerobic","affair","afford","afraid","again","age","agent","agree","ahead","aim","air","airport","aisle","alarm","album","alcohol","alert","alien","all","alley","allow","almost","alone","alpha","already","also","alter","always","amateur","amazing","among","amount","amused","analyst","anchor","ancient","anger","angle","angry","animal","ankle","announce","annual","another","answer","antenna","antique","anxiety","any","apart","apology","appear","apple","approve","april","arch","arctic","area","arena","argue","arm","armed","armor","army","around","arrange","arrest","arrive","arrow","art","artefact","artist","artwork","ask","aspect","assault","asset","assist","assume","asthma","athlete","atom","attack","attend","attitude","attract","auction","audit","august","aunt","author","auto","autumn","average","avocado","avoid","awake","aware","away","awesome","awful","awkward","axis","baby","bachelor","bacon","badge","bag","balance","balcony","ball","bamboo","banana","banner","bar","barely","bargain","barrel","base","basic","basket","battle","beach","bean","beauty","because","become","beef","before","begin","behave","behind","believe","below","belt","bench","benefit","best","betray","better","between","beyond","bicycle","bid","bike","bind","biology","bird","birth","bitter","black","blade","blame","blanket","blast","bleak","bless","blind","blood","blossom","blouse","blue","blur","blush","board","boat","body","boil","bomb","bone","bonus","book","boost","border","boring","borrow","boss","bottom","bounce","box","boy","bracket","brain","brand","brass","brave","bread","breeze","brick","bridge","brief","bright","bring","brisk","broccoli","broken","bronze","broom","brother","brown","brush","bubble","buddy","budget","buffalo","build","bulb","bulk","bullet","bundle","bunker","burden","burger","burst","bus","business","busy","butter","buyer","buzz","cabbage","cabin","cable","cactus","cage","cake","call","calm","camera","camp","can","canal","cancel","candy","cannon","canoe","canvas","canyon","capable","capital","captain","car","carbon","card","cargo","carpet","carry","cart","case","cash","casino","castle","casual","cat","catalog","catch","category","cattle","caught","cause","caution","cave","ceiling","celery","cement","census","century","cereal","certain","chair","chalk","champion","change","chaos","chapter","charge","chase","chat","cheap","check","cheese","chef","cherry","chest","chicken","chief","child","chimney","choice","choose","chronic","chuckle","chunk","churn","cigar","cinnamon","circle","citizen","city","civil","claim","clap","clarify","claw","clay","clean","clerk","clever","click","client","cliff","climb","clinic","clip","clock","clog","close","cloth","cloud","clown","club","clump","cluster","clutch","coach","coast","coconut","code","coffee","coil","coin","collect","color","column","combine","come","comfort","comic","common","company","concert","conduct","confirm","congress","connect","consider","control","convince","cook","cool","copper","copy","coral","core","corn","correct","cost","cotton","couch","country","couple","course","cousin","cover","coyote","crack","cradle","craft","cram","crane","crash","crater","crawl","crazy","cream","credit","creek","crew","cricket","crime","crisp","critic","crop","cross","crouch","crowd","crucial","cruel","cruise","crumble","crunch","crush","cry","crystal","cube","culture","cup","cupboard","curious","current","curtain","curve","cushion","custom","cute","cycle","dad","damage","damp","dance","danger","daring","dash","daughter","dawn","day","deal","debate","debris","decade","december","decide","decline","decorate","decrease","deer","defense","define","defy","degree","delay","deliver","demand","demise","denial","dentist","deny","depart","depend","deposit","depth","deputy","derive","describe","desert","design","desk","despair","destroy","detail","detect","develop","device","devote","diagram","dial","diamond","diary","dice","diesel","diet","differ","digital","dignity","dilemma","dinner","dinosaur","direct","dirt","disagree","discover","disease","dish","dismiss","disorder","display","distance","divert","divide","divorce","dizzy","doctor","document","dog","doll","dolphin","domain","donate","donkey","donor","door","dose","double","dove","draft","dragon","drama","drastic","draw","dream","dress","drift","drill","drink","drip","drive","drop","drum","dry","duck","dumb","dune","during","dust","dutch","duty","dwarf","dynamic","eager","eagle","early","earn","earth","easily","east","easy","echo","ecology","economy","edge","edit","educate","effort","egg","eight","either","elbow","elder","electric","elegant","element","elephant","elevator","elite","else","embark","embody","embrace","emerge","emotion","employ","empower","empty","enable","enact","end","endless","endorse","enemy","energy","enforce","engage","engine","enhance","enjoy","enlist","enough","enrich","enroll","ensure","enter","entire","entry","envelope","episode","equal","equip","era","erase","erode","erosion","error","erupt","escape","essay","essence","estate","eternal","ethics","evidence","evil","evoke","evolve","exact","example","excess","exchange","excite","exclude","excuse","execute","exercise","exhaust","exhibit","exile","exist","exit","exotic","expand","expect","expire","explain","expose","express","extend","extra","eye","eyebrow","fabric","face","faculty","fade","faint","faith","fall","false","fame","family","famous","fan","fancy","fantasy","farm","fashion","fat","fatal","father","fatigue","fault","favorite","feature","february","federal","fee","feed","feel","female","fence","festival","fetch","fever","few","fiber","fiction","field","figure","file","film","filter","final","find","fine","finger","finish","fire","firm","first","fiscal","fish","fit","fitness","fix","flag","flame","flash","flat","flavor","flee","flight","flip","float","flock","floor","flower","fluid","flush","fly","foam","focus","fog","foil","fold","follow","food","foot","force","forest","forget","fork","fortune","forum","forward","fossil","foster","found","fox","fragile","frame","frequent","fresh","friend","fringe","frog","front","frost","frown","frozen","fruit","fuel","fun","funny","furnace","fury","future","gadget","gain","galaxy","gallery","game","gap","garage","garbage","garden","garlic","garment","gas","gasp","gate","gather","gauge","gaze","general","genius","genre","gentle","genuine","gesture","ghost","giant","gift","giggle","ginger","giraffe","girl","give","glad","glance","glare","glass","glide","glimpse","globe","gloom","glory","glove","glow","glue","goat","goddess","gold","good","goose","gorilla","gospel","gossip","govern","gown","grab","grace","grain","grant","grape","grass","gravity","great","green","grid","grief","grit","grocery","group","grow","grunt","guard","guess","guide","guilt","guitar","gun","gym","habit","hair","half","hammer","hamster","hand","happy","harbor","hard","harsh","harvest","hat","have","hawk","hazard","head","health","heart","heavy","hedgehog","height","hello","helmet","help","hen","hero","hidden","high","hill","hint","hip","hire","history","hobby","hockey","hold","hole","holiday","hollow","home","honey","hood","hope","horn","horror","horse","hospital","host","hotel","hour","hover","hub","huge","human","humble","humor","hundred","hungry","hunt","hurdle","hurry","hurt","husband","hybrid","ice","icon","idea","identify","idle","ignore","ill","illegal","illness","image","imitate","immense","immune","impact","impose","improve","impulse","inch","include","income","increase","index","indicate","indoor","industry","infant","inflict","inform","inhale","inherit","initial","inject","injury","inmate","inner","innocent","input","inquiry","insane","insect","inside","inspire","install","intact","interest","into","invest","invite","involve","iron","island","isolate","issue","item","ivory","jacket","jaguar","jar","jazz","jealous","jeans","jelly","jewel","job","join","joke","journey","joy","judge","juice","jump","jungle","junior","junk","just","kangaroo","keen","keep","ketchup","key","kick","kid","kidney","kind","kingdom","kiss","kit","kitchen","kite","kitten","kiwi","knee","knife","knock","know","lab","label","labor","ladder","lady","lake","lamp","language","laptop","large","later","latin","laugh","laundry","lava","law","lawn","lawsuit","layer","lazy","leader","leaf","learn","leave","lecture","left","leg","legal","legend","leisure","lemon","lend","length","lens","leopard","lesson","letter","level","liar","liberty","library","license","life","lift","light","like","limb","limit","link","lion","liquid","list","little","live","lizard","load","loan","lobster","local","lock","logic","lonely","long","loop","lottery","loud","lounge","love","loyal","lucky","luggage","lumber","lunar","lunch","luxury","lyrics","machine","mad","magic","magnet","maid","mail","main","major","make","mammal","man","manage","mandate","mango","mansion","manual","maple","marble","march","margin","marine","market","marriage","mask","mass","master","match","material","math","matrix","matter","maximum","maze","meadow","mean","measure","meat","mechanic","medal","media","melody","melt","member","memory","mention","menu","mercy","merge","merit","merry","mesh","message","metal","method","middle","midnight","milk","million","mimic","mind","minimum","minor","minute","miracle","mirror","misery","miss","mistake","mix","mixed","mixture","mobile","model","modify","mom","moment","monitor","monkey","monster","month","moon","moral","more","morning","mosquito","mother","motion","motor","mountain","mouse","move","movie","much","muffin","mule","multiply","muscle","museum","mushroom","music","must","mutual","myself","mystery","myth","naive","name","napkin","narrow","nasty","nation","nature","near","neck","need","negative","neglect","neither","nephew","nerve","nest","net","network","neutral","never","news","next","nice","night","noble","noise","nominee","noodle","normal","north","nose","notable","note","nothing","notice","novel","now","nuclear","number","nurse","nut","oak","obey","object","oblige","obscure","observe","obtain","obvious","occur","ocean","october","odor","off","offer","office","often","oil","okay","old","olive","olympic","omit","once","one","onion","online","only","open","opera","opinion","oppose","option","orange","orbit","orchard","order","ordinary","organ","orient","original","orphan","ostrich","other","outdoor","outer","output","outside","oval","oven","over","own","owner","oxygen","oyster","ozone","pact","paddle","page","pair","palace","palm","panda","panel","panic","panther","paper","parade","parent","park","parrot","party","pass","patch","path","patient","patrol","pattern","pause","pave","payment","peace","peanut","pear","peasant","pelican","pen","penalty","pencil","people","pepper","perfect","permit","person","pet","phone","photo","phrase","physical","piano","picnic","picture","piece","pig","pigeon","pill","pilot","pink","pioneer","pipe","pistol","pitch","pizza","place","planet","plastic","plate","play","please","pledge","pluck","plug","plunge","poem","poet","point","polar","pole","police","pond","pony","pool","popular","portion","position","possible","post","potato","pottery","poverty","powder","power","practice","praise","predict","prefer","prepare","present","pretty","prevent","price","pride","primary","print","priority","prison","private","prize","problem","process","produce","profit","program","project","promote","proof","property","prosper","protect","proud","provide","public","pudding","pull","pulp","pulse","pumpkin","punch","pupil","puppy","purchase","purity","purpose","purse","push","put","puzzle","pyramid","quality","quantum","quarter","question","quick","quit","quiz","quote","rabbit","raccoon","race","rack","radar","radio","rail","rain","raise","rally","ramp","ranch","random","range","rapid","rare","rate","rather","raven","raw","razor","ready","real","reason","rebel","rebuild","recall","receive","recipe","record","recycle","reduce","reflect","reform","refuse","region","regret","regular","reject","relax","release","relief","rely","remain","remember","remind","remove","render","renew","rent","reopen","repair","repeat","replace","report","require","rescue","resemble","resist","resource","response","result","retire","retreat","return","reunion","reveal","review","reward","rhythm","rib","ribbon","rice","rich","ride","ridge","rifle","right","rigid","ring","riot","ripple","risk","ritual","rival","river","road","roast","robot","robust","rocket","romance","roof","rookie","room","rose","rotate","rough","round","route","royal","rubber","rude","rug","rule","run","runway","rural","sad","saddle","sadness","safe","sail","salad","salmon","salon","salt","salute","same","sample","sand","satisfy","satoshi","sauce","sausage","save","say","scale","scan","scare","scatter","scene","scheme","school","science","scissors","scorpion","scout","scrap","screen","script","scrub","sea","search","season","seat","second","secret","section","security","seed","seek","segment","select","sell","seminar","senior","sense","sentence","series","service","session","settle","setup","seven","shadow","shaft","shallow","share","shed","shell","sheriff","shield","shift","shine","ship","shiver","shock","shoe","shoot","shop","short","shoulder","shove","shrimp","shrug","shuffle","shy","sibling","sick","side","siege","sight","sign","silent","silk","silly","silver","similar","simple","since","sing","siren","sister","situate","six","size","skate","sketch","ski","skill","skin","skirt","skull","slab","slam","sleep","slender","slice","slide","slight","slim","slogan","slot","slow","slush","small","smart","smile","smoke","smooth","snack","snake","snap","sniff","snow","soap","soccer","social","sock","soda","soft","solar","soldier","solid","solution","solve","someone","song","soon","sorry","sort","soul","sound","soup","source","south","space","spare","spatial","spawn","speak","special","speed","spell","spend","sphere","spice","spider","spike","spin","spirit","split","spoil","sponsor","spoon","sport","spot","spray","spread","spring","spy","square","squeeze","squirrel","stable","stadium","staff","stage","stairs","stamp","stand","start","state","stay","steak","steel","stem","step","stereo","stick","still","sting","stock","stomach","stone","stool","story","stove","strategy","street","strike","strong","struggle","student","stuff","stumble","style","subject","submit","subway","success","such","sudden","suffer","sugar","suggest","suit","summer","sun","sunny","sunset","super","supply","supreme","sure","surface","surge","surprise","surround","survey","suspect","sustain","swallow","swamp","swap","swarm","swear","sweet","swift","swim","swing","switch","sword","symbol","symptom","syrup","system","table","tackle","tag","tail","talent","talk","tank","tape","target","task","taste","tattoo","taxi","teach","team","tell","ten","tenant","tennis","tent","term","test","text","thank","that","theme","then","theory","there","they","thing","this","thought","three","thrive","throw","thumb","thunder","ticket","tide","tiger","tilt","timber","time","tiny","tip","tired","tissue","title","toast","tobacco","today","toddler","toe","together","toilet","token","tomato","tomorrow","tone","tongue","tonight","tool","tooth","top","topic","topple","torch","tornado","tortoise","toss","total","tourist","toward","tower","town","toy","track","trade","traffic","tragic","train","transfer","trap","trash","travel","tray","treat","tree","trend","trial","tribe","trick","trigger","trim","trip","trophy","trouble","truck","true","truly","trumpet","trust","truth","try","tube","tuition","tumble","tuna","tunnel","turkey","turn","turtle","twelve","twenty","twice","twin","twist","two","type","typical","ugly","umbrella","unable","unaware","uncle","uncover","under","undo","unfair","unfold","unhappy","uniform","unique","unit","universe","unknown","unlock","until","unusual","unveil","update","upgrade","uphold","upon","upper","upset","urban","urge","usage","use","used","useful","useless","usual","utility","vacant","vacuum","vague","valid","valley","valve","van","vanish","vapor","various","vast","vault","vehicle","velvet","vendor","venture","venue","verb","verify","version","very","vessel","veteran","viable","vibrant","vicious","victory","video","view","village","vintage","violin","virtual","virus","visa","visit","visual","vital","vivid","vocal","voice","void","volcano","volume","vote","voyage","wage","wagon","wait","walk","wall","walnut","want","warfare","warm","warrior","wash","wasp","waste","water","wave","way","wealth","weapon","wear","weasel","weather","web","wedding","weekend","weird","welcome","west","wet","whale","what","wheat","wheel","when","where","whip","whisper","wide","width","wife","wild","will","win","window","wine","wing","wink","winner","winter","wire","wisdom","wise","wish","witness","wolf","woman","wonder","wood","wool","word","work","world","worry","worth","wrap","wreck","wrestle","wrist","write","wrong","yard","year","yellow","you","young","youth","zebra","zero","zone","zoo"];var sr=new TextEncoder().encode("calimero.mdma.account-link.v1\0"),ir=new TextEncoder().encode("calimero.mdma.account-login.v1\0"),ie=256,Nt=ie/32,Qe=(ie+Nt)/11;async function _t(){let n=crypto.getRandomValues(new Uint8Array(ie/8));return Ht(b(n),await ar(n))}async function qt(n){let e=or(n);return Ht(b(await cr(e)),e.join(" "))}async function L(n){return y(n,"secret",32),{secret:n,publicKey:b(await P(n)),accountId:await xe(n)}}async function oe(n){if(!n.nonce)throw new Error("nonce is required to sign an account link");let e=await I(n.rootSecret),t=new Uint8Array(await crypto.subtle.sign("Ed25519",e,h(sr,new TextEncoder().encode(n.nonce))));return Lt(t)}async function Ce(n){if(!n.nonce)throw new Error("nonce is required to sign an account login");let e=await I(n.rootSecret),t=new Uint8Array(await crypto.subtle.sign("Ed25519",e,h(ir,new TextEncoder().encode(n.nonce))));return Lt(t)}async function Ht(n,e){return{...await L(n),phrase:e}}function or(n){let e=n.normalize("NFKD").toLowerCase().trim().split(/\s+/),t=e[0]===""?0:e.length;if(t!==Qe)throw new Error(`recovery phrase must be ${Qe} words, got ${t}`);return e}async function ar(n){let e=Ot(n)+await Gt(n),t=[];for(let r=0;r<Qe;r+=1){let s=Number.parseInt(e.slice(r*11,r*11+11),2);t.push(ze[s])}return t.join(" ")}async function cr(n){let e="";for(let r of n){let s=ze.indexOf(r);if(s<0)throw new Error(`"${r}" is not a word in the recovery phrase list`);e+=s.toString(2).padStart(11,"0")}let t=new Uint8Array(ie/8);for(let r=0;r<t.length;r+=1)t[r]=Number.parseInt(e.slice(r*8,r*8+8),2);if(e.slice(ie)!==await Gt(t))throw new Error("recovery phrase checksum failed \u2014 check for a mistyped or transposed word");return t}async function Gt(n){let e=new Uint8Array(await crypto.subtle.digest("SHA-256",n));return Ot(e).slice(0,Nt)}function Ot(n){let e="";for(let t of n)e+=t.toString(2).padStart(8,"0");return e}function Lt(n){let e="";for(let t of n)e+=String.fromCharCode(t);return btoa(e)}var lr=new TextEncoder().encode("calimero.mdma.routing-read.v1\0");async function $t(n,e){y(e,"deviceSecret",32);let t=await I(e),r=h(lr,new TextEncoder().encode(n)),s=new Uint8Array(await crypto.subtle.sign({name:"Ed25519"},t,r));return ur(s)}async function ve(n,e){return{"X-Calimero-Credential":e.credential,"X-Calimero-Nonce":n.nonce,"X-Calimero-Signature":await $t(n.nonce,e.deviceSecret)}}function ur(n){let e="";for(let t of n)e+=String.fromCharCode(t);return btoa(e)}var dr="https://cloud.calimero.network";function Ye(n,e){return typeof n.can_execute=="boolean"?n.can_execute:e}function Xe(n){return typeof n.tee_role=="string"&&n.tee_role!==""?n.tee_role:null}function Ze(n){return n.find(e=>e.canExecute&&e.relayUrl!==null&&e.executorAccount!==null)??null}var pr="x-mdma-session-refresh",gr="x-mdma-session-expires",$=class{constructor(e={}){this.session=null;this.baseUrl=(e.cloudBaseUrl||dr).replace(/\/+$/,""),this.onSession=e.onSession;let t=e.fetch;this.fetchImpl=t?(r,s)=>t(r,s):(r,s)=>globalThis.fetch(r,s),this.timeoutMs=e.timeoutMs??1e4,this.routingCredential=e.routingCredential,e.sessionToken&&(this.session={sessionToken:e.sessionToken,expiresAt:0,user:{email:""}})}getSession(){return this.session}isSignedIn(){return this.session!==null}async signInWithGoogle(e){let t=await this.request("POST","/api/auth/google",{body:{id_token:e},anonymous:!0});return this.adoptSession(t)}async signInWithAccount(e){let t=await L(e),{nonce:r}=await this.getAccountLoginChallenge();return this.submitAccountLogin({rootPublicKey:t.publicKey,nonce:r,signature:await Ce({rootSecret:e,nonce:r})})}async getAccountLoginChallenge(){let e=await this.request("GET","/api/auth/account/challenge",{anonymous:!0});return{nonce:e.nonce,expiresAtMs:e.expires_at_ms}}async submitAccountLogin(e){let t=await this.request("POST","/api/auth/account",{body:{root_public_key:e.rootPublicKey,nonce:e.nonce,signature:e.signature},anonymous:!0});return this.adoptSession(t)}async refreshSession(){let e=await this.request("POST","/api/auth/refresh");return this.adoptSession(e)}async signOut(){try{this.session&&await this.request("POST","/api/auth/logout",{parse:"none"})}finally{this.session=null,this.notifySession(null)}}async getMyNamespaces(){return(await this.request("GET","/api/cloud/me/namespaces")).map(t=>({namespaceId:String(t.namespace_id??t.group_id??""),groupId:String(t.group_id??t.namespace_id??""),haStatus:String(t.ha_status??"none"),haEnabledAt:t.ha_enabled_at??null,contexts:Array.isArray(t.contexts)?t.contexts:[],fleetReplicas:t.fleet_replicas}))}async getNamespaceRelays(e){return((await this.request("GET",`/api/cloud/me/namespaces/${encodeURIComponent(e)}/relays`)).relays??[]).map(r=>{let s=r.relay_url??null,i=r.executor_account??null,a=r.authorship_ready===!0;return{peerId:String(r.peer_id??""),relayUrl:s,executorAccount:i,status:String(r.status??""),authorshipReady:a,teeRole:Xe(r),canExecute:Ye(r,a&&s!==null&&i!==null),lastSeenAt:r.last_seen_at??null,confirmedAt:r.confirmed_at??null}})}async findExecutingRelay(e){let t=await this.getNamespaceRelays(e);return Ze(t)}async getNamespaceRouting(e){let t=await this.request("GET",`/api/cloud/namespaces/${encodeURIComponent(e)}/admitters`,{anonymous:!0,headers:await this.routingProof(e)});return{namespaceId:String(t.namespace_id??e),nodes:(t.admitters??[]).map(r=>{let s=r.account??null,i=r.relay_url??null,a=r.fresh===!0,o=r.authorship_ready===!0;return{peerId:String(r.peer_id??""),account:s,relayUrl:i,admitUrl:r.admit_url??null,status:String(r.status??""),fresh:a,canAdmit:r.can_admit===!0,authorshipReady:o,teeRole:Xe(r),canExecute:Ye(r,o&&i!==null&&s!==null&&a)}}),servable:t.servable===!0,writable:t.writable===!0}}async getRoutingChallenge(e){let t=await this.request("GET",`/api/cloud/namespaces/${encodeURIComponent(e)}/challenge`,{anonymous:!0});return{namespaceId:String(t.namespace_id??e),nonce:String(t.nonce??""),expiresAtMs:Number(t.expires_at_ms??0)}}async routingProof(e){if(!this.routingCredential)return;let t=await this.getRoutingChallenge(e);return ve(t,this.routingCredential)}async getAccountRelaysChallenge(e){let t=await this.request("GET",`/api/cloud/accounts/${encodeURIComponent(e)}/challenge`,{anonymous:!0});return{accountId:String(t.account_id??e),nonce:String(t.nonce??""),expiresAtMs:Number(t.expires_at_ms??0)}}async getAccountRelays(e){if(!this.routingCredential)throw new Error("getAccountRelays needs a routingCredential: construct the client with the device certificate and its signing secret, since this read is proven by the device, not by a session.");let t=await this.getAccountRelaysChallenge(e);return((await this.request("GET",`/api/cloud/accounts/${encodeURIComponent(e)}/relays`,{anonymous:!0,headers:await ve(t,this.routingCredential)})).relays??[]).map(s=>({peerId:String(s.peer_id??""),relayUrl:s.relay_url??null,fresh:s.fresh===!0}))}async findAdmitter(e,t){let{nodes:r}=await this.getNamespaceRouting(e),s=t&&t.length>0?new Set(t.map(i=>i.toLowerCase())):null;return r.find(i=>i.canAdmit&&(s===null||i.account!==null&&s.has(i.account.toLowerCase())))??null}async getMyMachines(){return((await this.request("GET","/api/cloud/me/machines")).machines??[]).map(t=>{let r=t.relay_url??null,s=t.executor_account??null,i=(Array.isArray(t.namespaces)?t.namespaces:[]).map(a=>{let o=a;return{namespaceId:String(o.namespace_id??""),status:String(o.status??""),authorshipReady:o.authorship_ready===!0,teeRole:Xe(o),fresh:o.fresh===!0,confirmedAt:o.confirmed_at??null,lastSeenAt:o.last_seen_at??null}});return{peerId:String(t.peer_id??""),relayUrl:r,executorAccount:s,canExecute:Ye(t,r!==null&&s!==null&&i.some(a=>a.authorshipReady)),namespaces:i}})}async claimNamespace(e,t){await this.request("POST","/api/cloud/namespaces/claim",{body:{namespace_id:e,ownership_proof:t}})}async enableNamespaceHa(e,t={}){let r=t.admitterAddrs===void 0?{}:{admitter_addrs:[...t.admitterAddrs]};return this.request("POST",`/api/cloud/namespaces/${encodeURIComponent(e)}/enable-ha`,{body:r})}async disableNamespaceHa(e){return this.request("POST",`/api/cloud/namespaces/${encodeURIComponent(e)}/disable-ha`,{body:{}})}async getMyNode(){return(await this.request("GET","/api/cloud/me/node")).node??null}enableHA(e){let t=new URLSearchParams({group_id:e.groupId,context_id:e.contextId});e.redirectUrl&&t.set("redirect_url",e.redirectUrl),window.open(`${this.baseUrl}/enable-ha?${t.toString()}`)}disableHA(e){let t=new URLSearchParams({group_id:e.groupId,context_id:e.contextId});e.redirectUrl&&t.set("redirect_url",e.redirectUrl),window.open(`${this.baseUrl}/disable-ha?${t.toString()}`)}async linkAccount(e){let t=await L(e),{nonce:r}=await this.getAccountLinkChallenge();return this.submitAccountLink({accountId:t.accountId,rootPublicKey:t.publicKey,nonce:r,signature:await oe({rootSecret:e,nonce:r})})}async getAccountLinkChallenge(){let e=await this.request("GET","/api/cloud/me/accounts/challenge");return{nonce:e.nonce,expiresAtMs:e.expires_at_ms??null}}async submitAccountLink(e){let t=await this.request("POST","/api/cloud/me/accounts",{body:{account_id:e.accountId,root_public_key:e.rootPublicKey,nonce:e.nonce,signature:e.signature}});return{accountId:String(t.account_id??e.accountId),linkedAt:t.linked_at??null,alreadyLinked:t.already_linked===!0}}static accountLinkHandoff(e){let t=e.portalUrl.replace(/\/+$/,""),r=new URLSearchParams({"link-account":e.accountId,"callback-url":e.callbackUrl});return{url:`${t}/?${r.toString()}`,accountId:e.accountId}}static readAccountLinkCallback(e){let t=e??(typeof window>"u"?"":window.location.href);if(!t)return{};let r;try{r=new URL(t).hash}catch{return{}}let s=new URLSearchParams(r.replace(/^#/,"")),i=s.get("grant"),a=s.get("account"),o=s.get("error"),c={};return i&&(c.grant=i),a&&(c.accountId=a),o&&(c.error=o),c}async linkAccountWithGrant(e){let t=await L(e.rootSecret),r=await this.request("POST","/api/cloud/accounts/link",{body:{grant:e.grant,root_public_key:t.publicKey,signature:await oe({rootSecret:e.rootSecret,nonce:e.grant})},anonymous:!0});return{accountId:String(r.account_id??t.accountId),linkedAt:r.linked_at??null,alreadyLinked:r.already_linked===!0}}async getMyAccounts(){let e=await this.request("GET","/api/cloud/me/accounts");return{accounts:(e.accounts??[]).map(t=>({accountId:String(t.account_id??""),linkedAt:t.linked_at??null,hasRecoveryEnvelope:t.has_recovery_envelope===!0})),limit:e.limit??null}}adoptSession(e){return this.session={sessionToken:e.session_token,expiresAt:e.expires_at,user:e.user},this.notifySession(this.session),this.session}async notifySession(e){try{await this.onSession?.(e)}catch{}}async request(e,t,r={}){if(!r.anonymous&&!this.session)throw new Error("Not signed in to Calimero Cloud: call signInWithGoogle() or construct the client with a sessionToken");let s={...r.headers,Accept:"application/json"};r.body!==void 0&&(s["Content-Type"]="application/json"),!r.anonymous&&this.session&&(s.Authorization=`Bearer ${this.session.sessionToken}`);let i=new AbortController,a=setTimeout(()=>i.abort(),this.timeoutMs),o;try{o=await this.fetchImpl(`${this.baseUrl}${t}`,{method:e,headers:s,body:r.body===void 0?void 0:JSON.stringify(r.body),signal:i.signal})}catch(c){throw new v(0,c instanceof Error?c.name:"NetworkError",`${this.baseUrl}${t}`,new Headers,c instanceof Error?c.message:String(c))}finally{clearTimeout(a)}if(this.adoptRollingRefresh(o),!o.ok){let c=await o.text().catch(()=>{});throw new v(o.status,o.statusText,`${this.baseUrl}${t}`,o.headers,c)}if(!(r.parse==="none"||o.status===204))return await o.json()}adoptRollingRefresh(e){let t=e.headers.get(pr);if(!t||!this.session)return;let r=e.headers.get(gr),s=Number(r);this.session={sessionToken:t,expiresAt:Number.isFinite(s)&&s>0?s:this.session.expiresAt,user:this.session.user},this.notifySession(this.session)}};function mr(n){let e=n&&typeof n=="object"?n.data??n:void 0;if(!e||typeof e!="object")return[];let t=e.localPeerId,r=e.externalAddrs;if(typeof t!="string"||!t||!Array.isArray(r))return[];let s=`/p2p/${t}`,i=[];for(let a of r){if(typeof a!="string")continue;let o=a.trim();if(!o.startsWith("/"))continue;let c=o.endsWith(s)?o:`${o}${s}`;i.includes(c)||i.push(c)}return i}function hr(n){if(n)return typeof n=="function"?n:be(n)}var fr="mero-js:token-refresh";function yr(){let e=globalThis.navigator?.locks;return e&&typeof e.request=="function"?e:null}function et(n,e){try{let t=n.split(".");if(t.length===3){let r=t[1].replace(/-/g,"+").replace(/_/g,"/");for(;r.length%4;)r+="=";let s=JSON.parse(atob(r));if(typeof s.exp=="number")return s.exp*1e3}}catch{}return e}var ke=class{constructor(e){this.tokenData=null;this.refreshPromise=null;this.rpcClient=null;this.sseClient=null;this.wsClient=null;this.wsWarned=!1;this.config={timeoutMs:1e4,...e},this.tokenStore=e.tokenStore??null,this.tokenStore&&(this.tokenData=this.tokenStore.getTokens());let t=typeof window<"u"&&"__TAURI_INTERNALS__"in window,r=hr(this.config.requestProof);this.httpClient=F({baseUrl:this.config.baseUrl,fetch:this.config.fetch,getProof:r,getAuthToken:async()=>(await this.getValidToken())?.access_token||"",refreshToken:async()=>(await this.performTokenRefresh()).access_token,onTokenRefresh:async s=>{this.tokenData&&(this.tokenData.access_token=s,this.tokenStore?.setTokens(this.tokenData))},onAuthRevoked:async()=>{this.clearToken();try{await this.config.onAuthRevoked?.()}catch{}},timeoutMs:this.config.timeoutMs,credentials:this.config.requestCredentials??(t?"omit":void 0)}),this.authClient=At(this.httpClient,{baseUrl:this.config.baseUrl,getAuthToken:async()=>(await this.getValidToken())?.access_token||"",timeoutMs:this.config.timeoutMs}),this.adminClient=kt(this.httpClient,{baseUrl:this.config.baseUrl,getAuthToken:async()=>(await this.getValidToken())?.access_token||"",timeoutMs:this.config.timeoutMs})}get auth(){return this.authClient}get admin(){return this.adminClient}get rpc(){return this.rpcClient||(this.rpcClient=new re({httpClient:this.httpClient})),this.rpcClient}get events(){return this.sseClient||(this.sseClient=new W({baseUrl:this.config.baseUrl,fetch:this.config.fetch,getAuthToken:async()=>(await this.getValidToken())?.access_token||""})),this.sseClient}get ephemeral(){return this.ephemeralClient||(this.ephemeralClient=new se({httpClient:this.httpClient,sse:this.events})),this.ephemeralClient}get cloud(){return this.cloudClient||(this.cloudClient=new $(this.config.cloud??{})),this.cloudClient}get ws(){return this.wsWarned||(this.wsWarned=!0,console.warn("[mero-js] WsClient is experimental. Use mero.events (SSE) for production.")),this.wsClient||(this.wsClient=new J({baseUrl:this.config.baseUrl,getAuthToken:async()=>(await this.getValidToken())?.access_token||""})),this.wsClient}async authenticate(e){let t=e||this.config.credentials;if(!t)throw new Error("No credentials provided for authentication");try{let r={auth_method:"user_password",public_key:t.username,client_name:"mero-js-sdk",permissions:["admin"],timestamp:Math.floor(Date.now()/1e3),provider_data:{username:t.username,password:t.password}},s=await this.authClient.generateTokens(r),i=s.data.access_token;return this.tokenData={access_token:i,refresh_token:s.data.refresh_token,expires_at:et(i,Date.now()+36e5)},this.tokenStore?.setTokens(this.tokenData),this.tokenData}catch(r){throw new Error(`Authentication failed: ${r instanceof Error?r.message:"Unknown error"}`)}}async getValidToken(){return this.tokenData}async performTokenRefresh(){if(this.refreshPromise)return this.refreshPromise;let e=this.tokenData?.access_token,t=this.withRefreshLock(()=>this.performTokenRefreshLocked(e));this.refreshPromise=t;try{return await t}finally{this.refreshPromise===t&&(this.refreshPromise=null)}}withRefreshLock(e){let t=yr();return t?t.request(fr,e):e()}async performTokenRefreshLocked(e){let t=this.tokenStore?.getTokens()??null;if(t?.refresh_token&&(this.tokenData=t,e&&t.access_token!==e))return t;if(!this.tokenData?.refresh_token)throw new Error("No refresh token available");try{let r=await this.authClient.refreshToken({access_token:this.tokenData.access_token,refresh_token:this.tokenData.refresh_token}),s=r.data.access_token;return this.tokenData={access_token:s,refresh_token:r.data.refresh_token,expires_at:et(s,Date.now()+36e5)},this.tokenStore?.setTokens(this.tokenData),this.tokenData}catch(r){throw new Error(`Token refresh failed: ${r instanceof Error?r.message:"Unknown error"}`)}}clearToken(){this.refreshPromise=null,this.tokenData=null,this.tokenStore?.clear()}isAuthenticated(){return this.tokenData!==null}setTokenData(e){let t=e.expires_at||et(e.access_token,Date.now()+36e5);this.tokenData={...e,expires_at:t},this.tokenStore?.setTokens(this.tokenData)}getTokenData(){return this.tokenData}close(){this.sseClient?.close(),this.wsClient?.close()}static parseAuthCallback(e){return Ke(e)}static buildAuthLoginUrl(e,t){return je(e,t)}};function br(n){return new ke(n)}var tt=class{constructor(){this.tokens=null}getTokens(){return this.tokens}setTokens(e){this.tokens=e}clear(){this.tokens=null}},wr="mero-tokens",nt=class{constructor(e=wr){this.key=e}getTokens(){try{if(typeof localStorage>"u")return null;let e=localStorage.getItem(this.key);if(!e)return null;let t=JSON.parse(e);return t&&t.access_token&&t.refresh_token?{access_token:t.access_token,refresh_token:t.refresh_token,expires_at:typeof t.expires_at=="number"?t.expires_at:Date.now()+36e5}:null}catch{return null}}setTokens(e){try{if(typeof localStorage>"u")return;localStorage.setItem(this.key,JSON.stringify(e))}catch{}}clear(){try{if(typeof localStorage>"u")return;localStorage.removeItem(this.key)}catch{}}};var Rr=new TextEncoder().encode("calimero.warrant.v2"),Ar=new TextEncoder().encode("calimero.warrant.intent.v1"),Bt=64,xr="00".repeat(32);async function rt(n,e){let t=new TextEncoder().encode(JSON.stringify(e));return U(Ar,[new TextEncoder().encode(n),t])}async function Se(n){let e=y(n.context,"context",32),t=y(n.authorAccount,"authorAccount",32),r=y(n.executor,"executor",32),s=y(n.appVersion??xr,"appVersion",32),i=Kt(n.accountHeads,"accountHeads"),a=Kt(n.governanceFloor,"governanceFloor"),o=await I(n.deviceSecret),c=await P(n.deviceSecret),l=new TextEncoder().encode(n.method),d=await rt(n.method,n.argsJson),p=S(n.nonce),m=S(n.notAfter),f=await U(Rr,[e,t,c,r,s,l,d,S(i.length),...i,S(a.length),...a,p,m]),g=new Uint8Array(await crypto.subtle.sign({name:"Ed25519"},o,f));return b(h(e,t,c,r,s,k(l.length),l,d,k(i.length),...i,k(a.length),...a,p,m,g))}function Kt(n,e){let t=n??[];if(t.length>Bt)throw new Error(`${e} cites ${t.length} heads, over the ${Bt} a node accepts`);return t.map((r,s)=>y(r,`${e}[${s}]`,32))}var Cr=300,Te=class extends Error{constructor(t,r,s){super(`relay refused the intent (HTTP ${s}): ${t}`);this.reason=t;this.retryable=r;this.status=s;this.name="IntentRefusedError"}},ae=class{constructor(e){this.config=e,this.baseUrl=e.relayUrl.replace(/\/+$/,""),this.executorAccount=e.executorAccount;let t=e.fetch;this.fetchImpl=t?(r,s)=>t(r,s):(r,s)=>globalThis.fetch(r,s),this.timeoutMs=e.timeoutMs??1e4}async describe(e){let t=await this.json("GET",`/admin-api/contexts/${encodeURIComponent(e)}/intents`);return this.executorAccount=t.data.executorAccount,t.data}async execute(e,t,r={}){let s=this.executorAccount??(await this.describe(e)).executorAccount,i=await this.config.nonces.next(),a=this.config.ttlSeconds??Cr,o=await Se({context:e,authorAccount:this.config.authorAccount,executor:s,method:t,argsJson:r,nonce:i,notAfter:BigInt(Math.floor(Date.now()/1e3))+BigInt(a),deviceSecret:this.config.deviceSecret}),c=await this.json("POST",`/admin-api/contexts/${encodeURIComponent(e)}/intents`,{method:t,argsJson:r,warrant:o,authorProof:this.config.authorProof});return{rootHash:c.data.rootHash,returns:c.data.returns??null}}async json(e,t,r){let s={Accept:"application/json"};r!==void 0&&(s["Content-Type"]="application/json");let i=new AbortController,a=setTimeout(()=>i.abort(),this.timeoutMs),o;try{o=await this.fetchImpl(`${this.baseUrl}${t}`,{method:e,headers:s,body:r===void 0?void 0:JSON.stringify(r),signal:i.signal})}catch(c){throw new v(0,c instanceof Error?c.name:"NetworkError",`${this.baseUrl}${t}`,new Headers,c instanceof Error?c.message:String(c))}finally{clearTimeout(a)}if(!o.ok){let c=await o.text().catch(()=>"");if(o.status===400||o.status===403){let l=vr(c);throw new Te(l,/nonce/i.test(l),o.status)}throw new v(o.status,o.statusText,`${this.baseUrl}${t}`,o.headers,c)}return await o.json()}};function vr(n){if(!n)return"no reason given";try{let e=JSON.parse(n);for(let t of[e.error,e.message])if(typeof t=="string"&&t)return t}catch{}return n}function st(n=1){let e=BigInt(n);if(e<1n)throw new Error("nonce sequence starts at 1");return{next(){let t=e;return e+=1n,Promise.resolve(t)}}}function it(n,e){let t=e??globalThis.localStorage;if(!t)throw new Error("no localStorage in this runtime: pass a storage implementation, or use createMemoryNonceSource for a process that owns its whole sequence");return{next(){let r=t.getItem(n),s=0n;if(r!==null&&r!=="")try{let a=BigInt(r);a>0n&&(s=a)}catch{s=0n}let i=s+1n;return t.setItem(n,i.toString()),Promise.resolve(i)}}}var kr="Noise_NK_25519_AESGCM_SHA256",ot=32,Sr=16,Tr=new Uint8Array([48,46,2,1,0,48,5,6,3,43,101,110,4,34,4,32]),ct=class n{constructor(e){this.k=null;this.n=0;this.ck=e,this.h=e}static async initialize(){let e=new TextEncoder().encode(kr),t=new Uint8Array(32);return t.set(e),new n(t)}async mixHash(e){this.h=await Ir(h(this.h,e))}async mixKey(e){let[t,r]=await Ft(this.ck,e);this.ck=t,this.k=await crypto.subtle.importKey("raw",r,"AES-GCM",!1,["encrypt","decrypt"]),this.n=0}async encryptAndHash(e){if(!this.k)throw new Error("Noise: no cipher key yet");let t=new Uint8Array(await crypto.subtle.encrypt({name:"AES-GCM",iv:Vt(this.n),additionalData:this.h},this.k,e));return this.n+=1,await this.mixHash(t),t}async decryptAndHash(e){if(!this.k)throw new Error("Noise: no cipher key yet");let t=new Uint8Array(await crypto.subtle.decrypt({name:"AES-GCM",iv:Vt(this.n),additionalData:this.h},this.k,e));return this.n+=1,await this.mixHash(e),t}split(){return Ft(this.ck,new Uint8Array)}};async function Wt(n,e,t){let r=await ct.initialize();await r.mixHash(e),await r.mixHash(n);let s=await Er(t);return await r.mixHash(s.publicKey),await r.mixKey(await jt(s.privateKey,n)),{message1:h(s.publicKey,await r.encryptAndHash(new Uint8Array)),async finish(a){if(a.length<ot+Sr)throw new Error("Noise: message 2 is too short");let o=a.slice(0,ot);return await r.mixHash(o),await r.mixKey(await jt(s.privateKey,o)),{payload:await r.decryptAndHash(a.slice(ot)),split:await r.split()}}}}async function Er(n){if(!n){let r;try{r=await crypto.subtle.generateKey({name:"X25519"},!0,["deriveBits"])}catch(i){throw new Error(`Sealed transport needs WebCrypto with X25519, which this runtime lacks (${i?.name??"no crypto.subtle"}). Current browsers and Node have it; in React Native, install react-native-quick-crypto as the global crypto.`)}let s=new Uint8Array(await crypto.subtle.exportKey("raw",r.publicKey));return{privateKey:r.privateKey,publicKey:s}}let e=await crypto.subtle.importKey("pkcs8",h(Tr,n),{name:"X25519"},!0,["deriveBits"]),t=await crypto.subtle.exportKey("jwk",e);return{privateKey:e,publicKey:Pr(t.x)}}async function jt(n,e){let t=await crypto.subtle.importKey("raw",e,{name:"X25519"},!1,[]);return new Uint8Array(await crypto.subtle.deriveBits({name:"X25519",public:t},n,256))}function Vt(n){let e=new Uint8Array(12);return new DataView(e.buffer).setBigUint64(4,BigInt(n),!1),e}async function Ir(n){return new Uint8Array(await crypto.subtle.digest("SHA-256",n))}async function at(n,e){let t=await crypto.subtle.importKey("raw",n,{name:"HMAC",hash:"SHA-256"},!1,["sign"]);return new Uint8Array(await crypto.subtle.sign("HMAC",t,e))}async function Ft(n,e){let t=await at(n,e),r=await at(t,new Uint8Array([1])),s=await at(t,h(r,new Uint8Array([2])));return[r,s]}function Pr(n){let e=n.replace(/-/g,"+").replace(/_/g,"/"),t=atob(e+"=".repeat((4-e.length%4)%4));return Uint8Array.from(t,r=>r.charCodeAt(0))}var Yt="/sealed/v2/handshake",Xt="/sealed/v2",le="application/vnd.calimero.sealed",lt=2,Dr=new TextEncoder().encode("calimero/sealed-http/v2"),ce=16,Ur=1+ce+8,Mr=0,Nr=1,_r=2,qr=1+64*1024+16,Hr=1024,Gr=6e4,Or="calimero.tee-attest.transport-key.v1",Lr=[101,204,205,304],Zt=new TextEncoder,ue=class extends Error{constructor(){super("The node no longer holds the attested transport key; attest again for the current one");this.name="StaleTransportKeyError"}},z=class extends Error{constructor(t,r,s,i){super(s);this.status=t;this.code=r;this.retryAfterMs=i;this.name="SealedTransportError"}};async function en(n,e){let t=await crypto.subtle.digest("SHA-256",h(Zt.encode(Or),n,e));return new Uint8Array(t)}async function tn(n,e,t={}){if(t.applicationId===void 0!=(t.applicationHash===void 0))throw new Error("applicationId and applicationHash are given together or not at all");let r=b(crypto.getRandomValues(new Uint8Array(32))),s=await n.teeAttest({nonce:r,applicationId:t.applicationId,bindTransportKey:!0,...e.includeCollateral?{includeCollateral:!0}:{}});if(!s.transportPublicKey)throw new Error("The node did not report a transport key; it predates sealed transport");let i=y(s.transportPublicKey,"transportPublicKey",32),a=t.applicationHash?y(t.applicationHash,"applicationHash",32):new Uint8Array(32),o=b(await en(a,i));if(!await e({quoteB64:s.quoteB64,nonce:r,reportDataSuffix:o,collateral:s.collateral}))throw new Error("The attestation did not verify, so its transport key is not trusted");return i}function de(n){let e=n.fetch??((s,i)=>fetch(s,i)),t=`${n.baseUrl.replace(/\/+$/,"")}/admin-api/tee/attest`,r={async teeAttest(s){let i=await e(t,{method:"POST",headers:{"content-type":"application/json"},body:JSON.stringify(s)});if(!i.ok)throw new Error(`The node refused to attest: HTTP ${i.status}`);let a=await i.json();if(!a.data)throw new Error("The attestation response is malformed");return a.data}};return nn({baseUrl:n.baseUrl,transportPublicKey:()=>tn(r,n.verify,{applicationId:n.applicationId,applicationHash:n.applicationHash}),fetch:e})}function nn(n){let e=new URL(n.baseUrl),t=e.pathname.replace(/\/+$/,""),r=`${e.origin}${t}`,s=n.fetch??((p,m)=>fetch(p,m)),i=typeof n.transportPublicKey=="function"?n.transportPublicKey:void 0,a=i?Promise.resolve().then(i):Promise.resolve(n.transportPublicKey);a.catch(()=>{});let o,c=async()=>{try{return await zt(s,r,await Jt(a))}catch(p){if(!(p instanceof ue)||!i)throw p;return a=Promise.resolve().then(i),zt(s,r,await Jt(a))}},l=async()=>{let p=o??d(void 0),m=await p;if(Date.now()<m.renewAt)return[m,p];let f=d(p);return[await f,f]},d=p=>{if(o===p){let m=c();o=m,m.catch(()=>{o===m&&(o=void 0)})}return o};return async(p,m)=>{let f=new Request(p,m),g=new URL(f.url);if(g.origin!==e.origin||!g.pathname.startsWith(`${t}/`))throw new Error(`Refusing to send ${g.href} outside the sealed node at ${n.baseUrl}`);let R=[];f.headers.forEach((E,N)=>R.push([N,E]));let C={method:f.method,path:g.pathname.slice(t.length)+g.search,headers:R},A=new Uint8Array(await f.arrayBuffer()),x=async E=>{let{requestId:N,header:B,envelope:K}=await Br(E,C,A),j=await s(`${r}${Xt}`,{method:"POST",headers:{"content-type":le},body:K,signal:f.signal});if(j.headers.get("content-type")!==le)throw await sn(j);return jr(j,E.responseKey,B,N)},[T,H]=await l();try{return await x(T)}catch(E){if(!(E instanceof z)||E.code!=="unknown_session")throw E;return x(await d(H))}}}async function Jt(n){let e=await n;if(e.length!==32)throw new Error("transportPublicKey must be 32 bytes");return e}async function zt(n,e,t){try{return await Qt(n,e,t)}catch(r){if(!(r instanceof z)||r.code!=="busy")throw r;return await new Promise(s=>setTimeout(s,r.retryAfterMs??1e3)),Qt(n,e,t)}}async function Qt(n,e,t){let r=await Wt(t,Dr),s=await n(`${e}${Yt}`,{method:"POST",headers:{"content-type":le},body:h(new Uint8Array([lt]),t,r.message1)});if(s.headers.get("content-type")!==le)throw await sn(s);let i=await Fr(s,Hr);return $r(r,i)}async function $r(n,e){if(e[0]!==lt)throw new Error("The handshake reply is malformed");let t;try{t=await n.finish(e.slice(1))}catch{throw new Error("The handshake reply did not open: it did not come from the attested node")}let{payload:r,split:s}=t;if(r.length!==ce+4)throw new Error("The handshake reply is malformed");let i=r.slice(0,ce),a=new DataView(r.buffer,r.byteOffset).getUint32(ce,!1),[o,c]=await Promise.all([crypto.subtle.importKey("raw",s[0],"AES-GCM",!1,["encrypt"]),crypto.subtle.importKey("raw",s[1],"AES-GCM",!1,["decrypt"])]);return s[0].fill(0),s[1].fill(0),{requestKey:o,responseKey:c,nextRequestId:1,renewAt:Date.now()+a*1e3-Gr,header(l){let d=new Uint8Array(Ur);return d[0]=lt,d.set(i,1),new DataView(d.buffer).setBigUint64(1+ce,BigInt(l),!1),d}}}async function Br(n,e,t){let r=n.nextRequestId;n.nextRequestId+=1;let s=n.header(r),i=await crypto.subtle.encrypt({name:"AES-GCM",iv:rn(r,0),additionalData:s},n.requestKey,Kr(e,t));return{requestId:r,header:s,envelope:h(s,new Uint8Array(i))}}function rn(n,e){let t=new Uint8Array(12),r=new DataView(t.buffer);return r.setBigUint64(0,BigInt(n),!1),r.setUint32(8,e,!1),t}function Kr(n,e){let t=Zt.encode(JSON.stringify(n)),r=new Uint8Array(4);return new DataView(r.buffer).setUint32(0,t.length,!1),h(r,t,e)}async function jr(n,e,t,r){if(!n.body)throw new Error("The sealed response has no body");let s=Vr(n.body.getReader()),i=0,a=async()=>{let d=await s.next();if(!d)return null;let p;try{p=new Uint8Array(await crypto.subtle.decrypt({name:"AES-GCM",iv:rn(r,i),additionalData:t},e,d))}catch{throw new Error("A sealed response frame did not open: it was not sealed by the attested node")}return i+=1,{kind:p[0],data:p.slice(1)}},o=await a();if(!o||o.kind!==Mr)throw new Error("The sealed response is malformed");let c=JSON.parse(new TextDecoder().decode(o.data));if(Lr.includes(c.status))return await s.cancel(),new Response(null,{status:c.status,headers:c.headers});let l=new ReadableStream({async pull(d){try{let p=await a();p?p.kind===Nr?d.enqueue(p.data):p.kind===_r?(d.close(),await s.cancel()):d.error(new Error("The sealed response is malformed")):d.error(new Error("The sealed response was cut short"))}catch(p){d.error(p)}},cancel(){return s.cancel()}});return new Response(l,{status:c.status,headers:c.headers})}function Vr(n){let e=new Uint8Array,t=async r=>{for(;e.length<r;){let{done:s,value:i}=await n.read();if(s)return!1;e=h(e,i)}return!0};return{async next(){if(!await t(4))return null;let r=new DataView(e.buffer,e.byteOffset).getUint32(0,!1);if(r>qr)throw await n.cancel(),new Error("The sealed response is malformed: a frame is larger than the node sends");if(!await t(4+r))return null;let s=e.slice(4,4+r);return e=e.slice(4+r),s},cancel:()=>n.cancel()}}async function Fr(n,e){if(!n.body)return new Uint8Array;let t=n.body.getReader(),r=new Uint8Array;for(;;){let{done:s,value:i}=await t.read();if(s)return r;if(r=h(r,i),r.length>e)throw await t.cancel(),new Error("The handshake reply is malformed: it is too long")}}async function sn(n){let e,t=`Sealed request refused: HTTP ${n.status}`;try{let i=await n.json();e=i?.error?.code,i?.error?.message&&(t=`Sealed request refused: ${i.error.message}`)}catch{}if(e==="stale_transport_key")return new ue;let r=Number(n.headers.get("retry-after")),s=Number.isFinite(r)&&r>0?Math.min(r,5)*1e3:void 0;return new z(n.status,e,t,s)}async function Wr(n){if(!n.sessionToken&&!n.googleIdToken)throw new Error("connectCloud needs either a sessionToken or a googleIdToken");let e=new $({cloudBaseUrl:n.cloudBaseUrl,sessionToken:n.sessionToken,onSession:n.onSession,fetch:n.fetch,timeoutMs:n.timeoutMs});!n.sessionToken&&n.googleIdToken&&await e.signInWithGoogle(n.googleIdToken);let t=await e.getMyNamespaces(),r=Jr(t,n.namespaceId),s=await e.getNamespaceRelays(r.namespaceId),i=Ze(s);if(!i)throw new Error(zr(r,s));let a=new ae({relayUrl:i.relayUrl,executorAccount:i.executorAccount,authorAccount:n.authorAccount,authorProof:n.authorProof,deviceSecret:n.deviceSecret,nonces:n.nonces??await Qr(n.deviceSecret),ttlSeconds:n.ttlSeconds,fetch:n.seal?de({baseUrl:i.relayUrl,verify:n.seal,fetch:n.fetch}):n.fetch,timeoutMs:n.timeoutMs});return{cloud:e,relay:a,namespaceId:r.namespaceId,namespace:r,relayInfo:i,execute:(o,c,l)=>a.execute(o,c,l)}}function Jr(n,e){if(e){let t=n.find(r=>r.namespaceId===e);if(!t)throw new Error(`this account owns no namespace ${e}; it owns: ${n.map(r=>r.namespaceId).join(", ")||"(none)"}`);return t}if(n.length===0)throw new Error("this account owns no namespaces in the cloud: claim one first, then enable HA on it so a relay is assigned");if(n.length>1)throw new Error(`this account owns ${n.length} namespaces \u2014 pass namespaceId to say which: ${n.map(t=>t.namespaceId).join(", ")}`);return n[0]}function zr(n,e){if(e.length===0)return n.haStatus==="enabled"?`namespace ${n.namespaceId} has HA enabled but no relay has been assigned to it yet \u2014 retry shortly`:`namespace ${n.namespaceId} has no relays: enable HA on it in the cloud to have an attested node assigned`;let t=e.filter(s=>s.teeRole!==null&&s.teeRole!=="RelayTee");if(t.length===e.length)return`every node on namespace ${n.namespaceId} is a read-only TEE replica (ReadOnlyTee), which never relays delegated writes: an admin of the namespace must set its TEE admission policy with mode: 'relay' \u2014 AdminClient.setTeeAdmissionPolicy(namespaceId, { ..., mode: 'relay' }) \u2014 so fleet nodes are admitted as RelayTee`;let r=e.filter(s=>s.teeRole===null&&!s.authorshipReady);return r.length>0&&r.length+t.length===e.length?`every relay on namespace ${n.namespaceId} is waiting for the authorship grant: an admin of the namespace must grant CAN_AUTHOR_ON_BEHALF to ${r.map(s=>s.executorAccount??s.peerId).join(", ")||"the relay account"} \u2014 with AdminClient.grantAuthorship(namespaceId, account) for these relays, and AdminClient.openToDelegatedExecution(namespaceId) so relays assigned later need no further op`:`namespace ${n.namespaceId} has ${e.length} relay(s), but none is ready to relay yet (a URL, an executor account and a recent heartbeat) \u2014 retry shortly`}async function Qr(n){let e=`mero-js:warrant-nonce:${b(await P(n))}`;try{return it(e)}catch{return st(1)}}var Yr=/^[0-9a-f]{96}$/,Xr=["mrtd","rtmr0","rtmr1","rtmr2","rtmr3"];function ut(n){let e=Q(n.allowedMrtd??[],"allowedMrtd"),t=(n.allowedMeasurements??[]).map((o,c)=>{let l=`allowedMeasurements[${c}]`;return Xr.map(d=>Q([o?.[d]??""],`${l}.${d}`)[0]).join()}),r=[Q(n.allowedRtmr0??[],"allowedRtmr0"),Q(n.allowedRtmr1??[],"allowedRtmr1"),Q(n.allowedRtmr2??[],"allowedRtmr2"),Q(n.allowedRtmr3??[],"allowedRtmr3")];if(n.allowedMeasurements!==void 0&&t.length===0)throw new Error("allowedMeasurements is empty, so no quote would ever be accepted");if(t.length===0){if(e.length===0)throw new Error("No image is trusted: give allowedMeasurements, or allowedMrtd with allowedRtmr1-3");if(r.slice(1).some(o=>o.length===0))throw new Error("allowedMrtd alone does not name an image: the MRTD measures the TD firmware, which images share, and the image is in RTMR1-3. Give allowedMeasurements, or allowedRtmr1-3 too")}let s=n.allowedTcbStatuses??["UpToDate"],i=n.now??Date.now;return Object.assign(async({quoteB64:o,nonce:c,reportDataSuffix:l,collateral:d})=>{let p=es(o),m=d??await n.fetchCollateral?.(p);if(!m)throw new Error("The node sent no collateral for its quote, and no fetchCollateral was given to get it");let f;try{f=n.dcapVerify(p,m,Math.floor(i()/1e3))}catch(A){throw new Error(`The quote did not verify: ${A?.message??String(A)}`)}if(f.status==="Revoked"||!s.includes(f.status))throw new Error(`The platform's TCB status is ${f.status}, which is not accepted`);let g=Zr(f.report),R=[g.mrTd,g.rtMr0,g.rtMr1,g.rtMr2,g.rtMr3].map(A=>b(A));if(t.length>0&&!t.includes(R.join()))throw new Error(`The measurements (MRTD ${R[0]}, RTMR0-3 ${R.slice(1).join(", ")}) are not an image this verifier trusts`);if(e.length>0&&!e.includes(R[0]))throw new Error(`MRTD ${R[0]} is not an image this verifier trusts`);if([g.rtMr0,g.rtMr1,g.rtMr2,g.rtMr3].forEach((A,x)=>{let T=r[x];if(T.length>0&&!T.includes(b(A)))throw new Error(`RTMR${x} ${b(A)} is not one this verifier trusts`)}),b(g.reportData)!==`${c}${l}`.toLowerCase())throw new Error("The quote does not commit to this nonce and binding");return!0},{includeCollateral:!0})}function Q(n,e){return n.map(t=>{let r=t.trim().toLowerCase();if(!Yr.test(r))throw new Error(`${e} holds ${JSON.stringify(t)}, which is not a 48-byte hex measurement`);return r})}function Zr(n){let e=n.data;if(n.type==="td10"&&e)return e;if(n.type==="td15"&&e?.base)return e.base;throw new Error(`The quote is a ${n.type} quote, not a TDX one`)}function es(n){let e=atob(n),t=new Uint8Array(e.length);for(let r=0;r<e.length;r+=1)t[r]=e.charCodeAt(r);return t}var ts=["UpToDate","SWHardeningNeeded","ConfigurationNeeded","ConfigurationAndSWHardeningNeeded","OutOfDate","OutOfDateConfigurationNeeded"];function dt(n,{profile:e}){if(n.length===0)throw new Error("No release given, so no image would be trusted");let t,r=n.map((s,i)=>{let a=s?.tag??`releases[${i}]`;if(s?.role!==void 0&&s.role!=="node")throw new Error(`${a} lists the measurements of a ${s.role}, not of a node image`);let o=s?.profiles?.[e];if(!o){let l=Object.keys(s?.profiles??{}).join(", ")||"none";throw new Error(`${a} has no profile ${JSON.stringify(e)} (it has: ${l})`)}let c=(o.allowed_tcb_statuses??["uptodate"]).map(l=>ns(l,a));return t=t===void 0?c:t.filter(l=>c.includes(l)),{mrtd:o.mrtd,rtmr0:o.rtmr0,rtmr1:o.rtmr1,rtmr2:o.rtmr2,rtmr3:o.rtmr3}});if(!t||t.length===0)throw new Error("The releases given accept no TCB status in common");return{allowedMeasurements:r,allowedTcbStatuses:t}}function ns(n,e){let t=ts.find(r=>r.toLowerCase()===String(n).toLowerCase());if(!t)throw new Error(`${e} accepts TCB status ${JSON.stringify(n)}, which this verifier does not know`);return t}var on=[{uri:"https://fulcio.sigstore.dev",validFor:{start:"2021-03-07T03:20:29Z",end:"2022-12-31T23:59:59.999Z"},certChain:["MIIB+DCCAX6gAwIBAgITNVkDZoCiofPDsy7dfm6geLbuhzAKBggqhkjOPQQDAzAqMRUwEwYDVQQKEwxzaWdzdG9yZS5kZXYxETAPBgNVBAMTCHNpZ3N0b3JlMB4XDTIxMDMwNzAzMjAyOVoXDTMxMDIyMzAzMjAyOVowKjEVMBMGA1UEChMMc2lnc3RvcmUuZGV2MREwDwYDVQQDEwhzaWdzdG9yZTB2MBAGByqGSM49AgEGBSuBBAAiA2IABLSyA7Ii5k+pNO8ZEWY0ylemWDowOkNa3kL+GZE5Z5GWehL9/A9bRNA3RbrsZ5i0JcastaRL7Sp5fp/jD5dxqc/UdTVnlvS16an+2Yfswe/QuLolRUCrcOE2+2iA5+tzd6NmMGQwDgYDVR0PAQH/BAQDAgEGMBIGA1UdEwEB/wQIMAYBAf8CAQEwHQYDVR0OBBYEFMjFHQBBmiQpMlEk6w2uSu1KBtPsMB8GA1UdIwQYMBaAFMjFHQBBmiQpMlEk6w2uSu1KBtPsMAoGCCqGSM49BAMDA2gAMGUCMH8liWJfMui6vXXBhjDgY4MwslmN/TJxVe/83WrFomwmNf056y1X48F9c4m3a3ozXAIxAKjRay5/aj/jsKKGIkmQatjI8uupHr/+CxFvaJWmpYqNkLDGRU+9orzh5hI2RrcuaQ=="]},{uri:"https://fulcio.sigstore.dev",validFor:{start:"2022-04-13T20:06:15Z"},certChain:["MIICGjCCAaGgAwIBAgIUALnViVfnU0brJasmRkHrn/UnfaQwCgYIKoZIzj0EAwMwKjEVMBMGA1UEChMMc2lnc3RvcmUuZGV2MREwDwYDVQQDEwhzaWdzdG9yZTAeFw0yMjA0MTMyMDA2MTVaFw0zMTEwMDUxMzU2NThaMDcxFTATBgNVBAoTDHNpZ3N0b3JlLmRldjEeMBwGA1UEAxMVc2lnc3RvcmUtaW50ZXJtZWRpYXRlMHYwEAYHKoZIzj0CAQYFK4EEACIDYgAE8RVS/ysH+NOvuDZyPIZtilgUF9NlarYpAd9HP1vBBH1U5CV77LSS7s0ZiH4nE7Hv7ptS6LvvR/STk798LVgMzLlJ4HeIfF3tHSaexLcYpSASr1kS0N/RgBJz/9jWCiXno3sweTAOBgNVHQ8BAf8EBAMCAQYwEwYDVR0lBAwwCgYIKwYBBQUHAwMwEgYDVR0TAQH/BAgwBgEB/wIBADAdBgNVHQ4EFgQU39Ppz1YkEZb5qNjpKFWixi4YZD8wHwYDVR0jBBgwFoAUWMAeX5FFpWapesyQoZMi0CrFxfowCgYIKoZIzj0EAwMDZwAwZAIwPCsQK4DYiZYDPIaDi5HFKnfxXx6ASSVmERfsynYBiX2X6SJRnZU84/9DZdnFvvxmAjBOt6QpBlc4J/0DxvkTCqpclvziL6BCCPnjdlIB3Pu3BxsPmygUY7Ii2zbdCdliiow=","MIIB9zCCAXygAwIBAgIUALZNAPFdxHPwjeDloDwyYChAO/4wCgYIKoZIzj0EAwMwKjEVMBMGA1UEChMMc2lnc3RvcmUuZGV2MREwDwYDVQQDEwhzaWdzdG9yZTAeFw0yMTEwMDcxMzU2NTlaFw0zMTEwMDUxMzU2NThaMCoxFTATBgNVBAoTDHNpZ3N0b3JlLmRldjERMA8GA1UEAxMIc2lnc3RvcmUwdjAQBgcqhkjOPQIBBgUrgQQAIgNiAAT7XeFT4rb3PQGwS4IajtLk3/OlnpgangaBclYpsYBr5i+4ynB07ceb3LP0OIOZdxexX69c5iVuyJRQ+Hz05yi+UF3uBWAlHpiS5sh0+H2GHE7SXrk1EC5m1Tr19L9gg92jYzBhMA4GA1UdDwEB/wQEAwIBBjAPBgNVHRMBAf8EBTADAQH/MB0GA1UdDgQWBBRYwB5fkUWlZql6zJChkyLQKsXF+jAfBgNVHSMEGDAWgBRYwB5fkUWlZql6zJChkyLQKsXF+jAKBggqhkjOPQQDAwNpADBmAjEAj1nHeXZp+13NWBNa+EDsDP8G1WWg1tCMWP/WHPqpaVo0jhsweNFZgSs0eE7wYI4qAjEA2WB9ot98sIkoF3vZYdd3/VtWB5b9TNMea7Ix/stJ5TfcLLeABLE4BNJOsQ4vnBHJ"]}],an=[{baseUrl:"https://rekor.sigstore.dev",logId:"c0d23d6ad406973f9559f3ba2d1ca01f84147d8ffc5b8445c224f98b9591801d",publicKey:"MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAE2G2Y+2tabdTV5BcGiBIx0a9fAFwrkBbmLSGtks4L3qX6yYY0zufBnhC8Ur/iy55GhWP/9A/bY2LhC30M9+RYtw==",validFor:{start:"2021-01-12T11:53:27Z"}}];var rs="1.2.840.10045.2.1",ss={"1.2.840.10045.3.1.7":"P-256","1.3.132.0.34":"P-384"},is={"P-256":32,"P-384":48},os={"1.2.840.10045.4.3.2":"SHA-256","1.2.840.10045.4.3.3":"SHA-384"};function cn(n,e,t){if(e+2>t)throw new Error("it is truncated");let r=n[e];if((r&31)===31)throw new Error("it uses a tag this parser does not read");let s=n[e+1],i=e+2;if(s&128){let o=s&127;if(o===0||o>4||i+o>t)throw new Error("it has a length this parser does not read");s=0;for(let c=0;c<o;c+=1)s=s*256+n[i+c];i+=o}let a=i+s;if(a>t)throw new Error("it is truncated");return{tag:r,bytes:n,offset:e,start:i,end:a}}function X(n){let e=cn(n,0,n.length);if(e.end!==n.length)throw new Error("it has trailing bytes");return e}function D(n){let e=[];for(let t=n.start;t<n.end;){let r=cn(n.bytes,t,n.end);e.push(r),t=r.end}return e}function w(n,e,t){if(!n||n.tag!==e)throw new Error(`${t} is not where it should be`);return n}function q(n){return n.bytes.slice(n.start,n.end)}function Ee(n){return n.bytes.slice(n.offset,n.end)}function Y(n){let e=q(w(n,6,"An object identifier"));if(e.length===0||e[e.length-1]&128)throw new Error("it has a malformed object identifier");let t=[],r=0;for(let a of e)r=r*128+(a&127),a&128||(t.push(r),r=0);let s=t.shift();return[...s<80?[Math.floor(s/40),s%40]:[2,s-80],...t].join(".")}function as(n){let e=new TextDecoder().decode(q(n)),t=n.tag===23?/^(\d{2})(\d{2})(\d{2})(\d{2})(\d{2})(\d{2})Z$/.exec(e):null,r=n.tag===24?/^(\d{4})(\d{2})(\d{2})(\d{2})(\d{2})(\d{2})Z$/.exec(e):null,s=t??r;if(!s)throw new Error(`it holds the time ${JSON.stringify(e)}, which is not one a certificate may hold`);let i=Number(s[1]);t&&(i+=i<50?2e3:1900);let[a,o,c,l,d]=s.slice(2).map(Number);return Date.UTC(i,a-1,o,c,l,d)}function pt(n){try{let[e,t,r,...s]=D(w(X(n),48,"The certificate"));if(s.length>0)throw new Error("it has fields after its signature");let i=D(w(e,48,"The signed part")),a=i[0]?.tag===160?1:0,[o,c,l,d,p,m,...f]=i.slice(a);w(o,2,"The serial number");let g=Y(D(w(t,48,"The signature algorithm"))[0]);if(Y(D(w(c,48,"The signature algorithm"))[0])!==g)throw new Error("it names two different signature algorithms");let C=os[g];if(!C)throw new Error(`it is signed with ${g}, not ECDSA with SHA-256 or SHA-384`);let[A,x]=D(w(d,48,"The validity")).map(as);if(x===void 0)throw new Error("its validity is incomplete");let[T,H]=D(w(m,48,"The public key"));w(H,3,"The public key");let[E,N]=D(w(T,48,"The key algorithm"));if(Y(E)!==rs)throw new Error("its key is not an elliptic-curve key");let B=ss[Y(N)];if(!B)throw new Error("its key is on a curve other than P-256 or P-384");let K=new Map,j=f.find(Me=>Me.tag===163);if(j){let[Me]=D(j);for(let _n of D(w(Me,48,"The extensions"))){let V=D(w(_n,48,"An extension"));if(V.length===3)w(V[1],1,"An extension's critical flag");else if(V.length!==2)throw new Error("it has a malformed extension");let Ne=Y(V[0]);if(K.has(Ne))throw new Error(`it has extension ${Ne} twice`);K.set(Ne,q(w(V[V.length-1],4,"An extension value")))}}let wt=q(w(r,3,"The signature"));if(wt[0]!==0)throw new Error("its signature is not a whole number of bytes");return{der:n,tbs:Ee(e),signatureHash:C,signature:wt.slice(1),issuer:Ee(w(l,48,"The issuer")),subject:Ee(w(p,48,"The subject")),notBefore:A,notAfter:x,spki:Ee(m),curve:B,extensions:K}}catch(e){throw new Error(`The certificate is malformed: ${e.message}`)}}function ln(n){return D(w(X(n),48,"The list")).map(Y)}function cs(n,e){let t=is[e],r=D(w(X(n),48,"The signature"));if(r.length!==2)throw new Error("The signature is not an ECDSA signature");let s=new Uint8Array(t*2);return r.forEach((i,a)=>{let o=q(w(i,2,"The signature"));if(o.length===0||o[0]&128)throw new Error("The signature is not an ECDSA signature");for(;o.length>1&&o[0]===0;)o=o.subarray(1);if(o.length>t)throw new Error("The signature is not an ECDSA signature");s.set(o,a*t+t-o.length)}),s}async function Ie(n,e,t,r,s){let i;try{i=cs(r,e)}catch{return!1}let a=await crypto.subtle.importKey("spki",n,{name:"ECDSA",namedCurve:e},!1,["verify"]);return crypto.subtle.verify({name:"ECDSA",hash:t},a,i,s)}function M(n,e){let t=n.replace(/\s+/g,"");if(!/^[A-Za-z0-9+/]*={0,2}$/.test(t)||t.length%4!==0)throw new Error(`${e} is not base64`);let r=atob(t),s=new Uint8Array(r.length);for(let i=0;i<r.length;i+=1)s[i]=r.charCodeAt(i);return s}function gt(n,e){let t=[...n.matchAll(/-----BEGIN CERTIFICATE-----([^-]*)-----END CERTIFICATE-----/g)];if(t.length!==1||n.replace(t[0][0],"").trim()!=="")throw new Error(`${e} is not exactly one PEM certificate`);return M(t[0][1],e)}var ls="2.5.29.17",us="2.5.29.37",ds="1.3.6.1.5.5.7.3.3",ps="1.3.6.1.4.1.57264.1.8",gs="1.3.6.1.4.1.57264.1.1",ms=134,hs=12,fs=48,ys=new TextEncoder,Z=n=>new TextDecoder().decode(n);async function un(n,e,t,r){let{base64Signature:s,cert:i,rekorBundle:a}=As(e),o=a.Payload;if(r!==void 0&&r.trim()!==s.trim())throw new Error("The detached signature is not the one in the bundle");let c=M(s,"The bundle signature"),l=gt(Z(M(i,"The bundle certificate")),"The bundle certificate"),d=pt(l),p=an.find(x=>x.logId===o.logID.toLowerCase());if(!p)throw new Error(`The bundle names Rekor log ${o.logID}, which the Sigstore trust root does not know`);let m=ys.encode(`{"body":${JSON.stringify(o.body)},"integratedTime":${o.integratedTime},"logID":${JSON.stringify(o.logID)},"logIndex":${o.logIndex}}`);if(!await Ie(M(p.publicKey,"The Rekor key"),"P-256","SHA-256",M(a.SignedEntryTimestamp,"The signed entry timestamp"),m))throw new Error("Rekor's signed entry timestamp does not verify: the log entry, its time or its index was altered");let g=o.integratedTime*1e3;if(!gn(g,p.validFor))throw new Error(`Rekor log ${p.baseUrl} was not in use at ${pe(g)}, when the entry says it was logged`);let R=xs(o.body),C=b(new Uint8Array(await crypto.subtle.digest("SHA-256",n)));if(R.hash!==C)throw new Error("The file's SHA-256 is not the one Rekor logged: the file is not the one signed");if(!mt(M(R.signature,"The logged signature"),c))throw new Error("The bundle's signature is not the one Rekor logged");let A=gt(Z(M(R.publicKey,"The logged certificate")),"The logged certificate");if(!mt(A,l))throw new Error("The bundle's certificate is not the one Rekor logged");if(d.curve!=="P-256"||!await Ie(d.spki,"P-256","SHA-256",c,n))throw new Error("The signature over the file does not verify with the bundle's certificate");return await bs(d,g),Rs(d,t),{integratedTime:o.integratedTime}}async function bs(n,e){if(!pn(n,e))throw new Error(`The signing certificate was valid ${pe(n.notBefore)} to ${pe(n.notAfter)}, not at ${pe(e)} when Rekor logged it`);let t=[];for(let r of on){let s=r.certChain.map(a=>pt(M(a,"A Fulcio certificate")));if(!await dn(n,s[0]))continue;let i=await ws(s,r.validFor,e);if(!i)return;t.push(`${r.uri} (valid from ${r.validFor.start}): ${i}`)}throw new Error(t.length>0?`The signing certificate's CA is not trusted at ${pe(e)}: ${t.join("; ")}`:"The signing certificate was not issued by a Fulcio CA in the Sigstore trust root")}async function ws(n,e,t){if(!gn(t,e))return"the trust root says it was not issuing certificates then";for(let[r,s]of n.entries()){if(!pn(s,t))return`its certificate ${r} was not valid then`;let i=n[r+1]??s;if(!await dn(s,i))return`its certificate ${r} is not signed by the next`}}async function dn(n,e){return mt(n.issuer,e.subject)?Ie(e.spki,e.curve,n.signatureHash,n.signature,n.tbs):!1}function Rs(n,e){let t=n.extensions.get(ls);if(!t)throw new Error("The signing certificate names no identity");let r=D(w(X(t),fs,"The subject alternative name")),s=r.filter(l=>l.tag===ms).map(l=>Z(q(l)));if(s.length!==1||r.length!==1||s[0]!==e.identity)throw new Error(`The release was signed by ${JSON.stringify(s.length===1?s[0]:s)}, not by ${e.identity}`);let i=n.extensions.get(ps),a=n.extensions.get(gs),o=i?Z(q(w(X(i),hs,"The OIDC issuer"))):a?Z(a):void 0;if(o!==e.issuer)throw new Error(`The signing certificate's OIDC issuer is ${o===void 0?"missing":JSON.stringify(o)}, not ${e.issuer}`);let c=n.extensions.get(us);if(!c||!ln(c).includes(ds))throw new Error("The signing certificate is not for code signing")}function As(n){let e=n;if(typeof e=="string")try{e=JSON.parse(e)}catch{throw new Error("The bundle is not JSON")}let t=e,r=t?.rekorBundle?.Payload;if(!(typeof t?.base64Signature=="string"&&typeof t.cert=="string"&&typeof t.rekorBundle?.SignedEntryTimestamp=="string"&&typeof r?.body=="string"&&typeof r.logID=="string"&&/^[0-9a-fA-F]{64}$/.test(r.logID)&&Number.isSafeInteger(r.integratedTime)&&r.integratedTime>=0&&Number.isSafeInteger(r.logIndex)&&r.logIndex>=0))throw new Error("The bundle is not a cosign bundle with a Rekor entry");return t}function xs(n){let e;try{e=JSON.parse(Z(M(n,"The logged entry")))}catch{throw new Error("The logged entry is not JSON")}let t=e?.spec?.data?.hash,r=e?.spec?.signature;if(e?.kind!=="hashedrekord"||t?.algorithm!=="sha256"||typeof t.value!="string"||typeof r?.content!="string"||typeof r.publicKey?.content!="string")throw new Error("The logged entry is not a SHA-256 hashedrekord");return{hash:t.value.toLowerCase(),signature:r.content,publicKey:r.publicKey.content}}function pn(n,e){return n.notBefore<=e&&e<=n.notAfter}function gn(n,e){return Date.parse(e.start)<=n&&(e.end===void 0||n<=Date.parse(e.end))}function pe(n){return new Date(n).toISOString()}function mt(n,e){return n.length===e.length&&n.every((t,r)=>t===e[r])}var ht=Object.freeze({identity:"https://github.com/calimero-network/mero-tee/.github/workflows/release-node-image-gcp.yaml@refs/heads/master",issuer:"https://token.actions.githubusercontent.com"}),Cs=["mrtd","rtmr0","rtmr1","rtmr2","rtmr3"],hn="https://cloud.calimero.network";async function fn(n,e,t={}){let r=typeof n=="string"?new TextEncoder().encode(n):n;await un(r,e,{identity:t.identity??ht.identity,issuer:t.issuer??ht.issuer},t.signature);let s;try{s=JSON.parse(new TextDecoder().decode(r))}catch{throw new Error("The signed release is not JSON")}if(s?.role!=="node")throw new Error(`The signed release is a ${JSON.stringify(s?.role)} release, not a node one`);let i=Object.values(s.profiles??{}),a=o=>Cs.every(c=>/^[0-9a-f]{96}$/i.test(String(o?.[c])));if(typeof s.tag!="string"||i.length===0||!i.every(a))throw new Error("The signed file is not a release's published-mrtds.json: it lists no measured profiles");if(t.expectedVersion!==void 0&&s.tag!==t.expectedVersion)throw new Error(`The signed release is ${s.tag}, not ${t.expectedVersion}`);return s}async function yn(n,e={}){let t=await(e.fetch??fetch)(n,{headers:{accept:"application/json"}});if(!t.ok)throw new Error(`Fetching the node release from ${n} failed: HTTP ${t.status}`);let r;try{r=(await t.json())?.data}catch{throw new Error(`${n} did not answer with JSON`)}if(typeof r?.version!="string"||typeof r.publishedMrtds!="string"||typeof r.bundle!="string")throw new Error(`${n} did not answer with a signed node release`);return{version:r.version,publishedMrtds:r.publishedMrtds,bundle:r.bundle}}function bn(n){let e=/^merotee-.+-(\d+)-(\d+)-(\d+)$/.exec(n);if(!e)throw new Error(`${JSON.stringify(n)} is not the image of a mero-tee node release`);return`${Number(e[1])}.${Number(e[2])}.${Number(e[3])}`}async function wn(n,e={}){let t=`${n.replace(/\/+$/,"")}/admin-api/tee/info`,r=await(e.fetch??fetch)(t,{headers:{accept:"application/json"}});if(!r.ok)throw new Error(`Fetching ${t} failed: HTTP ${r.status}`);let s;try{s=(await r.json())?.data?.osImage}catch{throw new Error(`${t} did not answer with JSON`)}if(typeof s!="string")throw new Error(`${t} did not name the node's image`);return bn(s)}function Rn(n,e){return`${n.replace(/\/+$/,"")}/api/tee/node-releases/${Pe(e,"version")}`}async function An(n){let e=Pe(n.minReleaseVersion,"minReleaseVersion"),t=Pe(n.release?.version,"The release version"),r=await fn(n.release.publishedMrtds,n.release.bundle,{expectedVersion:t,identity:n.identity,issuer:n.issuer});if(ks(t,e)<0)throw new Error(`Release ${t} is older than the minimum trusted, ${e}`);return dt([r],{profile:n.profile})}function xn(n){return Pe(n.minReleaseVersion,"minReleaseVersion"),Object.assign(async t=>{let r=await An({release:await n.release(),profile:n.profile,minReleaseVersion:n.minReleaseVersion,identity:n.identity,issuer:n.issuer});return ut({dcapVerify:n.dcapVerify,fetchCollateral:n.fetchCollateral,now:n.now,...r})(t)},{includeCollateral:!0})}function vs(n){let{baseUrl:e,releaseSource:t,applicationId:r,applicationHash:s,fetch:i,...a}=n,o=i??((l,d)=>fetch(l,d));return de({baseUrl:e,verify:xn({...a,release:async()=>{let l=await wn(e,{fetch:o});return typeof t=="function"?t({baseUrl:e,version:l,fetch:o}):yn(Rn(t??hn,l),{fetch:o})}}),applicationId:r,applicationHash:s,fetch:o})}var Cn=/^(\d+)\.(\d+)\.(\d+)(?:-([0-9A-Za-z.-]+))?(?:\+[0-9A-Za-z.-]+)?$/;function Pe(n,e){let t=typeof n=="string"?Cn.exec(n):null,r=t?.[4];if(!t||r!==void 0&&r.split(".").some(s=>s===""))throw new Error(`${e} is ${JSON.stringify(n)}, not a release version like 2.3.87`);return n}function ks(n,e){let[t,r]=mn(n),[s,i]=mn(e);for(let c=0;c<3;c+=1)if(t[c]!==s[c])return t[c]<s[c]?-1:1;if(r===void 0||i===void 0)return r===i?0:r===void 0?1:-1;let a=r.split("."),o=i.split(".");for(let c=0;c<Math.max(a.length,o.length);c+=1){let[l,d]=[a[c],o[c]];if(l===void 0||d===void 0)return l===void 0?-1:1;let[p,m]=[/^\d+$/.test(l),/^\d+$/.test(d)],f=p&&m?Math.sign(Number(l)-Number(d)):p!==m?p?-1:1:l<d?-1:l>d?1:0;if(f!==0)return f}return 0}function mn(n){let e=Cn.exec(n);return[[Number(e[1]),Number(e[2]),Number(e[3])],e[4]]}var yt=12,Ss=new TextEncoder().encode("calimero.namespace.v1"),Ts=0,vn={GroupCreated:0,GroupReparented:1,GroupDeleted:2,AdminChanged:3,PolicyUpdated:4,MemberJoined:5,KeyDelivery:6,MemberJoinedOpen:7,MemberJoinedAt:8,NamespaceCreatedV2:9,MemberJoinedViaTeeAttestation:10};function ft(n,e){if(n.length!==32)throw new Error(`${e} must be 32 bytes, got ${n.length}`);return new Uint8Array(n)}function kn(n,e){if(n==null)return new Uint8Array([0]);if(n.length!==32)throw new Error(`${e} must be 32 bytes, got ${n.length}`);return h(new Uint8Array([1]),new Uint8Array(n))}function Sn(n){let e=new TextEncoder().encode(n);return h(k(e.length),e)}function De(n){let e=n.invitation,t=e.admitters??[],r=n.admitter_addrs??[];return h(ft(e.inviter_identity,"inviter_identity"),ft(e.group_id,"group_id"),S(e.expiration_timestamp),ft(e.secret_salt,"secret_salt"),new Uint8Array([e.invited_role]),k(t.length),...t.map((s,i)=>y(s,`admitters[${i}]`,32)),Sn(n.inviter_signature),n.inviter_account?h(new Uint8Array([1]),y(n.inviter_account,"inviter_account",32)):new Uint8Array([0]),k(r.length),...r.map(Sn),kn(n.application_id,"application_id"),kn(n.app_key,"app_key"))}async function Tn(n){let e=n.parentOpHashes??[],t=y(n.credential,"credential",n.credential.length/2),r=BigInt(n.invitation.invitation.expiration_timestamp??0)!==0n,s=n.joinedAt??BigInt(Math.floor(Date.now()/1e3)),i=r?h(new Uint8Array([vn.MemberJoinedAt]),y(n.member,"member",32),De(n.invitation),S(s),t):h(new Uint8Array([vn.MemberJoined]),y(n.member,"member",32),De(n.invitation),t),a=h(new Uint8Array([Ts]),i),o=await I(n.deviceSecret),c=await P(n.deviceSecret),l=h(new Uint8Array([yt]),y(n.namespaceId,"namespaceId",32),k(e.length),...e.map((m,f)=>y(m,`parentOpHashes[${f}]`,32)),c,S(n.nonce),a),d=new Uint8Array(await crypto.subtle.sign("Ed25519",o,h(Ss,l))),p=new Uint8Array([0]);return b(h(l,d,p))}var Es=new TextEncoder().encode("calimero.auth.login.v1"),En={webOrigin:0,codeSigningId:1,cli:2};function In(n){let e=new TextEncoder;switch(n.kind){case"webOrigin":return e.encode(n.origin);case"codeSigningId":return e.encode(n.id);case"cli":return new Uint8Array(0)}}function Is(n){return h(Uint8Array.from([En[n.kind]]),In(n))}function Ps(n){let e=Uint8Array.from([En[n.kind]]);if(n.kind==="cli")return e;let t=In(n);return h(e,k(t.length),t)}async function Ue(n){let e=y(n.node,"node",32),t=y(n.challenge,"challenge",32),r=y(n.sessionKey,"sessionKey",32),s=await I(n.deviceSecret),i=await P(n.deviceSecret),a=S(n.issuedAt),o=S(n.expiresAt),c=await U(Es,[e,Is(n.audience),t,r,i,a,o]),l=new Uint8Array(await crypto.subtle.sign({name:"Ed25519"},s,c));return b(h(e,Ps(n.audience),t,r,i,a,o,l))}var Ds=300;async function bt(){let n=b(crypto.getRandomValues(new Uint8Array(32)));return{publicKey:b(await P(n)),secret:n}}function Us(n){switch(n.kind){case"webOrigin":return n.origin;case"codeSigningId":return n.id;case"cli":return"cli"}}async function Pn(n){let e=n.nodeUrl.replace(/\/+$/,""),t=n.timeoutMs??1e4,r=async(m,f)=>{let g=new AbortController,R=setTimeout(()=>g.abort(),t),C;try{let x={...f,signal:g.signal};C=n.fetch?await n.fetch(`${e}${m}`,x):await globalThis.fetch(`${e}${m}`,x)}catch(x){throw new v(0,x instanceof Error?x.name:"NetworkError",`${e}${m}`,new Headers,x instanceof Error?x.message:String(x))}finally{clearTimeout(R)}let A=await C.text().catch(()=>"");if(!C.ok)throw new v(C.status,C.statusText,`${e}${m}`,C.headers,A);return A?JSON.parse(A):{}},s=await r("/auth/challenge",{method:"GET",headers:{Accept:"application/json"}}),i=s.challenge??s.data?.challenge;if(!i)throw new Error("the node issued no challenge");let a=await bt(),o=Math.floor(Date.now()/1e3),c=await Ue({node:n.node,audience:n.audience,challenge:i,sessionKey:a.publicKey,issuedAt:o,expiresAt:o+(n.ttlSeconds??Ds),deviceSecret:n.deviceSecret}),l=await r("/auth/token",{method:"POST",headers:{"Content-Type":"application/json",Accept:"application/json"},body:JSON.stringify({auth_method:"account_proof",public_key:a.publicKey,client_name:n.clientName??Us(n.audience),timestamp:o,provider_data:{challenge:i,login_statement:c,account_proof:n.accountProof}})}),d=l.access_token??l.data?.access_token,p=l.refresh_token??l.data?.refresh_token;if(!d)throw new Error("the node minted no session");return{accessToken:d,refreshToken:p??"",sessionKey:a.publicKey,sessionSecret:a.secret}}var Dn=[2428,2429,2528,2529],Ms="localhost",Ns=2e3;function Un(n){return`http://${Ms}:${n}`}function Mn(n,e){let t=n.endsWith("/")?n:`${n}/`;return new URL(e,t).toString()}async function Nn(n,e={}){let{timeoutMs:t=Ns,signal:r}=e;if(r?.aborted)return!1;let s=new AbortController,i=()=>s.abort(),a=r;a&&a.addEventListener("abort",i,{once:!0});let o=setTimeout(()=>s.abort(),t);try{let c=Mn(n,"admin-api/health"),l=await fetch(c,{method:"GET",signal:s.signal});if(!l.ok)return!1;try{let d=await l.json(),p=d?.data?.status!==void 0?d?.data?.status:d?.status;return p===void 0?!0:typeof p=="string"&&p.toLowerCase()==="alive"}catch{return!0}}catch{return!1}finally{clearTimeout(o),a&&a.removeEventListener("abort",i)}}async function _s(n={}){let{ports:e=Dn,timeoutMs:t,signal:r}=n;return(await Promise.all(e.map(async i=>{let a=Un(i);return await Nn(a,{timeoutMs:t,signal:r})?a:null}))).filter(i=>i!==null)}export{O as AdminApiClient,G as AuthApiClient,_ as AuthRevokedError,ne as CAPABILITIES,$ as CloudClient,Dn as DEFAULT_LOCAL_NODE_PORTS,hn as DEFAULT_RELEASE_MIRROR,se as EphemeralClient,Yt as HANDSHAKE_PATH,v as HTTPError,Te as IntentRefusedError,nt as LocalStorageTokenStore,tt as MemoryTokenStore,ke as MeroJs,ht as NODE_RELEASE_SIGNER,ae as RelayClient,re as RpcClient,we as RpcError,le as SEALED_CONTENT_TYPE,Xt as SEALED_PATH,yt as SIGNED_NAMESPACE_OP_SCHEMA_VERSION,z as SealedTransportError,W as SseClient,ue as StaleTransportKeyError,ee as WebHttpClient,J as WsClient,xe as accountForRoot,qt as accountRootFromPhrase,L as accountRootFromSecret,mr as admitterAddrsFromNetworkStatus,je as buildAuthLoginUrl,ye as callerProof,Be as classifyError,Rn as cloudNodeReleaseUrl,_e as combineSignals,Jn as compareSemver,Wr as connectCloud,wi as createAdminApiClient,kt as createAdminApiClientFromHttpClient,de as createAttestedSealedFetch,Zs as createAuthApiClient,At as createAuthApiClientFromHttpClient,yi as createBrowserAdminApiClient,Ys as createBrowserAuthApiClient,F as createBrowserHttpClient,He as createHttpClient,it as createLocalStorageNonceSource,st as createMemoryNonceSource,br as createMeroJs,bi as createNodeAdminApiClient,Xs as createNodeAuthApiClient,ge as createNodeHttpClient,be as createProofSigner,ut as createQuoteVerifier,$n as createRetryableMethod,nn as createSealedFetch,vs as createSignedReleaseSealedFetch,xn as createSignedReleaseVerifier,qe as createTimeoutSignal,Ge as createUniversalHttpClient,Je as deviceCertPayload,_s as discoverLocalNodes,De as encodeSignedInvitation,tn as fetchAttestedTransportKey,yn as fetchNodeRelease,wn as fetchNodeReleaseVersion,_t as generateAccountRoot,bt as generateSessionKey,Oe as hasCap,pi as hexEncodeUtf8,rt as intentHash,Vn as isSettled,Ae as jsonCodec,Un as localNodeUrl,Pn as login,Dt as mintDeviceId,Mn as nodeEndpoint,bn as nodeReleaseVersionFromImage,Ke as parseAuthCallback,Nn as probeNodeHealth,xt as redeemInvitation,Ve as requestBodyHash,ve as routingProofHeaders,ii as shouldRetain,oe as signAccountLink,Ce as signAccountLogin,Mt as signDeviceCert,Ue as signLoginStatement,Tn as signMemberJoinOp,fe as signRequest,$t as signRoutingChallenge,Se as signWarrant,en as transportKeyBinding,An as trustSignedRelease,dt as trustedMeasurementsFromReleases,fn as verifySignedNodeRelease,Le as withCap,me as withRetry,ti as withoutCap};
|
|
1
|
+
function Oe(n){let e=n.filter(Boolean);if(e.length===0)return;let t=AbortSignal;if(typeof t.any=="function")try{return t.any(e)}catch{}let r=new AbortController,s=i=>{r.abort(i.target.reason);for(let a of e)a.removeEventListener("abort",s)};for(let i of e){if(i.aborted)return AbortSignal.abort(i.reason);i.addEventListener("abort",s,{once:!0})}return r.signal}function $e(n){if(typeof AbortSignal.timeout=="function")return AbortSignal.timeout(n);let e=new AbortController;return setTimeout(()=>{e.abort(new DOMException("Timeout","TimeoutError"))},n),e.signal}var Vn=new Set(["token_reuse","token_revoked"]);function Fn(n){if(n)try{let e=JSON.parse(n),t=e?.error;if(typeof t=="string"&&t.trim()!=="")return t;let r=t?.message;if(typeof r=="string"&&r.trim()!=="")return r;let s=e?.detail;if(typeof s=="string"&&s.trim()!=="")return s}catch{}}var S=class extends Error{constructor(t,r,s,i,a){let o=Fn(a)??(t===0&&a?.trim()?a.trim():void 0);super(o?`HTTP ${t} ${r}: ${o}`:`HTTP ${t} ${r}`);this.status=t;this.statusText=r;this.url=s;this.headers=i;this.bodyText=a;this.name="HTTPError";this.explanation=o}toJSON(){return{status:this.status,statusText:this.statusText,url:this.url,headers:vt(this.headers),bodyText:this.bodyText}}},_=class extends S{constructor(t,r,s,i,a,o){super(r,s,i,a,o);this.reason=t;this.name="AuthRevokedError";this.message=`Authentication revoked (${t}): HTTP ${r} ${s}`}};function vt(n){let e={};return n.forEach((t,r)=>{e[r]=t}),e}function Wn(n){try{return new URL(n).pathname}catch{let e=n.indexOf("?");return e===-1?n:n.slice(0,e)}}var te=class{constructor(e){this.transport=e;this.refreshTokenPromise=null;this.onTokenRefreshPromise=null}async get(e,t){return this.request(e,{...t,method:"GET"})}async post(e,t,r){return this.request(e,{...r,method:"POST",body:t?JSON.stringify(t):void 0,headers:{"Content-Type":"application/json",...r?.headers}})}async put(e,t,r){return this.request(e,{...r,method:"PUT",body:t?JSON.stringify(t):void 0,headers:{"Content-Type":"application/json",...r?.headers}})}async delete(e,t){return this.request(e,{...t,method:"DELETE"})}async patch(e,t,r){return this.request(e,{...r,method:"PATCH",body:t?JSON.stringify(t):void 0,headers:{"Content-Type":"application/json",...r?.headers}})}async head(e,t){let r=await this.makeRequest(e,{...t,method:"HEAD",parse:"response"});return{headers:vt(r.headers),status:r.status}}async request(e,t){return this.makeRequest(e,t)}async makeRequest(e,t,r=0,s){let a=this.buildUrl(e),o=s??Date.now(),c=this.createAbortSignal(t),u=await this.buildHeaders(t?.headers),d;u instanceof Headers?(d={},u.forEach((m,A)=>{d[A]=m})):d=u;let l={method:t?.method||"GET",headers:d},g=t?.body instanceof ReadableStream||typeof t?.body=="object"&&t?.body!==null&&"getReader"in t.body&&!(t.body instanceof Blob);(t?.body!==void 0&&!g||t?.body!==void 0&&g&&r===0)&&(l.body=t.body);let f;if(r>0&&s!==void 0){let m=t?.timeoutMs||this.transport.timeoutMs;if(m){let A=Date.now()-o,k=Math.max(0,m-A);f=this.createAbortSignal({...t,timeoutMs:k})}else f=this.createAbortSignal(t)}else f=c;if(f&&(l.signal=f),this.transport.credentials!==void 0&&(l.credentials=this.transport.credentials),t?.mode!==void 0&&(l.mode=t.mode),t?.cache!==void 0&&(l.cache=t.cache),t?.redirect!==void 0&&(l.redirect=t.redirect),t?.referrer!==void 0&&(l.referrer=t.referrer),t?.referrerPolicy!==void 0&&(l.referrerPolicy=t.referrerPolicy),t?.integrity!==void 0&&(l.integrity=t.integrity),t?.keepalive!==void 0&&(l.keepalive=t.keepalive),this.transport.getProof){let m=await this.transport.getProof({method:l.method??"GET",path:Wn(a),body:l.body});m&&(d["X-Calimero-Proof"]=m)}try{let m=await this.transport.fetch(a,l);if(!m.ok){let A=await this.getBodyText(m),k=new S(m.status,m.statusText,a,m.headers,A),C=m.headers.get("x-auth-error");if((m.status===401||m.status===403)&&C&&Vn.has(C)){if(this.refreshTokenPromise=null,this.onTokenRefreshPromise=null,this.transport.onAuthRevoked)try{await this.transport.onAuthRevoked()}catch{}throw new _(C,m.status,m.statusText,a,m.headers,A)}let v=t?.signal?.aborted===!0;if(m.status===401&&this.transport.refreshToken&&C==="token_expired"&&r<1&&!g&&!v)try{let P=this.refreshTokenPromise;P||(P=this.transport.refreshToken(),this.refreshTokenPromise=P);let q=await P;if(!q||q.trim()==="")throw this.refreshTokenPromise=null,this.onTokenRefreshPromise=null,new Error("Refresh token returned empty token");if(!this.transport.onTokenRefresh)throw this.refreshTokenPromise=null,this.onTokenRefreshPromise=null,new Error("onTokenRefresh callback is required when refreshToken is provided. The callback must update the token storage so getAuthToken() returns the new token.");let D=this.onTokenRefreshPromise;D||(D=this.transport.onTokenRefresh(q),this.onTokenRefreshPromise=D),await D,this.refreshTokenPromise=null,this.onTokenRefreshPromise=null;let N=t?.timeoutMs||this.transport.timeoutMs;if(N&&s!==void 0){let B=Date.now()-o;if(N-B<=0)throw k}return this.makeRequest(e,t,r+1,o)}catch(P){throw this.refreshTokenPromise=null,this.onTokenRefreshPromise=null,P instanceof Error&&P.message.includes("onTokenRefresh")?P:k}throw k}return this.parseResponse(m,t?.parse)}catch(m){throw m instanceof S||m instanceof Error&&m.message.includes("onTokenRefresh")?m:new S(0,"Network Error",a,new Headers,m instanceof Error?m.message:"Unknown error")}}buildUrl(e){if(e.startsWith("http://")||e.startsWith("https://"))return e;let t=this.transport.baseUrl;return e.startsWith("/")?`${t.endsWith("/")?t.slice(0,-1):t}${e}`:`${t.endsWith("/")?t:`${t}/`}${e}`}createAbortSignal(e){let t=[];this.transport.defaultAbortSignal&&t.push(this.transport.defaultAbortSignal),e?.signal&&t.push(e.signal);let r=e?.timeoutMs||this.transport.timeoutMs;return r&&t.push($e(r)),t.length>0?Oe(t):void 0}async buildHeaders(e){let t={...this.transport.defaultHeaders};if(this.transport.getAuthToken)try{let r=await this.transport.getAuthToken();r&&r.trim()!==""&&(t.Authorization=`Bearer ${r}`)}catch{}return e&&(e instanceof Headers?e.forEach((r,s)=>{t[s]=r}):Array.isArray(e)?e.forEach(([r,s])=>{t[r]=s}):Object.assign(t,e)),t}async parseResponse(e,t){switch(t){case"text":return await e.text();case"blob":return await e.blob();case"arrayBuffer":return await e.arrayBuffer();case"response":return e;default:{let r=await e.text();if(!r)return null;try{return JSON.parse(r)}catch{throw new Error(`Expected a JSON response (status ${e.status}) but body was not JSON: ${r.slice(0,200)}`)}}}}async getBodyText(e){try{let t=await e.text();return t.length>65536?t.slice(0,65536)+"...":t}catch{return""}}};function Ge(n){return new te(n)}function F(n){let e={fetch:(t,r)=>n.fetch?n.fetch(t,r):globalThis.fetch(t,r),baseUrl:n.baseUrl,getAuthToken:n.getAuthToken,getProof:n.getProof,onTokenRefresh:n.onTokenRefresh,refreshToken:n.refreshToken,onAuthRevoked:n.onAuthRevoked,defaultHeaders:n.defaultHeaders,timeoutMs:n.timeoutMs,credentials:n.credentials,defaultAbortSignal:n.defaultAbortSignal};return Ge(e)}function fe(n){let e=n.fetch??globalThis.fetch;if(!e)throw new Error("No fetch implementation available. Please provide a fetch implementation (e.g., undici.fetch) or use Node.js 18+ which has native fetch support.");let r={fetch:e===globalThis.fetch?(s,i)=>globalThis.fetch(s,i):(s,i)=>e(s,i),baseUrl:n.baseUrl,getAuthToken:n.getAuthToken,getProof:n.getProof,onTokenRefresh:n.onTokenRefresh,refreshToken:n.refreshToken,onAuthRevoked:n.onAuthRevoked,defaultHeaders:n.defaultHeaders,timeoutMs:n.timeoutMs,credentials:n.credentials,defaultAbortSignal:n.defaultAbortSignal};return Ge(r)}function Le(n){return typeof window<"u"?F(n):fe(n)}function Jn(n,e){if(e<=0)return!1;let r=n?.name;if(r==="TimeoutError")return!0;if(r==="AbortError")return!1;let s=n;if("status"in s&&typeof s.status=="number"){let i=s.status;return i>=500||i===429}return r==="TypeError"}function zn(n){let t=250*Math.pow(2,n-1),r=(Math.random()-.5)*.4*t;return Math.max(0,t+r)}async function ye(n,e={}){let{attempts:t=3}=e,r;for(let s=1;s<=t;s++)try{return await n(s)}catch(i){if(r=i,!Jn(r,t-s))throw r;let a=zn(s),u=r.headers?.get?.("Retry-After");if(u){let d=parseInt(u,10);if(!isNaN(d))a=Math.max(a,d*1e3);else{let l=new Date(u);if(!isNaN(l.getTime())){let g=Math.max(0,l.getTime()-Date.now());a=Math.max(a,Math.min(g,6e4))}}}await new Promise(d=>setTimeout(d,a))}throw r||new Error("Retry failed without error")}function Qn(n,e={}){return async(...t)=>ye(()=>n(...t),e)}var O=class{constructor(e){this.httpClient=e}async getHealth(){let e=await this.httpClient.get("/auth/health");if(!e.data)throw new Error("Health response data is null");return e.data}async getIdentity(){let e=await this.httpClient.get("/admin/identity");if(!e.data)throw new Error("Identity response data is null");return e.data}async getProviders(){let e=await this.httpClient.get("/auth/providers");if(!e.data)throw new Error("Providers response data is null");return e.data}async getLoginPage(){return this.httpClient.get("/auth/login",{parse:"text"})}async generateTokens(e){return this.httpClient.post("/auth/token",e)}async refreshToken(e){return this.httpClient.post("/auth/refresh",e)}async generateMockTokens(e){return this.httpClient.post("/auth/mock-token",e)}async validateToken(e){try{let t=await this.validateTokenGet(e);return{valid:t.status===200,headers:t.headers,status:t.status}}catch{return{valid:!1,headers:{},status:401}}}async validateTokenGet(e){let t=await this.httpClient.head("/auth/validate",{headers:{Authorization:`Bearer ${e}`}});return{status:t.status,headers:t.headers}}async revokeTokens(e){let t=await this.httpClient.post("/admin/revoke",e);if(!t.data)throw new Error("Revoke tokens response data is null");return t.data}async listRootKeys(){let e=await this.httpClient.get("/admin/keys");if(!e.data)throw new Error("Root keys response data is null");return e.data}async createRootKey(e){let t=await this.httpClient.post("/admin/keys",e);if(!t.data)throw new Error("Create root key response data is null");return t.data}async deleteRootKey(e){return this.httpClient.delete(`/admin/keys/${e}`)}async listClientKeys(){let e=await this.httpClient.get("/admin/keys/clients");if(!e.data)throw new Error("Client keys response data is null");return e.data}async generateClientKey(e){return this.httpClient.post("/admin/client-key",e)}async deleteClientKey(e,t){return this.httpClient.delete(`/admin/keys/${e}/clients/${t}`)}async getKeyPermissions(e){return this.httpClient.get(`/admin/keys/${e}/permissions`)}async updateKeyPermissions(e,t){return this.httpClient.put(`/admin/keys/${e}/permissions`,{add:t.add,remove:t.remove})}};var ne=class{async get(){throw new Error("HTTP client not implemented - use createAuthApiClientFromHttpClient with a real HTTP client")}async post(){throw new Error("HTTP client not implemented - use createAuthApiClientFromHttpClient with a real HTTP client")}async put(){throw new Error("HTTP client not implemented - use createAuthApiClientFromHttpClient with a real HTTP client")}async delete(){throw new Error("HTTP client not implemented - use createAuthApiClientFromHttpClient with a real HTTP client")}async patch(){throw new Error("HTTP client not implemented - use createAuthApiClientFromHttpClient with a real HTTP client")}async head(){throw new Error("HTTP client not implemented - use createAuthApiClientFromHttpClient with a real HTTP client")}async request(){throw new Error("HTTP client not implemented - use createAuthApiClientFromHttpClient with a real HTTP client")}};function ci(n){let e=new ne;return new O(e)}function li(n){let e=new ne;return new O(e)}function ui(n){let e=new ne;return new O(e)}function kt(n,e){return new O(n)}var re={CAN_CREATE_CONTEXT:1,CAN_INVITE_MEMBERS:2,CAN_JOIN_OPEN_SUBGROUPS:4,MANAGE_MEMBERS:8,MANAGE_APPLICATION:16,CAN_CREATE_SUBGROUP:32,CAN_DELETE_SUBGROUP:64,CAN_MANAGE_VISIBILITY:128,CAN_MANAGE_METADATA:256,CAN_AUTHOR_ON_BEHALF:512};function Be(n,e){let t=e>>>0;return(n&t)>>>0===t}function Ke(n,e){return(n|e)>>>0}function pi(n,e){return(n&~e)>>>0}var Yn={invalid:!1,unauthorized:!0,forbidden:!1,"not-found":!0,conflict:!1,gone:!1,"too-large":!1,"rate-limited":!0,unavailable:!0,server:!0,unreachable:!0,unknown:!0};function Xn(n){switch(n){case 0:return"unreachable";case 400:return"invalid";case 401:return"unauthorized";case 403:return"forbidden";case 404:return"not-found";case 409:return"conflict";case 410:return"gone";case 413:return"too-large";case 429:return"rate-limited";case 503:case 504:return"unavailable"}return n>=500&&n<600?"server":n>=400&&n<500?"invalid":"unknown"}function Zn(n){if(!n||typeof n!="object")return;let e=n;for(let t of[e.status,e.response?.status,e.statusCode])if(typeof t=="number"&&Number.isFinite(t))return t}function je(n,e="Request failed"){if(typeof n=="string")return n.trim()||e;if(!n||typeof n!="object")return e;let t=n;for(let r of[t.explanation,t.response?.data?.error,t.response?.data?.message,t.data?.message,t.error,t.message])if(typeof r=="string"&&r.trim())return r;return e}function Ve(n){let e=Zn(n),t;return e!==void 0?t=Xn(e):n instanceof TypeError?t="unreachable":t="unknown",{kind:t,status:e,retryable:Yn[t],message:je(n)}}function er(n){return n.status!=="failed"||!n.retryable}function fi(n){return!er(n)}function tr(n){let e=n.toLowerCase();return e.includes("invitation has expired")||e.includes("invitation expired")||/\binvitation for group .+? expired at \d+/.test(e)?"expired":e.includes("malformed")||e.includes("does not match namespace_id")||e.includes("invalid invitation")||/\binvitation for group .+? is invalid:/.test(e)?"invalid":null}function nr(n,e){let{kind:t,status:r}=Ve(n),s=tr(e);return r===401?{reason:"signed-out",retryable:!0}:r===400?{reason:s??"invalid",retryable:!1}:r===410?{reason:"expired",retryable:!1}:r===403?{reason:"refused",retryable:!1}:r===409?{reason:s??"refused",retryable:!1}:t==="unavailable"?{reason:"no-one-online",retryable:!0}:t==="unreachable"?{reason:"node-unreachable",retryable:!0}:s?{reason:s,retryable:!1}:{reason:"unknown",retryable:!0}}async function St(n,e){let{namespaceId:t,invitation:r,teamName:s}=n,i=null;try{await e.join(t,r)}catch(d){i=d}let a=null;try{a=(await e.memberships()).includes(t)}catch{a=null}if(i===null)return{status:"joined",namespaceId:t,teamName:s};if(a===!0)return{status:"already-member",namespaceId:t,teamName:s};let o=je(i,"Could not join. Check the invitation."),{reason:c,retryable:u}=nr(i,o);return{status:"failed",namespaceId:t,message:o,reason:c,retryable:u}}function p(n){return n?.data}function rr(n,e){let[t,r]=Tt(n),[s,i]=Tt(e),a=t.split("."),o=s.split("."),c=Math.max(a.length,o.length);for(let g=0;g<c;g++){let f=Et(a[g]??"0",o[g]??"0");if(f!==0)return f}if(r===void 0&&i===void 0)return 0;if(r===void 0)return 1;if(i===void 0)return-1;let u=r.split("."),d=i.split("."),l=Math.max(u.length,d.length);for(let g=0;g<l;g++){if(g>=u.length)return-1;if(g>=d.length)return 1;let f=Et(u[g],d[g]);if(f!==0)return f}return 0}function Tt(n){let e=n.split("+",1)[0],t=e.indexOf("-");return t===-1?[e,void 0]:[e.slice(0,t),e.slice(t+1)]}function Et(n,e){let t=/^\d+$/.test(n),r=/^\d+$/.test(e);if(t&&r){let s=Number(n)-Number(e);return s<0?-1:s>0?1:0}return t?-1:r?1:n<e?-1:n>e?1:0}async function sr(n,e,t){return ye(async()=>{let r=await fetch(n,{signal:AbortSignal.timeout(1e4)});if(!r.ok){let s=new Error(`registry ${e} fetch failed (${r.status}) for ${t}`);throw s.status=r.status,s}return r},{attempts:3})}var $=class{constructor(e){this.httpClient=e}async healthCheck(){return p(await this.httpClient.get("/admin-api/health"))}async readinessCheck(){return p(await this.httpClient.get("/admin-api/ready"))}async isAuthed(){return this.httpClient.get("/admin-api/is-authed")}async installApplication(e){return p(await this.httpClient.post("/admin-api/install-application",e))}async getRegistryVersions(e,t){let r=new URL("/api/v2/bundles",new URL(e).origin);r.searchParams.set("package",t);let i=await(await sr(r.toString(),"versions",t)).json();return(Array.isArray(i)?i:[]).map(a=>a.appVersion).filter(a=>typeof a=="string").sort((a,o)=>rr(o,a))}async installDevApplication(e){return p(await this.httpClient.post("/admin-api/install-dev-application",e))}async uninstallApplication(e){return p(await this.httpClient.delete(`/admin-api/applications/${e}`))}async listApplications(){return p(await this.httpClient.get("/admin-api/applications"))}async getApplication(e){return p(await this.httpClient.get(`/admin-api/applications/${e}`))}async listApplicationVersions(e){return p(await this.httpClient.get(`/admin-api/applications/${e}/versions`))}async getApplicationAbi(e,t){let r=t?`?service_name=${encodeURIComponent(t)}`:"";return p(await this.httpClient.get(`/admin-api/applications/${e}/abi${r}`))}async listPackages(){let e=await this.httpClient.get("/admin-api/packages");return e?.data??e}async listPackageVersions(e){let t=await this.httpClient.get(`/admin-api/packages/${encodeURIComponent(e)}/versions`);return t?.data??t}async getLatestPackageVersion(e){return this.httpClient.get(`/admin-api/packages/${encodeURIComponent(e)}/latest`)}async createContext(e){let t={...e,initializationParams:e.initializationParams??[]};return p(await this.httpClient.post("/admin-api/contexts",t))}async deleteContext(e){return p(await this.httpClient.delete(`/admin-api/contexts/${e}`))}async getContexts(){return p(await this.httpClient.get("/admin-api/contexts"))}async getContext(e){return p(await this.httpClient.get(`/admin-api/contexts/${e}`))}async getContextsForApplication(e){return p(await this.httpClient.get(`/admin-api/contexts/for-application/${e}`))}async generateContextIdentity(){return p(await this.httpClient.post("/admin-api/identity/context",{}))}async getContextIdentities(e){return p(await this.httpClient.get(`/admin-api/contexts/${e}/identities`))}async getContextIdentitiesOwned(e){return p(await this.httpClient.get(`/admin-api/contexts/${e}/identities-owned`))}async joinContext(e){return p(await this.httpClient.post(`/admin-api/contexts/${e}/join`,{}))}async getContextGroup(e){return p(await this.httpClient.get(`/admin-api/contexts/${e}/group`))}async getContextStorage(e){return p(await this.httpClient.get(`/admin-api/contexts/${e}/storage`))}async syncContext(e){let t=e?`/admin-api/contexts/sync/${e}`:"/admin-api/contexts/sync";await this.httpClient.post(t,{})}async resyncContext(e,t={}){return await this.httpClient.post(`/admin-api/contexts/${e}/resync`,t)??{contextId:e,resyncStarted:!0}}async inviteSpecializedNode(e){return p(await this.httpClient.post("/admin-api/contexts/invite-specialized-node",e))}async updateContextApplication(e,t){await this.httpClient.post(`/admin-api/contexts/${e}/application`,t)}async getContextsWithExecutorsForApplication(e){let t=await this.httpClient.get(`/admin-api/contexts/with-executors/for-application/${e}`);return Array.isArray(t)?t:t?.contexts??t?.data??[]}async uploadBlob(e){let t=new URLSearchParams;e.hash&&t.set("hash",e.hash),e.contextId&&t.set("context_id",e.contextId);let r=t.toString(),s=r?`/admin-api/blobs?${r}`:"/admin-api/blobs",i=p(await this.httpClient.request(s,{method:"PUT",body:e.data,headers:{"Content-Type":"application/octet-stream"}}));return{blobId:i.blob_id,size:i.size}}async deleteBlob(e){let t=await this.httpClient.delete(`/admin-api/blobs/${e}`);return{blobId:t.blob_id,deleted:t.deleted}}async listBlobs(){return{blobs:p(await this.httpClient.get("/admin-api/blobs")).blobs.map(t=>({blobId:t.blob_id,size:t.size}))}}blobPath(e,t){if(!t?.contextId)return`/admin-api/blobs/${e}`;let r=new URLSearchParams({context_id:t.contextId});return`/admin-api/blobs/${e}?${r.toString()}`}async getBlob(e,t){return this.httpClient.get(this.blobPath(e,t),{parse:"arrayBuffer"})}async getBlobInfo(e,t){let{headers:r}=await this.httpClient.head(this.blobPath(e,t)),s=Number(r["content-length"]);return{blobId:r["x-blob-id"]??e,size:Number.isFinite(s)?s:0,hash:r["x-blob-hash"],mimeType:r["x-blob-mime-type"],source:r["x-blob-source"]}}async createContextAlias(e){return p(await this.httpClient.post("/admin-api/alias/create/context",{alias:e.alias,contextId:e.contextId}))}async createApplicationAlias(e){return p(await this.httpClient.post("/admin-api/alias/create/application",{alias:e.alias,applicationId:e.applicationId}))}async lookupContextAlias(e){return p(await this.httpClient.post(`/admin-api/alias/lookup/context/${encodeURIComponent(e)}`,{}))}async lookupApplicationAlias(e){return p(await this.httpClient.post(`/admin-api/alias/lookup/application/${encodeURIComponent(e)}`,{}))}async deleteContextAlias(e){return p(await this.httpClient.post(`/admin-api/alias/delete/context/${encodeURIComponent(e)}`,{}))}async deleteApplicationAlias(e){return p(await this.httpClient.post(`/admin-api/alias/delete/application/${encodeURIComponent(e)}`,{}))}async listContextAliases(){return p(await this.httpClient.get("/admin-api/alias/list/context"))}async listApplicationAliases(){return p(await this.httpClient.get("/admin-api/alias/list/application"))}async createDeviceAlias(e){return p(await this.httpClient.post("/admin-api/alias/create/device",{alias:e.alias,deviceId:e.deviceId}))}async lookupDeviceAlias(e){return p(await this.httpClient.post(`/admin-api/alias/lookup/device/${encodeURIComponent(e)}`,{}))}async deleteDeviceAlias(e){return p(await this.httpClient.post(`/admin-api/alias/delete/device/${encodeURIComponent(e)}`,{}))}async listDeviceAliases(){return p(await this.httpClient.get("/admin-api/alias/list/device"))}async listNamespaces(){return p(await this.httpClient.get("/admin-api/namespaces"))}async getNamespace(e){return p(await this.httpClient.get(`/admin-api/namespaces/${e}`))}async getNodeIdentity(){return p(await this.httpClient.get("/admin-api/identity"))}async getNamespaceIdentity(e){let t=await this.getNodeIdentity();return{namespaceId:e,publicKey:t.publicKey,account:t.accountId}}async performIntent(e,t){return p(await this.httpClient.post(`/admin-api/contexts/${e}/intents`,t))}async queryContext(e,t){return p(await this.httpClient.post(`/admin-api/contexts/${e}/query`,t))}async getIntentRelay(e){return p(await this.httpClient.get(`/admin-api/contexts/${e}/intents`))}async listNamespacesForApplication(e){return p(await this.httpClient.get(`/admin-api/namespaces/for-application/${e}`))}async createNamespace(e){return p(await this.httpClient.post("/admin-api/namespaces",e))}async deleteNamespace(e){return p(await this.httpClient.request(`/admin-api/namespaces/${e}`,{method:"DELETE",body:"{}",headers:{"Content-Type":"application/json"}}))}async createNamespaceInvitation(e,t){return p(await this.httpClient.post(`/admin-api/namespaces/${e}/invite`,t??{}))}async admitJoin(e,t){return p(await this.httpClient.post(`/admin-api/namespaces/${e}/admit`,t,{timeoutMs:65e3}))}async joinNamespace(e,t){let r=p(await this.httpClient.post(`/admin-api/namespaces/${e}/join`,t,{timeoutMs:65e3}));return r&&r.namespaceId===void 0&&r.groupId!==void 0?{...r,namespaceId:r.groupId}:r}async redeemInvitation(e,t,r={}){let{groupName:s,teamName:i}=r;return St({namespaceId:e,invitation:t,teamName:i},{join:async()=>{await this.joinNamespace(e,s===void 0?{invitation:t}:{invitation:t,groupName:s})},memberships:async()=>(await this.listNamespaces()??[]).map(a=>a.namespaceId)})}async createGroupInNamespace(e,t){return p(await this.httpClient.post(`/admin-api/namespaces/${e}/groups`,t??{}))}async listNamespaceGroups(e){return p(await this.httpClient.get(`/admin-api/namespaces/${e}/groups`))}async initAccountPairing(e){return p(await this.httpClient.post("/admin-api/account/pair-init",e))}async completeAccountPairing(e){return p(await this.httpClient.post("/admin-api/account/pair-complete",e))}async relinkAccountDevice(e,t){return p(await this.httpClient.post(`/admin-api/account/devices/${e}/relink`,t??{}))}async rescopeAccountDevice(e,t){return p(await this.httpClient.put(`/admin-api/account/devices/${encodeURIComponent(e)}/scope`,t))}async signWithAccountRoot(e){return p(await this.httpClient.post("/admin-api/account/sign-with-root",e))}async labelAccountDevice(e,t){return p(await this.httpClient.put(`/admin-api/account/devices/${encodeURIComponent(e)}/label`,t))}async listAccountDevices(){return(await this.httpClient.get("/admin-api/account/devices")).devices}async listAccountApplications(){return(await this.httpClient.get("/admin-api/account/applications")).applications}async revokeAccountDevice(e,t){return p(await this.httpClient.post(`/admin-api/namespaces/${e}/account/revoke`,t))}async getGroupInfo(e){return p(await this.httpClient.get(`/admin-api/groups/${e}`))}async getDefaultCapabilities(e){return(await this.getGroupInfo(e)).defaultCapabilities}async getSubgroupVisibility(e){return(await this.getGroupInfo(e)).subgroupVisibility}async deleteGroup(e){return p(await this.httpClient.request(`/admin-api/groups/${e}`,{method:"DELETE",body:"{}",headers:{"Content-Type":"application/json"}}))}async listGroupMembers(e){let t=await this.httpClient.get(`/admin-api/groups/${e}/members`);if(!Array.isArray(t?.members)){let r=String(e).replace(/[\r\n\t\s]/g,"").slice(0,64);throw new Error(`Invalid listGroupMembers response for group ${r}: missing or non-array \`members\` field`)}return t}async listMemberDevices(e,t={}){let r=new URLSearchParams;t.offset!==void 0&&r.set("offset",String(t.offset)),t.limit!==void 0&&r.set("limit",String(t.limit));let s=r.toString(),i=`/admin-api/groups/${e}/member-devices${s?`?${s}`:""}`;return this.httpClient.get(i)}async sealToAccount(e,t,r){return p(await this.httpClient.post(`/admin-api/groups/${e}/accounts/${t}/seal`,r))}async listGroupContexts(e){return p(await this.httpClient.get(`/admin-api/groups/${e}/contexts`))}async addGroupMembers(e,t){await this.httpClient.post(`/admin-api/groups/${e}/members`,t)}async removeGroupMembers(e,t){await this.httpClient.post(`/admin-api/groups/${e}/members/remove`,t)}async updateMemberRole(e,t,r){await this.httpClient.put(`/admin-api/groups/${e}/members/${t}/role`,r)}async getMemberCapabilities(e,t){return p(await this.httpClient.get(`/admin-api/groups/${e}/members/${t}/capabilities`))}async setMemberCapabilities(e,t,r){await this.httpClient.put(`/admin-api/groups/${e}/members/${t}/capabilities`,r)}async setDefaultCapabilities(e,t){await this.httpClient.put(`/admin-api/groups/${e}/settings/default-capabilities`,t)}async openToDelegatedExecution(e){let t=await this.getDefaultCapabilities(e);if(Be(t,re.CAN_AUTHOR_ON_BEHALF))return{changed:!1,defaultCapabilities:t};let r=Ke(t,re.CAN_AUTHOR_ON_BEHALF);return await this.setDefaultCapabilities(e,{defaultCapabilities:r}),{changed:!0,defaultCapabilities:r}}async grantAuthorship(e,t){let{capabilities:r}=await this.getMemberCapabilities(e,t);if(Be(r,re.CAN_AUTHOR_ON_BEHALF))return{changed:!1,capabilities:r};let s=Ke(r,re.CAN_AUTHOR_ON_BEHALF);return await this.setMemberCapabilities(e,t,{capabilities:s}),{changed:!0,capabilities:s}}async setSubgroupVisibility(e,t){await this.httpClient.put(`/admin-api/groups/${e}/settings/subgroup-visibility`,t)}async setTeeAdmissionPolicy(e,t){await this.httpClient.put(`/admin-api/groups/${e}/settings/tee-admission-policy`,t)}async getTeeAdmissionPolicy(e){let t=await this.httpClient.get(`/admin-api/groups/${e}/settings/tee-admission-policy`);return t.data??t}async setTeeAuthoringPolicy(e,t){await this.httpClient.put(`/admin-api/groups/${e}/settings/tee-authoring-policy`,t)}async disableTeeAuthoringPolicy(e){await this.httpClient.delete(`/admin-api/groups/${e}/settings/tee-authoring-policy`)}async setGroupMetadata(e,t){await this.httpClient.put(`/admin-api/groups/${e}/metadata`,t)}async getGroupMetadata(e){return(await this.httpClient.get(`/admin-api/groups/${e}/metadata`))?.data??null}async setMemberMetadata(e,t,r){await this.httpClient.put(`/admin-api/groups/${e}/members/${t}/metadata`,r)}async getMemberMetadata(e,t){return(await this.httpClient.get(`/admin-api/groups/${e}/members/${t}/metadata`))?.data??null}async setContextMetadata(e,t,r){await this.httpClient.put(`/admin-api/groups/${e}/contexts/${t}/metadata`,r)}async getContextMetadata(e,t){return(await this.httpClient.get(`/admin-api/groups/${e}/contexts/${t}/metadata`))?.data??null}async syncGroup(e){return p(await this.httpClient.post(`/admin-api/groups/${e}/sync`,{}))}async upgradeGroup(e,t){return p(await this.httpClient.post(`/admin-api/groups/${e}/upgrade`,t))}async getGroupUpgradeStatus(e){return p(await this.httpClient.get(`/admin-api/groups/${e}/upgrade/status`))}async getMigrationStatus(e){let t=encodeURIComponent(e);return this.httpClient.get(`/admin-api/groups/${t}/migration-status`)}async getCascadeStatus(e){let t=encodeURIComponent(e);return p(await this.httpClient.get(`/admin-api/groups/${t}/cascade-status`))}async retryGroupUpgrade(e){return p(await this.httpClient.post(`/admin-api/groups/${e}/upgrade/retry`,{}))}async reparentGroup(e,t){let r=await this.httpClient.post(`/admin-api/groups/${e}/reparent`,t);return r?.data??r}async listSubgroups(e){let t=await this.httpClient.get(`/admin-api/groups/${e}/subgroups`);return t.subgroups??t.data??[]}async detachContextFromGroup(e,t){await this.httpClient.post(`/admin-api/groups/${e}/contexts/${t}/remove`,{})}async createGroupInvitation(e,t){return p(await this.httpClient.post(`/admin-api/groups/${e}/invite`,t??{}))}async joinGroup(e){return p(await this.httpClient.post("/admin-api/groups/join",e))}async joinSubgroupInheritance(e){return p(await this.httpClient.post(`/admin-api/groups/${e}/join-via-inheritance`,{}))}async getTeeInfo(){return p(await this.httpClient.get("/admin-api/tee/info"))}async teeAttest(e){return p(await this.httpClient.post("/admin-api/tee/attest",e))}async teeVerifyQuote(e){return p(await this.httpClient.post("/admin-api/tee/verify-quote",e))}async getPeersCount(){return this.httpClient.get("/admin-api/peers")}async getNetworkStatus(){return this.httpClient.get("/admin-api/network/status")}async getUsage(){return this.httpClient.get("/admin-api/usage")}async createGroup(e){return p(await this.httpClient.post("/admin-api/groups",e))}async leaveGroup(e){await this.httpClient.post(`/admin-api/groups/${e}/leave`,{})}async leaveContext(e){await this.httpClient.post(`/admin-api/contexts/${e}/leave`,{})}async leaveNamespace(e){await this.httpClient.post(`/admin-api/namespaces/${e}/leave`,{})}async issueOwnershipProof(e,t){return this.httpClient.post(`/admin-api/groups/${e}/issue-ownership-proof`,t)}async issueNamespaceOwnershipProof(e,t){return this.httpClient.post(`/admin-api/groups/${e}/issue-namespace-ownership-proof`,t)}async setMemberAutoFollow(e,t,r){await this.httpClient.put(`/admin-api/groups/${e}/members/${t}/auto-follow`,r)}async abortMigration(e){return this.httpClient.post(`/admin-api/groups/${e}/migration/abort`,{})}};function Ci(n){return Array.from(new TextEncoder().encode(n),e=>e.toString(16).padStart(2,"0")).join("")}function Ei(n){return new $(F(n))}function Ii(n){return new $(fe(n))}function Pi(n){return new $(Le(n))}function It(n,e){return new $(n)}function Fe(n){try{let e=n.indexOf("#");if(e===-1)return null;let t=n.substring(e+1),r=new URLSearchParams(t),s=r.get("access_token");return s?{accessToken:s,refreshToken:r.get("refresh_token")??"",applicationId:r.get("application_id")??"",contextId:r.get("context_id")??"",contextIdentity:r.get("context_identity")??"",nodeUrl:r.get("node_url")??""}:null}catch{return null}}function We(n,e){let t=new URLSearchParams;return t.set("callback-url",e.callbackUrl),e.permissions&&e.permissions.length>0&&t.set("permissions",e.permissions.join(",")),t.set("mode",e.mode),e.packageName&&(t.set("package-name",e.packageName),e.packageVersion&&t.set("package-version",e.packageVersion),e.registryUrl&&t.set("registry-url",e.registryUrl)),`${n.replace(/\/+$/,"")}/auth/login?${t.toString()}`}var Pt=new Uint8Array([48,46,2,1,0,48,5,6,3,43,101,112,4,34,4,32]);function R(n){let e=new Uint8Array(4);return new DataView(e.buffer).setUint32(0,n,!0),e}function y(n){return Array.from(n,e=>e.toString(16).padStart(2,"0")).join("")}function b(n,e,t){let r=n.trim();if(!/^[0-9a-fA-F]*$/.test(r)||r.length!==t*2)throw new Error(`${e} must be ${t*2} hex characters, got ${r.length}`);let s=new Uint8Array(t);for(let i=0;i<t;i+=1)s[i]=Number.parseInt(r.slice(i*2,i*2+2),16);return s}function be(n,e){let t=n.trim();if(!/^[0-9a-fA-F]*$/.test(t)||t.length%2!==0)throw new Error(`${e} must be an even number of hex characters`);let r=new Uint8Array(t.length/2);for(let s=0;s<r.length;s+=1)r[s]=Number.parseInt(t.slice(s*2,s*2+2),16);return r}function w(n){let e=new Uint8Array(8);return new DataView(e.buffer).setBigUint64(0,BigInt(n),!0),e}function h(...n){let e=new Uint8Array(n.reduce((r,s)=>r+s.length,0)),t=0;for(let r of n)e.set(r,t),t+=r.length;return e}async function I(n,e){let t=[w(n.length),n];for(let r of e)t.push(w(r.length),r);return new Uint8Array(await crypto.subtle.digest("SHA-256",h(...t)))}async function T(n){let e=b(n,"deviceSecret",32),t=h(Pt,e);try{return await crypto.subtle.importKey("pkcs8",t,{name:"Ed25519"},!1,["sign"])}catch(r){let s=r instanceof Error?`: ${r.message}`:"";throw new Error("this runtime has no WebCrypto Ed25519, so a warrant cannot be signed here. Node 18.4+, Safari 17+, Firefox 129+ and Chrome 137+ have it; otherwise mint the warrant with `merod account warrant`"+s)}}async function E(n){let e=b(n,"deviceSecret",32),t=h(Pt,e),r=await crypto.subtle.importKey("pkcs8",t,{name:"Ed25519"},!0,["sign"]),i=(await crypto.subtle.exportKey("jwk",r)).x;return Uint8Array.from(atob(i.replace(/-/g,"+").replace(/_/g,"/")),a=>a.charCodeAt(0))}var ir=new TextEncoder().encode("calimero.auth.request.v1"),or=new TextEncoder().encode("calimero.auth.request.body.v1");function Dt(n){let e=new TextEncoder().encode(n);return h(R(e.length),e)}async function Je(n=new Uint8Array(0)){let e=typeof n=="string"?new TextEncoder().encode(n):n;return I(or,[e])}async function we(n){let e=await T(n.signerSecret),t=await Je(n.body),r=w(n.issuedAt),s=w(n.expiresAt),i=new TextEncoder,a=await I(ir,[i.encode(n.method),i.encode(n.path),t,r,s]),o=new Uint8Array(await crypto.subtle.sign({name:"Ed25519"},e,a));return y(h(Dt(n.method),Dt(n.path),t,r,s,o))}function Ae(n){let e=be(n.credential,"credential"),t=be(n.request,"request signature"),r=n.session===void 0?new Uint8Array([0]):h(new Uint8Array([1]),be(n.session,"session statement"));return y(h(e,r,t))}function ar(n){if(n==null)return new Uint8Array(0);if(typeof n=="string"||n instanceof Uint8Array)return n;if(n instanceof ArrayBuffer)return new Uint8Array(n);if(ArrayBuffer.isView(n))return new Uint8Array(n.buffer,n.byteOffset,n.byteLength);throw new Error(`a request-carried proof cannot commit to a ${n?.constructor?.name??typeof n} body: pass a string or bytes, or use a session for this call`)}function Re(n){let e=n.ttlSeconds??120,t=n.now??(()=>Math.floor(Date.now()/1e3));return async r=>{let s=t();return Ae({credential:n.credential,session:n.session,request:await we({method:r.method,path:r.path,body:ar(r.body),issuedAt:s,expiresAt:s+e,signerSecret:n.signerSecret})})}}var xe=class extends Error{constructor(e,t,r,s){super(t),this.name="RpcError",this.code=e,this.data=r,this.type=s}};async function ze(n,e,t){let r=await n.post("/jsonrpc",{jsonrpc:"2.0",id:1,method:e,params:t});if(r.error){let s=r.error;throw new xe(s.code??-1,s.message??s.type??"RPC error",s.data,s.type)}return r.result}var se=class{constructor(e){this.httpClient=e.httpClient}async execute(e){let t=await ze(this.httpClient,"execute",{contextId:e.contextId,method:e.method,argsJson:e.argsJson??{}});return t&&"output"in t?t.output:t}async migrateMyEntries(e){return this.execute({contextId:e,method:"migrate_my_entries"})}async countMyPending(e){return this.execute({contextId:e,method:"count_my_pending"})}};var cr=new Set(["MigrationStarted","MigrationProgress","CascadeProgress","MigrationCompleted"]);function Ut(n){return n.type!==void 0&&cr.has(n.type)}var lr=new Set(["token_reuse","token_revoked"]),ur=new Set([401,403]);function Mt(n){let e=n.headers.get("x-auth-error");return!e||!ur.has(n.status)?null:lr.has(e)?e:null}var W=class{constructor(e){this.sessionId=null;this.abortController=null;this.reconnectTimer=null;this.subscribedContextIds=new Set;this.subscribedGroupIds=new Set;this.closed=!1;this.listeners={connect:[],event:[],error:[]};this.baseUrl=e.baseUrl.replace(/\/+$/,""),this.getAuthToken=e.getAuthToken,this.reconnectDelayMs=e.reconnectDelayMs??3e3,this.fetchImpl=(t,r)=>e.fetch?e.fetch(t,r):globalThis.fetch(t,r)}on(e,t){let r=e;if(r in this.listeners){let s=this.listeners[r];s.includes(t)||s.push(t)}}off(e,t){let r=e;if(r in this.listeners){let s=this.listeners[r],i=s.indexOf(t);i!==-1&&s.splice(i,1)}}onAppVersionChanged(e){let t=r=>{if(r.type!=="AppVersionChanged")return;let s=r.data;e({contextId:r.contextId,fromVersion:s?.fromVersion,toVersion:s?.toVersion})};return this.on("event",t),()=>this.off("event",t)}onMigrationEvent(e){let t=r=>{Ut(r)&&e(r)};return this.on("event",t),()=>this.off("event",t)}emit(e,t){let r=e;if(r in this.listeners)for(let s of this.listeners[r])try{s(t)}catch{}}async connect(){if(!(this.abortController&&!this.closed)){this.closed=!1,this.abortController=new AbortController;try{let e=await this.getAuthToken(),t=`${this.baseUrl}/sse`,r=await this.fetchImpl(t,{headers:{Authorization:`Bearer ${e}`,Accept:"text/event-stream"},signal:this.abortController.signal});if(!r.ok){let s=Mt(r),i=await r.text().catch(()=>{});throw s?new _(s,r.status,r.statusText,t,r.headers,i):new S(r.status,r.statusText,t,r.headers,i)}if(!r.body)throw new Error("SSE response has no body");this.readStream(r.body).catch(s=>{if(this.closed)return;let i=s instanceof Error?s:new Error(String(s));this.emit("error",i),this.scheduleReconnect()})}catch(e){if(this.closed)return;let t=e instanceof Error?e:new Error(String(e));if(this.emit("error",t),t instanceof _)return;this.scheduleReconnect()}}}async readStream(e){let t=e.getReader(),r=new TextDecoder,s="";try{for(;;){let{done:i,value:a}=await t.read();if(i)break;s+=r.decode(a,{stream:!0});let o=s.split(`
|
|
2
|
+
`);s=o.pop()??"";for(let c of o)if(c.startsWith("data:")){let u=c.slice(5).trim();u&&this.handleMessage(u)}}if(s+=r.decode(),s.startsWith("data:")){let i=s.slice(5).trim();i&&this.handleMessage(i)}}catch(i){if(this.closed)return;let a=i instanceof Error?i:new Error(String(i));this.emit("error",a)}this.closed||this.scheduleReconnect()}handleMessage(e){try{let t=JSON.parse(e);if(t.type==="connect"&&t.session_id){this.sessionId=t.session_id,this.emit("connect",t.session_id),(this.subscribedContextIds.size>0||this.subscribedGroupIds.size>0)&&this.sendSubscription("subscribe",{contextIds:[...this.subscribedContextIds],groupIds:[...this.subscribedGroupIds]});return}if(!t.result)return;let r=t.result.data;if(Array.isArray(r))try{let s=new Uint8Array(r),i=new TextDecoder().decode(s);r=JSON.parse(i)}catch{}if(t.result.contextId){this.emit("event",{contextId:t.result.contextId,type:t.result.type,data:r});return}t.result.groupId&&this.emit("event",{groupId:t.result.groupId,type:t.result.type,data:r})}catch{}}async subscribe(e){let t=Array.isArray(e)?{contextIds:e}:e,r=(t.contextIds??[]).filter(i=>!this.subscribedContextIds.has(i)),s=(t.groupIds??[]).filter(i=>!this.subscribedGroupIds.has(i));for(let i of t.contextIds??[])this.subscribedContextIds.add(i);for(let i of t.groupIds??[])this.subscribedGroupIds.add(i);(r.length>0||s.length>0)&&this.sessionId&&await this.sendSubscription("subscribe",{contextIds:r,groupIds:s})}async unsubscribe(e){let t=Array.isArray(e)?{contextIds:e}:e,r=(t.contextIds??[]).filter(i=>this.subscribedContextIds.has(i)),s=(t.groupIds??[]).filter(i=>this.subscribedGroupIds.has(i));for(let i of t.contextIds??[])this.subscribedContextIds.delete(i);for(let i of t.groupIds??[])this.subscribedGroupIds.delete(i);(r.length>0||s.length>0)&&this.sessionId&&await this.sendSubscription("unsubscribe",{contextIds:r,groupIds:s})}async sendSubscription(e,t){try{let r=await this.getAuthToken(),s={};t.contextIds&&t.contextIds.length>0&&(s.contextIds=t.contextIds),t.groupIds&&t.groupIds.length>0&&(s.groupIds=t.groupIds);let i=await this.fetchImpl(`${this.baseUrl}/sse/subscription`,{method:"POST",headers:{Authorization:`Bearer ${r}`,"Content-Type":"application/json"},body:JSON.stringify({id:this.sessionId,method:e,params:s})});if(!i.ok){let a=Mt(i),o=await i.text().catch(()=>{});this.emit("error",a?new _(a,i.status,i.statusText,`${this.baseUrl}/sse/subscription`,i.headers,o):new S(i.status,i.statusText,`${this.baseUrl}/sse/subscription`,i.headers,o))}}catch(r){this.emit("error",r instanceof Error?r:new Error(`SSE ${e} failed`))}}forceReconnect(){this.abortController&&(this.abortController.abort(),this.abortController=null),this.sessionId=null,this.connect()}scheduleReconnect(){this.closed||(this.reconnectTimer&&clearTimeout(this.reconnectTimer),this.reconnectTimer=setTimeout(()=>{this.reconnectTimer=null,this.forceReconnect()},this.reconnectDelayMs))}close(){this.closed=!0,this.abortController&&(this.abortController.abort(),this.abortController=null),this.reconnectTimer&&(clearTimeout(this.reconnectTimer),this.reconnectTimer=null),this.sessionId=null,this.subscribedContextIds.clear(),this.subscribedGroupIds.clear()}};var Ce=class Ce{constructor(e){this.ws=null;this.closed=!1;this.reconnectAttempt=0;this.reconnectTimer=null;this.subscribedContextIds=new Set;this.subscribedGroupIds=new Set;this.listeners={connect:[],event:[],error:[]};this.baseUrl=e.baseUrl.replace(/\/+$/,""),this.getAuthToken=e.getAuthToken}on(e,t){let r=e;if(r in this.listeners){let s=this.listeners[r];s.includes(t)||s.push(t)}}off(e,t){let r=e;if(r in this.listeners){let s=this.listeners[r],i=s.indexOf(t);i!==-1&&s.splice(i,1)}}emit(e,t){let r=e;if(r in this.listeners)for(let s of this.listeners[r])try{s(t)}catch{}}async connect(){if(!(this.ws&&(this.ws.readyState===0||this.ws.readyState===1))){this.closed=!1;try{let e=await this.getAuthToken();if(!e)throw new Error("No authentication token available for WebSocket connection");let t=this.baseUrl.replace(/^http/,"ws");this.ws=new WebSocket(`${t}/ws?token=${encodeURIComponent(e)}`),this.ws.onopen=()=>{this.reconnectAttempt=0,this.emit("connect"),this.resubscribeAfterReconnect()},this.ws.onmessage=r=>{this.handleMessage(r.data)},this.ws.onerror=()=>{this.emit("error",new Error("WebSocket error"))},this.ws.onclose=()=>{this.closed||this.scheduleReconnect()}}catch(e){if(this.closed)return;let t=e instanceof Error?e:new Error(String(e));this.emit("error",t),this.scheduleReconnect()}}}handleMessage(e){if(typeof e=="string")try{let t=JSON.parse(e);if(!t.result)return;let r=t.result.data;if(Array.isArray(r))try{let s=new Uint8Array(r),i=new TextDecoder().decode(s);r=JSON.parse(i)}catch{}if(t.result.contextId){this.emit("event",{contextId:t.result.contextId,type:t.result.type,data:r});return}t.result.groupId&&this.emit("event",{groupId:t.result.groupId,type:t.result.type,data:r})}catch{}}subscribe(e){let t=Array.isArray(e)?{contextIds:e}:e;for(let r of t.contextIds??[])this.subscribedContextIds.add(r);for(let r of t.groupIds??[])this.subscribedGroupIds.add(r);this.ws&&this.ws.readyState===1&&this.sendMessage({id:null,method:"subscribe",params:this.buildSubscriptionParams(t.contextIds??[],t.groupIds??[])})}unsubscribe(e){let t=Array.isArray(e)?{contextIds:e}:e;for(let r of t.contextIds??[])this.subscribedContextIds.delete(r);for(let r of t.groupIds??[])this.subscribedGroupIds.delete(r);this.ws&&this.ws.readyState===1&&this.sendMessage({id:null,method:"unsubscribe",params:this.buildSubscriptionParams(t.contextIds??[],t.groupIds??[])})}resubscribeAfterReconnect(){this.subscribedContextIds.size===0&&this.subscribedGroupIds.size===0||this.sendMessage({id:null,method:"subscribe",params:this.buildSubscriptionParams([...this.subscribedContextIds],[...this.subscribedGroupIds])})}buildSubscriptionParams(e,t){let r={};return e.length>0&&(r.contextIds=e),t.length>0&&(r.groupIds=t),r}sendMessage(e){this.ws&&this.ws.readyState===1&&this.ws.send(JSON.stringify(e))}scheduleReconnect(){if(this.closed)return;this.reconnectTimer&&clearTimeout(this.reconnectTimer);let e=Math.min(1e3*Math.pow(2,this.reconnectAttempt),Ce.MAX_BACKOFF_MS);this.reconnectAttempt++,this.reconnectTimer=setTimeout(()=>{this.reconnectTimer=null,this.connect()},e)}close(){this.closed=!0,this.reconnectTimer&&(clearTimeout(this.reconnectTimer),this.reconnectTimer=null),this.ws&&(this.ws.onclose=null,this.ws.close(),this.ws=null),this.subscribedContextIds.clear(),this.subscribedGroupIds.clear()}};Ce.MAX_BACKOFF_MS=3e4;var J=Ce;function ve(){return{encode(n){return Array.from(new TextEncoder().encode(JSON.stringify(n)))},decode(n){return JSON.parse(new TextDecoder().decode(new Uint8Array(n)))}}}var ie=class{constructor(e){this.httpClient=e.httpClient,this.sse=e.sse}async set(e,t,r=ve()){await ze(this.httpClient,"set_ephemeral",{contextId:e,state:r.encode(t)})}subscribe(e,t,r=ve()){let s=i=>{let a=i;if(a.type!=="Ephemeral"||a.contextId!==e)return;let o=a.data;if(!o?.author)return;let c=!!o.removed,u={author:o.author,state:c||o.state===void 0?void 0:r.decode(o.state),removed:c};o.ageMs!==void 0&&(u.ageMs=o.ageMs),t(u)};return this.sse.on("event",s),this.sse.connect().catch(()=>{}),this.sse.subscribe([e]).catch(()=>{}),()=>{this.sse.off("event",s)}}};var dr=new TextEncoder().encode("calimero.device.cert.v1"),Nt=new TextEncoder().encode("calimero.account.genesis.v1"),pr=new TextEncoder().encode("calimero.device.id.v1"),Qe=2;async function _t(n,e){if(e.length!==16)throw new Error(`nonce must be 16 bytes, got ${e.length}`);let t=b(n,"account",32);return y(await I(pr,[t,e]))}async function Ye(n){return I(dr,[b(n.account,"account",32),b(n.device,"device",32),b(n.signPublicKey,"signPublicKey",32),b(n.kemPublicKey,"kemPublicKey",32),R(n.keyEpoch),R(n.deviceEpoch)])}function Ht(n){return h(new Uint8Array([Qe]),n.rootPublicKey,R(0),b(n.account,"account",32),b(n.device,"device",32),b(n.signPublicKey,"signPublicKey",32),b(n.kemPublicKey,"kemPublicKey",32),R(n.keyEpoch),R(n.deviceEpoch),n.signature)}async function qt(n){let e=await E(n.rootSecret),t=y(await I(Nt,[h(new Uint8Array([Qe]),e)])),r=0,s=await Ye({account:t,device:n.device,signPublicKey:n.signPublicKey,kemPublicKey:n.kemPublicKey,keyEpoch:r,deviceEpoch:n.deviceEpoch}),i=await T(n.rootSecret),a=new Uint8Array(await crypto.subtle.sign("Ed25519",i,s)),o=Ht({rootPublicKey:e,account:t,device:n.device,signPublicKey:n.signPublicKey,kemPublicKey:n.kemPublicKey,keyEpoch:r,deviceEpoch:n.deviceEpoch,signature:a});return y(o)}async function ke(n){let e=await E(n);return y(await I(Nt,[h(new Uint8Array([Qe]),e)]))}var Xe=["abandon","ability","able","about","above","absent","absorb","abstract","absurd","abuse","access","accident","account","accuse","achieve","acid","acoustic","acquire","across","act","action","actor","actress","actual","adapt","add","addict","address","adjust","admit","adult","advance","advice","aerobic","affair","afford","afraid","again","age","agent","agree","ahead","aim","air","airport","aisle","alarm","album","alcohol","alert","alien","all","alley","allow","almost","alone","alpha","already","also","alter","always","amateur","amazing","among","amount","amused","analyst","anchor","ancient","anger","angle","angry","animal","ankle","announce","annual","another","answer","antenna","antique","anxiety","any","apart","apology","appear","apple","approve","april","arch","arctic","area","arena","argue","arm","armed","armor","army","around","arrange","arrest","arrive","arrow","art","artefact","artist","artwork","ask","aspect","assault","asset","assist","assume","asthma","athlete","atom","attack","attend","attitude","attract","auction","audit","august","aunt","author","auto","autumn","average","avocado","avoid","awake","aware","away","awesome","awful","awkward","axis","baby","bachelor","bacon","badge","bag","balance","balcony","ball","bamboo","banana","banner","bar","barely","bargain","barrel","base","basic","basket","battle","beach","bean","beauty","because","become","beef","before","begin","behave","behind","believe","below","belt","bench","benefit","best","betray","better","between","beyond","bicycle","bid","bike","bind","biology","bird","birth","bitter","black","blade","blame","blanket","blast","bleak","bless","blind","blood","blossom","blouse","blue","blur","blush","board","boat","body","boil","bomb","bone","bonus","book","boost","border","boring","borrow","boss","bottom","bounce","box","boy","bracket","brain","brand","brass","brave","bread","breeze","brick","bridge","brief","bright","bring","brisk","broccoli","broken","bronze","broom","brother","brown","brush","bubble","buddy","budget","buffalo","build","bulb","bulk","bullet","bundle","bunker","burden","burger","burst","bus","business","busy","butter","buyer","buzz","cabbage","cabin","cable","cactus","cage","cake","call","calm","camera","camp","can","canal","cancel","candy","cannon","canoe","canvas","canyon","capable","capital","captain","car","carbon","card","cargo","carpet","carry","cart","case","cash","casino","castle","casual","cat","catalog","catch","category","cattle","caught","cause","caution","cave","ceiling","celery","cement","census","century","cereal","certain","chair","chalk","champion","change","chaos","chapter","charge","chase","chat","cheap","check","cheese","chef","cherry","chest","chicken","chief","child","chimney","choice","choose","chronic","chuckle","chunk","churn","cigar","cinnamon","circle","citizen","city","civil","claim","clap","clarify","claw","clay","clean","clerk","clever","click","client","cliff","climb","clinic","clip","clock","clog","close","cloth","cloud","clown","club","clump","cluster","clutch","coach","coast","coconut","code","coffee","coil","coin","collect","color","column","combine","come","comfort","comic","common","company","concert","conduct","confirm","congress","connect","consider","control","convince","cook","cool","copper","copy","coral","core","corn","correct","cost","cotton","couch","country","couple","course","cousin","cover","coyote","crack","cradle","craft","cram","crane","crash","crater","crawl","crazy","cream","credit","creek","crew","cricket","crime","crisp","critic","crop","cross","crouch","crowd","crucial","cruel","cruise","crumble","crunch","crush","cry","crystal","cube","culture","cup","cupboard","curious","current","curtain","curve","cushion","custom","cute","cycle","dad","damage","damp","dance","danger","daring","dash","daughter","dawn","day","deal","debate","debris","decade","december","decide","decline","decorate","decrease","deer","defense","define","defy","degree","delay","deliver","demand","demise","denial","dentist","deny","depart","depend","deposit","depth","deputy","derive","describe","desert","design","desk","despair","destroy","detail","detect","develop","device","devote","diagram","dial","diamond","diary","dice","diesel","diet","differ","digital","dignity","dilemma","dinner","dinosaur","direct","dirt","disagree","discover","disease","dish","dismiss","disorder","display","distance","divert","divide","divorce","dizzy","doctor","document","dog","doll","dolphin","domain","donate","donkey","donor","door","dose","double","dove","draft","dragon","drama","drastic","draw","dream","dress","drift","drill","drink","drip","drive","drop","drum","dry","duck","dumb","dune","during","dust","dutch","duty","dwarf","dynamic","eager","eagle","early","earn","earth","easily","east","easy","echo","ecology","economy","edge","edit","educate","effort","egg","eight","either","elbow","elder","electric","elegant","element","elephant","elevator","elite","else","embark","embody","embrace","emerge","emotion","employ","empower","empty","enable","enact","end","endless","endorse","enemy","energy","enforce","engage","engine","enhance","enjoy","enlist","enough","enrich","enroll","ensure","enter","entire","entry","envelope","episode","equal","equip","era","erase","erode","erosion","error","erupt","escape","essay","essence","estate","eternal","ethics","evidence","evil","evoke","evolve","exact","example","excess","exchange","excite","exclude","excuse","execute","exercise","exhaust","exhibit","exile","exist","exit","exotic","expand","expect","expire","explain","expose","express","extend","extra","eye","eyebrow","fabric","face","faculty","fade","faint","faith","fall","false","fame","family","famous","fan","fancy","fantasy","farm","fashion","fat","fatal","father","fatigue","fault","favorite","feature","february","federal","fee","feed","feel","female","fence","festival","fetch","fever","few","fiber","fiction","field","figure","file","film","filter","final","find","fine","finger","finish","fire","firm","first","fiscal","fish","fit","fitness","fix","flag","flame","flash","flat","flavor","flee","flight","flip","float","flock","floor","flower","fluid","flush","fly","foam","focus","fog","foil","fold","follow","food","foot","force","forest","forget","fork","fortune","forum","forward","fossil","foster","found","fox","fragile","frame","frequent","fresh","friend","fringe","frog","front","frost","frown","frozen","fruit","fuel","fun","funny","furnace","fury","future","gadget","gain","galaxy","gallery","game","gap","garage","garbage","garden","garlic","garment","gas","gasp","gate","gather","gauge","gaze","general","genius","genre","gentle","genuine","gesture","ghost","giant","gift","giggle","ginger","giraffe","girl","give","glad","glance","glare","glass","glide","glimpse","globe","gloom","glory","glove","glow","glue","goat","goddess","gold","good","goose","gorilla","gospel","gossip","govern","gown","grab","grace","grain","grant","grape","grass","gravity","great","green","grid","grief","grit","grocery","group","grow","grunt","guard","guess","guide","guilt","guitar","gun","gym","habit","hair","half","hammer","hamster","hand","happy","harbor","hard","harsh","harvest","hat","have","hawk","hazard","head","health","heart","heavy","hedgehog","height","hello","helmet","help","hen","hero","hidden","high","hill","hint","hip","hire","history","hobby","hockey","hold","hole","holiday","hollow","home","honey","hood","hope","horn","horror","horse","hospital","host","hotel","hour","hover","hub","huge","human","humble","humor","hundred","hungry","hunt","hurdle","hurry","hurt","husband","hybrid","ice","icon","idea","identify","idle","ignore","ill","illegal","illness","image","imitate","immense","immune","impact","impose","improve","impulse","inch","include","income","increase","index","indicate","indoor","industry","infant","inflict","inform","inhale","inherit","initial","inject","injury","inmate","inner","innocent","input","inquiry","insane","insect","inside","inspire","install","intact","interest","into","invest","invite","involve","iron","island","isolate","issue","item","ivory","jacket","jaguar","jar","jazz","jealous","jeans","jelly","jewel","job","join","joke","journey","joy","judge","juice","jump","jungle","junior","junk","just","kangaroo","keen","keep","ketchup","key","kick","kid","kidney","kind","kingdom","kiss","kit","kitchen","kite","kitten","kiwi","knee","knife","knock","know","lab","label","labor","ladder","lady","lake","lamp","language","laptop","large","later","latin","laugh","laundry","lava","law","lawn","lawsuit","layer","lazy","leader","leaf","learn","leave","lecture","left","leg","legal","legend","leisure","lemon","lend","length","lens","leopard","lesson","letter","level","liar","liberty","library","license","life","lift","light","like","limb","limit","link","lion","liquid","list","little","live","lizard","load","loan","lobster","local","lock","logic","lonely","long","loop","lottery","loud","lounge","love","loyal","lucky","luggage","lumber","lunar","lunch","luxury","lyrics","machine","mad","magic","magnet","maid","mail","main","major","make","mammal","man","manage","mandate","mango","mansion","manual","maple","marble","march","margin","marine","market","marriage","mask","mass","master","match","material","math","matrix","matter","maximum","maze","meadow","mean","measure","meat","mechanic","medal","media","melody","melt","member","memory","mention","menu","mercy","merge","merit","merry","mesh","message","metal","method","middle","midnight","milk","million","mimic","mind","minimum","minor","minute","miracle","mirror","misery","miss","mistake","mix","mixed","mixture","mobile","model","modify","mom","moment","monitor","monkey","monster","month","moon","moral","more","morning","mosquito","mother","motion","motor","mountain","mouse","move","movie","much","muffin","mule","multiply","muscle","museum","mushroom","music","must","mutual","myself","mystery","myth","naive","name","napkin","narrow","nasty","nation","nature","near","neck","need","negative","neglect","neither","nephew","nerve","nest","net","network","neutral","never","news","next","nice","night","noble","noise","nominee","noodle","normal","north","nose","notable","note","nothing","notice","novel","now","nuclear","number","nurse","nut","oak","obey","object","oblige","obscure","observe","obtain","obvious","occur","ocean","october","odor","off","offer","office","often","oil","okay","old","olive","olympic","omit","once","one","onion","online","only","open","opera","opinion","oppose","option","orange","orbit","orchard","order","ordinary","organ","orient","original","orphan","ostrich","other","outdoor","outer","output","outside","oval","oven","over","own","owner","oxygen","oyster","ozone","pact","paddle","page","pair","palace","palm","panda","panel","panic","panther","paper","parade","parent","park","parrot","party","pass","patch","path","patient","patrol","pattern","pause","pave","payment","peace","peanut","pear","peasant","pelican","pen","penalty","pencil","people","pepper","perfect","permit","person","pet","phone","photo","phrase","physical","piano","picnic","picture","piece","pig","pigeon","pill","pilot","pink","pioneer","pipe","pistol","pitch","pizza","place","planet","plastic","plate","play","please","pledge","pluck","plug","plunge","poem","poet","point","polar","pole","police","pond","pony","pool","popular","portion","position","possible","post","potato","pottery","poverty","powder","power","practice","praise","predict","prefer","prepare","present","pretty","prevent","price","pride","primary","print","priority","prison","private","prize","problem","process","produce","profit","program","project","promote","proof","property","prosper","protect","proud","provide","public","pudding","pull","pulp","pulse","pumpkin","punch","pupil","puppy","purchase","purity","purpose","purse","push","put","puzzle","pyramid","quality","quantum","quarter","question","quick","quit","quiz","quote","rabbit","raccoon","race","rack","radar","radio","rail","rain","raise","rally","ramp","ranch","random","range","rapid","rare","rate","rather","raven","raw","razor","ready","real","reason","rebel","rebuild","recall","receive","recipe","record","recycle","reduce","reflect","reform","refuse","region","regret","regular","reject","relax","release","relief","rely","remain","remember","remind","remove","render","renew","rent","reopen","repair","repeat","replace","report","require","rescue","resemble","resist","resource","response","result","retire","retreat","return","reunion","reveal","review","reward","rhythm","rib","ribbon","rice","rich","ride","ridge","rifle","right","rigid","ring","riot","ripple","risk","ritual","rival","river","road","roast","robot","robust","rocket","romance","roof","rookie","room","rose","rotate","rough","round","route","royal","rubber","rude","rug","rule","run","runway","rural","sad","saddle","sadness","safe","sail","salad","salmon","salon","salt","salute","same","sample","sand","satisfy","satoshi","sauce","sausage","save","say","scale","scan","scare","scatter","scene","scheme","school","science","scissors","scorpion","scout","scrap","screen","script","scrub","sea","search","season","seat","second","secret","section","security","seed","seek","segment","select","sell","seminar","senior","sense","sentence","series","service","session","settle","setup","seven","shadow","shaft","shallow","share","shed","shell","sheriff","shield","shift","shine","ship","shiver","shock","shoe","shoot","shop","short","shoulder","shove","shrimp","shrug","shuffle","shy","sibling","sick","side","siege","sight","sign","silent","silk","silly","silver","similar","simple","since","sing","siren","sister","situate","six","size","skate","sketch","ski","skill","skin","skirt","skull","slab","slam","sleep","slender","slice","slide","slight","slim","slogan","slot","slow","slush","small","smart","smile","smoke","smooth","snack","snake","snap","sniff","snow","soap","soccer","social","sock","soda","soft","solar","soldier","solid","solution","solve","someone","song","soon","sorry","sort","soul","sound","soup","source","south","space","spare","spatial","spawn","speak","special","speed","spell","spend","sphere","spice","spider","spike","spin","spirit","split","spoil","sponsor","spoon","sport","spot","spray","spread","spring","spy","square","squeeze","squirrel","stable","stadium","staff","stage","stairs","stamp","stand","start","state","stay","steak","steel","stem","step","stereo","stick","still","sting","stock","stomach","stone","stool","story","stove","strategy","street","strike","strong","struggle","student","stuff","stumble","style","subject","submit","subway","success","such","sudden","suffer","sugar","suggest","suit","summer","sun","sunny","sunset","super","supply","supreme","sure","surface","surge","surprise","surround","survey","suspect","sustain","swallow","swamp","swap","swarm","swear","sweet","swift","swim","swing","switch","sword","symbol","symptom","syrup","system","table","tackle","tag","tail","talent","talk","tank","tape","target","task","taste","tattoo","taxi","teach","team","tell","ten","tenant","tennis","tent","term","test","text","thank","that","theme","then","theory","there","they","thing","this","thought","three","thrive","throw","thumb","thunder","ticket","tide","tiger","tilt","timber","time","tiny","tip","tired","tissue","title","toast","tobacco","today","toddler","toe","together","toilet","token","tomato","tomorrow","tone","tongue","tonight","tool","tooth","top","topic","topple","torch","tornado","tortoise","toss","total","tourist","toward","tower","town","toy","track","trade","traffic","tragic","train","transfer","trap","trash","travel","tray","treat","tree","trend","trial","tribe","trick","trigger","trim","trip","trophy","trouble","truck","true","truly","trumpet","trust","truth","try","tube","tuition","tumble","tuna","tunnel","turkey","turn","turtle","twelve","twenty","twice","twin","twist","two","type","typical","ugly","umbrella","unable","unaware","uncle","uncover","under","undo","unfair","unfold","unhappy","uniform","unique","unit","universe","unknown","unlock","until","unusual","unveil","update","upgrade","uphold","upon","upper","upset","urban","urge","usage","use","used","useful","useless","usual","utility","vacant","vacuum","vague","valid","valley","valve","van","vanish","vapor","various","vast","vault","vehicle","velvet","vendor","venture","venue","verb","verify","version","very","vessel","veteran","viable","vibrant","vicious","victory","video","view","village","vintage","violin","virtual","virus","visa","visit","visual","vital","vivid","vocal","voice","void","volcano","volume","vote","voyage","wage","wagon","wait","walk","wall","walnut","want","warfare","warm","warrior","wash","wasp","waste","water","wave","way","wealth","weapon","wear","weasel","weather","web","wedding","weekend","weird","welcome","west","wet","whale","what","wheat","wheel","when","where","whip","whisper","wide","width","wife","wild","will","win","window","wine","wing","wink","winner","winter","wire","wisdom","wise","wish","witness","wolf","woman","wonder","wood","wool","word","work","world","worry","worth","wrap","wreck","wrestle","wrist","write","wrong","yard","year","yellow","you","young","youth","zebra","zero","zone","zoo"];var gr=new TextEncoder().encode("calimero.mdma.account-link.v1\0"),mr=new TextEncoder().encode("calimero.mdma.account-login.v1\0"),oe=256,Ot=oe/32,Ze=(oe+Ot)/11;async function $t(){let n=crypto.getRandomValues(new Uint8Array(oe/8));return Lt(y(n),await fr(n))}async function Gt(n){let e=hr(n);return Lt(y(await yr(e)),e.join(" "))}async function G(n){return b(n,"secret",32),{secret:n,publicKey:y(await E(n)),accountId:await ke(n)}}async function ae(n){if(!n.nonce)throw new Error("nonce is required to sign an account link");let e=await T(n.rootSecret),t=new Uint8Array(await crypto.subtle.sign("Ed25519",e,h(gr,new TextEncoder().encode(n.nonce))));return jt(t)}async function Se(n){if(!n.nonce)throw new Error("nonce is required to sign an account login");let e=await T(n.rootSecret),t=new Uint8Array(await crypto.subtle.sign("Ed25519",e,h(mr,new TextEncoder().encode(n.nonce))));return jt(t)}async function Lt(n,e){return{...await G(n),phrase:e}}function hr(n){let e=n.normalize("NFKD").toLowerCase().trim().split(/\s+/),t=e[0]===""?0:e.length;if(t!==Ze)throw new Error(`recovery phrase must be ${Ze} words, got ${t}`);return e}async function fr(n){let e=Kt(n)+await Bt(n),t=[];for(let r=0;r<Ze;r+=1){let s=Number.parseInt(e.slice(r*11,r*11+11),2);t.push(Xe[s])}return t.join(" ")}async function yr(n){let e="";for(let r of n){let s=Xe.indexOf(r);if(s<0)throw new Error(`"${r}" is not a word in the recovery phrase list`);e+=s.toString(2).padStart(11,"0")}let t=new Uint8Array(oe/8);for(let r=0;r<t.length;r+=1)t[r]=Number.parseInt(e.slice(r*8,r*8+8),2);if(e.slice(oe)!==await Bt(t))throw new Error("recovery phrase checksum failed \u2014 check for a mistyped or transposed word");return t}async function Bt(n){let e=new Uint8Array(await crypto.subtle.digest("SHA-256",n));return Kt(e).slice(0,Ot)}function Kt(n){let e="";for(let t of n)e+=t.toString(2).padStart(8,"0");return e}function jt(n){let e="";for(let t of n)e+=String.fromCharCode(t);return btoa(e)}var br=new TextEncoder().encode("calimero.mdma.routing-read.v1\0");async function Vt(n,e){b(e,"deviceSecret",32);let t=await T(e),r=h(br,new TextEncoder().encode(n)),s=new Uint8Array(await crypto.subtle.sign({name:"Ed25519"},t,r));return wr(s)}async function Te(n,e){return{"X-Calimero-Credential":e.credential,"X-Calimero-Nonce":n.nonce,"X-Calimero-Signature":await Vt(n.nonce,e.deviceSecret)}}function wr(n){let e="";for(let t of n)e+=String.fromCharCode(t);return btoa(e)}var Ar="https://cloud.calimero.network";function et(n,e){return typeof n.can_execute=="boolean"?n.can_execute:e}function tt(n){return typeof n.tee_role=="string"&&n.tee_role!==""?n.tee_role:null}function nt(n){return n.find(e=>e.canExecute&&e.relayUrl!==null&&e.executorAccount!==null)??null}var Rr="x-mdma-session-refresh",xr="x-mdma-session-expires",L=class{constructor(e={}){this.session=null;this.baseUrl=(e.cloudBaseUrl||Ar).replace(/\/+$/,""),this.onSession=e.onSession;let t=e.fetch;this.fetchImpl=t?(r,s)=>t(r,s):(r,s)=>globalThis.fetch(r,s),this.timeoutMs=e.timeoutMs??1e4,this.routingCredential=e.routingCredential,e.sessionToken&&(this.session={sessionToken:e.sessionToken,expiresAt:0,user:{email:""}})}getSession(){return this.session}isSignedIn(){return this.session!==null}async signInWithGoogle(e){let t=await this.request("POST","/api/auth/google",{body:{id_token:e},anonymous:!0});return this.adoptSession(t)}async signInWithAccount(e){let t=await G(e),{nonce:r}=await this.getAccountLoginChallenge();return this.submitAccountLogin({rootPublicKey:t.publicKey,nonce:r,signature:await Se({rootSecret:e,nonce:r})})}async getAccountLoginChallenge(){let e=await this.request("GET","/api/auth/account/challenge",{anonymous:!0});return{nonce:e.nonce,expiresAtMs:e.expires_at_ms}}async submitAccountLogin(e){let t=await this.request("POST","/api/auth/account",{body:{root_public_key:e.rootPublicKey,nonce:e.nonce,signature:e.signature},anonymous:!0});return this.adoptSession(t)}async refreshSession(){let e=await this.request("POST","/api/auth/refresh");return this.adoptSession(e)}async signOut(){try{this.session&&await this.request("POST","/api/auth/logout",{parse:"none"})}finally{this.session=null,this.notifySession(null)}}async getMyNamespaces(){return(await this.request("GET","/api/cloud/me/namespaces")).map(t=>({namespaceId:String(t.namespace_id??t.group_id??""),groupId:String(t.group_id??t.namespace_id??""),haStatus:String(t.ha_status??"none"),haEnabledAt:t.ha_enabled_at??null,contexts:Array.isArray(t.contexts)?t.contexts:[],fleetReplicas:t.fleet_replicas}))}async getNamespaceRelays(e){return((await this.request("GET",`/api/cloud/me/namespaces/${encodeURIComponent(e)}/relays`)).relays??[]).map(r=>{let s=r.relay_url??null,i=r.executor_account??null,a=r.authorship_ready===!0;return{peerId:String(r.peer_id??""),relayUrl:s,executorAccount:i,status:String(r.status??""),authorshipReady:a,teeRole:tt(r),canExecute:et(r,a&&s!==null&&i!==null),lastSeenAt:r.last_seen_at??null,confirmedAt:r.confirmed_at??null}})}async findExecutingRelay(e){let t=await this.getNamespaceRelays(e);return nt(t)}async getNamespaceRouting(e){let t=await this.request("GET",`/api/cloud/namespaces/${encodeURIComponent(e)}/admitters`,{anonymous:!0,headers:await this.routingProof(e)});return{namespaceId:String(t.namespace_id??e),nodes:(t.admitters??[]).map(r=>{let s=r.account??null,i=r.relay_url??null,a=r.fresh===!0,o=r.authorship_ready===!0;return{peerId:String(r.peer_id??""),account:s,relayUrl:i,admitUrl:r.admit_url??null,status:String(r.status??""),fresh:a,canAdmit:r.can_admit===!0,authorshipReady:o,teeRole:tt(r),canExecute:et(r,o&&i!==null&&s!==null&&a)}}),servable:t.servable===!0,writable:t.writable===!0}}async getRoutingChallenge(e){let t=await this.request("GET",`/api/cloud/namespaces/${encodeURIComponent(e)}/challenge`,{anonymous:!0});return{namespaceId:String(t.namespace_id??e),nonce:String(t.nonce??""),expiresAtMs:Number(t.expires_at_ms??0)}}async routingProof(e){if(!this.routingCredential)return;let t=await this.getRoutingChallenge(e);return Te(t,this.routingCredential)}async getAccountRelaysChallenge(e){let t=await this.request("GET",`/api/cloud/accounts/${encodeURIComponent(e)}/challenge`,{anonymous:!0});return{accountId:String(t.account_id??e),nonce:String(t.nonce??""),expiresAtMs:Number(t.expires_at_ms??0)}}async getAccountRelays(e){if(!this.routingCredential)throw new Error("getAccountRelays needs a routingCredential: construct the client with the device certificate and its signing secret, since this read is proven by the device, not by a session.");let t=await this.getAccountRelaysChallenge(e);return((await this.request("GET",`/api/cloud/accounts/${encodeURIComponent(e)}/relays`,{anonymous:!0,headers:await Te(t,this.routingCredential)})).relays??[]).map(s=>({peerId:String(s.peer_id??""),relayUrl:s.relay_url??null,fresh:s.fresh===!0}))}async findAdmitter(e,t){let{nodes:r}=await this.getNamespaceRouting(e),s=t&&t.length>0?new Set(t.map(i=>i.toLowerCase())):null;return r.find(i=>i.canAdmit&&(s===null||i.account!==null&&s.has(i.account.toLowerCase())))??null}async getMyMachines(){return((await this.request("GET","/api/cloud/me/machines")).machines??[]).map(t=>{let r=t.relay_url??null,s=t.executor_account??null,i=(Array.isArray(t.namespaces)?t.namespaces:[]).map(a=>{let o=a;return{namespaceId:String(o.namespace_id??""),status:String(o.status??""),authorshipReady:o.authorship_ready===!0,teeRole:tt(o),fresh:o.fresh===!0,confirmedAt:o.confirmed_at??null,lastSeenAt:o.last_seen_at??null}});return{peerId:String(t.peer_id??""),relayUrl:r,executorAccount:s,canExecute:et(t,r!==null&&s!==null&&i.some(a=>a.authorshipReady)),namespaces:i}})}async claimNamespace(e,t){await this.request("POST","/api/cloud/namespaces/claim",{body:{namespace_id:e,ownership_proof:t}})}async enableNamespaceHa(e,t={}){let r=t.admitterAddrs===void 0?{}:{admitter_addrs:[...t.admitterAddrs]};return this.request("POST",`/api/cloud/namespaces/${encodeURIComponent(e)}/enable-ha`,{body:r})}async disableNamespaceHa(e){return this.request("POST",`/api/cloud/namespaces/${encodeURIComponent(e)}/disable-ha`,{body:{}})}async getMyNode(){return(await this.request("GET","/api/cloud/me/node")).node??null}enableHA(e){let t=new URLSearchParams({group_id:e.groupId,context_id:e.contextId});e.redirectUrl&&t.set("redirect_url",e.redirectUrl),window.open(`${this.baseUrl}/enable-ha?${t.toString()}`)}disableHA(e){let t=new URLSearchParams({group_id:e.groupId,context_id:e.contextId});e.redirectUrl&&t.set("redirect_url",e.redirectUrl),window.open(`${this.baseUrl}/disable-ha?${t.toString()}`)}async linkAccount(e){let t=await G(e),{nonce:r}=await this.getAccountLinkChallenge();return this.submitAccountLink({accountId:t.accountId,rootPublicKey:t.publicKey,nonce:r,signature:await ae({rootSecret:e,nonce:r})})}async getAccountLinkChallenge(){let e=await this.request("GET","/api/cloud/me/accounts/challenge");return{nonce:e.nonce,expiresAtMs:e.expires_at_ms??null}}async submitAccountLink(e){let t=await this.request("POST","/api/cloud/me/accounts",{body:{account_id:e.accountId,root_public_key:e.rootPublicKey,nonce:e.nonce,signature:e.signature}});return{accountId:String(t.account_id??e.accountId),linkedAt:t.linked_at??null,alreadyLinked:t.already_linked===!0}}static accountLinkHandoff(e){let t=e.portalUrl.replace(/\/+$/,""),r=new URLSearchParams({"link-account":e.accountId,"callback-url":e.callbackUrl});return{url:`${t}/?${r.toString()}`,accountId:e.accountId}}static readAccountLinkCallback(e){let t=e??(typeof window>"u"?"":window.location.href);if(!t)return{};let r;try{r=new URL(t).hash}catch{return{}}let s=new URLSearchParams(r.replace(/^#/,"")),i=s.get("grant"),a=s.get("account"),o=s.get("error"),c={};return i&&(c.grant=i),a&&(c.accountId=a),o&&(c.error=o),c}async linkAccountWithGrant(e){let t=await G(e.rootSecret),r=await this.request("POST","/api/cloud/accounts/link",{body:{grant:e.grant,root_public_key:t.publicKey,signature:await ae({rootSecret:e.rootSecret,nonce:e.grant})},anonymous:!0});return{accountId:String(r.account_id??t.accountId),linkedAt:r.linked_at??null,alreadyLinked:r.already_linked===!0}}async getMyAccounts(){let e=await this.request("GET","/api/cloud/me/accounts");return{accounts:(e.accounts??[]).map(t=>({accountId:String(t.account_id??""),linkedAt:t.linked_at??null,hasRecoveryEnvelope:t.has_recovery_envelope===!0})),limit:e.limit??null}}adoptSession(e){return this.session={sessionToken:e.session_token,expiresAt:e.expires_at,user:e.user},this.notifySession(this.session),this.session}async notifySession(e){try{await this.onSession?.(e)}catch{}}async request(e,t,r={}){if(!r.anonymous&&!this.session)throw new Error("Not signed in to Calimero Cloud: call signInWithGoogle() or construct the client with a sessionToken");let s={...r.headers,Accept:"application/json"};r.body!==void 0&&(s["Content-Type"]="application/json"),!r.anonymous&&this.session&&(s.Authorization=`Bearer ${this.session.sessionToken}`);let i=new AbortController,a=setTimeout(()=>i.abort(),this.timeoutMs),o;try{o=await this.fetchImpl(`${this.baseUrl}${t}`,{method:e,headers:s,body:r.body===void 0?void 0:JSON.stringify(r.body),signal:i.signal})}catch(c){throw new S(0,c instanceof Error?c.name:"NetworkError",`${this.baseUrl}${t}`,new Headers,c instanceof Error?c.message:String(c))}finally{clearTimeout(a)}if(this.adoptRollingRefresh(o),!o.ok){let c=await o.text().catch(()=>{});throw new S(o.status,o.statusText,`${this.baseUrl}${t}`,o.headers,c)}if(!(r.parse==="none"||o.status===204))return await o.json()}adoptRollingRefresh(e){let t=e.headers.get(Rr);if(!t||!this.session)return;let r=e.headers.get(xr),s=Number(r);this.session={sessionToken:t,expiresAt:Number.isFinite(s)&&s>0?s:this.session.expiresAt,user:this.session.user},this.notifySession(this.session)}};function Cr(n){let e=n&&typeof n=="object"?n.data??n:void 0;if(!e||typeof e!="object")return[];let t=e.localPeerId,r=e.externalAddrs;if(typeof t!="string"||!t||!Array.isArray(r))return[];let s=`/p2p/${t}`,i=[];for(let a of r){if(typeof a!="string")continue;let o=a.trim();if(!o.startsWith("/"))continue;let c=o.endsWith(s)?o:`${o}${s}`;i.includes(c)||i.push(c)}return i}function vr(n){if(n)return typeof n=="function"?n:Re(n)}var kr="mero-js:token-refresh";function Sr(){let e=globalThis.navigator?.locks;return e&&typeof e.request=="function"?e:null}function rt(n,e){try{let t=n.split(".");if(t.length===3){let r=t[1].replace(/-/g,"+").replace(/_/g,"/");for(;r.length%4;)r+="=";let s=JSON.parse(atob(r));if(typeof s.exp=="number")return s.exp*1e3}}catch{}return e}var Ee=class{constructor(e){this.tokenData=null;this.refreshPromise=null;this.rpcClient=null;this.sseClient=null;this.wsClient=null;this.wsWarned=!1;this.config={timeoutMs:1e4,...e},this.tokenStore=e.tokenStore??null,this.tokenStore&&(this.tokenData=this.tokenStore.getTokens());let t=typeof window<"u"&&"__TAURI_INTERNALS__"in window,r=vr(this.config.requestProof);this.httpClient=F({baseUrl:this.config.baseUrl,fetch:this.config.fetch,getProof:r,getAuthToken:async()=>(await this.getValidToken())?.access_token||"",refreshToken:async()=>(await this.performTokenRefresh()).access_token,onTokenRefresh:async s=>{this.tokenData&&(this.tokenData.access_token=s,this.tokenStore?.setTokens(this.tokenData))},onAuthRevoked:async()=>{this.clearToken();try{await this.config.onAuthRevoked?.()}catch{}},timeoutMs:this.config.timeoutMs,credentials:this.config.requestCredentials??(t?"omit":void 0)}),this.authClient=kt(this.httpClient,{baseUrl:this.config.baseUrl,getAuthToken:async()=>(await this.getValidToken())?.access_token||"",timeoutMs:this.config.timeoutMs}),this.adminClient=It(this.httpClient,{baseUrl:this.config.baseUrl,getAuthToken:async()=>(await this.getValidToken())?.access_token||"",timeoutMs:this.config.timeoutMs})}get auth(){return this.authClient}get admin(){return this.adminClient}get rpc(){return this.rpcClient||(this.rpcClient=new se({httpClient:this.httpClient})),this.rpcClient}get events(){return this.sseClient||(this.sseClient=new W({baseUrl:this.config.baseUrl,fetch:this.config.fetch,getAuthToken:async()=>(await this.getValidToken())?.access_token||""})),this.sseClient}get ephemeral(){return this.ephemeralClient||(this.ephemeralClient=new ie({httpClient:this.httpClient,sse:this.events})),this.ephemeralClient}get cloud(){return this.cloudClient||(this.cloudClient=new L(this.config.cloud??{})),this.cloudClient}get ws(){return this.wsWarned||(this.wsWarned=!0,console.warn("[mero-js] WsClient is experimental. Use mero.events (SSE) for production.")),this.wsClient||(this.wsClient=new J({baseUrl:this.config.baseUrl,getAuthToken:async()=>(await this.getValidToken())?.access_token||""})),this.wsClient}async authenticate(e){let t=e||this.config.credentials;if(!t)throw new Error("No credentials provided for authentication");try{let r={auth_method:"user_password",public_key:t.username,client_name:"mero-js-sdk",permissions:["admin"],timestamp:Math.floor(Date.now()/1e3),provider_data:{username:t.username,password:t.password}},s=await this.authClient.generateTokens(r),i=s.data.access_token;return this.tokenData={access_token:i,refresh_token:s.data.refresh_token,expires_at:rt(i,Date.now()+36e5)},this.tokenStore?.setTokens(this.tokenData),this.tokenData}catch(r){throw new Error(`Authentication failed: ${r instanceof Error?r.message:"Unknown error"}`)}}async getValidToken(){return this.tokenData}async performTokenRefresh(){if(this.refreshPromise)return this.refreshPromise;let e=this.tokenData?.access_token,t=this.withRefreshLock(()=>this.performTokenRefreshLocked(e));this.refreshPromise=t;try{return await t}finally{this.refreshPromise===t&&(this.refreshPromise=null)}}withRefreshLock(e){let t=Sr();return t?t.request(kr,e):e()}async performTokenRefreshLocked(e){let t=this.tokenStore?.getTokens()??null;if(t?.refresh_token&&(this.tokenData=t,e&&t.access_token!==e))return t;if(!this.tokenData?.refresh_token)throw new Error("No refresh token available");try{let r=await this.authClient.refreshToken({access_token:this.tokenData.access_token,refresh_token:this.tokenData.refresh_token}),s=r.data.access_token;return this.tokenData={access_token:s,refresh_token:r.data.refresh_token,expires_at:rt(s,Date.now()+36e5)},this.tokenStore?.setTokens(this.tokenData),this.tokenData}catch(r){throw new Error(`Token refresh failed: ${r instanceof Error?r.message:"Unknown error"}`)}}clearToken(){this.refreshPromise=null,this.tokenData=null,this.tokenStore?.clear()}isAuthenticated(){return this.tokenData!==null}setTokenData(e){let t=e.expires_at||rt(e.access_token,Date.now()+36e5);this.tokenData={...e,expires_at:t},this.tokenStore?.setTokens(this.tokenData)}getTokenData(){return this.tokenData}close(){this.sseClient?.close(),this.wsClient?.close()}static parseAuthCallback(e){return Fe(e)}static buildAuthLoginUrl(e,t){return We(e,t)}};function Tr(n){return new Ee(n)}var st=class{constructor(){this.tokens=null}getTokens(){return this.tokens}setTokens(e){this.tokens=e}clear(){this.tokens=null}},Er="mero-tokens",it=class{constructor(e=Er){this.key=e}getTokens(){try{if(typeof localStorage>"u")return null;let e=localStorage.getItem(this.key);if(!e)return null;let t=JSON.parse(e);return t&&t.access_token&&t.refresh_token?{access_token:t.access_token,refresh_token:t.refresh_token,expires_at:typeof t.expires_at=="number"?t.expires_at:Date.now()+36e5}:null}catch{return null}}setTokens(e){try{if(typeof localStorage>"u")return;localStorage.setItem(this.key,JSON.stringify(e))}catch{}}clear(){try{if(typeof localStorage>"u")return;localStorage.removeItem(this.key)}catch{}}};var Ir=new TextEncoder().encode("calimero.warrant.v2"),Pr=new TextEncoder().encode("calimero.warrant.intent.v1"),Ft=64,Dr="00".repeat(32);async function ot(n,e){let t=new TextEncoder().encode(JSON.stringify(e));return I(Pr,[new TextEncoder().encode(n),t])}async function Ie(n){let e=b(n.context,"context",32),t=b(n.authorAccount,"authorAccount",32),r=b(n.executor,"executor",32),s=b(n.appVersion??Dr,"appVersion",32),i=ce(n.accountHeads,"accountHeads"),a=ce(n.governanceFloor,"governanceFloor"),o=await T(n.deviceSecret),c=await E(n.deviceSecret),u=new TextEncoder().encode(n.method),d=await ot(n.method,n.argsJson),l=w(n.nonce),g=w(n.notAfter),f=await I(Ir,[e,t,c,r,s,u,d,w(i.length),...i,w(a.length),...a,l,g]),m=new Uint8Array(await crypto.subtle.sign({name:"Ed25519"},o,f));return y(h(e,t,c,r,s,R(u.length),u,d,R(i.length),...i,R(a.length),...a,l,g,m))}function ce(n,e){let t=n??[];if(t.length>Ft)throw new Error(`${e} cites ${t.length} heads, over the ${Ft} a node accepts`);return t.map((r,s)=>b(r,`${e}[${s}]`,32))}var Ur=new TextEncoder().encode("calimero.context-creation-warrant.v1"),Mr=new TextEncoder().encode("calimero.context-creation.init.v1"),Pe=256;async function at(n){let e=new TextEncoder().encode(JSON.stringify(n));return I(Mr,[e])}async function Nr(n){return I(Ur,[n.group,n.seed,n.authorAccount,n.deviceKey,n.executor,n.applicationId,new Uint8Array([n.serviceName===null?0:1]),n.serviceName??new Uint8Array(0),new Uint8Array([n.name===null?0:1]),n.name??new Uint8Array(0),n.initHash,w(n.accountHeads.length),...n.accountHeads,w(n.governanceFloor.length),...n.governanceFloor,w(n.nonce),w(n.notAfter)])}async function De(n){let e=b(n.group,"group",32),t=n.seed===void 0?crypto.getRandomValues(new Uint8Array(32)):b(n.seed,"seed",32),r=b(n.authorAccount,"authorAccount",32),s=b(n.executor,"executor",32),i=b(n.applicationId,"applicationId",32),a=Wt(n.serviceName,"serviceName"),o=Wt(n.name,"name"),c=ce(n.accountHeads,"accountHeads"),u=ce(n.governanceFloor,"governanceFloor"),d=await T(n.deviceSecret),l=await E(n.deviceSecret),g=await at(n.initArgs),f=await Nr({group:e,seed:t,authorAccount:r,deviceKey:l,executor:s,applicationId:i,serviceName:a,name:o,initHash:g,accountHeads:c,governanceFloor:u,nonce:n.nonce,notAfter:n.notAfter}),m=new Uint8Array(await crypto.subtle.sign({name:"Ed25519"},d,f));return{warrant:y(h(e,t,r,l,s,i,Jt(a),Jt(o),g,R(c.length),...c,R(u.length),...u,w(n.nonce),w(n.notAfter),m)),seed:y(t)}}function zt(n){let e=n.trim();if(!/^[0-9a-fA-F]*$/.test(e)||e.length%2!==0)throw new Error("creation warrant must be an even number of hex characters");let t=new Uint8Array(e.length/2);for(let l=0;l<t.length;l+=1)t[l]=Number.parseInt(e.slice(l*2,l*2+2),16);let r=new DataView(t.buffer),s=0,i=(l,g)=>{if(s+l>t.length)throw new Error(`creation warrant ends inside ${g} (${t.length} bytes)`);let f=t.subarray(s,s+l);return s+=l,f},a=l=>r.getUint32(i(4,l).byteOffset,!0),o=l=>r.getBigUint64(i(8,l).byteOffset,!0),c=l=>{let g=i(1,l)[0];if(g===0)return null;if(g!==1)throw new Error(`${l} has option tag ${g}, expected 0 or 1`);let f=a(l);if(f>Pe)throw new Error(`${l} is ${f} bytes, over the ${Pe} a node accepts`);return new TextDecoder("utf-8",{fatal:!0}).decode(i(f,l))},u=l=>{let g=a(l);if(g>64)throw new Error(`${l} cites ${g} heads, over the 64 a node accepts`);return Array.from({length:g},()=>y(i(32,l)))},d={group:y(i(32,"group")),seed:y(i(32,"seed")),authorAccount:y(i(32,"authorAccount")),deviceKey:y(i(32,"deviceKey")),executor:y(i(32,"executor")),applicationId:y(i(32,"applicationId")),serviceName:c("serviceName"),name:c("name"),initHash:y(i(32,"initHash")),accountHeads:u("accountHeads"),governanceFloor:u("governanceFloor"),nonce:o("nonce"),notAfter:o("notAfter"),signature:y(i(64,"signature"))};if(s!==t.length)throw new Error(`creation warrant is ${t.length} bytes but its fields account for ${s}`);return d}function Wt(n,e){if(n===void 0)return null;let t=new TextEncoder().encode(n);if(t.length>Pe)throw new Error(`${e} is ${t.length} UTF-8 bytes, over the ${Pe} a node accepts`);return t}function Jt(n){return n===null?new Uint8Array([0]):h(new Uint8Array([1]),R(n.length),n)}var Qt=300,z=class extends Error{constructor(t,r,s){super(`relay refused the intent (HTTP ${s}): ${t}`);this.reason=t;this.retryable=r;this.status=s;this.name="IntentRefusedError"}},le=class{constructor(e){this.config=e,this.baseUrl=e.relayUrl.replace(/\/+$/,""),this.executorAccount=e.executorAccount;let t=e.fetch;this.fetchImpl=t?(r,s)=>t(r,s):(r,s)=>globalThis.fetch(r,s),this.timeoutMs=e.timeoutMs??1e4}async describe(e){let t=await this.json("GET",`/admin-api/contexts/${encodeURIComponent(e)}/intents`);return this.executorAccount=t.data.executorAccount,t.data}async execute(e,t,r={}){let s=this.executorAccount??(await this.describe(e)).executorAccount,i=await this.config.nonces.next(),a=this.config.ttlSeconds??Qt,o=await Ie({context:e,authorAccount:this.config.authorAccount,executor:s,method:t,argsJson:r,nonce:i,notAfter:BigInt(Math.floor(Date.now()/1e3))+BigInt(a),deviceSecret:this.config.deviceSecret}),c=await this.json("POST",`/admin-api/contexts/${encodeURIComponent(e)}/intents`,{method:t,argsJson:r,warrant:o,authorProof:this.config.authorProof});return{rootHash:c.data.rootHash,returns:c.data.returns??null}}async describeCreation(e,t={}){let r=t.author?`?author=${encodeURIComponent(t.author)}`:"";return(await this.json("GET",`/admin-api/groups/${encodeURIComponent(e)}/context-intents${r}`)).data}async createContext(e){let t=await this.describeCreation(e.groupId,{author:this.config.authorAccount});if(!t.canCreateOnBehalf)throw new z(`the relay (${t.executorAccount}) has no standing to act for members of group ${t.groupId}; an admin must admit it as a relay or grant it CAN_AUTHOR_ON_BEHALF (checked before signing \u2014 no nonce was spent)`,!1,403);if(t.authorMayCreate===!1)throw new z(`the author (${this.config.authorAccount}) may not create contexts in group ${t.groupId}; it needs CAN_CREATE_CONTEXT or admin (checked before signing \u2014 no nonce was spent)`,!1,403);let r=this.config.executorAccount;if(r&&r.toLowerCase()!==t.executorAccount.toLowerCase())throw new Error(`configured executorAccount ${r} is not the relay's account ${t.executorAccount}; a warrant naming it would be unspendable`);let s=t.executorAccount;this.executorAccount=s;let i=e.initArgs??{},a=await this.config.nonces.next(),o=this.config.ttlSeconds??Qt,{warrant:c}=await De({group:e.groupId,seed:e.seed,authorAccount:this.config.authorAccount,executor:s,applicationId:e.applicationId,serviceName:e.serviceName,name:e.name,initArgs:i,nonce:a,notAfter:BigInt(Math.floor(Date.now()/1e3))+BigInt(o),deviceSecret:this.config.deviceSecret}),u=await this.json("POST",`/admin-api/groups/${encodeURIComponent(e.groupId)}/context-intents`,{warrant:c,authorProof:this.config.authorProof,initArgs:i});return{contextId:u.data.contextId,groupId:u.data.groupId,memberPublicKey:u.data.memberPublicKey}}async json(e,t,r){let s={Accept:"application/json"};r!==void 0&&(s["Content-Type"]="application/json");let i=new AbortController,a=setTimeout(()=>i.abort(),this.timeoutMs),o;try{o=await this.fetchImpl(`${this.baseUrl}${t}`,{method:e,headers:s,body:r===void 0?void 0:JSON.stringify(r),signal:i.signal})}catch(c){throw new S(0,c instanceof Error?c.name:"NetworkError",`${this.baseUrl}${t}`,new Headers,c instanceof Error?c.message:String(c))}finally{clearTimeout(a)}if(!o.ok){let c=await o.text().catch(()=>"");if(o.status===400||o.status===403){let u=_r(c);throw new z(u,/nonce/i.test(u),o.status)}throw new S(o.status,o.statusText,`${this.baseUrl}${t}`,o.headers,c)}return await o.json()}};function _r(n){if(!n)return"no reason given";try{let e=JSON.parse(n);for(let t of[e.error,e.message])if(typeof t=="string"&&t)return t}catch{}return n}function ct(n=1){let e=BigInt(n);if(e<1n)throw new Error("nonce sequence starts at 1");return{next(){let t=e;return e+=1n,Promise.resolve(t)}}}function lt(n,e){let t=e??globalThis.localStorage;if(!t)throw new Error("no localStorage in this runtime: pass a storage implementation, or use createMemoryNonceSource for a process that owns its whole sequence");return{next(){let r=t.getItem(n),s=0n;if(r!==null&&r!=="")try{let a=BigInt(r);a>0n&&(s=a)}catch{s=0n}let i=s+1n;return t.setItem(n,i.toString()),Promise.resolve(i)}}}var Hr="Noise_NK_25519_AESGCM_SHA256",ut=32,qr=16,Or=new Uint8Array([48,46,2,1,0,48,5,6,3,43,101,110,4,34,4,32]),pt=class n{constructor(e){this.k=null;this.n=0;this.ck=e,this.h=e}static async initialize(){let e=new TextEncoder().encode(Hr),t=new Uint8Array(32);return t.set(e),new n(t)}async mixHash(e){this.h=await Gr(h(this.h,e))}async mixKey(e){let[t,r]=await Zt(this.ck,e);this.ck=t,this.k=await crypto.subtle.importKey("raw",r,"AES-GCM",!1,["encrypt","decrypt"]),this.n=0}async encryptAndHash(e){if(!this.k)throw new Error("Noise: no cipher key yet");let t=new Uint8Array(await crypto.subtle.encrypt({name:"AES-GCM",iv:Xt(this.n),additionalData:this.h},this.k,e));return this.n+=1,await this.mixHash(t),t}async decryptAndHash(e){if(!this.k)throw new Error("Noise: no cipher key yet");let t=new Uint8Array(await crypto.subtle.decrypt({name:"AES-GCM",iv:Xt(this.n),additionalData:this.h},this.k,e));return this.n+=1,await this.mixHash(e),t}split(){return Zt(this.ck,new Uint8Array)}};async function en(n,e,t){let r=await pt.initialize();await r.mixHash(e),await r.mixHash(n);let s=await $r(t);return await r.mixHash(s.publicKey),await r.mixKey(await Yt(s.privateKey,n)),{message1:h(s.publicKey,await r.encryptAndHash(new Uint8Array)),async finish(a){if(a.length<ut+qr)throw new Error("Noise: message 2 is too short");let o=a.slice(0,ut);return await r.mixHash(o),await r.mixKey(await Yt(s.privateKey,o)),{payload:await r.decryptAndHash(a.slice(ut)),split:await r.split()}}}}async function $r(n){if(!n){let r;try{r=await crypto.subtle.generateKey({name:"X25519"},!0,["deriveBits"])}catch(i){throw new Error(`Sealed transport needs WebCrypto with X25519, which this runtime lacks (${i?.name??"no crypto.subtle"}). Current browsers and Node have it; in React Native, install react-native-quick-crypto as the global crypto.`)}let s=new Uint8Array(await crypto.subtle.exportKey("raw",r.publicKey));return{privateKey:r.privateKey,publicKey:s}}let e=await crypto.subtle.importKey("pkcs8",h(Or,n),{name:"X25519"},!0,["deriveBits"]),t=await crypto.subtle.exportKey("jwk",e);return{privateKey:e,publicKey:Lr(t.x)}}async function Yt(n,e){let t=await crypto.subtle.importKey("raw",e,{name:"X25519"},!1,[]);return new Uint8Array(await crypto.subtle.deriveBits({name:"X25519",public:t},n,256))}function Xt(n){let e=new Uint8Array(12);return new DataView(e.buffer).setBigUint64(4,BigInt(n),!1),e}async function Gr(n){return new Uint8Array(await crypto.subtle.digest("SHA-256",n))}async function dt(n,e){let t=await crypto.subtle.importKey("raw",n,{name:"HMAC",hash:"SHA-256"},!1,["sign"]);return new Uint8Array(await crypto.subtle.sign("HMAC",t,e))}async function Zt(n,e){let t=await dt(n,e),r=await dt(t,new Uint8Array([1])),s=await dt(t,h(r,new Uint8Array([2])));return[r,s]}function Lr(n){let e=n.replace(/-/g,"+").replace(/_/g,"/"),t=atob(e+"=".repeat((4-e.length%4)%4));return Uint8Array.from(t,r=>r.charCodeAt(0))}var sn="/sealed/v2/handshake",on="/sealed/v2",de="application/vnd.calimero.sealed",gt=2,Br=new TextEncoder().encode("calimero/sealed-http/v2"),ue=16,Kr=1+ue+8,jr=0,Vr=1,Fr=2,Wr=1+64*1024+16,Jr=1024,zr=6e4,Qr="calimero.tee-attest.transport-key.v1",Yr=[101,204,205,304],an=new TextEncoder,pe=class extends Error{constructor(){super("The node no longer holds the attested transport key; attest again for the current one");this.name="StaleTransportKeyError"}},Q=class extends Error{constructor(t,r,s,i){super(s);this.status=t;this.code=r;this.retryAfterMs=i;this.name="SealedTransportError"}};async function cn(n,e){let t=await crypto.subtle.digest("SHA-256",h(an.encode(Qr),n,e));return new Uint8Array(t)}async function ln(n,e,t={}){if(t.applicationId===void 0!=(t.applicationHash===void 0))throw new Error("applicationId and applicationHash are given together or not at all");let r=y(crypto.getRandomValues(new Uint8Array(32))),s=await n.teeAttest({nonce:r,applicationId:t.applicationId,bindTransportKey:!0,...e.includeCollateral?{includeCollateral:!0}:{}});if(!s.transportPublicKey)throw new Error("The node did not report a transport key; it predates sealed transport");let i=b(s.transportPublicKey,"transportPublicKey",32),a=t.applicationHash?b(t.applicationHash,"applicationHash",32):new Uint8Array(32),o=y(await cn(a,i));if(!await e({quoteB64:s.quoteB64,nonce:r,reportDataSuffix:o,collateral:s.collateral}))throw new Error("The attestation did not verify, so its transport key is not trusted");return i}function ge(n){let e=n.fetch??((s,i)=>fetch(s,i)),t=`${n.baseUrl.replace(/\/+$/,"")}/admin-api/tee/attest`,r={async teeAttest(s){let i=await e(t,{method:"POST",headers:{"content-type":"application/json"},body:JSON.stringify(s)});if(!i.ok)throw new Error(`The node refused to attest: HTTP ${i.status}`);let a=await i.json();if(!a.data)throw new Error("The attestation response is malformed");return a.data}};return un({baseUrl:n.baseUrl,transportPublicKey:()=>ln(r,n.verify,{applicationId:n.applicationId,applicationHash:n.applicationHash}),fetch:e})}function un(n){let e=new URL(n.baseUrl),t=e.pathname.replace(/\/+$/,""),r=`${e.origin}${t}`,s=n.fetch??((l,g)=>fetch(l,g)),i=typeof n.transportPublicKey=="function"?n.transportPublicKey:void 0,a=i?Promise.resolve().then(i):Promise.resolve(n.transportPublicKey);a.catch(()=>{});let o,c=async()=>{try{return await nn(s,r,await tn(a))}catch(l){if(!(l instanceof pe)||!i)throw l;return a=Promise.resolve().then(i),nn(s,r,await tn(a))}},u=async()=>{let l=o??d(void 0),g=await l;if(Date.now()<g.renewAt)return[g,l];let f=d(l);return[await f,f]},d=l=>{if(o===l){let g=c();o=g,g.catch(()=>{o===g&&(o=void 0)})}return o};return async(l,g)=>{let f=new Request(l,g),m=new URL(f.url);if(m.origin!==e.origin||!m.pathname.startsWith(`${t}/`))throw new Error(`Refusing to send ${m.href} outside the sealed node at ${n.baseUrl}`);let A=[];f.headers.forEach((D,N)=>A.push([N,D]));let k={method:f.method,path:m.pathname.slice(t.length)+m.search,headers:A},C=new Uint8Array(await f.arrayBuffer()),v=async D=>{let{requestId:N,header:B,envelope:K}=await Zr(D,k,C),j=await s(`${r}${on}`,{method:"POST",headers:{"content-type":de},body:K,signal:f.signal});if(j.headers.get("content-type")!==de)throw await pn(j);return ts(j,D.responseKey,B,N)},[P,q]=await u();try{return await v(P)}catch(D){if(!(D instanceof Q)||D.code!=="unknown_session")throw D;return v(await d(q))}}}async function tn(n){let e=await n;if(e.length!==32)throw new Error("transportPublicKey must be 32 bytes");return e}async function nn(n,e,t){try{return await rn(n,e,t)}catch(r){if(!(r instanceof Q)||r.code!=="busy")throw r;return await new Promise(s=>setTimeout(s,r.retryAfterMs??1e3)),rn(n,e,t)}}async function rn(n,e,t){let r=await en(t,Br),s=await n(`${e}${sn}`,{method:"POST",headers:{"content-type":de},body:h(new Uint8Array([gt]),t,r.message1)});if(s.headers.get("content-type")!==de)throw await pn(s);let i=await rs(s,Jr);return Xr(r,i)}async function Xr(n,e){if(e[0]!==gt)throw new Error("The handshake reply is malformed");let t;try{t=await n.finish(e.slice(1))}catch{throw new Error("The handshake reply did not open: it did not come from the attested node")}let{payload:r,split:s}=t;if(r.length!==ue+4)throw new Error("The handshake reply is malformed");let i=r.slice(0,ue),a=new DataView(r.buffer,r.byteOffset).getUint32(ue,!1),[o,c]=await Promise.all([crypto.subtle.importKey("raw",s[0],"AES-GCM",!1,["encrypt"]),crypto.subtle.importKey("raw",s[1],"AES-GCM",!1,["decrypt"])]);return s[0].fill(0),s[1].fill(0),{requestKey:o,responseKey:c,nextRequestId:1,renewAt:Date.now()+a*1e3-zr,header(u){let d=new Uint8Array(Kr);return d[0]=gt,d.set(i,1),new DataView(d.buffer).setBigUint64(1+ue,BigInt(u),!1),d}}}async function Zr(n,e,t){let r=n.nextRequestId;n.nextRequestId+=1;let s=n.header(r),i=await crypto.subtle.encrypt({name:"AES-GCM",iv:dn(r,0),additionalData:s},n.requestKey,es(e,t));return{requestId:r,header:s,envelope:h(s,new Uint8Array(i))}}function dn(n,e){let t=new Uint8Array(12),r=new DataView(t.buffer);return r.setBigUint64(0,BigInt(n),!1),r.setUint32(8,e,!1),t}function es(n,e){let t=an.encode(JSON.stringify(n)),r=new Uint8Array(4);return new DataView(r.buffer).setUint32(0,t.length,!1),h(r,t,e)}async function ts(n,e,t,r){if(!n.body)throw new Error("The sealed response has no body");let s=ns(n.body.getReader()),i=0,a=async()=>{let d=await s.next();if(!d)return null;let l;try{l=new Uint8Array(await crypto.subtle.decrypt({name:"AES-GCM",iv:dn(r,i),additionalData:t},e,d))}catch{throw new Error("A sealed response frame did not open: it was not sealed by the attested node")}return i+=1,{kind:l[0],data:l.slice(1)}},o=await a();if(!o||o.kind!==jr)throw new Error("The sealed response is malformed");let c=JSON.parse(new TextDecoder().decode(o.data));if(Yr.includes(c.status))return await s.cancel(),new Response(null,{status:c.status,headers:c.headers});let u=new ReadableStream({async pull(d){try{let l=await a();l?l.kind===Vr?d.enqueue(l.data):l.kind===Fr?(d.close(),await s.cancel()):d.error(new Error("The sealed response is malformed")):d.error(new Error("The sealed response was cut short"))}catch(l){d.error(l)}},cancel(){return s.cancel()}});return new Response(u,{status:c.status,headers:c.headers})}function ns(n){let e=new Uint8Array,t=async r=>{for(;e.length<r;){let{done:s,value:i}=await n.read();if(s)return!1;e=h(e,i)}return!0};return{async next(){if(!await t(4))return null;let r=new DataView(e.buffer,e.byteOffset).getUint32(0,!1);if(r>Wr)throw await n.cancel(),new Error("The sealed response is malformed: a frame is larger than the node sends");if(!await t(4+r))return null;let s=e.slice(4,4+r);return e=e.slice(4+r),s},cancel:()=>n.cancel()}}async function rs(n,e){if(!n.body)return new Uint8Array;let t=n.body.getReader(),r=new Uint8Array;for(;;){let{done:s,value:i}=await t.read();if(s)return r;if(r=h(r,i),r.length>e)throw await t.cancel(),new Error("The handshake reply is malformed: it is too long")}}async function pn(n){let e,t=`Sealed request refused: HTTP ${n.status}`;try{let i=await n.json();e=i?.error?.code,i?.error?.message&&(t=`Sealed request refused: ${i.error.message}`)}catch{}if(e==="stale_transport_key")return new pe;let r=Number(n.headers.get("retry-after")),s=Number.isFinite(r)&&r>0?Math.min(r,5)*1e3:void 0;return new Q(n.status,e,t,s)}async function ss(n){if(!n.sessionToken&&!n.googleIdToken)throw new Error("connectCloud needs either a sessionToken or a googleIdToken");let e=new L({cloudBaseUrl:n.cloudBaseUrl,sessionToken:n.sessionToken,onSession:n.onSession,fetch:n.fetch,timeoutMs:n.timeoutMs});!n.sessionToken&&n.googleIdToken&&await e.signInWithGoogle(n.googleIdToken);let t=await e.getMyNamespaces(),r=is(t,n.namespaceId),s=await e.getNamespaceRelays(r.namespaceId),i=nt(s);if(!i)throw new Error(os(r,s));let a=new le({relayUrl:i.relayUrl,executorAccount:i.executorAccount,authorAccount:n.authorAccount,authorProof:n.authorProof,deviceSecret:n.deviceSecret,nonces:n.nonces??await as(n.deviceSecret),ttlSeconds:n.ttlSeconds,fetch:n.seal?ge({baseUrl:i.relayUrl,verify:n.seal,fetch:n.fetch}):n.fetch,timeoutMs:n.timeoutMs});return{cloud:e,relay:a,namespaceId:r.namespaceId,namespace:r,relayInfo:i,execute:(o,c,u)=>a.execute(o,c,u)}}function is(n,e){if(e){let t=n.find(r=>r.namespaceId===e);if(!t)throw new Error(`this account owns no namespace ${e}; it owns: ${n.map(r=>r.namespaceId).join(", ")||"(none)"}`);return t}if(n.length===0)throw new Error("this account owns no namespaces in the cloud: claim one first, then enable HA on it so a relay is assigned");if(n.length>1)throw new Error(`this account owns ${n.length} namespaces \u2014 pass namespaceId to say which: ${n.map(t=>t.namespaceId).join(", ")}`);return n[0]}function os(n,e){if(e.length===0)return n.haStatus==="enabled"?`namespace ${n.namespaceId} has HA enabled but no relay has been assigned to it yet \u2014 retry shortly`:`namespace ${n.namespaceId} has no relays: enable HA on it in the cloud to have an attested node assigned`;let t=e.filter(s=>s.teeRole!==null&&s.teeRole!=="RelayTee");if(t.length===e.length)return`every node on namespace ${n.namespaceId} is a read-only TEE replica (ReadOnlyTee), which never relays delegated writes: an admin of the namespace must set its TEE admission policy with mode: 'relay' \u2014 AdminClient.setTeeAdmissionPolicy(namespaceId, { ..., mode: 'relay' }) \u2014 so fleet nodes are admitted as RelayTee`;let r=e.filter(s=>s.teeRole===null&&!s.authorshipReady);return r.length>0&&r.length+t.length===e.length?`every relay on namespace ${n.namespaceId} is waiting for the authorship grant: an admin of the namespace must grant CAN_AUTHOR_ON_BEHALF to ${r.map(s=>s.executorAccount??s.peerId).join(", ")||"the relay account"} \u2014 with AdminClient.grantAuthorship(namespaceId, account) for these relays, and AdminClient.openToDelegatedExecution(namespaceId) so relays assigned later need no further op`:`namespace ${n.namespaceId} has ${e.length} relay(s), but none is ready to relay yet (a URL, an executor account and a recent heartbeat) \u2014 retry shortly`}async function as(n){let e=`mero-js:warrant-nonce:${y(await E(n))}`;try{return lt(e)}catch{return ct(1)}}var cs=/^[0-9a-f]{96}$/,ls=["mrtd","rtmr0","rtmr1","rtmr2","rtmr3"];function mt(n){let e=Y(n.allowedMrtd??[],"allowedMrtd"),t=(n.allowedMeasurements??[]).map((o,c)=>{let u=`allowedMeasurements[${c}]`;return ls.map(d=>Y([o?.[d]??""],`${u}.${d}`)[0]).join()}),r=[Y(n.allowedRtmr0??[],"allowedRtmr0"),Y(n.allowedRtmr1??[],"allowedRtmr1"),Y(n.allowedRtmr2??[],"allowedRtmr2"),Y(n.allowedRtmr3??[],"allowedRtmr3")];if(n.allowedMeasurements!==void 0&&t.length===0)throw new Error("allowedMeasurements is empty, so no quote would ever be accepted");if(t.length===0){if(e.length===0)throw new Error("No image is trusted: give allowedMeasurements, or allowedMrtd with allowedRtmr1-3");if(r.slice(1).some(o=>o.length===0))throw new Error("allowedMrtd alone does not name an image: the MRTD measures the TD firmware, which images share, and the image is in RTMR1-3. Give allowedMeasurements, or allowedRtmr1-3 too")}let s=n.allowedTcbStatuses??["UpToDate"],i=n.now??Date.now;return Object.assign(async({quoteB64:o,nonce:c,reportDataSuffix:u,collateral:d})=>{let l=ds(o),g=d??await n.fetchCollateral?.(l);if(!g)throw new Error("The node sent no collateral for its quote, and no fetchCollateral was given to get it");let f;try{f=n.dcapVerify(l,g,Math.floor(i()/1e3))}catch(C){throw new Error(`The quote did not verify: ${C?.message??String(C)}`)}if(f.status==="Revoked"||!s.includes(f.status))throw new Error(`The platform's TCB status is ${f.status}, which is not accepted`);let m=us(f.report),A=[m.mrTd,m.rtMr0,m.rtMr1,m.rtMr2,m.rtMr3].map(C=>y(C));if(t.length>0&&!t.includes(A.join()))throw new Error(`The measurements (MRTD ${A[0]}, RTMR0-3 ${A.slice(1).join(", ")}) are not an image this verifier trusts`);if(e.length>0&&!e.includes(A[0]))throw new Error(`MRTD ${A[0]} is not an image this verifier trusts`);if([m.rtMr0,m.rtMr1,m.rtMr2,m.rtMr3].forEach((C,v)=>{let P=r[v];if(P.length>0&&!P.includes(y(C)))throw new Error(`RTMR${v} ${y(C)} is not one this verifier trusts`)}),y(m.reportData)!==`${c}${u}`.toLowerCase())throw new Error("The quote does not commit to this nonce and binding");return!0},{includeCollateral:!0})}function Y(n,e){return n.map(t=>{let r=t.trim().toLowerCase();if(!cs.test(r))throw new Error(`${e} holds ${JSON.stringify(t)}, which is not a 48-byte hex measurement`);return r})}function us(n){let e=n.data;if(n.type==="td10"&&e)return e;if(n.type==="td15"&&e?.base)return e.base;throw new Error(`The quote is a ${n.type} quote, not a TDX one`)}function ds(n){let e=atob(n),t=new Uint8Array(e.length);for(let r=0;r<e.length;r+=1)t[r]=e.charCodeAt(r);return t}var ps=["UpToDate","SWHardeningNeeded","ConfigurationNeeded","ConfigurationAndSWHardeningNeeded","OutOfDate","OutOfDateConfigurationNeeded"];function ht(n,{profile:e}){if(n.length===0)throw new Error("No release given, so no image would be trusted");let t,r=n.map((s,i)=>{let a=s?.tag??`releases[${i}]`;if(s?.role!==void 0&&s.role!=="node")throw new Error(`${a} lists the measurements of a ${s.role}, not of a node image`);let o=s?.profiles?.[e];if(!o){let u=Object.keys(s?.profiles??{}).join(", ")||"none";throw new Error(`${a} has no profile ${JSON.stringify(e)} (it has: ${u})`)}let c=(o.allowed_tcb_statuses??["uptodate"]).map(u=>gs(u,a));return t=t===void 0?c:t.filter(u=>c.includes(u)),{mrtd:o.mrtd,rtmr0:o.rtmr0,rtmr1:o.rtmr1,rtmr2:o.rtmr2,rtmr3:o.rtmr3}});if(!t||t.length===0)throw new Error("The releases given accept no TCB status in common");return{allowedMeasurements:r,allowedTcbStatuses:t}}function gs(n,e){let t=ps.find(r=>r.toLowerCase()===String(n).toLowerCase());if(!t)throw new Error(`${e} accepts TCB status ${JSON.stringify(n)}, which this verifier does not know`);return t}var gn=[{uri:"https://fulcio.sigstore.dev",validFor:{start:"2021-03-07T03:20:29Z",end:"2022-12-31T23:59:59.999Z"},certChain:["MIIB+DCCAX6gAwIBAgITNVkDZoCiofPDsy7dfm6geLbuhzAKBggqhkjOPQQDAzAqMRUwEwYDVQQKEwxzaWdzdG9yZS5kZXYxETAPBgNVBAMTCHNpZ3N0b3JlMB4XDTIxMDMwNzAzMjAyOVoXDTMxMDIyMzAzMjAyOVowKjEVMBMGA1UEChMMc2lnc3RvcmUuZGV2MREwDwYDVQQDEwhzaWdzdG9yZTB2MBAGByqGSM49AgEGBSuBBAAiA2IABLSyA7Ii5k+pNO8ZEWY0ylemWDowOkNa3kL+GZE5Z5GWehL9/A9bRNA3RbrsZ5i0JcastaRL7Sp5fp/jD5dxqc/UdTVnlvS16an+2Yfswe/QuLolRUCrcOE2+2iA5+tzd6NmMGQwDgYDVR0PAQH/BAQDAgEGMBIGA1UdEwEB/wQIMAYBAf8CAQEwHQYDVR0OBBYEFMjFHQBBmiQpMlEk6w2uSu1KBtPsMB8GA1UdIwQYMBaAFMjFHQBBmiQpMlEk6w2uSu1KBtPsMAoGCCqGSM49BAMDA2gAMGUCMH8liWJfMui6vXXBhjDgY4MwslmN/TJxVe/83WrFomwmNf056y1X48F9c4m3a3ozXAIxAKjRay5/aj/jsKKGIkmQatjI8uupHr/+CxFvaJWmpYqNkLDGRU+9orzh5hI2RrcuaQ=="]},{uri:"https://fulcio.sigstore.dev",validFor:{start:"2022-04-13T20:06:15Z"},certChain:["MIICGjCCAaGgAwIBAgIUALnViVfnU0brJasmRkHrn/UnfaQwCgYIKoZIzj0EAwMwKjEVMBMGA1UEChMMc2lnc3RvcmUuZGV2MREwDwYDVQQDEwhzaWdzdG9yZTAeFw0yMjA0MTMyMDA2MTVaFw0zMTEwMDUxMzU2NThaMDcxFTATBgNVBAoTDHNpZ3N0b3JlLmRldjEeMBwGA1UEAxMVc2lnc3RvcmUtaW50ZXJtZWRpYXRlMHYwEAYHKoZIzj0CAQYFK4EEACIDYgAE8RVS/ysH+NOvuDZyPIZtilgUF9NlarYpAd9HP1vBBH1U5CV77LSS7s0ZiH4nE7Hv7ptS6LvvR/STk798LVgMzLlJ4HeIfF3tHSaexLcYpSASr1kS0N/RgBJz/9jWCiXno3sweTAOBgNVHQ8BAf8EBAMCAQYwEwYDVR0lBAwwCgYIKwYBBQUHAwMwEgYDVR0TAQH/BAgwBgEB/wIBADAdBgNVHQ4EFgQU39Ppz1YkEZb5qNjpKFWixi4YZD8wHwYDVR0jBBgwFoAUWMAeX5FFpWapesyQoZMi0CrFxfowCgYIKoZIzj0EAwMDZwAwZAIwPCsQK4DYiZYDPIaDi5HFKnfxXx6ASSVmERfsynYBiX2X6SJRnZU84/9DZdnFvvxmAjBOt6QpBlc4J/0DxvkTCqpclvziL6BCCPnjdlIB3Pu3BxsPmygUY7Ii2zbdCdliiow=","MIIB9zCCAXygAwIBAgIUALZNAPFdxHPwjeDloDwyYChAO/4wCgYIKoZIzj0EAwMwKjEVMBMGA1UEChMMc2lnc3RvcmUuZGV2MREwDwYDVQQDEwhzaWdzdG9yZTAeFw0yMTEwMDcxMzU2NTlaFw0zMTEwMDUxMzU2NThaMCoxFTATBgNVBAoTDHNpZ3N0b3JlLmRldjERMA8GA1UEAxMIc2lnc3RvcmUwdjAQBgcqhkjOPQIBBgUrgQQAIgNiAAT7XeFT4rb3PQGwS4IajtLk3/OlnpgangaBclYpsYBr5i+4ynB07ceb3LP0OIOZdxexX69c5iVuyJRQ+Hz05yi+UF3uBWAlHpiS5sh0+H2GHE7SXrk1EC5m1Tr19L9gg92jYzBhMA4GA1UdDwEB/wQEAwIBBjAPBgNVHRMBAf8EBTADAQH/MB0GA1UdDgQWBBRYwB5fkUWlZql6zJChkyLQKsXF+jAfBgNVHSMEGDAWgBRYwB5fkUWlZql6zJChkyLQKsXF+jAKBggqhkjOPQQDAwNpADBmAjEAj1nHeXZp+13NWBNa+EDsDP8G1WWg1tCMWP/WHPqpaVo0jhsweNFZgSs0eE7wYI4qAjEA2WB9ot98sIkoF3vZYdd3/VtWB5b9TNMea7Ix/stJ5TfcLLeABLE4BNJOsQ4vnBHJ"]}],mn=[{baseUrl:"https://rekor.sigstore.dev",logId:"c0d23d6ad406973f9559f3ba2d1ca01f84147d8ffc5b8445c224f98b9591801d",publicKey:"MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAE2G2Y+2tabdTV5BcGiBIx0a9fAFwrkBbmLSGtks4L3qX6yYY0zufBnhC8Ur/iy55GhWP/9A/bY2LhC30M9+RYtw==",validFor:{start:"2021-01-12T11:53:27Z"}}];var ms="1.2.840.10045.2.1",hs={"1.2.840.10045.3.1.7":"P-256","1.3.132.0.34":"P-384"},fs={"P-256":32,"P-384":48},ys={"1.2.840.10045.4.3.2":"SHA-256","1.2.840.10045.4.3.3":"SHA-384"};function hn(n,e,t){if(e+2>t)throw new Error("it is truncated");let r=n[e];if((r&31)===31)throw new Error("it uses a tag this parser does not read");let s=n[e+1],i=e+2;if(s&128){let o=s&127;if(o===0||o>4||i+o>t)throw new Error("it has a length this parser does not read");s=0;for(let c=0;c<o;c+=1)s=s*256+n[i+c];i+=o}let a=i+s;if(a>t)throw new Error("it is truncated");return{tag:r,bytes:n,offset:e,start:i,end:a}}function Z(n){let e=hn(n,0,n.length);if(e.end!==n.length)throw new Error("it has trailing bytes");return e}function U(n){let e=[];for(let t=n.start;t<n.end;){let r=hn(n.bytes,t,n.end);e.push(r),t=r.end}return e}function x(n,e,t){if(!n||n.tag!==e)throw new Error(`${t} is not where it should be`);return n}function H(n){return n.bytes.slice(n.start,n.end)}function Ue(n){return n.bytes.slice(n.offset,n.end)}function X(n){let e=H(x(n,6,"An object identifier"));if(e.length===0||e[e.length-1]&128)throw new Error("it has a malformed object identifier");let t=[],r=0;for(let a of e)r=r*128+(a&127),a&128||(t.push(r),r=0);let s=t.shift();return[...s<80?[Math.floor(s/40),s%40]:[2,s-80],...t].join(".")}function bs(n){let e=new TextDecoder().decode(H(n)),t=n.tag===23?/^(\d{2})(\d{2})(\d{2})(\d{2})(\d{2})(\d{2})Z$/.exec(e):null,r=n.tag===24?/^(\d{4})(\d{2})(\d{2})(\d{2})(\d{2})(\d{2})Z$/.exec(e):null,s=t??r;if(!s)throw new Error(`it holds the time ${JSON.stringify(e)}, which is not one a certificate may hold`);let i=Number(s[1]);t&&(i+=i<50?2e3:1900);let[a,o,c,u,d]=s.slice(2).map(Number);return Date.UTC(i,a-1,o,c,u,d)}function ft(n){try{let[e,t,r,...s]=U(x(Z(n),48,"The certificate"));if(s.length>0)throw new Error("it has fields after its signature");let i=U(x(e,48,"The signed part")),a=i[0]?.tag===160?1:0,[o,c,u,d,l,g,...f]=i.slice(a);x(o,2,"The serial number");let m=X(U(x(t,48,"The signature algorithm"))[0]);if(X(U(x(c,48,"The signature algorithm"))[0])!==m)throw new Error("it names two different signature algorithms");let k=ys[m];if(!k)throw new Error(`it is signed with ${m}, not ECDSA with SHA-256 or SHA-384`);let[C,v]=U(x(d,48,"The validity")).map(bs);if(v===void 0)throw new Error("its validity is incomplete");let[P,q]=U(x(g,48,"The public key"));x(q,3,"The public key");let[D,N]=U(x(P,48,"The key algorithm"));if(X(D)!==ms)throw new Error("its key is not an elliptic-curve key");let B=hs[X(N)];if(!B)throw new Error("its key is on a curve other than P-256 or P-384");let K=new Map,j=f.find(He=>He.tag===163);if(j){let[He]=U(j);for(let jn of U(x(He,48,"The extensions"))){let V=U(x(jn,48,"An extension"));if(V.length===3)x(V[1],1,"An extension's critical flag");else if(V.length!==2)throw new Error("it has a malformed extension");let qe=X(V[0]);if(K.has(qe))throw new Error(`it has extension ${qe} twice`);K.set(qe,H(x(V[V.length-1],4,"An extension value")))}}let Ct=H(x(r,3,"The signature"));if(Ct[0]!==0)throw new Error("its signature is not a whole number of bytes");return{der:n,tbs:Ue(e),signatureHash:k,signature:Ct.slice(1),issuer:Ue(x(u,48,"The issuer")),subject:Ue(x(l,48,"The subject")),notBefore:C,notAfter:v,spki:Ue(g),curve:B,extensions:K}}catch(e){throw new Error(`The certificate is malformed: ${e.message}`)}}function fn(n){return U(x(Z(n),48,"The list")).map(X)}function ws(n,e){let t=fs[e],r=U(x(Z(n),48,"The signature"));if(r.length!==2)throw new Error("The signature is not an ECDSA signature");let s=new Uint8Array(t*2);return r.forEach((i,a)=>{let o=H(x(i,2,"The signature"));if(o.length===0||o[0]&128)throw new Error("The signature is not an ECDSA signature");for(;o.length>1&&o[0]===0;)o=o.subarray(1);if(o.length>t)throw new Error("The signature is not an ECDSA signature");s.set(o,a*t+t-o.length)}),s}async function Me(n,e,t,r,s){let i;try{i=ws(r,e)}catch{return!1}let a=await crypto.subtle.importKey("spki",n,{name:"ECDSA",namedCurve:e},!1,["verify"]);return crypto.subtle.verify({name:"ECDSA",hash:t},a,i,s)}function M(n,e){let t=n.replace(/\s+/g,"");if(!/^[A-Za-z0-9+/]*={0,2}$/.test(t)||t.length%4!==0)throw new Error(`${e} is not base64`);let r=atob(t),s=new Uint8Array(r.length);for(let i=0;i<r.length;i+=1)s[i]=r.charCodeAt(i);return s}function yt(n,e){let t=[...n.matchAll(/-----BEGIN CERTIFICATE-----([^-]*)-----END CERTIFICATE-----/g)];if(t.length!==1||n.replace(t[0][0],"").trim()!=="")throw new Error(`${e} is not exactly one PEM certificate`);return M(t[0][1],e)}var As="2.5.29.17",Rs="2.5.29.37",xs="1.3.6.1.5.5.7.3.3",Cs="1.3.6.1.4.1.57264.1.8",vs="1.3.6.1.4.1.57264.1.1",ks=134,Ss=12,Ts=48,Es=new TextEncoder,ee=n=>new TextDecoder().decode(n);async function yn(n,e,t,r){let{base64Signature:s,cert:i,rekorBundle:a}=Us(e),o=a.Payload;if(r!==void 0&&r.trim()!==s.trim())throw new Error("The detached signature is not the one in the bundle");let c=M(s,"The bundle signature"),u=yt(ee(M(i,"The bundle certificate")),"The bundle certificate"),d=ft(u),l=mn.find(v=>v.logId===o.logID.toLowerCase());if(!l)throw new Error(`The bundle names Rekor log ${o.logID}, which the Sigstore trust root does not know`);let g=Es.encode(`{"body":${JSON.stringify(o.body)},"integratedTime":${o.integratedTime},"logID":${JSON.stringify(o.logID)},"logIndex":${o.logIndex}}`);if(!await Me(M(l.publicKey,"The Rekor key"),"P-256","SHA-256",M(a.SignedEntryTimestamp,"The signed entry timestamp"),g))throw new Error("Rekor's signed entry timestamp does not verify: the log entry, its time or its index was altered");let m=o.integratedTime*1e3;if(!An(m,l.validFor))throw new Error(`Rekor log ${l.baseUrl} was not in use at ${me(m)}, when the entry says it was logged`);let A=Ms(o.body),k=y(new Uint8Array(await crypto.subtle.digest("SHA-256",n)));if(A.hash!==k)throw new Error("The file's SHA-256 is not the one Rekor logged: the file is not the one signed");if(!bt(M(A.signature,"The logged signature"),c))throw new Error("The bundle's signature is not the one Rekor logged");let C=yt(ee(M(A.publicKey,"The logged certificate")),"The logged certificate");if(!bt(C,u))throw new Error("The bundle's certificate is not the one Rekor logged");if(d.curve!=="P-256"||!await Me(d.spki,"P-256","SHA-256",c,n))throw new Error("The signature over the file does not verify with the bundle's certificate");return await Is(d,m),Ds(d,t),{integratedTime:o.integratedTime}}async function Is(n,e){if(!wn(n,e))throw new Error(`The signing certificate was valid ${me(n.notBefore)} to ${me(n.notAfter)}, not at ${me(e)} when Rekor logged it`);let t=[];for(let r of gn){let s=r.certChain.map(a=>ft(M(a,"A Fulcio certificate")));if(!await bn(n,s[0]))continue;let i=await Ps(s,r.validFor,e);if(!i)return;t.push(`${r.uri} (valid from ${r.validFor.start}): ${i}`)}throw new Error(t.length>0?`The signing certificate's CA is not trusted at ${me(e)}: ${t.join("; ")}`:"The signing certificate was not issued by a Fulcio CA in the Sigstore trust root")}async function Ps(n,e,t){if(!An(t,e))return"the trust root says it was not issuing certificates then";for(let[r,s]of n.entries()){if(!wn(s,t))return`its certificate ${r} was not valid then`;let i=n[r+1]??s;if(!await bn(s,i))return`its certificate ${r} is not signed by the next`}}async function bn(n,e){return bt(n.issuer,e.subject)?Me(e.spki,e.curve,n.signatureHash,n.signature,n.tbs):!1}function Ds(n,e){let t=n.extensions.get(As);if(!t)throw new Error("The signing certificate names no identity");let r=U(x(Z(t),Ts,"The subject alternative name")),s=r.filter(u=>u.tag===ks).map(u=>ee(H(u)));if(s.length!==1||r.length!==1||s[0]!==e.identity)throw new Error(`The release was signed by ${JSON.stringify(s.length===1?s[0]:s)}, not by ${e.identity}`);let i=n.extensions.get(Cs),a=n.extensions.get(vs),o=i?ee(H(x(Z(i),Ss,"The OIDC issuer"))):a?ee(a):void 0;if(o!==e.issuer)throw new Error(`The signing certificate's OIDC issuer is ${o===void 0?"missing":JSON.stringify(o)}, not ${e.issuer}`);let c=n.extensions.get(Rs);if(!c||!fn(c).includes(xs))throw new Error("The signing certificate is not for code signing")}function Us(n){let e=n;if(typeof e=="string")try{e=JSON.parse(e)}catch{throw new Error("The bundle is not JSON")}let t=e,r=t?.rekorBundle?.Payload;if(!(typeof t?.base64Signature=="string"&&typeof t.cert=="string"&&typeof t.rekorBundle?.SignedEntryTimestamp=="string"&&typeof r?.body=="string"&&typeof r.logID=="string"&&/^[0-9a-fA-F]{64}$/.test(r.logID)&&Number.isSafeInteger(r.integratedTime)&&r.integratedTime>=0&&Number.isSafeInteger(r.logIndex)&&r.logIndex>=0))throw new Error("The bundle is not a cosign bundle with a Rekor entry");return t}function Ms(n){let e;try{e=JSON.parse(ee(M(n,"The logged entry")))}catch{throw new Error("The logged entry is not JSON")}let t=e?.spec?.data?.hash,r=e?.spec?.signature;if(e?.kind!=="hashedrekord"||t?.algorithm!=="sha256"||typeof t.value!="string"||typeof r?.content!="string"||typeof r.publicKey?.content!="string")throw new Error("The logged entry is not a SHA-256 hashedrekord");return{hash:t.value.toLowerCase(),signature:r.content,publicKey:r.publicKey.content}}function wn(n,e){return n.notBefore<=e&&e<=n.notAfter}function An(n,e){return Date.parse(e.start)<=n&&(e.end===void 0||n<=Date.parse(e.end))}function me(n){return new Date(n).toISOString()}function bt(n,e){return n.length===e.length&&n.every((t,r)=>t===e[r])}var wt=Object.freeze({identity:"https://github.com/calimero-network/mero-tee/.github/workflows/release-node-image-gcp.yaml@refs/heads/master",issuer:"https://token.actions.githubusercontent.com"}),Ns=["mrtd","rtmr0","rtmr1","rtmr2","rtmr3"],xn="https://cloud.calimero.network";async function Cn(n,e,t={}){let r=typeof n=="string"?new TextEncoder().encode(n):n;await yn(r,e,{identity:t.identity??wt.identity,issuer:t.issuer??wt.issuer},t.signature);let s;try{s=JSON.parse(new TextDecoder().decode(r))}catch{throw new Error("The signed release is not JSON")}if(s?.role!=="node")throw new Error(`The signed release is a ${JSON.stringify(s?.role)} release, not a node one`);let i=Object.values(s.profiles??{}),a=o=>Ns.every(c=>/^[0-9a-f]{96}$/i.test(String(o?.[c])));if(typeof s.tag!="string"||i.length===0||!i.every(a))throw new Error("The signed file is not a release's published-mrtds.json: it lists no measured profiles");if(t.expectedVersion!==void 0&&s.tag!==t.expectedVersion)throw new Error(`The signed release is ${s.tag}, not ${t.expectedVersion}`);return s}async function vn(n,e={}){let t=await(e.fetch??fetch)(n,{headers:{accept:"application/json"}});if(!t.ok)throw new Error(`Fetching the node release from ${n} failed: HTTP ${t.status}`);let r;try{r=(await t.json())?.data}catch{throw new Error(`${n} did not answer with JSON`)}if(typeof r?.version!="string"||typeof r.publishedMrtds!="string"||typeof r.bundle!="string")throw new Error(`${n} did not answer with a signed node release`);return{version:r.version,publishedMrtds:r.publishedMrtds,bundle:r.bundle}}function kn(n){let e=/^merotee-.+-(\d+)-(\d+)-(\d+)$/.exec(n);if(!e)throw new Error(`${JSON.stringify(n)} is not the image of a mero-tee node release`);return`${Number(e[1])}.${Number(e[2])}.${Number(e[3])}`}async function Sn(n,e={}){let t=await En(n,e.fetch??fetch);if(t===void 0)throw new Error(`${Tn(n)} was refused unsealed: the node requires sealing and is too old to name its release before a session exists. Name the release it runs yourself (createSignedReleaseSealedFetch's \`releaseVersion\`), or upgrade the node`);return t}function Tn(n){return`${n.replace(/\/+$/,"")}/admin-api/tee/info`}async function En(n,e){let t=Tn(n),r=await e(t,{headers:{accept:"application/json"}});if(r.status===403&&await r.json().then(a=>a?.error?.code).catch(()=>{})==="sealed_required")return;if(!r.ok)throw new Error(`Fetching ${t} failed: HTTP ${r.status}`);let s;try{s=(await r.json())?.data?.osImage}catch{throw new Error(`${t} did not answer with JSON`)}if(typeof s!="string")throw new Error(`${t} did not name the node's image`);return kn(s)}function In(n,e){return`${n.replace(/\/+$/,"")}/api/tee/node-releases/${he(e,"version")}`}async function Pn(n){let e=he(n.minReleaseVersion,"minReleaseVersion"),t=he(n.release?.version,"The release version"),r=await Cn(n.release.publishedMrtds,n.release.bundle,{expectedVersion:t,identity:n.identity,issuer:n.issuer});if(Hs(t,e)<0)throw new Error(`Release ${t} is older than the minimum trusted, ${e}`);return ht([r],{profile:n.profile})}function Dn(n){return he(n.minReleaseVersion,"minReleaseVersion"),Object.assign(async t=>{let r=await Pn({release:await n.release(),profile:n.profile,minReleaseVersion:n.minReleaseVersion,identity:n.identity,issuer:n.issuer});return mt({dcapVerify:n.dcapVerify,fetchCollateral:n.fetchCollateral,now:n.now,...r})(t)},{includeCollateral:!0})}function _s(n){let{baseUrl:e,releaseSource:t,releaseVersion:r,applicationId:s,applicationHash:i,fetch:a,...o}=n;r!==void 0&&he(r,"releaseVersion");let c=a??((d,l)=>fetch(d,l));return ge({baseUrl:e,verify:Dn({...o,release:async()=>{let d=r===void 0?await Sn(e,{fetch:c}):await En(e,c)??r;return typeof t=="function"?t({baseUrl:e,version:d,fetch:c}):vn(In(t??xn,d),{fetch:c})}}),applicationId:s,applicationHash:i,fetch:c})}var Un=/^(\d+)\.(\d+)\.(\d+)(?:-([0-9A-Za-z.-]+))?(?:\+[0-9A-Za-z.-]+)?$/;function he(n,e){let t=typeof n=="string"?Un.exec(n):null,r=t?.[4];if(!t||r!==void 0&&r.split(".").some(s=>s===""))throw new Error(`${e} is ${JSON.stringify(n)}, not a release version like 2.3.87`);return n}function Hs(n,e){let[t,r]=Rn(n),[s,i]=Rn(e);for(let c=0;c<3;c+=1)if(t[c]!==s[c])return t[c]<s[c]?-1:1;if(r===void 0||i===void 0)return r===i?0:r===void 0?1:-1;let a=r.split("."),o=i.split(".");for(let c=0;c<Math.max(a.length,o.length);c+=1){let[u,d]=[a[c],o[c]];if(u===void 0||d===void 0)return u===void 0?-1:1;let[l,g]=[/^\d+$/.test(u),/^\d+$/.test(d)],f=l&&g?Math.sign(Number(u)-Number(d)):l!==g?l?-1:1:u<d?-1:u>d?1:0;if(f!==0)return f}return 0}function Rn(n){let e=Un.exec(n);return[[Number(e[1]),Number(e[2]),Number(e[3])],e[4]]}var Rt=12,qs=new TextEncoder().encode("calimero.namespace.v1"),Os=0,Mn={GroupCreated:0,GroupReparented:1,GroupDeleted:2,AdminChanged:3,PolicyUpdated:4,MemberJoined:5,KeyDelivery:6,MemberJoinedOpen:7,MemberJoinedAt:8,NamespaceCreatedV2:9,MemberJoinedViaTeeAttestation:10};function At(n,e){if(n.length!==32)throw new Error(`${e} must be 32 bytes, got ${n.length}`);return new Uint8Array(n)}function Nn(n,e){if(n==null)return new Uint8Array([0]);if(n.length!==32)throw new Error(`${e} must be 32 bytes, got ${n.length}`);return h(new Uint8Array([1]),new Uint8Array(n))}function _n(n){let e=new TextEncoder().encode(n);return h(R(e.length),e)}function Ne(n){let e=n.invitation,t=e.admitters??[],r=n.admitter_addrs??[];return h(At(e.inviter_identity,"inviter_identity"),At(e.group_id,"group_id"),w(e.expiration_timestamp),At(e.secret_salt,"secret_salt"),new Uint8Array([e.invited_role]),R(t.length),...t.map((s,i)=>b(s,`admitters[${i}]`,32)),_n(n.inviter_signature),n.inviter_account?h(new Uint8Array([1]),b(n.inviter_account,"inviter_account",32)):new Uint8Array([0]),R(r.length),...r.map(_n),Nn(n.application_id,"application_id"),Nn(n.app_key,"app_key"))}async function Hn(n){let e=n.parentOpHashes??[],t=b(n.credential,"credential",n.credential.length/2),r=BigInt(n.invitation.invitation.expiration_timestamp??0)!==0n,s=n.joinedAt??BigInt(Math.floor(Date.now()/1e3)),i=r?h(new Uint8Array([Mn.MemberJoinedAt]),b(n.member,"member",32),Ne(n.invitation),w(s),t):h(new Uint8Array([Mn.MemberJoined]),b(n.member,"member",32),Ne(n.invitation),t),a=h(new Uint8Array([Os]),i),o=await T(n.deviceSecret),c=await E(n.deviceSecret),u=h(new Uint8Array([Rt]),b(n.namespaceId,"namespaceId",32),R(e.length),...e.map((g,f)=>b(g,`parentOpHashes[${f}]`,32)),c,w(n.nonce),a),d=new Uint8Array(await crypto.subtle.sign("Ed25519",o,h(qs,u))),l=new Uint8Array([0]);return y(h(u,d,l))}var $s=new TextEncoder().encode("calimero.auth.login.v1"),qn={webOrigin:0,codeSigningId:1,cli:2};function On(n){let e=new TextEncoder;switch(n.kind){case"webOrigin":return e.encode(n.origin);case"codeSigningId":return e.encode(n.id);case"cli":return new Uint8Array(0)}}function Gs(n){return h(Uint8Array.from([qn[n.kind]]),On(n))}function Ls(n){let e=Uint8Array.from([qn[n.kind]]);if(n.kind==="cli")return e;let t=On(n);return h(e,R(t.length),t)}async function _e(n){let e=b(n.node,"node",32),t=b(n.challenge,"challenge",32),r=b(n.sessionKey,"sessionKey",32),s=await T(n.deviceSecret),i=await E(n.deviceSecret),a=w(n.issuedAt),o=w(n.expiresAt),c=await I($s,[e,Gs(n.audience),t,r,i,a,o]),u=new Uint8Array(await crypto.subtle.sign({name:"Ed25519"},s,c));return y(h(e,Ls(n.audience),t,r,i,a,o,u))}var Bs=300;async function xt(){let n=y(crypto.getRandomValues(new Uint8Array(32)));return{publicKey:y(await E(n)),secret:n}}function Ks(n){switch(n.kind){case"webOrigin":return n.origin;case"codeSigningId":return n.id;case"cli":return"cli"}}async function $n(n){let e=n.nodeUrl.replace(/\/+$/,""),t=n.timeoutMs??1e4,r=async(g,f)=>{let m=new AbortController,A=setTimeout(()=>m.abort(),t),k;try{let v={...f,signal:m.signal};k=n.fetch?await n.fetch(`${e}${g}`,v):await globalThis.fetch(`${e}${g}`,v)}catch(v){throw new S(0,v instanceof Error?v.name:"NetworkError",`${e}${g}`,new Headers,v instanceof Error?v.message:String(v))}finally{clearTimeout(A)}let C=await k.text().catch(()=>"");if(!k.ok)throw new S(k.status,k.statusText,`${e}${g}`,k.headers,C);return C?JSON.parse(C):{}},s=await r("/auth/challenge",{method:"GET",headers:{Accept:"application/json"}}),i=s.challenge??s.data?.challenge;if(!i)throw new Error("the node issued no challenge");let a=await xt(),o=Math.floor(Date.now()/1e3),c=await _e({node:n.node,audience:n.audience,challenge:i,sessionKey:a.publicKey,issuedAt:o,expiresAt:o+(n.ttlSeconds??Bs),deviceSecret:n.deviceSecret}),u=await r("/auth/token",{method:"POST",headers:{"Content-Type":"application/json",Accept:"application/json"},body:JSON.stringify({auth_method:"account_proof",public_key:a.publicKey,client_name:n.clientName??Ks(n.audience),timestamp:o,provider_data:{challenge:i,login_statement:c,account_proof:n.accountProof}})}),d=u.access_token??u.data?.access_token,l=u.refresh_token??u.data?.refresh_token;if(!d)throw new Error("the node minted no session");return{accessToken:d,refreshToken:l??"",sessionKey:a.publicKey,sessionSecret:a.secret}}var Gn=[2428,2429,2528,2529],js="localhost",Vs=2e3;function Ln(n){return`http://${js}:${n}`}function Bn(n,e){let t=n.endsWith("/")?n:`${n}/`;return new URL(e,t).toString()}async function Kn(n,e={}){let{timeoutMs:t=Vs,signal:r}=e;if(r?.aborted)return!1;let s=new AbortController,i=()=>s.abort(),a=r;a&&a.addEventListener("abort",i,{once:!0});let o=setTimeout(()=>s.abort(),t);try{let c=Bn(n,"admin-api/health"),u=await fetch(c,{method:"GET",signal:s.signal});if(!u.ok)return!1;try{let d=await u.json(),l=d?.data?.status!==void 0?d?.data?.status:d?.status;return l===void 0?!0:typeof l=="string"&&l.toLowerCase()==="alive"}catch{return!0}}catch{return!1}finally{clearTimeout(o),a&&a.removeEventListener("abort",i)}}async function Fs(n={}){let{ports:e=Gn,timeoutMs:t,signal:r}=n;return(await Promise.all(e.map(async i=>{let a=Ln(i);return await Kn(a,{timeoutMs:t,signal:r})?a:null}))).filter(i=>i!==null)}export{$ as AdminApiClient,O as AuthApiClient,_ as AuthRevokedError,re as CAPABILITIES,L as CloudClient,Gn as DEFAULT_LOCAL_NODE_PORTS,xn as DEFAULT_RELEASE_MIRROR,ie as EphemeralClient,sn as HANDSHAKE_PATH,S as HTTPError,z as IntentRefusedError,it as LocalStorageTokenStore,st as MemoryTokenStore,Ee as MeroJs,wt as NODE_RELEASE_SIGNER,le as RelayClient,se as RpcClient,xe as RpcError,de as SEALED_CONTENT_TYPE,on as SEALED_PATH,Rt as SIGNED_NAMESPACE_OP_SCHEMA_VERSION,Q as SealedTransportError,W as SseClient,pe as StaleTransportKeyError,te as WebHttpClient,J as WsClient,ke as accountForRoot,Gt as accountRootFromPhrase,G as accountRootFromSecret,Cr as admitterAddrsFromNetworkStatus,We as buildAuthLoginUrl,Ae as callerProof,Ve as classifyError,In as cloudNodeReleaseUrl,Oe as combineSignals,rr as compareSemver,ss as connectCloud,Pi as createAdminApiClient,It as createAdminApiClientFromHttpClient,ge as createAttestedSealedFetch,ui as createAuthApiClient,kt as createAuthApiClientFromHttpClient,Ei as createBrowserAdminApiClient,ci as createBrowserAuthApiClient,F as createBrowserHttpClient,Ge as createHttpClient,lt as createLocalStorageNonceSource,ct as createMemoryNonceSource,Tr as createMeroJs,Ii as createNodeAdminApiClient,li as createNodeAuthApiClient,fe as createNodeHttpClient,Re as createProofSigner,mt as createQuoteVerifier,Qn as createRetryableMethod,un as createSealedFetch,_s as createSignedReleaseSealedFetch,Dn as createSignedReleaseVerifier,$e as createTimeoutSignal,Le as createUniversalHttpClient,at as creationInitHash,Ye as deviceCertPayload,Fs as discoverLocalNodes,Ne as encodeSignedInvitation,ln as fetchAttestedTransportKey,vn as fetchNodeRelease,Sn as fetchNodeReleaseVersion,$t as generateAccountRoot,xt as generateSessionKey,Be as hasCap,Ci as hexEncodeUtf8,ot as intentHash,er as isSettled,ve as jsonCodec,Ln as localNodeUrl,$n as login,_t as mintDeviceId,Bn as nodeEndpoint,kn as nodeReleaseVersionFromImage,Fe as parseAuthCallback,zt as parseCreationWarrant,Kn as probeNodeHealth,St as redeemInvitation,Je as requestBodyHash,Te as routingProofHeaders,fi as shouldRetain,ae as signAccountLink,Se as signAccountLogin,De as signCreationWarrant,qt as signDeviceCert,_e as signLoginStatement,Hn as signMemberJoinOp,we as signRequest,Vt as signRoutingChallenge,Ie as signWarrant,cn as transportKeyBinding,Pn as trustSignedRelease,ht as trustedMeasurementsFromReleases,Cn as verifySignedNodeRelease,Ke as withCap,ye as withRetry,pi as withoutCap};
|
|
3
3
|
//# sourceMappingURL=index.browser.mjs.map
|