@calimero-network/mero-js 19.3.0 → 19.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/dist/index.cjs CHANGED
@@ -63,10 +63,12 @@ __export(index_exports, {
63
63
  deviceCertPayload: () => deviceCertPayload,
64
64
  discoverLocalNodes: () => discoverLocalNodes,
65
65
  encodeSignedInvitation: () => encodeSignedInvitation,
66
+ generateSessionKey: () => generateSessionKey,
66
67
  hasCap: () => hasCap,
67
68
  intentHash: () => intentHash,
68
69
  jsonCodec: () => jsonCodec,
69
70
  localNodeUrl: () => localNodeUrl,
71
+ login: () => login,
70
72
  mintDeviceId: () => mintDeviceId,
71
73
  nodeEndpoint: () => nodeEndpoint,
72
74
  parseAuthCallback: () => parseAuthCallback,
@@ -3757,6 +3759,105 @@ async function signLoginStatement(input) {
3757
3759
  );
3758
3760
  }
3759
3761
 
3762
+ // src/login/session.ts
3763
+ var DEFAULT_TTL_SECONDS2 = 300;
3764
+ async function generateSessionKey() {
3765
+ const secret = hex(crypto.getRandomValues(new Uint8Array(32)));
3766
+ return { publicKey: hex(await derivePublicKey(secret)), secret };
3767
+ }
3768
+ function audienceLabel(audience) {
3769
+ switch (audience.kind) {
3770
+ case "webOrigin":
3771
+ return audience.origin;
3772
+ case "codeSigningId":
3773
+ return audience.id;
3774
+ case "cli":
3775
+ return "cli";
3776
+ }
3777
+ }
3778
+ async function login(config) {
3779
+ const baseUrl = config.nodeUrl.replace(/\/+$/, "");
3780
+ const doFetch = config.fetch ?? globalThis.fetch;
3781
+ const timeoutMs = config.timeoutMs ?? 1e4;
3782
+ const request = async (path, init) => {
3783
+ const controller = new AbortController();
3784
+ const timer = setTimeout(() => controller.abort(), timeoutMs);
3785
+ let response;
3786
+ try {
3787
+ response = await doFetch(`${baseUrl}${path}`, {
3788
+ ...init,
3789
+ signal: controller.signal
3790
+ });
3791
+ } catch (err) {
3792
+ throw new HTTPError(
3793
+ 0,
3794
+ err instanceof Error ? err.name : "NetworkError",
3795
+ `${baseUrl}${path}`,
3796
+ new Headers(),
3797
+ err instanceof Error ? err.message : String(err)
3798
+ );
3799
+ } finally {
3800
+ clearTimeout(timer);
3801
+ }
3802
+ const text = await response.text().catch(() => "");
3803
+ if (!response.ok) {
3804
+ throw new HTTPError(
3805
+ response.status,
3806
+ response.statusText,
3807
+ `${baseUrl}${path}`,
3808
+ response.headers,
3809
+ text
3810
+ );
3811
+ }
3812
+ return text ? JSON.parse(text) : {};
3813
+ };
3814
+ const challengeBody = await request("/auth/challenge", {
3815
+ method: "GET",
3816
+ headers: { Accept: "application/json" }
3817
+ });
3818
+ const challenge = challengeBody.challenge ?? challengeBody.data?.challenge;
3819
+ if (!challenge) {
3820
+ throw new Error("the node issued no challenge");
3821
+ }
3822
+ const session = await generateSessionKey();
3823
+ const issuedAt = Math.floor(Date.now() / 1e3);
3824
+ const statement = await signLoginStatement({
3825
+ node: config.node,
3826
+ audience: config.audience,
3827
+ challenge,
3828
+ sessionKey: session.publicKey,
3829
+ issuedAt,
3830
+ expiresAt: issuedAt + (config.ttlSeconds ?? DEFAULT_TTL_SECONDS2),
3831
+ deviceSecret: config.deviceSecret
3832
+ });
3833
+ const tokenBody = await request("/auth/token", {
3834
+ method: "POST",
3835
+ headers: { "Content-Type": "application/json", Accept: "application/json" },
3836
+ body: JSON.stringify({
3837
+ auth_method: "account_proof",
3838
+ public_key: session.publicKey,
3839
+ client_name: config.clientName ?? audienceLabel(config.audience),
3840
+ timestamp: issuedAt,
3841
+ provider_data: {
3842
+ challenge,
3843
+ login_statement: statement,
3844
+ account_proof: config.accountProof
3845
+ }
3846
+ })
3847
+ });
3848
+ const accessToken = tokenBody.access_token ?? tokenBody.data?.access_token;
3849
+ const refreshToken = tokenBody.refresh_token ?? tokenBody.data?.refresh_token;
3850
+ if (!accessToken) {
3851
+ throw new Error("the node minted no session");
3852
+ }
3853
+ return {
3854
+ accessToken,
3855
+ refreshToken: refreshToken ?? "",
3856
+ sessionKey: session.publicKey,
3857
+ sessionSecret: session.secret
3858
+ };
3859
+ }
3860
+
3760
3861
  // src/capabilities.ts
3761
3862
  var CAPABILITIES = {
3762
3863
  CAN_CREATE_CONTEXT: 1 << 0,