@calimero-network/mero-react 9.9.1 → 9.11.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/README.md +27 -2
- package/dist/index.cjs +286 -211
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +90 -9
- package/dist/index.d.ts +90 -9
- package/dist/index.js +284 -213
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -117,7 +117,7 @@ const {
|
|
|
117
117
|
isOnline, // boolean — SSE connection state
|
|
118
118
|
isLoading, // boolean — initial session restore
|
|
119
119
|
nodeUrl, // string | null
|
|
120
|
-
applicationId, // string | null —
|
|
120
|
+
applicationId, // string | null — node: from the auth callback; account: see below
|
|
121
121
|
contextId, // string | null — from auth callback
|
|
122
122
|
contextIdentity, // string | null — executor public key from auth callback
|
|
123
123
|
connectToNode, // (url: string) => void — starts auth redirect
|
|
@@ -125,6 +125,8 @@ const {
|
|
|
125
125
|
} = useMero();
|
|
126
126
|
```
|
|
127
127
|
|
|
128
|
+
How an account (delegated session) learns its `applicationId`: first from its own contexts on the relay — one distinct application across them is the answer; several leaves it `null` on purpose (nothing says which the tab is for). With no contexts yet, it is derived from the registry by `packageName`: the id is `sha256(borsh((package, signerId)))` of the newest non-yanked bundle, exactly as merod computes it at install, so a brand-new account can found its first namespace (`admin.createNamespace({ applicationId })`). Without a `packageName`, or when the registry lists the package under more than one publisher, it stays `null`. `applicationIdForBundle` and `resolveApplicationIdFromRegistry` are exported for apps that need the same derivation.
|
|
129
|
+
|
|
128
130
|
Through `mero` you access the full MeroJs API:
|
|
129
131
|
|
|
130
132
|
```tsx
|
|
@@ -335,7 +337,30 @@ Helpers: `defaultMeroTheme` (the full default palette), `resolveMeroTheme(partia
|
|
|
335
337
|
| `cloud` | `boolean` | `true` | The modal has two tabs: **Node** (discovery + URL, default) and **Cloud** (sign in with a Calimero account by enrolling at the wallet). `false` removes the Cloud tab. A page coming back from the wallet opens the modal on Cloud automatically. |
|
|
336
338
|
| `accountDefaults` | `{ walletUrl?: string }` | — | Local development only: enrol at a wallet other than the hosted one. |
|
|
337
339
|
|
|
338
|
-
|
|
340
|
+
### `<LoginModal>` and the Cloud tab
|
|
341
|
+
|
|
342
|
+
An app that mounts the modal itself gets the same two tabs with nothing wired: by default `LoginModal` sources its Cloud tab from `useAccountEnrolment()` internally, so "Enrol with your account" works and a page coming back from the wallet is completed (and lands on the Cloud tab) whether or not the modal is open. It must sit inside `MeroProvider`.
|
|
343
|
+
|
|
344
|
+
```tsx
|
|
345
|
+
// Node + Cloud tabs, account sign-in included (default)
|
|
346
|
+
<LoginModal isOpen={open} onConnect={connectToNode} onClose={() => setOpen(false)} />
|
|
347
|
+
|
|
348
|
+
// Node dialog only, no tabs — the pre-Cloud behaviour
|
|
349
|
+
<LoginModal cloud={false} isOpen={open} onConnect={connectToNode} onClose={() => setOpen(false)} />
|
|
350
|
+
|
|
351
|
+
// Your own enrolment (what ConnectButton does): the object is used verbatim
|
|
352
|
+
<LoginModal cloud={{ onEnrol, note, walletUrl, customWallet }} initialTab="cloud" ... />
|
|
353
|
+
```
|
|
354
|
+
|
|
355
|
+
| `cloud` | Cloud tab | Enrolment callback read by |
|
|
356
|
+
|---------|-----------|----------------------------|
|
|
357
|
+
| omitted | shown, sourced by the modal | the modal |
|
|
358
|
+
| `false` | none (no tabs) | nobody here |
|
|
359
|
+
| object | shown, the caller's values | the caller |
|
|
360
|
+
|
|
361
|
+
`initialTab` defaults to `'node'`; with `cloud` omitted it defaults to `'cloud'` on the load coming back from the wallet. An explicit `initialTab` wins.
|
|
362
|
+
|
|
363
|
+
The caller owns `isOpen`. To have the modal open by itself on the way back from the wallet (as `ConnectButton` does), seed its open state with `isReturningFromWallet()`, which peeks at the fragment without consuming it: `const [open, setOpen] = useState(isReturningFromWallet);`. The account flow without any of these is `useAccountEnrolment()` (returns `goToWallet`, `note`, `returning`, `walletUrl`, `customWallet`); its UI is `<AccountSignInPanel>`.
|
|
339
364
|
|
|
340
365
|
## Enums
|
|
341
366
|
|
package/dist/index.cjs
CHANGED
|
@@ -437,6 +437,56 @@ async function listDelegatedNamespaces(s) {
|
|
|
437
437
|
return out;
|
|
438
438
|
}
|
|
439
439
|
|
|
440
|
+
// src/delegated/application-id.ts
|
|
441
|
+
function borshString(s) {
|
|
442
|
+
const bytes = new TextEncoder().encode(s);
|
|
443
|
+
const out = new Uint8Array(4 + bytes.length);
|
|
444
|
+
new DataView(out.buffer).setUint32(0, bytes.length, true);
|
|
445
|
+
out.set(bytes, 4);
|
|
446
|
+
return out;
|
|
447
|
+
}
|
|
448
|
+
async function applicationIdForBundle(pkg, signerId) {
|
|
449
|
+
const a = borshString(pkg);
|
|
450
|
+
const b = borshString(signerId);
|
|
451
|
+
const tuple = new Uint8Array(a.length + b.length);
|
|
452
|
+
tuple.set(a, 0);
|
|
453
|
+
tuple.set(b, a.length);
|
|
454
|
+
const digest = await crypto.subtle.digest("SHA-256", tuple);
|
|
455
|
+
return Array.from(new Uint8Array(digest), (x) => x.toString(16).padStart(2, "0")).join("");
|
|
456
|
+
}
|
|
457
|
+
function newer(a, b) {
|
|
458
|
+
const [x, y] = [a, b].map((v) => v.split(/[.-]/).map((n) => Number.parseInt(n, 10) || 0));
|
|
459
|
+
for (let i = 0; i < Math.max(x.length, y.length); i++) {
|
|
460
|
+
if ((x[i] ?? 0) !== (y[i] ?? 0)) return (x[i] ?? 0) > (y[i] ?? 0);
|
|
461
|
+
}
|
|
462
|
+
return false;
|
|
463
|
+
}
|
|
464
|
+
function selectLatestBundle(bundles) {
|
|
465
|
+
return bundles.filter((b) => !b.yanked && typeof b.appVersion === "string" && b.appVersion.length > 0).reduce((best, b) => !best || newer(b.appVersion, best.appVersion) ? b : best, void 0);
|
|
466
|
+
}
|
|
467
|
+
async function fetchRegistryBundles(registryUrl, pkg, fetchFn = globalThis.fetch) {
|
|
468
|
+
const url = `${registryUrl.replace(/\/+$/, "")}/api/v2/bundles?package=${encodeURIComponent(pkg)}`;
|
|
469
|
+
const response = await fetchFn(url, { headers: { Accept: "application/json" } });
|
|
470
|
+
if (!response.ok) throw new Error(`the registry has no ${pkg} (HTTP ${response.status})`);
|
|
471
|
+
const body = await response.json();
|
|
472
|
+
return Array.isArray(body) ? body : [];
|
|
473
|
+
}
|
|
474
|
+
async function resolveApplicationIdFromRegistry(registryUrl, pkg, deps = {}) {
|
|
475
|
+
const bundles = await fetchRegistryBundles(registryUrl, pkg, deps.fetch);
|
|
476
|
+
const live = bundles.filter((b) => !b.yanked);
|
|
477
|
+
const signers = [...new Set(live.map((b) => b.signerId).filter((s) => typeof s === "string" && s.length > 0))];
|
|
478
|
+
if (signers.length > 1) {
|
|
479
|
+
throw new Error(
|
|
480
|
+
`the registry lists ${pkg} under more than one publisher (${signers.join(", ")}): refusing to guess which application it is`
|
|
481
|
+
);
|
|
482
|
+
}
|
|
483
|
+
const latest = selectLatestBundle(live);
|
|
484
|
+
if (!latest) throw new Error(`the registry lists no version of ${pkg}`);
|
|
485
|
+
const signerId = latest.signerId;
|
|
486
|
+
if (!signerId) throw new Error(`the registry names no publisher for ${pkg} ${latest.appVersion}`);
|
|
487
|
+
return { applicationId: await applicationIdForBundle(pkg, signerId), signerId, version: latest.appVersion };
|
|
488
|
+
}
|
|
489
|
+
|
|
440
490
|
// src/delegated/create-context.ts
|
|
441
491
|
function governanceNonce(relay, group) {
|
|
442
492
|
return { next: async () => nextGovernanceNonce(relay, group) };
|
|
@@ -585,20 +635,9 @@ async function enableHaBestEffort(s, namespaceId, salt, relayUrl, deps) {
|
|
|
585
635
|
}
|
|
586
636
|
}
|
|
587
637
|
async function latestPublishedVersion(registryUrl, pkg, fetchFn = globalThis.fetch) {
|
|
588
|
-
const
|
|
589
|
-
const response = await fetchFn(url, { headers: { Accept: "application/json" } });
|
|
590
|
-
if (!response.ok) throw new Error(`the registry has no ${pkg} (HTTP ${response.status})`);
|
|
591
|
-
const bundles = await response.json();
|
|
592
|
-
const newer = (a, b) => {
|
|
593
|
-
const [x, y] = [a, b].map((v) => v.split(/[.-]/).map((n) => Number.parseInt(n, 10) || 0));
|
|
594
|
-
for (let i = 0; i < Math.max(x.length, y.length); i++) {
|
|
595
|
-
if ((x[i] ?? 0) !== (y[i] ?? 0)) return (x[i] ?? 0) > (y[i] ?? 0);
|
|
596
|
-
}
|
|
597
|
-
return false;
|
|
598
|
-
};
|
|
599
|
-
const latest = bundles.map((b) => b.appVersion).filter((v) => typeof v === "string" && v.length > 0).reduce((best, v) => !best || newer(v, best) ? v : best, void 0);
|
|
638
|
+
const latest = selectLatestBundle(await fetchRegistryBundles(registryUrl, pkg, fetchFn));
|
|
600
639
|
if (!latest) throw new Error(`the registry lists no version of ${pkg}`);
|
|
601
|
-
return latest;
|
|
640
|
+
return latest.appVersion;
|
|
602
641
|
}
|
|
603
642
|
|
|
604
643
|
// src/admin/node-admin.ts
|
|
@@ -1395,6 +1434,7 @@ function clearAllStorage() {
|
|
|
1395
1434
|
});
|
|
1396
1435
|
}
|
|
1397
1436
|
var MeroContext = react.createContext(null);
|
|
1437
|
+
var DEFAULT_REGISTRY_URL = "https://apps.calimero.network";
|
|
1398
1438
|
var isBrowser = typeof window !== "undefined";
|
|
1399
1439
|
function getPermissionsForMode(mode) {
|
|
1400
1440
|
switch (mode) {
|
|
@@ -1568,6 +1608,17 @@ function MeroProvider({
|
|
|
1568
1608
|
if (apps.length === 1) {
|
|
1569
1609
|
setApplicationIdState(apps[0]);
|
|
1570
1610
|
}
|
|
1611
|
+
if (apps.length === 0 && packageName) {
|
|
1612
|
+
try {
|
|
1613
|
+
const { applicationId: derived } = await resolveApplicationIdFromRegistry(
|
|
1614
|
+
registryUrl ?? DEFAULT_REGISTRY_URL,
|
|
1615
|
+
packageName
|
|
1616
|
+
);
|
|
1617
|
+
if (active) setApplicationIdState(derived);
|
|
1618
|
+
} catch (e) {
|
|
1619
|
+
console.warn(`[mero-react] could not learn the application id of ${packageName} from the registry`, e);
|
|
1620
|
+
}
|
|
1621
|
+
}
|
|
1571
1622
|
} catch {
|
|
1572
1623
|
}
|
|
1573
1624
|
})();
|
|
@@ -1575,7 +1626,7 @@ function MeroProvider({
|
|
|
1575
1626
|
active = false;
|
|
1576
1627
|
stop.abort();
|
|
1577
1628
|
};
|
|
1578
|
-
}, [delegated, contextId]);
|
|
1629
|
+
}, [delegated, contextId, packageName, registryUrl]);
|
|
1579
1630
|
react.useEffect(() => {
|
|
1580
1631
|
if (delegated) return;
|
|
1581
1632
|
let active = true;
|
|
@@ -1933,6 +1984,196 @@ function AccountSignInPanel({
|
|
|
1933
1984
|
] })
|
|
1934
1985
|
] });
|
|
1935
1986
|
}
|
|
1987
|
+
var DEVICE_KEY = "calimero.device";
|
|
1988
|
+
var STATE_KEY = "calimero.enrol.state";
|
|
1989
|
+
var HOSTED_WALLET = "https://wallet.cloud.calimero.network/account-enroll";
|
|
1990
|
+
var hex = (b) => [...new Uint8Array(b)].map((x) => x.toString(16).padStart(2, "0")).join("");
|
|
1991
|
+
function executorOf(relay) {
|
|
1992
|
+
const value = relay.executorAccount;
|
|
1993
|
+
return typeof value === "string" && /^[0-9a-f]{64}$/.test(value) ? value : null;
|
|
1994
|
+
}
|
|
1995
|
+
function chooseRelay(relays) {
|
|
1996
|
+
const reachable = relays.filter(
|
|
1997
|
+
(r) => typeof r.relayUrl === "string" && r.relayUrl.length > 0
|
|
1998
|
+
);
|
|
1999
|
+
const fresh = reachable.find((r) => r.fresh);
|
|
2000
|
+
if (fresh) return { relayUrl: fresh.relayUrl, executorAccount: executorOf(fresh), note: null };
|
|
2001
|
+
if (reachable.length > 0) {
|
|
2002
|
+
return {
|
|
2003
|
+
relayUrl: reachable[0].relayUrl,
|
|
2004
|
+
executorAccount: executorOf(reachable[0]),
|
|
2005
|
+
note: "Connected through a relay whose last heartbeat has lapsed \u2014 it may not answer. It was the only one with an address."
|
|
2006
|
+
};
|
|
2007
|
+
}
|
|
2008
|
+
if (relays.length > 0) {
|
|
2009
|
+
return {
|
|
2010
|
+
relayUrl: null,
|
|
2011
|
+
executorAccount: null,
|
|
2012
|
+
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.`
|
|
2013
|
+
};
|
|
2014
|
+
}
|
|
2015
|
+
return {
|
|
2016
|
+
relayUrl: null,
|
|
2017
|
+
executorAccount: null,
|
|
2018
|
+
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."
|
|
2019
|
+
};
|
|
2020
|
+
}
|
|
2021
|
+
async function deviceKeys() {
|
|
2022
|
+
const had = localStorage.getItem(DEVICE_KEY);
|
|
2023
|
+
if (had) {
|
|
2024
|
+
const parsed = JSON.parse(had);
|
|
2025
|
+
if (parsed.kemSk && parsed.signSk && parsed.signPk && parsed.kemPk) {
|
|
2026
|
+
return parsed;
|
|
2027
|
+
}
|
|
2028
|
+
localStorage.removeItem(DEVICE_KEY);
|
|
2029
|
+
}
|
|
2030
|
+
const sign = await crypto.subtle.generateKey({ name: "Ed25519" }, true, [
|
|
2031
|
+
"sign",
|
|
2032
|
+
"verify"
|
|
2033
|
+
]);
|
|
2034
|
+
let agree;
|
|
2035
|
+
try {
|
|
2036
|
+
agree = await crypto.subtle.generateKey({ name: "X25519" }, true, [
|
|
2037
|
+
"deriveBits"
|
|
2038
|
+
]);
|
|
2039
|
+
} catch {
|
|
2040
|
+
throw new Error(
|
|
2041
|
+
"this browser cannot generate an X25519 key, so it cannot receive wrapped scope keys. Chrome 133+ and Safari 17+ support it; enrolling without one would certify a delivery key nothing holds the secret for."
|
|
2042
|
+
);
|
|
2043
|
+
}
|
|
2044
|
+
const keys = {
|
|
2045
|
+
signPk: hex(await crypto.subtle.exportKey("raw", sign.publicKey)),
|
|
2046
|
+
signSk: hex((await crypto.subtle.exportKey("pkcs8", sign.privateKey)).slice(16)),
|
|
2047
|
+
kemPk: hex(await crypto.subtle.exportKey("raw", agree.publicKey)),
|
|
2048
|
+
kemSk: hex((await crypto.subtle.exportKey("pkcs8", agree.privateKey)).slice(16))
|
|
2049
|
+
};
|
|
2050
|
+
localStorage.setItem(DEVICE_KEY, JSON.stringify(keys));
|
|
2051
|
+
return keys;
|
|
2052
|
+
}
|
|
2053
|
+
function isReturningFromWallet(location = typeof window === "undefined" ? void 0 : window.location) {
|
|
2054
|
+
if (!location) return false;
|
|
2055
|
+
const params = new URLSearchParams(location.hash.replace(/^#/, ""));
|
|
2056
|
+
if (params.get("error")) return true;
|
|
2057
|
+
return Boolean(params.get("credential") && params.get("account") && params.get("device"));
|
|
2058
|
+
}
|
|
2059
|
+
function useAccountEnrolment({
|
|
2060
|
+
walletUrl: walletOverride,
|
|
2061
|
+
enabled = true
|
|
2062
|
+
} = {}) {
|
|
2063
|
+
const { connectWithAccount, cloudBaseUrl } = useMero();
|
|
2064
|
+
const [note, setNote] = react.useState(null);
|
|
2065
|
+
const walletUrl = walletOverride ?? HOSTED_WALLET;
|
|
2066
|
+
const callbackRef = react.useRef(void 0);
|
|
2067
|
+
const callbackErrorRef = react.useRef(null);
|
|
2068
|
+
if (enabled && callbackRef.current === void 0 && typeof window !== "undefined") {
|
|
2069
|
+
try {
|
|
2070
|
+
callbackRef.current = meroJs.readEnrolmentCallback();
|
|
2071
|
+
} catch (e) {
|
|
2072
|
+
callbackRef.current = null;
|
|
2073
|
+
callbackErrorRef.current = e instanceof Error ? e.message : String(e);
|
|
2074
|
+
}
|
|
2075
|
+
}
|
|
2076
|
+
const returningRef = react.useRef(null);
|
|
2077
|
+
if (returningRef.current === null && callbackRef.current !== void 0) {
|
|
2078
|
+
returningRef.current = callbackRef.current !== null || callbackErrorRef.current !== null;
|
|
2079
|
+
}
|
|
2080
|
+
const completingRef = react.useRef(false);
|
|
2081
|
+
react.useEffect(() => {
|
|
2082
|
+
if (callbackErrorRef.current) {
|
|
2083
|
+
setNote(callbackErrorRef.current);
|
|
2084
|
+
callbackErrorRef.current = null;
|
|
2085
|
+
return;
|
|
2086
|
+
}
|
|
2087
|
+
const back = callbackRef.current;
|
|
2088
|
+
if (!back || completingRef.current) return;
|
|
2089
|
+
completingRef.current = true;
|
|
2090
|
+
let cancelled = false;
|
|
2091
|
+
(async () => {
|
|
2092
|
+
try {
|
|
2093
|
+
const keys = await deviceKeys();
|
|
2094
|
+
const sent = (() => {
|
|
2095
|
+
try {
|
|
2096
|
+
return sessionStorage.getItem(STATE_KEY);
|
|
2097
|
+
} catch {
|
|
2098
|
+
return null;
|
|
2099
|
+
}
|
|
2100
|
+
})();
|
|
2101
|
+
try {
|
|
2102
|
+
sessionStorage.removeItem(STATE_KEY);
|
|
2103
|
+
} catch {
|
|
2104
|
+
}
|
|
2105
|
+
if (!sent) {
|
|
2106
|
+
setNote(
|
|
2107
|
+
'This enrolment could not be verified as one this tab started, so it was not accepted. Start again from "Enrol with your account".'
|
|
2108
|
+
);
|
|
2109
|
+
return;
|
|
2110
|
+
}
|
|
2111
|
+
const enrolled = await meroJs.completeDeviceEnrolment({
|
|
2112
|
+
...back,
|
|
2113
|
+
devicePublicKey: keys.signPk,
|
|
2114
|
+
kemPublicKey: keys.kemPk,
|
|
2115
|
+
expectState: sent
|
|
2116
|
+
});
|
|
2117
|
+
saveDelegatedCredential({
|
|
2118
|
+
account: enrolled.account,
|
|
2119
|
+
credential: enrolled.credential,
|
|
2120
|
+
deviceSecret: keys.signSk
|
|
2121
|
+
});
|
|
2122
|
+
const cloud = new meroJs.CloudClient({
|
|
2123
|
+
cloudBaseUrl,
|
|
2124
|
+
routingCredential: {
|
|
2125
|
+
credential: enrolled.credential,
|
|
2126
|
+
deviceSecret: keys.signSk
|
|
2127
|
+
}
|
|
2128
|
+
});
|
|
2129
|
+
const chosen = chooseRelay(await cloud.getAccountRelays(enrolled.account));
|
|
2130
|
+
if (chosen.note) setNote(chosen.note);
|
|
2131
|
+
connectWithAccount({
|
|
2132
|
+
relayUrl: chosen.relayUrl,
|
|
2133
|
+
account: enrolled.account,
|
|
2134
|
+
credential: enrolled.credential,
|
|
2135
|
+
deviceSecret: keys.signSk,
|
|
2136
|
+
...chosen.executorAccount ? { executorAccount: chosen.executorAccount } : {}
|
|
2137
|
+
});
|
|
2138
|
+
} catch (e) {
|
|
2139
|
+
if (!cancelled) setNote(e instanceof Error ? e.message : String(e));
|
|
2140
|
+
}
|
|
2141
|
+
})();
|
|
2142
|
+
return () => {
|
|
2143
|
+
cancelled = true;
|
|
2144
|
+
};
|
|
2145
|
+
}, [connectWithAccount, cloudBaseUrl]);
|
|
2146
|
+
const goToWallet = react.useCallback(async () => {
|
|
2147
|
+
let keys;
|
|
2148
|
+
try {
|
|
2149
|
+
keys = await deviceKeys();
|
|
2150
|
+
} catch (e) {
|
|
2151
|
+
setNote(e instanceof Error ? e.message : String(e));
|
|
2152
|
+
return;
|
|
2153
|
+
}
|
|
2154
|
+
const state = hex(crypto.getRandomValues(new Uint8Array(16)));
|
|
2155
|
+
try {
|
|
2156
|
+
sessionStorage.setItem(STATE_KEY, state);
|
|
2157
|
+
} catch {
|
|
2158
|
+
}
|
|
2159
|
+
window.location.assign(
|
|
2160
|
+
meroJs.deviceEnrolmentUrl({
|
|
2161
|
+
walletUrl,
|
|
2162
|
+
devicePublicKey: keys.signPk,
|
|
2163
|
+
kemPublicKey: keys.kemPk,
|
|
2164
|
+
returnTo: window.location.origin + window.location.pathname,
|
|
2165
|
+
state
|
|
2166
|
+
})
|
|
2167
|
+
);
|
|
2168
|
+
}, [walletUrl]);
|
|
2169
|
+
return {
|
|
2170
|
+
goToWallet,
|
|
2171
|
+
note,
|
|
2172
|
+
returning: returningRef.current === true,
|
|
2173
|
+
walletUrl,
|
|
2174
|
+
customWallet: walletOverride !== void 0
|
|
2175
|
+
};
|
|
2176
|
+
}
|
|
1936
2177
|
var CUSTOM_SELECTION = "__custom__";
|
|
1937
2178
|
var TABS = [
|
|
1938
2179
|
{ id: "node", label: "Node" },
|
|
@@ -2216,8 +2457,21 @@ function LoginModal({
|
|
|
2216
2457
|
theme,
|
|
2217
2458
|
localNodePorts = meroJs.DEFAULT_LOCAL_NODE_PORTS,
|
|
2218
2459
|
cloud,
|
|
2219
|
-
initialTab
|
|
2460
|
+
initialTab: initialTabProp
|
|
2220
2461
|
}) {
|
|
2462
|
+
const own = useAccountEnrolment({ enabled: cloud === void 0 });
|
|
2463
|
+
const cloudTab = react.useMemo(
|
|
2464
|
+
() => cloud === void 0 ? {
|
|
2465
|
+
onEnrol: () => {
|
|
2466
|
+
void own.goToWallet();
|
|
2467
|
+
},
|
|
2468
|
+
note: own.note,
|
|
2469
|
+
walletUrl: own.walletUrl,
|
|
2470
|
+
customWallet: own.customWallet
|
|
2471
|
+
} : cloud || null,
|
|
2472
|
+
[cloud, own.goToWallet, own.note, own.walletUrl, own.customWallet]
|
|
2473
|
+
);
|
|
2474
|
+
const initialTab = initialTabProp ?? (cloud === void 0 && own.returning ? "cloud" : "node");
|
|
2221
2475
|
const [tab, setTab] = react.useState(initialTab);
|
|
2222
2476
|
react.useEffect(() => {
|
|
2223
2477
|
if (isOpen) setTab(initialTab);
|
|
@@ -2226,7 +2480,7 @@ function LoginModal({
|
|
|
2226
2480
|
node: null,
|
|
2227
2481
|
cloud: null
|
|
2228
2482
|
});
|
|
2229
|
-
const activeTab =
|
|
2483
|
+
const activeTab = cloudTab ? tab : "node";
|
|
2230
2484
|
const [selected, setSelected] = react.useState(CUSTOM_SELECTION);
|
|
2231
2485
|
const [discovered, setDiscovered] = react.useState([]);
|
|
2232
2486
|
const [discovering, setDiscovering] = react.useState(false);
|
|
@@ -2426,7 +2680,7 @@ function LoginModal({
|
|
|
2426
2680
|
}
|
|
2427
2681
|
) })
|
|
2428
2682
|
] });
|
|
2429
|
-
if (!
|
|
2683
|
+
if (!cloudTab) return body;
|
|
2430
2684
|
return /* @__PURE__ */ jsxRuntime.jsx(
|
|
2431
2685
|
"div",
|
|
2432
2686
|
{
|
|
@@ -2487,8 +2741,8 @@ function LoginModal({
|
|
|
2487
2741
|
/* @__PURE__ */ jsxRuntime.jsx(CalimeroLogo, { size: 44, color: cssVar(resolved, "primary") }),
|
|
2488
2742
|
/* @__PURE__ */ jsxRuntime.jsx("h1", { style: styles.title, children: "Connect to Calimero" })
|
|
2489
2743
|
] }),
|
|
2490
|
-
|
|
2491
|
-
activeTab === "cloud" &&
|
|
2744
|
+
cloudTab && renderTabs(),
|
|
2745
|
+
activeTab === "cloud" && cloudTab ? /* @__PURE__ */ jsxRuntime.jsx(
|
|
2492
2746
|
"div",
|
|
2493
2747
|
{
|
|
2494
2748
|
role: "tabpanel",
|
|
@@ -2498,10 +2752,10 @@ function LoginModal({
|
|
|
2498
2752
|
children: /* @__PURE__ */ jsxRuntime.jsx(
|
|
2499
2753
|
AccountSignInPanel,
|
|
2500
2754
|
{
|
|
2501
|
-
onEnrol:
|
|
2502
|
-
note:
|
|
2503
|
-
walletUrl:
|
|
2504
|
-
customWallet:
|
|
2755
|
+
onEnrol: cloudTab.onEnrol,
|
|
2756
|
+
note: cloudTab.note,
|
|
2757
|
+
walletUrl: cloudTab.walletUrl,
|
|
2758
|
+
customWallet: cloudTab.customWallet,
|
|
2505
2759
|
theme
|
|
2506
2760
|
}
|
|
2507
2761
|
)
|
|
@@ -2511,190 +2765,6 @@ function LoginModal({
|
|
|
2511
2765
|
] });
|
|
2512
2766
|
return reactDom.createPortal(modalContent, document.body);
|
|
2513
2767
|
}
|
|
2514
|
-
var DEVICE_KEY = "calimero.device";
|
|
2515
|
-
var STATE_KEY = "calimero.enrol.state";
|
|
2516
|
-
var HOSTED_WALLET = "https://wallet.cloud.calimero.network/account-enroll";
|
|
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
|
-
}
|
|
2522
|
-
function chooseRelay(relays) {
|
|
2523
|
-
const reachable = relays.filter(
|
|
2524
|
-
(r) => typeof r.relayUrl === "string" && r.relayUrl.length > 0
|
|
2525
|
-
);
|
|
2526
|
-
const fresh = reachable.find((r) => r.fresh);
|
|
2527
|
-
if (fresh) return { relayUrl: fresh.relayUrl, executorAccount: executorOf(fresh), note: null };
|
|
2528
|
-
if (reachable.length > 0) {
|
|
2529
|
-
return {
|
|
2530
|
-
relayUrl: reachable[0].relayUrl,
|
|
2531
|
-
executorAccount: executorOf(reachable[0]),
|
|
2532
|
-
note: "Connected through a relay whose last heartbeat has lapsed \u2014 it may not answer. It was the only one with an address."
|
|
2533
|
-
};
|
|
2534
|
-
}
|
|
2535
|
-
if (relays.length > 0) {
|
|
2536
|
-
return {
|
|
2537
|
-
relayUrl: null,
|
|
2538
|
-
executorAccount: null,
|
|
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.`
|
|
2540
|
-
};
|
|
2541
|
-
}
|
|
2542
|
-
return {
|
|
2543
|
-
relayUrl: null,
|
|
2544
|
-
executorAccount: null,
|
|
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."
|
|
2546
|
-
};
|
|
2547
|
-
}
|
|
2548
|
-
async function deviceKeys() {
|
|
2549
|
-
const had = localStorage.getItem(DEVICE_KEY);
|
|
2550
|
-
if (had) {
|
|
2551
|
-
const parsed = JSON.parse(had);
|
|
2552
|
-
if (parsed.kemSk && parsed.signSk && parsed.signPk && parsed.kemPk) {
|
|
2553
|
-
return parsed;
|
|
2554
|
-
}
|
|
2555
|
-
localStorage.removeItem(DEVICE_KEY);
|
|
2556
|
-
}
|
|
2557
|
-
const sign = await crypto.subtle.generateKey({ name: "Ed25519" }, true, [
|
|
2558
|
-
"sign",
|
|
2559
|
-
"verify"
|
|
2560
|
-
]);
|
|
2561
|
-
let agree;
|
|
2562
|
-
try {
|
|
2563
|
-
agree = await crypto.subtle.generateKey({ name: "X25519" }, true, [
|
|
2564
|
-
"deriveBits"
|
|
2565
|
-
]);
|
|
2566
|
-
} catch {
|
|
2567
|
-
throw new Error(
|
|
2568
|
-
"this browser cannot generate an X25519 key, so it cannot receive wrapped scope keys. Chrome 133+ and Safari 17+ support it; enrolling without one would certify a delivery key nothing holds the secret for."
|
|
2569
|
-
);
|
|
2570
|
-
}
|
|
2571
|
-
const keys = {
|
|
2572
|
-
signPk: hex(await crypto.subtle.exportKey("raw", sign.publicKey)),
|
|
2573
|
-
signSk: hex((await crypto.subtle.exportKey("pkcs8", sign.privateKey)).slice(16)),
|
|
2574
|
-
kemPk: hex(await crypto.subtle.exportKey("raw", agree.publicKey)),
|
|
2575
|
-
kemSk: hex((await crypto.subtle.exportKey("pkcs8", agree.privateKey)).slice(16))
|
|
2576
|
-
};
|
|
2577
|
-
localStorage.setItem(DEVICE_KEY, JSON.stringify(keys));
|
|
2578
|
-
return keys;
|
|
2579
|
-
}
|
|
2580
|
-
function useAccountEnrolment({
|
|
2581
|
-
walletUrl: walletOverride,
|
|
2582
|
-
enabled = true
|
|
2583
|
-
} = {}) {
|
|
2584
|
-
const { connectWithAccount, cloudBaseUrl } = useMero();
|
|
2585
|
-
const [note, setNote] = react.useState(null);
|
|
2586
|
-
const walletUrl = walletOverride ?? HOSTED_WALLET;
|
|
2587
|
-
const callbackRef = react.useRef(void 0);
|
|
2588
|
-
const callbackErrorRef = react.useRef(null);
|
|
2589
|
-
if (enabled && callbackRef.current === void 0 && typeof window !== "undefined") {
|
|
2590
|
-
try {
|
|
2591
|
-
callbackRef.current = meroJs.readEnrolmentCallback();
|
|
2592
|
-
} catch (e) {
|
|
2593
|
-
callbackRef.current = null;
|
|
2594
|
-
callbackErrorRef.current = e instanceof Error ? e.message : String(e);
|
|
2595
|
-
}
|
|
2596
|
-
}
|
|
2597
|
-
const returningRef = react.useRef(null);
|
|
2598
|
-
if (returningRef.current === null && callbackRef.current !== void 0) {
|
|
2599
|
-
returningRef.current = callbackRef.current !== null || callbackErrorRef.current !== null;
|
|
2600
|
-
}
|
|
2601
|
-
const completingRef = react.useRef(false);
|
|
2602
|
-
react.useEffect(() => {
|
|
2603
|
-
if (callbackErrorRef.current) {
|
|
2604
|
-
setNote(callbackErrorRef.current);
|
|
2605
|
-
callbackErrorRef.current = null;
|
|
2606
|
-
return;
|
|
2607
|
-
}
|
|
2608
|
-
const back = callbackRef.current;
|
|
2609
|
-
if (!back || completingRef.current) return;
|
|
2610
|
-
completingRef.current = true;
|
|
2611
|
-
let cancelled = false;
|
|
2612
|
-
(async () => {
|
|
2613
|
-
try {
|
|
2614
|
-
const keys = await deviceKeys();
|
|
2615
|
-
const sent = (() => {
|
|
2616
|
-
try {
|
|
2617
|
-
return sessionStorage.getItem(STATE_KEY);
|
|
2618
|
-
} catch {
|
|
2619
|
-
return null;
|
|
2620
|
-
}
|
|
2621
|
-
})();
|
|
2622
|
-
try {
|
|
2623
|
-
sessionStorage.removeItem(STATE_KEY);
|
|
2624
|
-
} catch {
|
|
2625
|
-
}
|
|
2626
|
-
if (!sent) {
|
|
2627
|
-
setNote(
|
|
2628
|
-
'This enrolment could not be verified as one this tab started, so it was not accepted. Start again from "Enrol with your account".'
|
|
2629
|
-
);
|
|
2630
|
-
return;
|
|
2631
|
-
}
|
|
2632
|
-
const enrolled = await meroJs.completeDeviceEnrolment({
|
|
2633
|
-
...back,
|
|
2634
|
-
devicePublicKey: keys.signPk,
|
|
2635
|
-
kemPublicKey: keys.kemPk,
|
|
2636
|
-
expectState: sent
|
|
2637
|
-
});
|
|
2638
|
-
saveDelegatedCredential({
|
|
2639
|
-
account: enrolled.account,
|
|
2640
|
-
credential: enrolled.credential,
|
|
2641
|
-
deviceSecret: keys.signSk
|
|
2642
|
-
});
|
|
2643
|
-
const cloud = new meroJs.CloudClient({
|
|
2644
|
-
cloudBaseUrl,
|
|
2645
|
-
routingCredential: {
|
|
2646
|
-
credential: enrolled.credential,
|
|
2647
|
-
deviceSecret: keys.signSk
|
|
2648
|
-
}
|
|
2649
|
-
});
|
|
2650
|
-
const chosen = chooseRelay(await cloud.getAccountRelays(enrolled.account));
|
|
2651
|
-
if (chosen.note) setNote(chosen.note);
|
|
2652
|
-
connectWithAccount({
|
|
2653
|
-
relayUrl: chosen.relayUrl,
|
|
2654
|
-
account: enrolled.account,
|
|
2655
|
-
credential: enrolled.credential,
|
|
2656
|
-
deviceSecret: keys.signSk,
|
|
2657
|
-
...chosen.executorAccount ? { executorAccount: chosen.executorAccount } : {}
|
|
2658
|
-
});
|
|
2659
|
-
} catch (e) {
|
|
2660
|
-
if (!cancelled) setNote(e instanceof Error ? e.message : String(e));
|
|
2661
|
-
}
|
|
2662
|
-
})();
|
|
2663
|
-
return () => {
|
|
2664
|
-
cancelled = true;
|
|
2665
|
-
};
|
|
2666
|
-
}, [connectWithAccount, cloudBaseUrl]);
|
|
2667
|
-
const goToWallet = react.useCallback(async () => {
|
|
2668
|
-
let keys;
|
|
2669
|
-
try {
|
|
2670
|
-
keys = await deviceKeys();
|
|
2671
|
-
} catch (e) {
|
|
2672
|
-
setNote(e instanceof Error ? e.message : String(e));
|
|
2673
|
-
return;
|
|
2674
|
-
}
|
|
2675
|
-
const state = hex(crypto.getRandomValues(new Uint8Array(16)));
|
|
2676
|
-
try {
|
|
2677
|
-
sessionStorage.setItem(STATE_KEY, state);
|
|
2678
|
-
} catch {
|
|
2679
|
-
}
|
|
2680
|
-
window.location.assign(
|
|
2681
|
-
meroJs.deviceEnrolmentUrl({
|
|
2682
|
-
walletUrl,
|
|
2683
|
-
devicePublicKey: keys.signPk,
|
|
2684
|
-
kemPublicKey: keys.kemPk,
|
|
2685
|
-
returnTo: window.location.origin + window.location.pathname,
|
|
2686
|
-
state
|
|
2687
|
-
})
|
|
2688
|
-
);
|
|
2689
|
-
}, [walletUrl]);
|
|
2690
|
-
return {
|
|
2691
|
-
goToWallet,
|
|
2692
|
-
note,
|
|
2693
|
-
returning: returningRef.current === true,
|
|
2694
|
-
walletUrl,
|
|
2695
|
-
customWallet: walletOverride !== void 0
|
|
2696
|
-
};
|
|
2697
|
-
}
|
|
2698
2768
|
function shortAccount(account) {
|
|
2699
2769
|
return account.length > 14 ? `${account.slice(0, 6)}\u2026${account.slice(-6)}` : account;
|
|
2700
2770
|
}
|
|
@@ -2873,7 +2943,7 @@ function ConnectButton({
|
|
|
2873
2943
|
note: enrolment.note,
|
|
2874
2944
|
walletUrl: enrolment.walletUrl,
|
|
2875
2945
|
customWallet: enrolment.customWallet
|
|
2876
|
-
} :
|
|
2946
|
+
} : false,
|
|
2877
2947
|
initialTab: cloud && enrolment.returning ? "cloud" : "node"
|
|
2878
2948
|
}
|
|
2879
2949
|
)
|
|
@@ -3033,7 +3103,8 @@ function ConnectButtonAccount({
|
|
|
3033
3103
|
connectToNode(url);
|
|
3034
3104
|
},
|
|
3035
3105
|
onClose: () => setIsNodeModalOpen(false),
|
|
3036
|
-
theme: resolvedTheme ?? theme
|
|
3106
|
+
theme: resolvedTheme ?? theme,
|
|
3107
|
+
cloud: false
|
|
3037
3108
|
}
|
|
3038
3109
|
)
|
|
3039
3110
|
] });
|
|
@@ -3904,7 +3975,7 @@ function useNamespacesForApplication(applicationId) {
|
|
|
3904
3975
|
);
|
|
3905
3976
|
return { namespaces: data, loading, error, refetch };
|
|
3906
3977
|
}
|
|
3907
|
-
var
|
|
3978
|
+
var DEFAULT_REGISTRY_URL2 = "https://apps.calimero.network";
|
|
3908
3979
|
function useCreateNamespace() {
|
|
3909
3980
|
const { mero, isDelegated, app, cloudBaseUrl } = useMero();
|
|
3910
3981
|
const { loading, error, run } = useAsyncMutation();
|
|
@@ -3918,7 +3989,7 @@ function useCreateNamespace() {
|
|
|
3918
3989
|
if (!pkg) {
|
|
3919
3990
|
throw new Error("this app names no registry package, so a founded namespace could not be given its application");
|
|
3920
3991
|
}
|
|
3921
|
-
const version = app.packageVersion ?? await latestPublishedVersion(app.registryUrl ??
|
|
3992
|
+
const version = app.packageVersion ?? await latestPublishedVersion(app.registryUrl ?? DEFAULT_REGISTRY_URL2, pkg);
|
|
3922
3993
|
return foundDelegatedNamespace(
|
|
3923
3994
|
session,
|
|
3924
3995
|
{
|
|
@@ -5099,6 +5170,7 @@ exports.MigrationAdminPanel = MigrationAdminPanel;
|
|
|
5099
5170
|
exports.MigrationPendingBanner = MigrationPendingBanner;
|
|
5100
5171
|
exports.NoRelayError = NoRelayError;
|
|
5101
5172
|
exports.NotForAccountError = NotForAccountError;
|
|
5173
|
+
exports.applicationIdForBundle = applicationIdForBundle;
|
|
5102
5174
|
exports.bootstrapFromInvitation = bootstrapFromInvitation;
|
|
5103
5175
|
exports.clearAllStorage = clearAllStorage;
|
|
5104
5176
|
exports.clearApplicationId = clearApplicationId;
|
|
@@ -5118,6 +5190,7 @@ exports.getContextIdentity = getContextIdentity;
|
|
|
5118
5190
|
exports.getNodeUrl = getNodeUrl;
|
|
5119
5191
|
exports.getTokenNodeUrl = getTokenNodeUrl;
|
|
5120
5192
|
exports.isFinalInvitationError = isFinalInvitationError;
|
|
5193
|
+
exports.isReturningFromWallet = isReturningFromWallet;
|
|
5121
5194
|
exports.joinWithNode = joinWithNode;
|
|
5122
5195
|
exports.listDelegatedContexts = listDelegatedContexts;
|
|
5123
5196
|
exports.localStorageTokenStorage = localStorageTokenStorage;
|
|
@@ -5128,9 +5201,11 @@ exports.readPinnedRelayNodeKey = readPinnedRelayNodeKey;
|
|
|
5128
5201
|
exports.readRelayMap = readRelayMap;
|
|
5129
5202
|
exports.relayForContext = relayForContext;
|
|
5130
5203
|
exports.rememberRelay = rememberRelay;
|
|
5204
|
+
exports.resolveApplicationIdFromRegistry = resolveApplicationIdFromRegistry;
|
|
5131
5205
|
exports.resolveMeroTheme = resolveMeroTheme;
|
|
5132
5206
|
exports.resolveRelayFromInvitation = resolveRelayFromInvitation;
|
|
5133
5207
|
exports.saveDelegatedCredential = saveDelegatedCredential;
|
|
5208
|
+
exports.selectLatestBundle = selectLatestBundle;
|
|
5134
5209
|
exports.setApplicationId = setApplicationId;
|
|
5135
5210
|
exports.setContextId = setContextId;
|
|
5136
5211
|
exports.setContextIdentity = setContextIdentity;
|