@calimero-network/mero-js 19.14.0 → 19.15.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
@@ -77,12 +77,14 @@ __export(index_exports, {
77
77
  nodeEndpoint: () => nodeEndpoint,
78
78
  parseAuthCallback: () => parseAuthCallback,
79
79
  probeNodeHealth: () => probeNodeHealth,
80
+ requestBodyHash: () => requestBodyHash,
80
81
  routingProofHeaders: () => routingProofHeaders,
81
82
  signAccountLink: () => signAccountLink,
82
83
  signAccountLogin: () => signAccountLogin,
83
84
  signDeviceCert: () => signDeviceCert,
84
85
  signLoginStatement: () => signLoginStatement,
85
86
  signMemberJoinOp: () => signMemberJoinOp,
87
+ signRequest: () => signRequest,
86
88
  signRoutingChallenge: () => signRoutingChallenge,
87
89
  signWarrant: () => signWarrant,
88
90
  withCap: () => withCap,
@@ -2052,6 +2054,13 @@ function isGroupMigrationEvent(event) {
2052
2054
 
2053
2055
  // src/events/sse.ts
2054
2056
  var TERMINAL_AUTH_ERRORS2 = /* @__PURE__ */ new Set(["token_reuse", "token_revoked"]);
2057
+ var TERMINAL_AUTH_STATUSES = /* @__PURE__ */ new Set([401, 403]);
2058
+ function isTerminalAuthFailure(response) {
2059
+ const authError = response.headers.get("x-auth-error");
2060
+ if (!authError) return null;
2061
+ if (!TERMINAL_AUTH_STATUSES.has(response.status)) return null;
2062
+ return TERMINAL_AUTH_ERRORS2.has(authError) ? authError : null;
2063
+ }
2055
2064
  var SseClient = class {
2056
2065
  constructor(opts) {
2057
2066
  this.sessionId = null;
@@ -2138,11 +2147,11 @@ var SseClient = class {
2138
2147
  signal: this.abortController.signal
2139
2148
  });
2140
2149
  if (!response.ok) {
2141
- const authError = response.headers.get("x-auth-error");
2150
+ const terminal = isTerminalAuthFailure(response);
2142
2151
  const bodyText = await response.text().catch(() => void 0);
2143
- if (response.status === 401 && authError && TERMINAL_AUTH_ERRORS2.has(authError)) {
2152
+ if (terminal) {
2144
2153
  throw new AuthRevokedError(
2145
- authError,
2154
+ terminal,
2146
2155
  response.status,
2147
2156
  response.statusText,
2148
2157
  url,
@@ -2171,6 +2180,7 @@ var SseClient = class {
2171
2180
  if (this.closed) return;
2172
2181
  const error = err instanceof Error ? err : new Error(String(err));
2173
2182
  this.emit("error", error);
2183
+ if (error instanceof AuthRevokedError) return;
2174
2184
  this.scheduleReconnect();
2175
2185
  }
2176
2186
  }
@@ -2301,7 +2311,25 @@ var SseClient = class {
2301
2311
  })
2302
2312
  });
2303
2313
  if (!response.ok) {
2304
- this.emit("error", new Error(`SSE ${method} failed: ${response.status}`));
2314
+ const terminal = isTerminalAuthFailure(response);
2315
+ const bodyText = await response.text().catch(() => void 0);
2316
+ this.emit(
2317
+ "error",
2318
+ terminal ? new AuthRevokedError(
2319
+ terminal,
2320
+ response.status,
2321
+ response.statusText,
2322
+ `${this.baseUrl}/sse/subscription`,
2323
+ response.headers,
2324
+ bodyText
2325
+ ) : new HTTPError(
2326
+ response.status,
2327
+ response.statusText,
2328
+ `${this.baseUrl}/sse/subscription`,
2329
+ response.headers,
2330
+ bodyText
2331
+ )
2332
+ );
2305
2333
  }
2306
2334
  } catch (err) {
2307
2335
  this.emit("error", err instanceof Error ? err : new Error(`SSE ${method} failed`));
@@ -6509,8 +6537,47 @@ async function signMemberJoinOp(input) {
6509
6537
  return hex(concat(signable, signature, unendorsed));
6510
6538
  }
6511
6539
 
6540
+ // src/request/request-sig.ts
6541
+ var SIGN_DOMAIN2 = new TextEncoder().encode("calimero.auth.request.v1");
6542
+ var BODY_DOMAIN = new TextEncoder().encode("calimero.auth.request.body.v1");
6543
+ function borshString2(value) {
6544
+ const bytes = new TextEncoder().encode(value);
6545
+ return concat(u32le(bytes.length), bytes);
6546
+ }
6547
+ async function requestBodyHash(body = new Uint8Array(0)) {
6548
+ const bytes = typeof body === "string" ? new TextEncoder().encode(body) : body;
6549
+ return domainHash(BODY_DOMAIN, [bytes]);
6550
+ }
6551
+ async function signRequest(input) {
6552
+ const key = await importSigningKey(input.signerSecret);
6553
+ const bodyHash = await requestBodyHash(input.body);
6554
+ const issuedAt = u64le(input.issuedAt);
6555
+ const expiresAt = u64le(input.expiresAt);
6556
+ const encoder = new TextEncoder();
6557
+ const preimage = await domainHash(SIGN_DOMAIN2, [
6558
+ encoder.encode(input.method),
6559
+ encoder.encode(input.path),
6560
+ bodyHash,
6561
+ issuedAt,
6562
+ expiresAt
6563
+ ]);
6564
+ const signature = new Uint8Array(
6565
+ await crypto.subtle.sign({ name: "Ed25519" }, key, preimage)
6566
+ );
6567
+ return hex(
6568
+ concat(
6569
+ borshString2(input.method),
6570
+ borshString2(input.path),
6571
+ bodyHash,
6572
+ issuedAt,
6573
+ expiresAt,
6574
+ signature
6575
+ )
6576
+ );
6577
+ }
6578
+
6512
6579
  // src/login/login.ts
6513
- var SIGN_DOMAIN2 = new TextEncoder().encode("calimero.auth.login.v1");
6580
+ var SIGN_DOMAIN3 = new TextEncoder().encode("calimero.auth.login.v1");
6514
6581
  var AUDIENCE_TAG = {
6515
6582
  webOrigin: 0,
6516
6583
  codeSigningId: 1,
@@ -6549,7 +6616,7 @@ async function signLoginStatement(input) {
6549
6616
  const deviceKey = await derivePublicKey(input.deviceSecret);
6550
6617
  const issuedAt = u64le(input.issuedAt);
6551
6618
  const expiresAt = u64le(input.expiresAt);
6552
- const preimage = await domainHash(SIGN_DOMAIN2, [
6619
+ const preimage = await domainHash(SIGN_DOMAIN3, [
6553
6620
  node,
6554
6621
  audienceSigningBytes(input.audience),
6555
6622
  challenge,