@calimero-network/mero-js 13.1.0 → 13.2.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
@@ -60,6 +60,9 @@ __export(index_exports, {
60
60
  nodeEndpoint: () => nodeEndpoint,
61
61
  parseAuthCallback: () => parseAuthCallback,
62
62
  probeNodeHealth: () => probeNodeHealth,
63
+ sameAccount: () => sameAccount,
64
+ toAccountBase58: () => toAccountBase58,
65
+ toAccountHex: () => toAccountHex,
63
66
  withCap: () => withCap,
64
67
  withRetry: () => withRetry,
65
68
  withoutCap: () => withoutCap
@@ -1595,6 +1598,7 @@ function isGroupMigrationEvent(event) {
1595
1598
  }
1596
1599
 
1597
1600
  // src/events/sse.ts
1601
+ var TERMINAL_AUTH_ERRORS2 = /* @__PURE__ */ new Set(["token_reuse", "token_revoked"]);
1598
1602
  var SseClient = class {
1599
1603
  constructor(opts) {
1600
1604
  this.sessionId = null;
@@ -1672,7 +1676,8 @@ var SseClient = class {
1672
1676
  this.abortController = new AbortController();
1673
1677
  try {
1674
1678
  const token = await this.getAuthToken();
1675
- const response = await fetch(`${this.baseUrl}/sse`, {
1679
+ const url = `${this.baseUrl}/sse`;
1680
+ const response = await fetch(url, {
1676
1681
  headers: {
1677
1682
  "Authorization": `Bearer ${token}`,
1678
1683
  "Accept": "text/event-stream"
@@ -1680,7 +1685,25 @@ var SseClient = class {
1680
1685
  signal: this.abortController.signal
1681
1686
  });
1682
1687
  if (!response.ok) {
1683
- throw new Error(`SSE connection failed: ${response.status}`);
1688
+ const authError = response.headers.get("x-auth-error");
1689
+ const bodyText = await response.text().catch(() => void 0);
1690
+ if (response.status === 401 && authError && TERMINAL_AUTH_ERRORS2.has(authError)) {
1691
+ throw new AuthRevokedError(
1692
+ authError,
1693
+ response.status,
1694
+ response.statusText,
1695
+ url,
1696
+ response.headers,
1697
+ bodyText
1698
+ );
1699
+ }
1700
+ throw new HTTPError(
1701
+ response.status,
1702
+ response.statusText,
1703
+ url,
1704
+ response.headers,
1705
+ bodyText
1706
+ );
1684
1707
  }
1685
1708
  if (!response.body) {
1686
1709
  throw new Error("SSE response has no body");
@@ -2446,6 +2469,93 @@ function createMeroJs(config) {
2446
2469
  return new MeroJs(config);
2447
2470
  }
2448
2471
 
2472
+ // src/account/index.ts
2473
+ var BASE58_ALPHABET = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz";
2474
+ var HEX_ACCOUNT = /^[0-9a-f]{64}$/i;
2475
+ function encodeBase58(bytes) {
2476
+ if (bytes.length === 0) return "";
2477
+ const digits = [0];
2478
+ for (const byte of bytes) {
2479
+ let carry = byte;
2480
+ for (let i = 0; i < digits.length; i++) {
2481
+ carry += digits[i] << 8;
2482
+ digits[i] = carry % 58;
2483
+ carry = carry / 58 | 0;
2484
+ }
2485
+ while (carry > 0) {
2486
+ digits.push(carry % 58);
2487
+ carry = carry / 58 | 0;
2488
+ }
2489
+ }
2490
+ let out = "";
2491
+ for (const byte of bytes) {
2492
+ if (byte !== 0) break;
2493
+ out += BASE58_ALPHABET[0];
2494
+ }
2495
+ for (let i = digits.length - 1; i >= 0; i--) {
2496
+ out += BASE58_ALPHABET[digits[i]];
2497
+ }
2498
+ return out;
2499
+ }
2500
+ function decodeBase58(value) {
2501
+ if (value.length === 0) return null;
2502
+ const bytes = [0];
2503
+ for (const char of value) {
2504
+ const index = BASE58_ALPHABET.indexOf(char);
2505
+ if (index < 0) return null;
2506
+ let carry = index;
2507
+ for (let i = 0; i < bytes.length; i++) {
2508
+ carry += bytes[i] * 58;
2509
+ bytes[i] = carry & 255;
2510
+ carry >>= 8;
2511
+ }
2512
+ while (carry > 0) {
2513
+ bytes.push(carry & 255);
2514
+ carry >>= 8;
2515
+ }
2516
+ }
2517
+ let leadingZeros = 0;
2518
+ for (const char of value) {
2519
+ if (char !== BASE58_ALPHABET[0]) break;
2520
+ leadingZeros++;
2521
+ }
2522
+ return Uint8Array.from([
2523
+ ...new Array(leadingZeros).fill(0),
2524
+ ...bytes.reverse()
2525
+ ]);
2526
+ }
2527
+ function toHex(bytes) {
2528
+ let out = "";
2529
+ for (const byte of bytes) out += byte.toString(16).padStart(2, "0");
2530
+ return out;
2531
+ }
2532
+ function fromHex(hex) {
2533
+ const bytes = new Uint8Array(hex.length / 2);
2534
+ for (let i = 0; i < bytes.length; i++) {
2535
+ bytes[i] = parseInt(hex.slice(i * 2, i * 2 + 2), 16);
2536
+ }
2537
+ return bytes;
2538
+ }
2539
+ function toAccountHex(id) {
2540
+ const trimmed = (id ?? "").trim();
2541
+ if (!trimmed) return "";
2542
+ if (HEX_ACCOUNT.test(trimmed)) return trimmed.toLowerCase();
2543
+ const decoded = decodeBase58(trimmed);
2544
+ if (!decoded || decoded.length !== 32) return trimmed;
2545
+ return toHex(decoded);
2546
+ }
2547
+ function toAccountBase58(id) {
2548
+ const trimmed = (id ?? "").trim();
2549
+ if (!trimmed) return "";
2550
+ if (!HEX_ACCOUNT.test(trimmed)) return trimmed;
2551
+ return encodeBase58(fromHex(trimmed));
2552
+ }
2553
+ function sameAccount(a, b) {
2554
+ const left = toAccountHex(a);
2555
+ const right = toAccountHex(b);
2556
+ return HEX_ACCOUNT.test(left) && left === right;
2557
+ }
2558
+
2449
2559
  // src/token-store/index.ts
2450
2560
  var MemoryTokenStore = class {
2451
2561
  constructor() {