@calimero-network/mero-js 19.2.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.d.ts CHANGED
@@ -22,6 +22,10 @@ export type { DeviceCertInput } from './device-cert/index.js';
22
22
  export { signMemberJoinOp, encodeSignedInvitation, SIGNED_NAMESPACE_OP_SCHEMA_VERSION, } from './namespace-op/index.js';
23
23
  export type { SignMemberJoinInput } from './namespace-op/index.js';
24
24
  export type { WarrantInput } from './warrant/index.js';
25
+ export { signLoginStatement } from './login/index.js';
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';
25
29
  export * from './capabilities.js';
26
30
  export { DEFAULT_LOCAL_NODE_PORTS, localNodeUrl, nodeEndpoint, probeNodeHealth, discoverLocalNodes, } from './nodeDiscovery.js';
27
31
  export type { DiscoverLocalNodesOptions } from './nodeDiscovery.js';
package/dist/index.js CHANGED
@@ -26,6 +26,9 @@ export * from './relay/index.js';
26
26
  export { signWarrant, intentHash } from './warrant/index.js';
27
27
  export { signDeviceCert, mintDeviceId, deviceCertPayload, accountForRoot, } from './device-cert/index.js';
28
28
  export { signMemberJoinOp, encodeSignedInvitation, SIGNED_NAMESPACE_OP_SCHEMA_VERSION, } from './namespace-op/index.js';
29
+ // Login-statement signing — the device's half of a password-free session
30
+ export { signLoginStatement } from './login/index.js';
31
+ export { login, generateSessionKey } from './login/index.js';
29
32
  // Member capability bitmask constants & helpers
30
33
  export * from './capabilities.js';
31
34
  // Utilities
package/dist/index.mjs CHANGED
@@ -3608,6 +3608,171 @@ async function signMemberJoinOp(input) {
3608
3608
  return hex(concat(signable, signature, unendorsed));
3609
3609
  }
3610
3610
 
3611
+ // src/login/login.ts
3612
+ var SIGN_DOMAIN2 = new TextEncoder().encode("calimero.auth.login.v1");
3613
+ var AUDIENCE_TAG = {
3614
+ webOrigin: 0,
3615
+ codeSigningId: 1,
3616
+ cli: 2
3617
+ };
3618
+ function audienceBody(audience) {
3619
+ const encoder = new TextEncoder();
3620
+ switch (audience.kind) {
3621
+ case "webOrigin":
3622
+ return encoder.encode(audience.origin);
3623
+ case "codeSigningId":
3624
+ return encoder.encode(audience.id);
3625
+ case "cli":
3626
+ return new Uint8Array(0);
3627
+ }
3628
+ }
3629
+ function audienceSigningBytes(audience) {
3630
+ return concat(
3631
+ Uint8Array.from([AUDIENCE_TAG[audience.kind]]),
3632
+ audienceBody(audience)
3633
+ );
3634
+ }
3635
+ function audienceWire(audience) {
3636
+ const tag = Uint8Array.from([AUDIENCE_TAG[audience.kind]]);
3637
+ if (audience.kind === "cli") {
3638
+ return tag;
3639
+ }
3640
+ const body = audienceBody(audience);
3641
+ return concat(tag, u32le(body.length), body);
3642
+ }
3643
+ async function signLoginStatement(input) {
3644
+ const node = fromHex(input.node, "node", 32);
3645
+ const challenge = fromHex(input.challenge, "challenge", 32);
3646
+ const sessionKey = fromHex(input.sessionKey, "sessionKey", 32);
3647
+ const key = await importSigningKey(input.deviceSecret);
3648
+ const deviceKey = await derivePublicKey(input.deviceSecret);
3649
+ const issuedAt = u64le(input.issuedAt);
3650
+ const expiresAt = u64le(input.expiresAt);
3651
+ const preimage = await domainHash(SIGN_DOMAIN2, [
3652
+ node,
3653
+ audienceSigningBytes(input.audience),
3654
+ challenge,
3655
+ sessionKey,
3656
+ deviceKey,
3657
+ issuedAt,
3658
+ expiresAt
3659
+ ]);
3660
+ const signature = new Uint8Array(
3661
+ await crypto.subtle.sign({ name: "Ed25519" }, key, preimage)
3662
+ );
3663
+ return hex(
3664
+ concat(
3665
+ node,
3666
+ audienceWire(input.audience),
3667
+ challenge,
3668
+ sessionKey,
3669
+ deviceKey,
3670
+ issuedAt,
3671
+ expiresAt,
3672
+ signature
3673
+ )
3674
+ );
3675
+ }
3676
+
3677
+ // src/login/session.ts
3678
+ var DEFAULT_TTL_SECONDS2 = 300;
3679
+ async function generateSessionKey() {
3680
+ const secret = hex(crypto.getRandomValues(new Uint8Array(32)));
3681
+ return { publicKey: hex(await derivePublicKey(secret)), secret };
3682
+ }
3683
+ function audienceLabel(audience) {
3684
+ switch (audience.kind) {
3685
+ case "webOrigin":
3686
+ return audience.origin;
3687
+ case "codeSigningId":
3688
+ return audience.id;
3689
+ case "cli":
3690
+ return "cli";
3691
+ }
3692
+ }
3693
+ async function login(config) {
3694
+ const baseUrl = config.nodeUrl.replace(/\/+$/, "");
3695
+ const doFetch = config.fetch ?? globalThis.fetch;
3696
+ const timeoutMs = config.timeoutMs ?? 1e4;
3697
+ const request = async (path, init) => {
3698
+ const controller = new AbortController();
3699
+ const timer = setTimeout(() => controller.abort(), timeoutMs);
3700
+ let response;
3701
+ try {
3702
+ response = await doFetch(`${baseUrl}${path}`, {
3703
+ ...init,
3704
+ signal: controller.signal
3705
+ });
3706
+ } catch (err) {
3707
+ throw new HTTPError(
3708
+ 0,
3709
+ err instanceof Error ? err.name : "NetworkError",
3710
+ `${baseUrl}${path}`,
3711
+ new Headers(),
3712
+ err instanceof Error ? err.message : String(err)
3713
+ );
3714
+ } finally {
3715
+ clearTimeout(timer);
3716
+ }
3717
+ const text = await response.text().catch(() => "");
3718
+ if (!response.ok) {
3719
+ throw new HTTPError(
3720
+ response.status,
3721
+ response.statusText,
3722
+ `${baseUrl}${path}`,
3723
+ response.headers,
3724
+ text
3725
+ );
3726
+ }
3727
+ return text ? JSON.parse(text) : {};
3728
+ };
3729
+ const challengeBody = await request("/auth/challenge", {
3730
+ method: "GET",
3731
+ headers: { Accept: "application/json" }
3732
+ });
3733
+ const challenge = challengeBody.challenge ?? challengeBody.data?.challenge;
3734
+ if (!challenge) {
3735
+ throw new Error("the node issued no challenge");
3736
+ }
3737
+ const session = await generateSessionKey();
3738
+ const issuedAt = Math.floor(Date.now() / 1e3);
3739
+ const statement = await signLoginStatement({
3740
+ node: config.node,
3741
+ audience: config.audience,
3742
+ challenge,
3743
+ sessionKey: session.publicKey,
3744
+ issuedAt,
3745
+ expiresAt: issuedAt + (config.ttlSeconds ?? DEFAULT_TTL_SECONDS2),
3746
+ deviceSecret: config.deviceSecret
3747
+ });
3748
+ const tokenBody = await request("/auth/token", {
3749
+ method: "POST",
3750
+ headers: { "Content-Type": "application/json", Accept: "application/json" },
3751
+ body: JSON.stringify({
3752
+ auth_method: "account_proof",
3753
+ public_key: session.publicKey,
3754
+ client_name: config.clientName ?? audienceLabel(config.audience),
3755
+ timestamp: issuedAt,
3756
+ provider_data: {
3757
+ challenge,
3758
+ login_statement: statement,
3759
+ account_proof: config.accountProof
3760
+ }
3761
+ })
3762
+ });
3763
+ const accessToken = tokenBody.access_token ?? tokenBody.data?.access_token;
3764
+ const refreshToken = tokenBody.refresh_token ?? tokenBody.data?.refresh_token;
3765
+ if (!accessToken) {
3766
+ throw new Error("the node minted no session");
3767
+ }
3768
+ return {
3769
+ accessToken,
3770
+ refreshToken: refreshToken ?? "",
3771
+ sessionKey: session.publicKey,
3772
+ sessionSecret: session.secret
3773
+ };
3774
+ }
3775
+
3611
3776
  // src/capabilities.ts
3612
3777
  var CAPABILITIES = {
3613
3778
  CAN_CREATE_CONTEXT: 1 << 0,
@@ -3740,15 +3905,18 @@ export {
3740
3905
  deviceCertPayload,
3741
3906
  discoverLocalNodes,
3742
3907
  encodeSignedInvitation,
3908
+ generateSessionKey,
3743
3909
  hasCap,
3744
3910
  intentHash,
3745
3911
  jsonCodec,
3746
3912
  localNodeUrl,
3913
+ login,
3747
3914
  mintDeviceId,
3748
3915
  nodeEndpoint,
3749
3916
  parseAuthCallback,
3750
3917
  probeNodeHealth,
3751
3918
  signDeviceCert,
3919
+ signLoginStatement,
3752
3920
  signMemberJoinOp,
3753
3921
  signWarrant,
3754
3922
  withCap,