@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/account/index.d.ts +43 -0
- package/dist/account/index.js +139 -0
- package/dist/events/sse.js +23 -2
- package/dist/index.browser.mjs +2 -2
- package/dist/index.browser.mjs.map +4 -4
- package/dist/index.cjs +112 -2
- package/dist/index.cjs.map +4 -4
- package/dist/index.d.ts +1 -0
- package/dist/index.js +2 -0
- package/dist/index.mjs +112 -2
- package/dist/index.mjs.map +4 -4
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -5,6 +5,7 @@ export * from './auth-api/index.js';
|
|
|
5
5
|
export * from './admin-api/index.js';
|
|
6
6
|
export { parseAuthCallback, buildAuthLoginUrl } from './auth/index.js';
|
|
7
7
|
export type { AuthCallbackResult, AuthLoginOptions } from './auth/index.js';
|
|
8
|
+
export { toAccountHex, toAccountBase58, sameAccount, } from './account/index.js';
|
|
8
9
|
export { MemoryTokenStore, LocalStorageTokenStore } from './token-store/index.js';
|
|
9
10
|
export type { TokenStore } from './token-store/index.js';
|
|
10
11
|
export { RpcClient, RpcError } from './rpc/index.js';
|
package/dist/index.js
CHANGED
|
@@ -10,6 +10,8 @@ export * from './auth-api/index.js';
|
|
|
10
10
|
export * from './admin-api/index.js';
|
|
11
11
|
// Auth utilities
|
|
12
12
|
export { parseAuthCallback, buildAuthLoginUrl } from './auth/index.js';
|
|
13
|
+
// Account id encoding (hex on the admin API, base58 in contract data)
|
|
14
|
+
export { toAccountHex, toAccountBase58, sameAccount, } from './account/index.js';
|
|
13
15
|
// Token store
|
|
14
16
|
export { MemoryTokenStore, LocalStorageTokenStore } from './token-store/index.js';
|
|
15
17
|
// RPC client
|
package/dist/index.mjs
CHANGED
|
@@ -1527,6 +1527,7 @@ function isGroupMigrationEvent(event) {
|
|
|
1527
1527
|
}
|
|
1528
1528
|
|
|
1529
1529
|
// src/events/sse.ts
|
|
1530
|
+
var TERMINAL_AUTH_ERRORS2 = /* @__PURE__ */ new Set(["token_reuse", "token_revoked"]);
|
|
1530
1531
|
var SseClient = class {
|
|
1531
1532
|
constructor(opts) {
|
|
1532
1533
|
this.sessionId = null;
|
|
@@ -1604,7 +1605,8 @@ var SseClient = class {
|
|
|
1604
1605
|
this.abortController = new AbortController();
|
|
1605
1606
|
try {
|
|
1606
1607
|
const token = await this.getAuthToken();
|
|
1607
|
-
const
|
|
1608
|
+
const url = `${this.baseUrl}/sse`;
|
|
1609
|
+
const response = await fetch(url, {
|
|
1608
1610
|
headers: {
|
|
1609
1611
|
"Authorization": `Bearer ${token}`,
|
|
1610
1612
|
"Accept": "text/event-stream"
|
|
@@ -1612,7 +1614,25 @@ var SseClient = class {
|
|
|
1612
1614
|
signal: this.abortController.signal
|
|
1613
1615
|
});
|
|
1614
1616
|
if (!response.ok) {
|
|
1615
|
-
|
|
1617
|
+
const authError = response.headers.get("x-auth-error");
|
|
1618
|
+
const bodyText = await response.text().catch(() => void 0);
|
|
1619
|
+
if (response.status === 401 && authError && TERMINAL_AUTH_ERRORS2.has(authError)) {
|
|
1620
|
+
throw new AuthRevokedError(
|
|
1621
|
+
authError,
|
|
1622
|
+
response.status,
|
|
1623
|
+
response.statusText,
|
|
1624
|
+
url,
|
|
1625
|
+
response.headers,
|
|
1626
|
+
bodyText
|
|
1627
|
+
);
|
|
1628
|
+
}
|
|
1629
|
+
throw new HTTPError(
|
|
1630
|
+
response.status,
|
|
1631
|
+
response.statusText,
|
|
1632
|
+
url,
|
|
1633
|
+
response.headers,
|
|
1634
|
+
bodyText
|
|
1635
|
+
);
|
|
1616
1636
|
}
|
|
1617
1637
|
if (!response.body) {
|
|
1618
1638
|
throw new Error("SSE response has no body");
|
|
@@ -2378,6 +2398,93 @@ function createMeroJs(config) {
|
|
|
2378
2398
|
return new MeroJs(config);
|
|
2379
2399
|
}
|
|
2380
2400
|
|
|
2401
|
+
// src/account/index.ts
|
|
2402
|
+
var BASE58_ALPHABET = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz";
|
|
2403
|
+
var HEX_ACCOUNT = /^[0-9a-f]{64}$/i;
|
|
2404
|
+
function encodeBase58(bytes) {
|
|
2405
|
+
if (bytes.length === 0) return "";
|
|
2406
|
+
const digits = [0];
|
|
2407
|
+
for (const byte of bytes) {
|
|
2408
|
+
let carry = byte;
|
|
2409
|
+
for (let i = 0; i < digits.length; i++) {
|
|
2410
|
+
carry += digits[i] << 8;
|
|
2411
|
+
digits[i] = carry % 58;
|
|
2412
|
+
carry = carry / 58 | 0;
|
|
2413
|
+
}
|
|
2414
|
+
while (carry > 0) {
|
|
2415
|
+
digits.push(carry % 58);
|
|
2416
|
+
carry = carry / 58 | 0;
|
|
2417
|
+
}
|
|
2418
|
+
}
|
|
2419
|
+
let out = "";
|
|
2420
|
+
for (const byte of bytes) {
|
|
2421
|
+
if (byte !== 0) break;
|
|
2422
|
+
out += BASE58_ALPHABET[0];
|
|
2423
|
+
}
|
|
2424
|
+
for (let i = digits.length - 1; i >= 0; i--) {
|
|
2425
|
+
out += BASE58_ALPHABET[digits[i]];
|
|
2426
|
+
}
|
|
2427
|
+
return out;
|
|
2428
|
+
}
|
|
2429
|
+
function decodeBase58(value) {
|
|
2430
|
+
if (value.length === 0) return null;
|
|
2431
|
+
const bytes = [0];
|
|
2432
|
+
for (const char of value) {
|
|
2433
|
+
const index = BASE58_ALPHABET.indexOf(char);
|
|
2434
|
+
if (index < 0) return null;
|
|
2435
|
+
let carry = index;
|
|
2436
|
+
for (let i = 0; i < bytes.length; i++) {
|
|
2437
|
+
carry += bytes[i] * 58;
|
|
2438
|
+
bytes[i] = carry & 255;
|
|
2439
|
+
carry >>= 8;
|
|
2440
|
+
}
|
|
2441
|
+
while (carry > 0) {
|
|
2442
|
+
bytes.push(carry & 255);
|
|
2443
|
+
carry >>= 8;
|
|
2444
|
+
}
|
|
2445
|
+
}
|
|
2446
|
+
let leadingZeros = 0;
|
|
2447
|
+
for (const char of value) {
|
|
2448
|
+
if (char !== BASE58_ALPHABET[0]) break;
|
|
2449
|
+
leadingZeros++;
|
|
2450
|
+
}
|
|
2451
|
+
return Uint8Array.from([
|
|
2452
|
+
...new Array(leadingZeros).fill(0),
|
|
2453
|
+
...bytes.reverse()
|
|
2454
|
+
]);
|
|
2455
|
+
}
|
|
2456
|
+
function toHex(bytes) {
|
|
2457
|
+
let out = "";
|
|
2458
|
+
for (const byte of bytes) out += byte.toString(16).padStart(2, "0");
|
|
2459
|
+
return out;
|
|
2460
|
+
}
|
|
2461
|
+
function fromHex(hex) {
|
|
2462
|
+
const bytes = new Uint8Array(hex.length / 2);
|
|
2463
|
+
for (let i = 0; i < bytes.length; i++) {
|
|
2464
|
+
bytes[i] = parseInt(hex.slice(i * 2, i * 2 + 2), 16);
|
|
2465
|
+
}
|
|
2466
|
+
return bytes;
|
|
2467
|
+
}
|
|
2468
|
+
function toAccountHex(id) {
|
|
2469
|
+
const trimmed = (id ?? "").trim();
|
|
2470
|
+
if (!trimmed) return "";
|
|
2471
|
+
if (HEX_ACCOUNT.test(trimmed)) return trimmed.toLowerCase();
|
|
2472
|
+
const decoded = decodeBase58(trimmed);
|
|
2473
|
+
if (!decoded || decoded.length !== 32) return trimmed;
|
|
2474
|
+
return toHex(decoded);
|
|
2475
|
+
}
|
|
2476
|
+
function toAccountBase58(id) {
|
|
2477
|
+
const trimmed = (id ?? "").trim();
|
|
2478
|
+
if (!trimmed) return "";
|
|
2479
|
+
if (!HEX_ACCOUNT.test(trimmed)) return trimmed;
|
|
2480
|
+
return encodeBase58(fromHex(trimmed));
|
|
2481
|
+
}
|
|
2482
|
+
function sameAccount(a, b) {
|
|
2483
|
+
const left = toAccountHex(a);
|
|
2484
|
+
const right = toAccountHex(b);
|
|
2485
|
+
return HEX_ACCOUNT.test(left) && left === right;
|
|
2486
|
+
}
|
|
2487
|
+
|
|
2381
2488
|
// src/token-store/index.ts
|
|
2382
2489
|
var MemoryTokenStore = class {
|
|
2383
2490
|
constructor() {
|
|
@@ -2576,6 +2683,9 @@ export {
|
|
|
2576
2683
|
nodeEndpoint,
|
|
2577
2684
|
parseAuthCallback,
|
|
2578
2685
|
probeNodeHealth,
|
|
2686
|
+
sameAccount,
|
|
2687
|
+
toAccountBase58,
|
|
2688
|
+
toAccountHex,
|
|
2579
2689
|
withCap,
|
|
2580
2690
|
withRetry,
|
|
2581
2691
|
withoutCap
|