@calimero-network/mero-react 9.9.1 → 9.10.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 +24 -1
- package/dist/index.cjs +216 -195
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +43 -9
- package/dist/index.d.ts +43 -9
- package/dist/index.js +217 -197
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -335,7 +335,30 @@ Helpers: `defaultMeroTheme` (the full default palette), `resolveMeroTheme(partia
|
|
|
335
335
|
| `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
336
|
| `accountDefaults` | `{ walletUrl?: string }` | — | Local development only: enrol at a wallet other than the hosted one. |
|
|
337
337
|
|
|
338
|
-
|
|
338
|
+
### `<LoginModal>` and the Cloud tab
|
|
339
|
+
|
|
340
|
+
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`.
|
|
341
|
+
|
|
342
|
+
```tsx
|
|
343
|
+
// Node + Cloud tabs, account sign-in included (default)
|
|
344
|
+
<LoginModal isOpen={open} onConnect={connectToNode} onClose={() => setOpen(false)} />
|
|
345
|
+
|
|
346
|
+
// Node dialog only, no tabs — the pre-Cloud behaviour
|
|
347
|
+
<LoginModal cloud={false} isOpen={open} onConnect={connectToNode} onClose={() => setOpen(false)} />
|
|
348
|
+
|
|
349
|
+
// Your own enrolment (what ConnectButton does): the object is used verbatim
|
|
350
|
+
<LoginModal cloud={{ onEnrol, note, walletUrl, customWallet }} initialTab="cloud" ... />
|
|
351
|
+
```
|
|
352
|
+
|
|
353
|
+
| `cloud` | Cloud tab | Enrolment callback read by |
|
|
354
|
+
|---------|-----------|----------------------------|
|
|
355
|
+
| omitted | shown, sourced by the modal | the modal |
|
|
356
|
+
| `false` | none (no tabs) | nobody here |
|
|
357
|
+
| object | shown, the caller's values | the caller |
|
|
358
|
+
|
|
359
|
+
`initialTab` defaults to `'node'`; with `cloud` omitted it defaults to `'cloud'` on the load coming back from the wallet. An explicit `initialTab` wins.
|
|
360
|
+
|
|
361
|
+
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
362
|
|
|
340
363
|
## Enums
|
|
341
364
|
|
package/dist/index.cjs
CHANGED
|
@@ -1933,6 +1933,196 @@ function AccountSignInPanel({
|
|
|
1933
1933
|
] })
|
|
1934
1934
|
] });
|
|
1935
1935
|
}
|
|
1936
|
+
var DEVICE_KEY = "calimero.device";
|
|
1937
|
+
var STATE_KEY = "calimero.enrol.state";
|
|
1938
|
+
var HOSTED_WALLET = "https://wallet.cloud.calimero.network/account-enroll";
|
|
1939
|
+
var hex = (b) => [...new Uint8Array(b)].map((x) => x.toString(16).padStart(2, "0")).join("");
|
|
1940
|
+
function executorOf(relay) {
|
|
1941
|
+
const value = relay.executorAccount;
|
|
1942
|
+
return typeof value === "string" && /^[0-9a-f]{64}$/.test(value) ? value : null;
|
|
1943
|
+
}
|
|
1944
|
+
function chooseRelay(relays) {
|
|
1945
|
+
const reachable = relays.filter(
|
|
1946
|
+
(r) => typeof r.relayUrl === "string" && r.relayUrl.length > 0
|
|
1947
|
+
);
|
|
1948
|
+
const fresh = reachable.find((r) => r.fresh);
|
|
1949
|
+
if (fresh) return { relayUrl: fresh.relayUrl, executorAccount: executorOf(fresh), note: null };
|
|
1950
|
+
if (reachable.length > 0) {
|
|
1951
|
+
return {
|
|
1952
|
+
relayUrl: reachable[0].relayUrl,
|
|
1953
|
+
executorAccount: executorOf(reachable[0]),
|
|
1954
|
+
note: "Connected through a relay whose last heartbeat has lapsed \u2014 it may not answer. It was the only one with an address."
|
|
1955
|
+
};
|
|
1956
|
+
}
|
|
1957
|
+
if (relays.length > 0) {
|
|
1958
|
+
return {
|
|
1959
|
+
relayUrl: null,
|
|
1960
|
+
executorAccount: null,
|
|
1961
|
+
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.`
|
|
1962
|
+
};
|
|
1963
|
+
}
|
|
1964
|
+
return {
|
|
1965
|
+
relayUrl: null,
|
|
1966
|
+
executorAccount: null,
|
|
1967
|
+
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."
|
|
1968
|
+
};
|
|
1969
|
+
}
|
|
1970
|
+
async function deviceKeys() {
|
|
1971
|
+
const had = localStorage.getItem(DEVICE_KEY);
|
|
1972
|
+
if (had) {
|
|
1973
|
+
const parsed = JSON.parse(had);
|
|
1974
|
+
if (parsed.kemSk && parsed.signSk && parsed.signPk && parsed.kemPk) {
|
|
1975
|
+
return parsed;
|
|
1976
|
+
}
|
|
1977
|
+
localStorage.removeItem(DEVICE_KEY);
|
|
1978
|
+
}
|
|
1979
|
+
const sign = await crypto.subtle.generateKey({ name: "Ed25519" }, true, [
|
|
1980
|
+
"sign",
|
|
1981
|
+
"verify"
|
|
1982
|
+
]);
|
|
1983
|
+
let agree;
|
|
1984
|
+
try {
|
|
1985
|
+
agree = await crypto.subtle.generateKey({ name: "X25519" }, true, [
|
|
1986
|
+
"deriveBits"
|
|
1987
|
+
]);
|
|
1988
|
+
} catch {
|
|
1989
|
+
throw new Error(
|
|
1990
|
+
"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."
|
|
1991
|
+
);
|
|
1992
|
+
}
|
|
1993
|
+
const keys = {
|
|
1994
|
+
signPk: hex(await crypto.subtle.exportKey("raw", sign.publicKey)),
|
|
1995
|
+
signSk: hex((await crypto.subtle.exportKey("pkcs8", sign.privateKey)).slice(16)),
|
|
1996
|
+
kemPk: hex(await crypto.subtle.exportKey("raw", agree.publicKey)),
|
|
1997
|
+
kemSk: hex((await crypto.subtle.exportKey("pkcs8", agree.privateKey)).slice(16))
|
|
1998
|
+
};
|
|
1999
|
+
localStorage.setItem(DEVICE_KEY, JSON.stringify(keys));
|
|
2000
|
+
return keys;
|
|
2001
|
+
}
|
|
2002
|
+
function isReturningFromWallet(location = typeof window === "undefined" ? void 0 : window.location) {
|
|
2003
|
+
if (!location) return false;
|
|
2004
|
+
const params = new URLSearchParams(location.hash.replace(/^#/, ""));
|
|
2005
|
+
if (params.get("error")) return true;
|
|
2006
|
+
return Boolean(params.get("credential") && params.get("account") && params.get("device"));
|
|
2007
|
+
}
|
|
2008
|
+
function useAccountEnrolment({
|
|
2009
|
+
walletUrl: walletOverride,
|
|
2010
|
+
enabled = true
|
|
2011
|
+
} = {}) {
|
|
2012
|
+
const { connectWithAccount, cloudBaseUrl } = useMero();
|
|
2013
|
+
const [note, setNote] = react.useState(null);
|
|
2014
|
+
const walletUrl = walletOverride ?? HOSTED_WALLET;
|
|
2015
|
+
const callbackRef = react.useRef(void 0);
|
|
2016
|
+
const callbackErrorRef = react.useRef(null);
|
|
2017
|
+
if (enabled && callbackRef.current === void 0 && typeof window !== "undefined") {
|
|
2018
|
+
try {
|
|
2019
|
+
callbackRef.current = meroJs.readEnrolmentCallback();
|
|
2020
|
+
} catch (e) {
|
|
2021
|
+
callbackRef.current = null;
|
|
2022
|
+
callbackErrorRef.current = e instanceof Error ? e.message : String(e);
|
|
2023
|
+
}
|
|
2024
|
+
}
|
|
2025
|
+
const returningRef = react.useRef(null);
|
|
2026
|
+
if (returningRef.current === null && callbackRef.current !== void 0) {
|
|
2027
|
+
returningRef.current = callbackRef.current !== null || callbackErrorRef.current !== null;
|
|
2028
|
+
}
|
|
2029
|
+
const completingRef = react.useRef(false);
|
|
2030
|
+
react.useEffect(() => {
|
|
2031
|
+
if (callbackErrorRef.current) {
|
|
2032
|
+
setNote(callbackErrorRef.current);
|
|
2033
|
+
callbackErrorRef.current = null;
|
|
2034
|
+
return;
|
|
2035
|
+
}
|
|
2036
|
+
const back = callbackRef.current;
|
|
2037
|
+
if (!back || completingRef.current) return;
|
|
2038
|
+
completingRef.current = true;
|
|
2039
|
+
let cancelled = false;
|
|
2040
|
+
(async () => {
|
|
2041
|
+
try {
|
|
2042
|
+
const keys = await deviceKeys();
|
|
2043
|
+
const sent = (() => {
|
|
2044
|
+
try {
|
|
2045
|
+
return sessionStorage.getItem(STATE_KEY);
|
|
2046
|
+
} catch {
|
|
2047
|
+
return null;
|
|
2048
|
+
}
|
|
2049
|
+
})();
|
|
2050
|
+
try {
|
|
2051
|
+
sessionStorage.removeItem(STATE_KEY);
|
|
2052
|
+
} catch {
|
|
2053
|
+
}
|
|
2054
|
+
if (!sent) {
|
|
2055
|
+
setNote(
|
|
2056
|
+
'This enrolment could not be verified as one this tab started, so it was not accepted. Start again from "Enrol with your account".'
|
|
2057
|
+
);
|
|
2058
|
+
return;
|
|
2059
|
+
}
|
|
2060
|
+
const enrolled = await meroJs.completeDeviceEnrolment({
|
|
2061
|
+
...back,
|
|
2062
|
+
devicePublicKey: keys.signPk,
|
|
2063
|
+
kemPublicKey: keys.kemPk,
|
|
2064
|
+
expectState: sent
|
|
2065
|
+
});
|
|
2066
|
+
saveDelegatedCredential({
|
|
2067
|
+
account: enrolled.account,
|
|
2068
|
+
credential: enrolled.credential,
|
|
2069
|
+
deviceSecret: keys.signSk
|
|
2070
|
+
});
|
|
2071
|
+
const cloud = new meroJs.CloudClient({
|
|
2072
|
+
cloudBaseUrl,
|
|
2073
|
+
routingCredential: {
|
|
2074
|
+
credential: enrolled.credential,
|
|
2075
|
+
deviceSecret: keys.signSk
|
|
2076
|
+
}
|
|
2077
|
+
});
|
|
2078
|
+
const chosen = chooseRelay(await cloud.getAccountRelays(enrolled.account));
|
|
2079
|
+
if (chosen.note) setNote(chosen.note);
|
|
2080
|
+
connectWithAccount({
|
|
2081
|
+
relayUrl: chosen.relayUrl,
|
|
2082
|
+
account: enrolled.account,
|
|
2083
|
+
credential: enrolled.credential,
|
|
2084
|
+
deviceSecret: keys.signSk,
|
|
2085
|
+
...chosen.executorAccount ? { executorAccount: chosen.executorAccount } : {}
|
|
2086
|
+
});
|
|
2087
|
+
} catch (e) {
|
|
2088
|
+
if (!cancelled) setNote(e instanceof Error ? e.message : String(e));
|
|
2089
|
+
}
|
|
2090
|
+
})();
|
|
2091
|
+
return () => {
|
|
2092
|
+
cancelled = true;
|
|
2093
|
+
};
|
|
2094
|
+
}, [connectWithAccount, cloudBaseUrl]);
|
|
2095
|
+
const goToWallet = react.useCallback(async () => {
|
|
2096
|
+
let keys;
|
|
2097
|
+
try {
|
|
2098
|
+
keys = await deviceKeys();
|
|
2099
|
+
} catch (e) {
|
|
2100
|
+
setNote(e instanceof Error ? e.message : String(e));
|
|
2101
|
+
return;
|
|
2102
|
+
}
|
|
2103
|
+
const state = hex(crypto.getRandomValues(new Uint8Array(16)));
|
|
2104
|
+
try {
|
|
2105
|
+
sessionStorage.setItem(STATE_KEY, state);
|
|
2106
|
+
} catch {
|
|
2107
|
+
}
|
|
2108
|
+
window.location.assign(
|
|
2109
|
+
meroJs.deviceEnrolmentUrl({
|
|
2110
|
+
walletUrl,
|
|
2111
|
+
devicePublicKey: keys.signPk,
|
|
2112
|
+
kemPublicKey: keys.kemPk,
|
|
2113
|
+
returnTo: window.location.origin + window.location.pathname,
|
|
2114
|
+
state
|
|
2115
|
+
})
|
|
2116
|
+
);
|
|
2117
|
+
}, [walletUrl]);
|
|
2118
|
+
return {
|
|
2119
|
+
goToWallet,
|
|
2120
|
+
note,
|
|
2121
|
+
returning: returningRef.current === true,
|
|
2122
|
+
walletUrl,
|
|
2123
|
+
customWallet: walletOverride !== void 0
|
|
2124
|
+
};
|
|
2125
|
+
}
|
|
1936
2126
|
var CUSTOM_SELECTION = "__custom__";
|
|
1937
2127
|
var TABS = [
|
|
1938
2128
|
{ id: "node", label: "Node" },
|
|
@@ -2216,8 +2406,21 @@ function LoginModal({
|
|
|
2216
2406
|
theme,
|
|
2217
2407
|
localNodePorts = meroJs.DEFAULT_LOCAL_NODE_PORTS,
|
|
2218
2408
|
cloud,
|
|
2219
|
-
initialTab
|
|
2409
|
+
initialTab: initialTabProp
|
|
2220
2410
|
}) {
|
|
2411
|
+
const own = useAccountEnrolment({ enabled: cloud === void 0 });
|
|
2412
|
+
const cloudTab = react.useMemo(
|
|
2413
|
+
() => cloud === void 0 ? {
|
|
2414
|
+
onEnrol: () => {
|
|
2415
|
+
void own.goToWallet();
|
|
2416
|
+
},
|
|
2417
|
+
note: own.note,
|
|
2418
|
+
walletUrl: own.walletUrl,
|
|
2419
|
+
customWallet: own.customWallet
|
|
2420
|
+
} : cloud || null,
|
|
2421
|
+
[cloud, own.goToWallet, own.note, own.walletUrl, own.customWallet]
|
|
2422
|
+
);
|
|
2423
|
+
const initialTab = initialTabProp ?? (cloud === void 0 && own.returning ? "cloud" : "node");
|
|
2221
2424
|
const [tab, setTab] = react.useState(initialTab);
|
|
2222
2425
|
react.useEffect(() => {
|
|
2223
2426
|
if (isOpen) setTab(initialTab);
|
|
@@ -2226,7 +2429,7 @@ function LoginModal({
|
|
|
2226
2429
|
node: null,
|
|
2227
2430
|
cloud: null
|
|
2228
2431
|
});
|
|
2229
|
-
const activeTab =
|
|
2432
|
+
const activeTab = cloudTab ? tab : "node";
|
|
2230
2433
|
const [selected, setSelected] = react.useState(CUSTOM_SELECTION);
|
|
2231
2434
|
const [discovered, setDiscovered] = react.useState([]);
|
|
2232
2435
|
const [discovering, setDiscovering] = react.useState(false);
|
|
@@ -2426,7 +2629,7 @@ function LoginModal({
|
|
|
2426
2629
|
}
|
|
2427
2630
|
) })
|
|
2428
2631
|
] });
|
|
2429
|
-
if (!
|
|
2632
|
+
if (!cloudTab) return body;
|
|
2430
2633
|
return /* @__PURE__ */ jsxRuntime.jsx(
|
|
2431
2634
|
"div",
|
|
2432
2635
|
{
|
|
@@ -2487,8 +2690,8 @@ function LoginModal({
|
|
|
2487
2690
|
/* @__PURE__ */ jsxRuntime.jsx(CalimeroLogo, { size: 44, color: cssVar(resolved, "primary") }),
|
|
2488
2691
|
/* @__PURE__ */ jsxRuntime.jsx("h1", { style: styles.title, children: "Connect to Calimero" })
|
|
2489
2692
|
] }),
|
|
2490
|
-
|
|
2491
|
-
activeTab === "cloud" &&
|
|
2693
|
+
cloudTab && renderTabs(),
|
|
2694
|
+
activeTab === "cloud" && cloudTab ? /* @__PURE__ */ jsxRuntime.jsx(
|
|
2492
2695
|
"div",
|
|
2493
2696
|
{
|
|
2494
2697
|
role: "tabpanel",
|
|
@@ -2498,10 +2701,10 @@ function LoginModal({
|
|
|
2498
2701
|
children: /* @__PURE__ */ jsxRuntime.jsx(
|
|
2499
2702
|
AccountSignInPanel,
|
|
2500
2703
|
{
|
|
2501
|
-
onEnrol:
|
|
2502
|
-
note:
|
|
2503
|
-
walletUrl:
|
|
2504
|
-
customWallet:
|
|
2704
|
+
onEnrol: cloudTab.onEnrol,
|
|
2705
|
+
note: cloudTab.note,
|
|
2706
|
+
walletUrl: cloudTab.walletUrl,
|
|
2707
|
+
customWallet: cloudTab.customWallet,
|
|
2505
2708
|
theme
|
|
2506
2709
|
}
|
|
2507
2710
|
)
|
|
@@ -2511,190 +2714,6 @@ function LoginModal({
|
|
|
2511
2714
|
] });
|
|
2512
2715
|
return reactDom.createPortal(modalContent, document.body);
|
|
2513
2716
|
}
|
|
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
2717
|
function shortAccount(account) {
|
|
2699
2718
|
return account.length > 14 ? `${account.slice(0, 6)}\u2026${account.slice(-6)}` : account;
|
|
2700
2719
|
}
|
|
@@ -2873,7 +2892,7 @@ function ConnectButton({
|
|
|
2873
2892
|
note: enrolment.note,
|
|
2874
2893
|
walletUrl: enrolment.walletUrl,
|
|
2875
2894
|
customWallet: enrolment.customWallet
|
|
2876
|
-
} :
|
|
2895
|
+
} : false,
|
|
2877
2896
|
initialTab: cloud && enrolment.returning ? "cloud" : "node"
|
|
2878
2897
|
}
|
|
2879
2898
|
)
|
|
@@ -3033,7 +3052,8 @@ function ConnectButtonAccount({
|
|
|
3033
3052
|
connectToNode(url);
|
|
3034
3053
|
},
|
|
3035
3054
|
onClose: () => setIsNodeModalOpen(false),
|
|
3036
|
-
theme: resolvedTheme ?? theme
|
|
3055
|
+
theme: resolvedTheme ?? theme,
|
|
3056
|
+
cloud: false
|
|
3037
3057
|
}
|
|
3038
3058
|
)
|
|
3039
3059
|
] });
|
|
@@ -5118,6 +5138,7 @@ exports.getContextIdentity = getContextIdentity;
|
|
|
5118
5138
|
exports.getNodeUrl = getNodeUrl;
|
|
5119
5139
|
exports.getTokenNodeUrl = getTokenNodeUrl;
|
|
5120
5140
|
exports.isFinalInvitationError = isFinalInvitationError;
|
|
5141
|
+
exports.isReturningFromWallet = isReturningFromWallet;
|
|
5121
5142
|
exports.joinWithNode = joinWithNode;
|
|
5122
5143
|
exports.listDelegatedContexts = listDelegatedContexts;
|
|
5123
5144
|
exports.localStorageTokenStorage = localStorageTokenStorage;
|