@calimero-network/mero-react 9.8.1 → 9.9.1
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 +115 -21
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +116 -22
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { createContext, useMemo, useState, useRef, useCallback, useEffect, useContext } from 'react';
|
|
2
|
-
import { signGroupInvitation, signMemberJoinOp, CloudClient, LocalStorageTokenStore, parseAuthCallback, MeroJs, buildAuthLoginUrl, DEFAULT_LOCAL_NODE_PORTS, discoverLocalNodes, nodeEndpoint, readEnrolmentCallback, completeDeviceEnrolment, deviceEnrolmentUrl, memberJoinedOpenOp, signerFromSecret, groupDeletedOp, memberAddedOp, memberRemovedOp, subgroupCreation, memberRoleSetOp, defaultCapabilitiesSetOp, subgroupVisibilitySetOp, groupMetadataSetOp, memberMetadataSetOp, contextMetadataSetOp, groupReparentedOp, contextDetachedOp, compareSemver, AuthRevokedError, HTTPError, memberLeftOp, memberCapabilitySetOp,
|
|
2
|
+
import { signGroupInvitation, signMemberJoinOp, CloudClient, LocalStorageTokenStore, parseAuthCallback, MeroJs, buildAuthLoginUrl, DEFAULT_LOCAL_NODE_PORTS, discoverLocalNodes, nodeEndpoint, readEnrolmentCallback, completeDeviceEnrolment, deviceEnrolmentUrl, memberJoinedOpenOp, signerFromSecret, groupDeletedOp, memberAddedOp, memberRemovedOp, subgroupCreation, memberRoleSetOp, defaultCapabilitiesSetOp, subgroupVisibilitySetOp, groupMetadataSetOp, memberMetadataSetOp, contextMetadataSetOp, groupReparentedOp, contextDetachedOp, compareSemver, AuthRevokedError, HTTPError, memberLeftOp, memberCapabilitySetOp, createMeroClient, RelayClient, createLocalStorageNonceSource, classifyError, attestRelayNodeKey, login, defaultAudience, AccountNotLinkedError, AccountHaRefusedError, createSignedReleaseVerifier, fetchNodeRelease, cloudNodeReleaseUrl, DEFAULT_RELEASE_MIRROR, fetchNodeReleaseVersion } from '@calimero-network/mero-js';
|
|
3
3
|
export * from '@calimero-network/mero-js';
|
|
4
4
|
export { CAPABILITIES, DEFAULT_LOCAL_NODE_PORTS, discoverLocalNodes, hasCap, localNodeUrl, nodeEndpoint, probeNodeHealth, withCap, withoutCap } from '@calimero-network/mero-js';
|
|
5
5
|
import { jsx, jsxs, Fragment } from 'react/jsx-runtime';
|
|
@@ -170,41 +170,120 @@ function dcapVerifyFrom(mod) {
|
|
|
170
170
|
}
|
|
171
171
|
return verify;
|
|
172
172
|
}
|
|
173
|
-
|
|
173
|
+
var RelayKeyTransportError = class extends Error {
|
|
174
|
+
constructor(step, cause) {
|
|
175
|
+
super(`${step}: ${cause instanceof Error ? cause.message : String(cause)}`);
|
|
176
|
+
this.step = step;
|
|
177
|
+
this.cause = cause;
|
|
178
|
+
this.name = "RelayKeyTransportError";
|
|
179
|
+
}
|
|
180
|
+
};
|
|
181
|
+
async function transport(step, run) {
|
|
182
|
+
try {
|
|
183
|
+
return await run();
|
|
184
|
+
} catch (e) {
|
|
185
|
+
throw e instanceof RelayKeyTransportError ? e : new RelayKeyTransportError(step, e);
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
function relayFetch(marked) {
|
|
189
|
+
return async (input, init) => {
|
|
190
|
+
let response;
|
|
191
|
+
try {
|
|
192
|
+
response = await globalThis.fetch(input, init);
|
|
193
|
+
} catch (e) {
|
|
194
|
+
marked.failure = e;
|
|
195
|
+
throw new RelayKeyTransportError(`reaching ${String(input)}`, e);
|
|
196
|
+
}
|
|
197
|
+
if (!response.ok) marked.failure ?? (marked.failure = new Error(`${String(input)} answered HTTP ${response.status}`));
|
|
198
|
+
return response;
|
|
199
|
+
};
|
|
200
|
+
}
|
|
201
|
+
function relayQuoteVerifier(relayUrl, fetchRelay) {
|
|
174
202
|
const verify = async (attestation) => {
|
|
175
|
-
|
|
176
|
-
(
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
203
|
+
const dcapVerify = await transport("loading the quote verifier", () => {
|
|
204
|
+
loadedDcap ?? (loadedDcap = import('@phala/dcap-qvl').then(
|
|
205
|
+
(mod) => dcapVerifyFrom(mod),
|
|
206
|
+
(error) => {
|
|
207
|
+
loadedDcap = void 0;
|
|
208
|
+
throw error;
|
|
209
|
+
}
|
|
210
|
+
));
|
|
211
|
+
return loadedDcap;
|
|
212
|
+
});
|
|
182
213
|
return createSignedReleaseVerifier({
|
|
183
|
-
dcapVerify
|
|
184
|
-
|
|
214
|
+
dcapVerify,
|
|
215
|
+
// Fetched only. Its signature, its version and the quote's registers are
|
|
216
|
+
// all checked by the verifier after this returns, and none of that is a
|
|
217
|
+
// transport question.
|
|
218
|
+
release: () => transport(
|
|
219
|
+
"fetching the release the relay runs",
|
|
220
|
+
async () => fetchNodeRelease(
|
|
221
|
+
cloudNodeReleaseUrl(DEFAULT_RELEASE_MIRROR, await fetchNodeReleaseVersion(relayUrl, { fetch: fetchRelay }))
|
|
222
|
+
)
|
|
223
|
+
),
|
|
185
224
|
profile: TRUSTED_RELAY_PROFILE,
|
|
186
225
|
minReleaseVersion: MIN_RELAY_RELEASE
|
|
187
226
|
})(attestation);
|
|
188
227
|
};
|
|
189
228
|
return Object.assign(verify, { includeCollateral: true });
|
|
190
229
|
}
|
|
191
|
-
async function
|
|
230
|
+
async function attemptRelayNodeKey(relayUrl) {
|
|
192
231
|
const pinned = readPinnedRelayNodeKey(relayUrl);
|
|
193
|
-
if (pinned) return pinned;
|
|
232
|
+
if (pinned) return { kind: "learned", nodeKey: pinned };
|
|
233
|
+
const marked = { failure: null };
|
|
234
|
+
const fetchRelay = relayFetch(marked);
|
|
194
235
|
try {
|
|
195
236
|
const local = isLoopback(relayUrl);
|
|
196
237
|
const { nodeKey } = await attestRelayNodeKey({
|
|
197
238
|
relayUrl,
|
|
198
239
|
allowMock: local,
|
|
199
|
-
|
|
240
|
+
fetch: fetchRelay,
|
|
241
|
+
...local ? {} : { verify: relayQuoteVerifier(relayUrl, fetchRelay) }
|
|
200
242
|
});
|
|
201
243
|
pinRelayNodeKey(relayUrl, nodeKey);
|
|
202
|
-
return nodeKey;
|
|
244
|
+
return { kind: "learned", nodeKey };
|
|
203
245
|
} catch (e) {
|
|
204
|
-
|
|
205
|
-
return
|
|
246
|
+
if (e instanceof RelayKeyTransportError || marked.failure) return { kind: "unavailable", error: e };
|
|
247
|
+
return { kind: "refused", error: e };
|
|
206
248
|
}
|
|
207
249
|
}
|
|
250
|
+
async function resolveRelayNodeKey(relayUrl) {
|
|
251
|
+
const attempt = await attemptRelayNodeKey(relayUrl);
|
|
252
|
+
if (attempt.kind === "learned") return attempt.nodeKey;
|
|
253
|
+
console.warn("[mero-react] could not learn the relay node key from its attestation", attempt.error);
|
|
254
|
+
return null;
|
|
255
|
+
}
|
|
256
|
+
var RELAY_NODE_KEY_RETRY_MS = [1e3, 2e3, 5e3, 1e4, 3e4];
|
|
257
|
+
function pause(ms, signal) {
|
|
258
|
+
return new Promise((resolve) => {
|
|
259
|
+
if (signal?.aborted) return resolve();
|
|
260
|
+
const timer = setTimeout(done, ms);
|
|
261
|
+
function done() {
|
|
262
|
+
clearTimeout(timer);
|
|
263
|
+
signal?.removeEventListener("abort", done);
|
|
264
|
+
resolve();
|
|
265
|
+
}
|
|
266
|
+
signal?.addEventListener("abort", done);
|
|
267
|
+
});
|
|
268
|
+
}
|
|
269
|
+
async function learnRelayNodeKey(relayUrl, options = {}) {
|
|
270
|
+
const { signal } = options;
|
|
271
|
+
for (let failures = 0; !signal?.aborted; failures += 1) {
|
|
272
|
+
const attempt = await attemptRelayNodeKey(relayUrl);
|
|
273
|
+
if (attempt.kind === "learned") return attempt.nodeKey;
|
|
274
|
+
if (attempt.kind === "refused") {
|
|
275
|
+
console.warn("[mero-react] refusing the relay's attestation; its node key stays unknown", attempt.error);
|
|
276
|
+
return null;
|
|
277
|
+
}
|
|
278
|
+
const wait = RELAY_NODE_KEY_RETRY_MS[Math.min(failures, RELAY_NODE_KEY_RETRY_MS.length - 1)];
|
|
279
|
+
console.warn(
|
|
280
|
+
`[mero-react] could not reach what proves the relay's node key; retrying in ${wait / 1e3} s`,
|
|
281
|
+
attempt.error
|
|
282
|
+
);
|
|
283
|
+
await pause(wait, signal);
|
|
284
|
+
}
|
|
285
|
+
return null;
|
|
286
|
+
}
|
|
208
287
|
function jwtExpiryMs(token) {
|
|
209
288
|
try {
|
|
210
289
|
const payload = JSON.parse(atob(token.split(".")[1].replace(/-/g, "+").replace(/_/g, "/")));
|
|
@@ -304,6 +383,11 @@ function rememberRelay(account, relayUrl, at) {
|
|
|
304
383
|
function relayForContext(account, contextId) {
|
|
305
384
|
return readRelayMap(account).contexts[contextId] ?? null;
|
|
306
385
|
}
|
|
386
|
+
function carryExecutorAccount(prev, next) {
|
|
387
|
+
if (next.executorAccount || !prev?.executorAccount || !prev.relayUrl || !next.relayUrl) return next;
|
|
388
|
+
if (relayOrigin(prev.relayUrl) !== relayOrigin(next.relayUrl)) return next;
|
|
389
|
+
return { ...next, executorAccount: prev.executorAccount };
|
|
390
|
+
}
|
|
307
391
|
function knownRelays(s) {
|
|
308
392
|
const map = readRelayMap(s.account);
|
|
309
393
|
const all = [s.relayUrl, ...Object.values(map.namespaces), ...Object.values(map.contexts)].filter((u) => typeof u === "string" && u.length > 0).map(relayOrigin);
|
|
@@ -1071,7 +1155,7 @@ async function joinAsAccount(session, namespaceId, invitation, opts) {
|
|
|
1071
1155
|
});
|
|
1072
1156
|
}
|
|
1073
1157
|
if (outcome.session.relayUrl) rememberRelay(session.account, outcome.session.relayUrl, { namespaceId });
|
|
1074
|
-
opts.onJoined(outcome.session);
|
|
1158
|
+
opts.onJoined(carryExecutorAccount(session, outcome.session));
|
|
1075
1159
|
return { namespaceId };
|
|
1076
1160
|
}
|
|
1077
1161
|
|
|
@@ -1465,10 +1549,11 @@ function MeroProvider({
|
|
|
1465
1549
|
setNodeUrlState(relayUrl);
|
|
1466
1550
|
setIsLoading(awaitingKey);
|
|
1467
1551
|
let active = true;
|
|
1552
|
+
const stop = new AbortController();
|
|
1468
1553
|
(async () => {
|
|
1469
1554
|
if (!client2 || !relayUrl) return;
|
|
1470
1555
|
if (awaitingKey) {
|
|
1471
|
-
const nodeKey = await
|
|
1556
|
+
const nodeKey = await learnRelayNodeKey(relayUrl, { signal: stop.signal });
|
|
1472
1557
|
if (!active) return;
|
|
1473
1558
|
setMero(nodeKey ? buildDelegatedClient(routed) ?? client2 : client2);
|
|
1474
1559
|
setIsLoading(false);
|
|
@@ -1488,6 +1573,7 @@ function MeroProvider({
|
|
|
1488
1573
|
})();
|
|
1489
1574
|
return () => {
|
|
1490
1575
|
active = false;
|
|
1576
|
+
stop.abort();
|
|
1491
1577
|
};
|
|
1492
1578
|
}, [delegated, contextId]);
|
|
1493
1579
|
useEffect(() => {
|
|
@@ -2429,26 +2515,33 @@ var DEVICE_KEY = "calimero.device";
|
|
|
2429
2515
|
var STATE_KEY = "calimero.enrol.state";
|
|
2430
2516
|
var HOSTED_WALLET = "https://wallet.cloud.calimero.network/account-enroll";
|
|
2431
2517
|
var hex = (b) => [...new Uint8Array(b)].map((x) => x.toString(16).padStart(2, "0")).join("");
|
|
2518
|
+
function executorOf(relay) {
|
|
2519
|
+
const value = relay.executorAccount;
|
|
2520
|
+
return typeof value === "string" && /^[0-9a-f]{64}$/.test(value) ? value : null;
|
|
2521
|
+
}
|
|
2432
2522
|
function chooseRelay(relays) {
|
|
2433
2523
|
const reachable = relays.filter(
|
|
2434
2524
|
(r) => typeof r.relayUrl === "string" && r.relayUrl.length > 0
|
|
2435
2525
|
);
|
|
2436
2526
|
const fresh = reachable.find((r) => r.fresh);
|
|
2437
|
-
if (fresh) return { relayUrl: fresh.relayUrl, note: null };
|
|
2527
|
+
if (fresh) return { relayUrl: fresh.relayUrl, executorAccount: executorOf(fresh), note: null };
|
|
2438
2528
|
if (reachable.length > 0) {
|
|
2439
2529
|
return {
|
|
2440
2530
|
relayUrl: reachable[0].relayUrl,
|
|
2531
|
+
executorAccount: executorOf(reachable[0]),
|
|
2441
2532
|
note: "Connected through a relay whose last heartbeat has lapsed \u2014 it may not answer. It was the only one with an address."
|
|
2442
2533
|
};
|
|
2443
2534
|
}
|
|
2444
2535
|
if (relays.length > 0) {
|
|
2445
2536
|
return {
|
|
2446
2537
|
relayUrl: null,
|
|
2538
|
+
executorAccount: null,
|
|
2447
2539
|
note: `Signed in. Your account has ${relays.length} relay${relays.length === 1 ? "" : "s"} assigned, but the cloud knows no address for any of them yet, so there is nowhere to write through for the moment. Reads and writes resume as soon as one reports in.`
|
|
2448
2540
|
};
|
|
2449
2541
|
}
|
|
2450
2542
|
return {
|
|
2451
2543
|
relayUrl: null,
|
|
2544
|
+
executorAccount: null,
|
|
2452
2545
|
note: "Signed in, with nowhere to write yet: a new account is a member of nothing, so no node serves it. Redeeming an invitation admits this account to a namespace and gives it a relay \u2014 that is the normal first step, not an error."
|
|
2453
2546
|
};
|
|
2454
2547
|
}
|
|
@@ -2560,7 +2653,8 @@ function useAccountEnrolment({
|
|
|
2560
2653
|
relayUrl: chosen.relayUrl,
|
|
2561
2654
|
account: enrolled.account,
|
|
2562
2655
|
credential: enrolled.credential,
|
|
2563
|
-
deviceSecret: keys.signSk
|
|
2656
|
+
deviceSecret: keys.signSk,
|
|
2657
|
+
...chosen.executorAccount ? { executorAccount: chosen.executorAccount } : {}
|
|
2564
2658
|
});
|
|
2565
2659
|
} catch (e) {
|
|
2566
2660
|
if (!cancelled) setNote(e instanceof Error ? e.message : String(e));
|
|
@@ -4824,7 +4918,7 @@ function useDelegatedBootstrap(options = {}) {
|
|
|
4824
4918
|
contextId: input.contextId
|
|
4825
4919
|
});
|
|
4826
4920
|
}
|
|
4827
|
-
connectWithAccount(outcome.session);
|
|
4921
|
+
connectWithAccount(carryExecutorAccount(readDelegatedSession(), outcome.session));
|
|
4828
4922
|
}
|
|
4829
4923
|
return outcome;
|
|
4830
4924
|
} finally {
|