@calimero-network/mero-js 19.3.0 → 19.5.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,
@@ -1140,6 +1142,33 @@ var AdminApiClient = class {
1140
1142
  async listApplicationAliases() {
1141
1143
  return unwrap(await this.httpClient.get("/admin-api/alias/list/application"));
1142
1144
  }
1145
+ async createDeviceAlias(request) {
1146
+ return unwrap(
1147
+ await this.httpClient.post(
1148
+ "/admin-api/alias/create/device",
1149
+ { alias: request.alias, deviceId: request.deviceId }
1150
+ )
1151
+ );
1152
+ }
1153
+ async lookupDeviceAlias(name) {
1154
+ return unwrap(
1155
+ await this.httpClient.post(
1156
+ `/admin-api/alias/lookup/device/${encodeURIComponent(name)}`,
1157
+ {}
1158
+ )
1159
+ );
1160
+ }
1161
+ async deleteDeviceAlias(name) {
1162
+ return unwrap(
1163
+ await this.httpClient.post(
1164
+ `/admin-api/alias/delete/device/${encodeURIComponent(name)}`,
1165
+ {}
1166
+ )
1167
+ );
1168
+ }
1169
+ async listDeviceAliases() {
1170
+ return unwrap(await this.httpClient.get("/admin-api/alias/list/device"));
1171
+ }
1143
1172
  // ---- Namespace Management ----
1144
1173
  async listNamespaces() {
1145
1174
  return unwrap(await this.httpClient.get("/admin-api/namespaces"));
@@ -3757,6 +3786,105 @@ async function signLoginStatement(input) {
3757
3786
  );
3758
3787
  }
3759
3788
 
3789
+ // src/login/session.ts
3790
+ var DEFAULT_TTL_SECONDS2 = 300;
3791
+ async function generateSessionKey() {
3792
+ const secret = hex(crypto.getRandomValues(new Uint8Array(32)));
3793
+ return { publicKey: hex(await derivePublicKey(secret)), secret };
3794
+ }
3795
+ function audienceLabel(audience) {
3796
+ switch (audience.kind) {
3797
+ case "webOrigin":
3798
+ return audience.origin;
3799
+ case "codeSigningId":
3800
+ return audience.id;
3801
+ case "cli":
3802
+ return "cli";
3803
+ }
3804
+ }
3805
+ async function login(config) {
3806
+ const baseUrl = config.nodeUrl.replace(/\/+$/, "");
3807
+ const doFetch = config.fetch ?? globalThis.fetch;
3808
+ const timeoutMs = config.timeoutMs ?? 1e4;
3809
+ const request = async (path, init) => {
3810
+ const controller = new AbortController();
3811
+ const timer = setTimeout(() => controller.abort(), timeoutMs);
3812
+ let response;
3813
+ try {
3814
+ response = await doFetch(`${baseUrl}${path}`, {
3815
+ ...init,
3816
+ signal: controller.signal
3817
+ });
3818
+ } catch (err) {
3819
+ throw new HTTPError(
3820
+ 0,
3821
+ err instanceof Error ? err.name : "NetworkError",
3822
+ `${baseUrl}${path}`,
3823
+ new Headers(),
3824
+ err instanceof Error ? err.message : String(err)
3825
+ );
3826
+ } finally {
3827
+ clearTimeout(timer);
3828
+ }
3829
+ const text = await response.text().catch(() => "");
3830
+ if (!response.ok) {
3831
+ throw new HTTPError(
3832
+ response.status,
3833
+ response.statusText,
3834
+ `${baseUrl}${path}`,
3835
+ response.headers,
3836
+ text
3837
+ );
3838
+ }
3839
+ return text ? JSON.parse(text) : {};
3840
+ };
3841
+ const challengeBody = await request("/auth/challenge", {
3842
+ method: "GET",
3843
+ headers: { Accept: "application/json" }
3844
+ });
3845
+ const challenge = challengeBody.challenge ?? challengeBody.data?.challenge;
3846
+ if (!challenge) {
3847
+ throw new Error("the node issued no challenge");
3848
+ }
3849
+ const session = await generateSessionKey();
3850
+ const issuedAt = Math.floor(Date.now() / 1e3);
3851
+ const statement = await signLoginStatement({
3852
+ node: config.node,
3853
+ audience: config.audience,
3854
+ challenge,
3855
+ sessionKey: session.publicKey,
3856
+ issuedAt,
3857
+ expiresAt: issuedAt + (config.ttlSeconds ?? DEFAULT_TTL_SECONDS2),
3858
+ deviceSecret: config.deviceSecret
3859
+ });
3860
+ const tokenBody = await request("/auth/token", {
3861
+ method: "POST",
3862
+ headers: { "Content-Type": "application/json", Accept: "application/json" },
3863
+ body: JSON.stringify({
3864
+ auth_method: "account_proof",
3865
+ public_key: session.publicKey,
3866
+ client_name: config.clientName ?? audienceLabel(config.audience),
3867
+ timestamp: issuedAt,
3868
+ provider_data: {
3869
+ challenge,
3870
+ login_statement: statement,
3871
+ account_proof: config.accountProof
3872
+ }
3873
+ })
3874
+ });
3875
+ const accessToken = tokenBody.access_token ?? tokenBody.data?.access_token;
3876
+ const refreshToken = tokenBody.refresh_token ?? tokenBody.data?.refresh_token;
3877
+ if (!accessToken) {
3878
+ throw new Error("the node minted no session");
3879
+ }
3880
+ return {
3881
+ accessToken,
3882
+ refreshToken: refreshToken ?? "",
3883
+ sessionKey: session.publicKey,
3884
+ sessionSecret: session.secret
3885
+ };
3886
+ }
3887
+
3760
3888
  // src/capabilities.ts
3761
3889
  var CAPABILITIES = {
3762
3890
  CAN_CREATE_CONTEXT: 1 << 0,