@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.d.ts CHANGED
@@ -24,6 +24,8 @@ export type { SignMemberJoinInput } from './namespace-op/index.js';
24
24
  export type { WarrantInput } from './warrant/index.js';
25
25
  export { signLoginStatement } from './login/index.js';
26
26
  export type { Audience, LoginStatementInput } from './login/index.js';
27
+ export { login, generateSessionKey } from './login/index.js';
28
+ export type { LoginConfig, DelegatedSession, SessionKeyPair, } from './login/index.js';
27
29
  export * from './capabilities.js';
28
30
  export { DEFAULT_LOCAL_NODE_PORTS, localNodeUrl, nodeEndpoint, probeNodeHealth, discoverLocalNodes, } from './nodeDiscovery.js';
29
31
  export type { DiscoverLocalNodesOptions } from './nodeDiscovery.js';
package/dist/index.js CHANGED
@@ -28,6 +28,7 @@ export { signDeviceCert, mintDeviceId, deviceCertPayload, accountForRoot, } from
28
28
  export { signMemberJoinOp, encodeSignedInvitation, SIGNED_NAMESPACE_OP_SCHEMA_VERSION, } from './namespace-op/index.js';
29
29
  // Login-statement signing — the device's half of a password-free session
30
30
  export { signLoginStatement } from './login/index.js';
31
+ export { login, generateSessionKey } from './login/index.js';
31
32
  // Member capability bitmask constants & helpers
32
33
  export * from './capabilities.js';
33
34
  // Utilities
package/dist/index.mjs CHANGED
@@ -1057,6 +1057,33 @@ var AdminApiClient = class {
1057
1057
  async listApplicationAliases() {
1058
1058
  return unwrap(await this.httpClient.get("/admin-api/alias/list/application"));
1059
1059
  }
1060
+ async createDeviceAlias(request) {
1061
+ return unwrap(
1062
+ await this.httpClient.post(
1063
+ "/admin-api/alias/create/device",
1064
+ { alias: request.alias, deviceId: request.deviceId }
1065
+ )
1066
+ );
1067
+ }
1068
+ async lookupDeviceAlias(name) {
1069
+ return unwrap(
1070
+ await this.httpClient.post(
1071
+ `/admin-api/alias/lookup/device/${encodeURIComponent(name)}`,
1072
+ {}
1073
+ )
1074
+ );
1075
+ }
1076
+ async deleteDeviceAlias(name) {
1077
+ return unwrap(
1078
+ await this.httpClient.post(
1079
+ `/admin-api/alias/delete/device/${encodeURIComponent(name)}`,
1080
+ {}
1081
+ )
1082
+ );
1083
+ }
1084
+ async listDeviceAliases() {
1085
+ return unwrap(await this.httpClient.get("/admin-api/alias/list/device"));
1086
+ }
1060
1087
  // ---- Namespace Management ----
1061
1088
  async listNamespaces() {
1062
1089
  return unwrap(await this.httpClient.get("/admin-api/namespaces"));
@@ -3674,6 +3701,105 @@ async function signLoginStatement(input) {
3674
3701
  );
3675
3702
  }
3676
3703
 
3704
+ // src/login/session.ts
3705
+ var DEFAULT_TTL_SECONDS2 = 300;
3706
+ async function generateSessionKey() {
3707
+ const secret = hex(crypto.getRandomValues(new Uint8Array(32)));
3708
+ return { publicKey: hex(await derivePublicKey(secret)), secret };
3709
+ }
3710
+ function audienceLabel(audience) {
3711
+ switch (audience.kind) {
3712
+ case "webOrigin":
3713
+ return audience.origin;
3714
+ case "codeSigningId":
3715
+ return audience.id;
3716
+ case "cli":
3717
+ return "cli";
3718
+ }
3719
+ }
3720
+ async function login(config) {
3721
+ const baseUrl = config.nodeUrl.replace(/\/+$/, "");
3722
+ const doFetch = config.fetch ?? globalThis.fetch;
3723
+ const timeoutMs = config.timeoutMs ?? 1e4;
3724
+ const request = async (path, init) => {
3725
+ const controller = new AbortController();
3726
+ const timer = setTimeout(() => controller.abort(), timeoutMs);
3727
+ let response;
3728
+ try {
3729
+ response = await doFetch(`${baseUrl}${path}`, {
3730
+ ...init,
3731
+ signal: controller.signal
3732
+ });
3733
+ } catch (err) {
3734
+ throw new HTTPError(
3735
+ 0,
3736
+ err instanceof Error ? err.name : "NetworkError",
3737
+ `${baseUrl}${path}`,
3738
+ new Headers(),
3739
+ err instanceof Error ? err.message : String(err)
3740
+ );
3741
+ } finally {
3742
+ clearTimeout(timer);
3743
+ }
3744
+ const text = await response.text().catch(() => "");
3745
+ if (!response.ok) {
3746
+ throw new HTTPError(
3747
+ response.status,
3748
+ response.statusText,
3749
+ `${baseUrl}${path}`,
3750
+ response.headers,
3751
+ text
3752
+ );
3753
+ }
3754
+ return text ? JSON.parse(text) : {};
3755
+ };
3756
+ const challengeBody = await request("/auth/challenge", {
3757
+ method: "GET",
3758
+ headers: { Accept: "application/json" }
3759
+ });
3760
+ const challenge = challengeBody.challenge ?? challengeBody.data?.challenge;
3761
+ if (!challenge) {
3762
+ throw new Error("the node issued no challenge");
3763
+ }
3764
+ const session = await generateSessionKey();
3765
+ const issuedAt = Math.floor(Date.now() / 1e3);
3766
+ const statement = await signLoginStatement({
3767
+ node: config.node,
3768
+ audience: config.audience,
3769
+ challenge,
3770
+ sessionKey: session.publicKey,
3771
+ issuedAt,
3772
+ expiresAt: issuedAt + (config.ttlSeconds ?? DEFAULT_TTL_SECONDS2),
3773
+ deviceSecret: config.deviceSecret
3774
+ });
3775
+ const tokenBody = await request("/auth/token", {
3776
+ method: "POST",
3777
+ headers: { "Content-Type": "application/json", Accept: "application/json" },
3778
+ body: JSON.stringify({
3779
+ auth_method: "account_proof",
3780
+ public_key: session.publicKey,
3781
+ client_name: config.clientName ?? audienceLabel(config.audience),
3782
+ timestamp: issuedAt,
3783
+ provider_data: {
3784
+ challenge,
3785
+ login_statement: statement,
3786
+ account_proof: config.accountProof
3787
+ }
3788
+ })
3789
+ });
3790
+ const accessToken = tokenBody.access_token ?? tokenBody.data?.access_token;
3791
+ const refreshToken = tokenBody.refresh_token ?? tokenBody.data?.refresh_token;
3792
+ if (!accessToken) {
3793
+ throw new Error("the node minted no session");
3794
+ }
3795
+ return {
3796
+ accessToken,
3797
+ refreshToken: refreshToken ?? "",
3798
+ sessionKey: session.publicKey,
3799
+ sessionSecret: session.secret
3800
+ };
3801
+ }
3802
+
3677
3803
  // src/capabilities.ts
3678
3804
  var CAPABILITIES = {
3679
3805
  CAN_CREATE_CONTEXT: 1 << 0,
@@ -3806,10 +3932,12 @@ export {
3806
3932
  deviceCertPayload,
3807
3933
  discoverLocalNodes,
3808
3934
  encodeSignedInvitation,
3935
+ generateSessionKey,
3809
3936
  hasCap,
3810
3937
  intentHash,
3811
3938
  jsonCodec,
3812
3939
  localNodeUrl,
3940
+ login,
3813
3941
  mintDeviceId,
3814
3942
  nodeEndpoint,
3815
3943
  parseAuthCallback,