@kheopskit/core 5.0.0 → 5.1.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/{chunk-7QSGAJ4A.mjs → chunk-3Y5N4N3Q.mjs} +119 -62
- package/dist/chunk-3Y5N4N3Q.mjs.map +1 -0
- package/dist/chunk-DF7EKK4G.mjs +106 -0
- package/dist/chunk-DF7EKK4G.mjs.map +1 -0
- package/dist/chunk-EH73P476.js +106 -0
- package/dist/chunk-EH73P476.js.map +1 -0
- package/dist/chunk-JVSCMJY5.mjs +147 -0
- package/dist/chunk-JVSCMJY5.mjs.map +1 -0
- package/dist/chunk-P7BPHHVV.js +76 -0
- package/dist/chunk-P7BPHHVV.js.map +1 -0
- package/dist/{chunk-4ENHC7G4.js → chunk-SO64R7Z6.js} +114 -57
- package/dist/chunk-SO64R7Z6.js.map +1 -0
- package/dist/chunk-WOPCW2OX.mjs +76 -0
- package/dist/chunk-WOPCW2OX.mjs.map +1 -0
- package/dist/chunk-XOGQMEV2.js +147 -0
- package/dist/chunk-XOGQMEV2.js.map +1 -0
- package/dist/ethereum.d.mts +2 -2
- package/dist/ethereum.d.ts +2 -2
- package/dist/ethereum.js +56 -116
- package/dist/ethereum.js.map +1 -1
- package/dist/ethereum.mjs +56 -116
- package/dist/ethereum.mjs.map +1 -1
- package/dist/index.d.mts +59 -3
- package/dist/index.d.ts +59 -3
- package/dist/index.js +442 -61
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +429 -48
- package/dist/index.mjs.map +1 -1
- package/dist/internal.d.mts +2 -39
- package/dist/internal.d.ts +2 -39
- package/dist/internal.js +3 -15
- package/dist/internal.js.map +1 -1
- package/dist/internal.mjs +6 -18
- package/dist/polkadot.d.mts +2 -2
- package/dist/polkadot.d.ts +2 -2
- package/dist/polkadot.js +75 -148
- package/dist/polkadot.js.map +1 -1
- package/dist/polkadot.mjs +76 -149
- package/dist/polkadot.mjs.map +1 -1
- package/dist/solana.d.mts +2 -2
- package/dist/solana.d.ts +2 -2
- package/dist/solana.js +90 -158
- package/dist/solana.js.map +1 -1
- package/dist/solana.mjs +86 -154
- package/dist/solana.mjs.map +1 -1
- package/dist/{types-C7V7DGlg.d.ts → types-0iDBydsK.d.mts} +24 -46
- package/dist/{types-C7V7DGlg.d.mts → types-0iDBydsK.d.ts} +24 -46
- package/package.json +1 -1
- package/dist/chunk-4ENHC7G4.js.map +0 -1
- package/dist/chunk-6XAZANB5.mjs +0 -450
- package/dist/chunk-6XAZANB5.mjs.map +0 -1
- package/dist/chunk-7QSGAJ4A.mjs.map +0 -1
- package/dist/chunk-B4L6GAYD.js +0 -179
- package/dist/chunk-B4L6GAYD.js.map +0 -1
- package/dist/chunk-XQWJM3KC.js +0 -450
- package/dist/chunk-XQWJM3KC.js.map +0 -1
- package/dist/chunk-YDLCHYHH.mjs +0 -179
- package/dist/chunk-YDLCHYHH.mjs.map +0 -1
package/dist/index.mjs
CHANGED
|
@@ -1,26 +1,20 @@
|
|
|
1
1
|
import {
|
|
2
|
-
|
|
2
|
+
cookieStorage,
|
|
3
3
|
getCachedIcon,
|
|
4
4
|
hydrateAccount,
|
|
5
5
|
hydrateWallet,
|
|
6
|
+
safeLocalStorage,
|
|
6
7
|
serializeAccount,
|
|
7
8
|
serializeWallet,
|
|
8
|
-
setCachedIcons
|
|
9
|
-
|
|
10
|
-
sortWallets
|
|
11
|
-
} from "./chunk-YDLCHYHH.mjs";
|
|
9
|
+
setCachedIcons
|
|
10
|
+
} from "./chunk-3Y5N4N3Q.mjs";
|
|
12
11
|
import "./chunk-BWUUHUDK.mjs";
|
|
13
12
|
import {
|
|
14
|
-
DEFAULT_STORAGE_KEY,
|
|
15
13
|
KheopskitError,
|
|
16
|
-
createKheopskitStore,
|
|
17
|
-
getDefaultStore,
|
|
18
14
|
getWalletAccountId,
|
|
19
15
|
isValidAddress,
|
|
20
|
-
parseWalletAccountId
|
|
21
|
-
|
|
22
|
-
store
|
|
23
|
-
} from "./chunk-6XAZANB5.mjs";
|
|
16
|
+
parseWalletAccountId
|
|
17
|
+
} from "./chunk-DF7EKK4G.mjs";
|
|
24
18
|
import {
|
|
25
19
|
WALLET_CONNECT_WALLET_ID,
|
|
26
20
|
clearAllCachedObservables,
|
|
@@ -29,8 +23,9 @@ import {
|
|
|
29
23
|
isInjectedWallet,
|
|
30
24
|
isValidWalletId,
|
|
31
25
|
isWalletConnectWallet,
|
|
26
|
+
isWalletPlatform,
|
|
32
27
|
parseWalletId
|
|
33
|
-
} from "./chunk-
|
|
28
|
+
} from "./chunk-WOPCW2OX.mjs";
|
|
34
29
|
|
|
35
30
|
// src/api/appKit.ts
|
|
36
31
|
import {
|
|
@@ -204,6 +199,87 @@ var getWalletConnectWallet$ = (config) => {
|
|
|
204
199
|
return cachedAppKit;
|
|
205
200
|
};
|
|
206
201
|
|
|
202
|
+
// src/api/config.ts
|
|
203
|
+
var DEFAULT_STORAGE_KEY = "kheopskit";
|
|
204
|
+
var DEFAULTS = {
|
|
205
|
+
autoReconnect: true,
|
|
206
|
+
debug: false,
|
|
207
|
+
storageKey: DEFAULT_STORAGE_KEY,
|
|
208
|
+
hydrationGracePeriod: 500
|
|
209
|
+
};
|
|
210
|
+
var resolveConfig = (config) => {
|
|
211
|
+
const platforms = config?.platforms ?? [];
|
|
212
|
+
const invalidPlatforms = platforms.filter(
|
|
213
|
+
(p) => typeof p !== "object" || p === null || typeof p.getWallets$ !== "function"
|
|
214
|
+
);
|
|
215
|
+
if (invalidPlatforms.length > 0) {
|
|
216
|
+
throw new Error(
|
|
217
|
+
`[kheopskit] config.platforms must contain plugin instances created by the per-platform factories (e.g. platforms: [polkadot(), ethereum(), solana()] imported from "@kheopskit/core/<platform>"). Invalid entries: ${JSON.stringify(invalidPlatforms)}. String platform names like "polkadot" were removed in v4 \u2014 see MIGRATING_TO_V4.md.`
|
|
218
|
+
);
|
|
219
|
+
}
|
|
220
|
+
if (platforms.length === 0) {
|
|
221
|
+
console.warn(
|
|
222
|
+
'[kheopskit] No platforms configured; wallets and accounts will be empty. Pass e.g. platforms: [polkadot()] from "@kheopskit/core/polkadot".'
|
|
223
|
+
);
|
|
224
|
+
}
|
|
225
|
+
return Object.assign({}, DEFAULTS, config, { platforms });
|
|
226
|
+
};
|
|
227
|
+
|
|
228
|
+
// src/utils/sortAccounts.ts
|
|
229
|
+
var PLATFORM_ORDER = {
|
|
230
|
+
polkadot: 0,
|
|
231
|
+
ethereum: 1,
|
|
232
|
+
solana: 2
|
|
233
|
+
};
|
|
234
|
+
var byWalletName = (a1, a2) => {
|
|
235
|
+
if (a1.walletName.toLowerCase() === "talisman") return -1;
|
|
236
|
+
if (a2.walletName.toLowerCase() === "talisman") return 1;
|
|
237
|
+
return a1.walletName.localeCompare(a2.walletName);
|
|
238
|
+
};
|
|
239
|
+
var sortAccounts = (a1, a2) => {
|
|
240
|
+
if (a1.platform !== a2.platform)
|
|
241
|
+
return PLATFORM_ORDER[a1.platform] - PLATFORM_ORDER[a2.platform];
|
|
242
|
+
if (a1.walletName !== a2.walletName) return byWalletName(a1, a2);
|
|
243
|
+
if (a1.platform === "polkadot" && a2.platform === "polkadot")
|
|
244
|
+
return a1.name !== a2.name ? (a1.name ?? "").localeCompare(a2.name ?? "") : a1.address.localeCompare(a2.address);
|
|
245
|
+
return a1.id.localeCompare(a2.id);
|
|
246
|
+
};
|
|
247
|
+
|
|
248
|
+
// src/utils/sortWallets.ts
|
|
249
|
+
var PLATFORM_ORDER2 = {
|
|
250
|
+
polkadot: 0,
|
|
251
|
+
ethereum: 1,
|
|
252
|
+
solana: 2
|
|
253
|
+
};
|
|
254
|
+
var orderOf = (wallet) => isWalletConnectWallet(wallet) ? 3 : PLATFORM_ORDER2[wallet.platform];
|
|
255
|
+
var sortWallets = (w1, w2) => {
|
|
256
|
+
const o1 = orderOf(w1);
|
|
257
|
+
const o2 = orderOf(w2);
|
|
258
|
+
if (o1 !== o2) return o1 - o2;
|
|
259
|
+
if (w1.name.toLowerCase() === "talisman") return -1;
|
|
260
|
+
if (w2.name.toLowerCase() === "talisman") return 1;
|
|
261
|
+
return w1.name.localeCompare(w2.name);
|
|
262
|
+
};
|
|
263
|
+
|
|
264
|
+
// src/api/platform.ts
|
|
265
|
+
var acceptsCachedAccount = (cached, platforms) => {
|
|
266
|
+
const plugin = platforms.find((p) => p.platform === cached.platform);
|
|
267
|
+
return plugin?.acceptsCachedAccount?.(cached) ?? true;
|
|
268
|
+
};
|
|
269
|
+
|
|
270
|
+
// src/api/hydration.ts
|
|
271
|
+
var getHydratedSnapshot = (store, platforms, options = {}) => {
|
|
272
|
+
const cached = store.getCachedState();
|
|
273
|
+
const wallets = cached.wallets.map((cachedWallet) => {
|
|
274
|
+
const wallet = hydrateWallet(cachedWallet);
|
|
275
|
+
if (!options.icons || wallet.icon) return wallet;
|
|
276
|
+
const icon = getCachedIcon(wallet.id);
|
|
277
|
+
return icon ? { ...wallet, icon } : wallet;
|
|
278
|
+
}).sort(sortWallets);
|
|
279
|
+
const accounts = cached.accounts.filter((account) => acceptsCachedAccount(account, platforms)).map(hydrateAccount).sort(sortAccounts);
|
|
280
|
+
return { wallets, accounts };
|
|
281
|
+
};
|
|
282
|
+
|
|
207
283
|
// src/api/kheopskit.ts
|
|
208
284
|
import {
|
|
209
285
|
combineLatest as combineLatest4,
|
|
@@ -364,6 +440,307 @@ var getAccounts$ = (config, wallets) => {
|
|
|
364
440
|
}).pipe(shareReplay3({ refCount: true, bufferSize: 1 }));
|
|
365
441
|
};
|
|
366
442
|
|
|
443
|
+
// src/api/store.ts
|
|
444
|
+
import { uniq } from "lodash-es";
|
|
445
|
+
|
|
446
|
+
// src/utils/createStore.ts
|
|
447
|
+
import { BehaviorSubject as BehaviorSubject3 } from "rxjs";
|
|
448
|
+
var createStore = (key, defaultValue, storage = safeLocalStorage) => {
|
|
449
|
+
const subject = new BehaviorSubject3(
|
|
450
|
+
getStoredData(key, defaultValue, storage)
|
|
451
|
+
);
|
|
452
|
+
let unsubscribeStorage;
|
|
453
|
+
if (typeof window !== "undefined" && storage.subscribe) {
|
|
454
|
+
unsubscribeStorage = storage.subscribe(key, (newValue) => {
|
|
455
|
+
subject.next(parseData(newValue, defaultValue));
|
|
456
|
+
});
|
|
457
|
+
}
|
|
458
|
+
const update = (val) => {
|
|
459
|
+
setStoredData(key, val, storage);
|
|
460
|
+
subject.next(val);
|
|
461
|
+
};
|
|
462
|
+
return {
|
|
463
|
+
observable: subject.asObservable(),
|
|
464
|
+
set: (val) => update(val),
|
|
465
|
+
mutate: (transform) => update(transform(subject.getValue())),
|
|
466
|
+
get: () => structuredClone(subject.getValue()),
|
|
467
|
+
/**
|
|
468
|
+
* Cleanup subscriptions. Call this when the store is no longer needed.
|
|
469
|
+
*/
|
|
470
|
+
destroy: () => {
|
|
471
|
+
unsubscribeStorage?.();
|
|
472
|
+
subject.complete();
|
|
473
|
+
}
|
|
474
|
+
};
|
|
475
|
+
};
|
|
476
|
+
var parseData = (str, defaultValue) => {
|
|
477
|
+
try {
|
|
478
|
+
if (str) return JSON.parse(str);
|
|
479
|
+
} catch {
|
|
480
|
+
}
|
|
481
|
+
return defaultValue;
|
|
482
|
+
};
|
|
483
|
+
var getStoredData = (key, defaultValue, storage) => {
|
|
484
|
+
const str = storage.getItem(key);
|
|
485
|
+
return parseData(str, defaultValue);
|
|
486
|
+
};
|
|
487
|
+
var setStoredData = (key, val, storage) => {
|
|
488
|
+
const str = JSON.stringify(val);
|
|
489
|
+
storage.setItem(key, str);
|
|
490
|
+
};
|
|
491
|
+
|
|
492
|
+
// src/api/compactStore.ts
|
|
493
|
+
var EMPTY_STORE_DATA = {};
|
|
494
|
+
var toCompactPolkadotAccountType = (type) => {
|
|
495
|
+
switch (type) {
|
|
496
|
+
case "sr25519":
|
|
497
|
+
return 0;
|
|
498
|
+
case "ed25519":
|
|
499
|
+
return 1;
|
|
500
|
+
case "ecdsa":
|
|
501
|
+
return 2;
|
|
502
|
+
case "ethereum":
|
|
503
|
+
return 3;
|
|
504
|
+
default:
|
|
505
|
+
return null;
|
|
506
|
+
}
|
|
507
|
+
};
|
|
508
|
+
var fromCompactPolkadotAccountType = (type) => {
|
|
509
|
+
switch (type) {
|
|
510
|
+
case 0:
|
|
511
|
+
return "sr25519";
|
|
512
|
+
case 1:
|
|
513
|
+
return "ed25519";
|
|
514
|
+
case 2:
|
|
515
|
+
return "ecdsa";
|
|
516
|
+
case 3:
|
|
517
|
+
return "ethereum";
|
|
518
|
+
default:
|
|
519
|
+
return void 0;
|
|
520
|
+
}
|
|
521
|
+
};
|
|
522
|
+
var toCompactPlatform = (platform) => {
|
|
523
|
+
switch (platform) {
|
|
524
|
+
case "polkadot":
|
|
525
|
+
return 0;
|
|
526
|
+
case "ethereum":
|
|
527
|
+
return 1;
|
|
528
|
+
case "solana":
|
|
529
|
+
return 2;
|
|
530
|
+
}
|
|
531
|
+
};
|
|
532
|
+
var fromCompactPlatform = (code) => {
|
|
533
|
+
switch (code) {
|
|
534
|
+
case 0:
|
|
535
|
+
return "polkadot";
|
|
536
|
+
case 1:
|
|
537
|
+
return "ethereum";
|
|
538
|
+
case 2:
|
|
539
|
+
return "solana";
|
|
540
|
+
default:
|
|
541
|
+
return void 0;
|
|
542
|
+
}
|
|
543
|
+
};
|
|
544
|
+
var isCompactStore = (value) => {
|
|
545
|
+
if (!value || typeof value !== "object" || Array.isArray(value)) return false;
|
|
546
|
+
if ("cachedWallets" in value || "cachedAccounts" in value) return false;
|
|
547
|
+
return "v" in value || "w" in value || "a" in value || "r" in value;
|
|
548
|
+
};
|
|
549
|
+
var toCompactStore = (data) => {
|
|
550
|
+
const wallets = data.cachedWallets?.map(
|
|
551
|
+
(wallet) => [
|
|
552
|
+
wallet.id,
|
|
553
|
+
wallet.name,
|
|
554
|
+
wallet.isConnected ? 1 : 0,
|
|
555
|
+
wallet.type === "walletconnect" ? 1 : 0
|
|
556
|
+
]
|
|
557
|
+
);
|
|
558
|
+
const accounts = data.cachedAccounts?.map(
|
|
559
|
+
(account) => [
|
|
560
|
+
account.walletId,
|
|
561
|
+
account.address,
|
|
562
|
+
account.name ?? null,
|
|
563
|
+
account.chainId ?? null,
|
|
564
|
+
toCompactPolkadotAccountType(account.polkadotAccountType),
|
|
565
|
+
toCompactPlatform(account.platform)
|
|
566
|
+
]
|
|
567
|
+
);
|
|
568
|
+
return {
|
|
569
|
+
v: 1,
|
|
570
|
+
r: data.autoReconnect,
|
|
571
|
+
w: wallets?.length ? wallets : void 0,
|
|
572
|
+
a: accounts?.length ? accounts : void 0
|
|
573
|
+
};
|
|
574
|
+
};
|
|
575
|
+
var fromCompactStore = (data) => {
|
|
576
|
+
const walletNameMap = /* @__PURE__ */ new Map();
|
|
577
|
+
const wallets = [];
|
|
578
|
+
for (const item of Array.isArray(data.w) ? data.w : []) {
|
|
579
|
+
if (!Array.isArray(item)) continue;
|
|
580
|
+
const [id, name, isConnected, type] = item;
|
|
581
|
+
if (!isValidWalletId(id)) continue;
|
|
582
|
+
const isWalletConnect = id === WALLET_CONNECT_WALLET_ID;
|
|
583
|
+
const walletType = type === 1 ? "walletconnect" : "injected";
|
|
584
|
+
if (isWalletConnect !== (walletType === "walletconnect")) continue;
|
|
585
|
+
walletNameMap.set(id, name);
|
|
586
|
+
wallets.push({
|
|
587
|
+
id,
|
|
588
|
+
platform: isWalletConnect ? void 0 : parseWalletId(id).platform,
|
|
589
|
+
type: walletType,
|
|
590
|
+
name,
|
|
591
|
+
isConnected: isConnected === 1
|
|
592
|
+
});
|
|
593
|
+
}
|
|
594
|
+
const accounts = [];
|
|
595
|
+
for (const item of Array.isArray(data.a) ? data.a : []) {
|
|
596
|
+
if (!Array.isArray(item)) continue;
|
|
597
|
+
const [
|
|
598
|
+
walletId,
|
|
599
|
+
address,
|
|
600
|
+
name,
|
|
601
|
+
chainId,
|
|
602
|
+
polkadotAccountType,
|
|
603
|
+
platformCode
|
|
604
|
+
] = item;
|
|
605
|
+
if (!isValidWalletId(walletId) || typeof address !== "string" || !address)
|
|
606
|
+
continue;
|
|
607
|
+
const platform = platformCode != null ? fromCompactPlatform(platformCode) : walletId === WALLET_CONNECT_WALLET_ID ? void 0 : parseWalletId(walletId).platform;
|
|
608
|
+
if (!isWalletPlatform(platform)) continue;
|
|
609
|
+
accounts.push({
|
|
610
|
+
id: getWalletAccountId(walletId, address),
|
|
611
|
+
platform,
|
|
612
|
+
address,
|
|
613
|
+
name: name ?? void 0,
|
|
614
|
+
chainId: chainId ?? void 0,
|
|
615
|
+
polkadotAccountType: platform === "polkadot" ? fromCompactPolkadotAccountType(polkadotAccountType) : void 0,
|
|
616
|
+
walletId,
|
|
617
|
+
walletName: walletNameMap.get(walletId) ?? walletId
|
|
618
|
+
});
|
|
619
|
+
}
|
|
620
|
+
return {
|
|
621
|
+
autoReconnect: data.r,
|
|
622
|
+
cachedWallets: wallets,
|
|
623
|
+
cachedAccounts: accounts
|
|
624
|
+
};
|
|
625
|
+
};
|
|
626
|
+
var decodeStore = (raw, fallback) => {
|
|
627
|
+
try {
|
|
628
|
+
const parsed = JSON.parse(raw);
|
|
629
|
+
if (isCompactStore(parsed)) return fromCompactStore(parsed);
|
|
630
|
+
return parsed;
|
|
631
|
+
} catch {
|
|
632
|
+
return fallback;
|
|
633
|
+
}
|
|
634
|
+
};
|
|
635
|
+
var encodeStore = (data) => JSON.stringify(toCompactStore(data));
|
|
636
|
+
var createCompactCookieStorage = (initialCookies) => {
|
|
637
|
+
const base = cookieStorage(initialCookies);
|
|
638
|
+
return {
|
|
639
|
+
getItem: (key) => {
|
|
640
|
+
const raw = base.getItem(key);
|
|
641
|
+
if (!raw) return null;
|
|
642
|
+
const expanded = decodeStore(raw, EMPTY_STORE_DATA);
|
|
643
|
+
if (typeof document !== "undefined") {
|
|
644
|
+
try {
|
|
645
|
+
const parsed = JSON.parse(raw);
|
|
646
|
+
if (!isCompactStore(parsed)) {
|
|
647
|
+
base.setItem(key, encodeStore(expanded));
|
|
648
|
+
}
|
|
649
|
+
} catch {
|
|
650
|
+
}
|
|
651
|
+
}
|
|
652
|
+
return JSON.stringify(expanded);
|
|
653
|
+
},
|
|
654
|
+
setItem: (key, value) => {
|
|
655
|
+
const expanded = decodeStore(value, EMPTY_STORE_DATA);
|
|
656
|
+
base.setItem(key, encodeStore(expanded));
|
|
657
|
+
},
|
|
658
|
+
removeItem: base.removeItem,
|
|
659
|
+
subscribe: (key, callback) => {
|
|
660
|
+
const unsubscribe = base.subscribe?.(key, (value) => {
|
|
661
|
+
if (!value) {
|
|
662
|
+
callback(null);
|
|
663
|
+
return;
|
|
664
|
+
}
|
|
665
|
+
const expanded = decodeStore(value, EMPTY_STORE_DATA);
|
|
666
|
+
callback(JSON.stringify(expanded));
|
|
667
|
+
});
|
|
668
|
+
return () => {
|
|
669
|
+
unsubscribe?.();
|
|
670
|
+
};
|
|
671
|
+
}
|
|
672
|
+
};
|
|
673
|
+
};
|
|
674
|
+
|
|
675
|
+
// src/api/store.ts
|
|
676
|
+
var DEFAULT_SETTINGS = {};
|
|
677
|
+
var isValidCachedWallet = (value) => {
|
|
678
|
+
if (!value || typeof value !== "object") return false;
|
|
679
|
+
const w = value;
|
|
680
|
+
if (!isValidWalletId(w.id)) return false;
|
|
681
|
+
if (typeof w.name !== "string" || typeof w.isConnected !== "boolean")
|
|
682
|
+
return false;
|
|
683
|
+
if (w.type === "walletconnect")
|
|
684
|
+
return w.id === WALLET_CONNECT_WALLET_ID && w.platform === void 0;
|
|
685
|
+
return w.type === "injected" && isWalletPlatform(w.platform);
|
|
686
|
+
};
|
|
687
|
+
var isValidCachedAccount = (value) => {
|
|
688
|
+
if (!value || typeof value !== "object") return false;
|
|
689
|
+
const a = value;
|
|
690
|
+
return typeof a.id === "string" && !!a.id && isWalletPlatform(a.platform) && typeof a.address === "string" && !!a.address && isValidWalletId(a.walletId) && typeof a.walletName === "string";
|
|
691
|
+
};
|
|
692
|
+
var createKheopskitStore = (options = {}) => {
|
|
693
|
+
const { ssrCookies, storageKey = DEFAULT_STORAGE_KEY } = options;
|
|
694
|
+
const storage = ssrCookies !== void 0 ? createCompactCookieStorage(ssrCookies) : safeLocalStorage;
|
|
695
|
+
const store = createStore(storageKey, DEFAULT_SETTINGS, storage);
|
|
696
|
+
const addEnabledWalletId = (walletId) => {
|
|
697
|
+
parseWalletId(walletId);
|
|
698
|
+
store.mutate((prev) => ({
|
|
699
|
+
...prev,
|
|
700
|
+
autoReconnect: uniq((prev.autoReconnect ?? []).concat(walletId))
|
|
701
|
+
}));
|
|
702
|
+
};
|
|
703
|
+
const removeEnabledWalletId = (walletId) => {
|
|
704
|
+
store.mutate((prev) => ({
|
|
705
|
+
...prev,
|
|
706
|
+
autoReconnect: uniq(
|
|
707
|
+
(prev.autoReconnect ?? []).filter((id) => id !== walletId)
|
|
708
|
+
)
|
|
709
|
+
}));
|
|
710
|
+
};
|
|
711
|
+
const getCachedState = () => {
|
|
712
|
+
const data = store.get();
|
|
713
|
+
const cachedWallets = Array.isArray(data?.cachedWallets) ? data.cachedWallets : [];
|
|
714
|
+
const cachedAccounts = Array.isArray(data?.cachedAccounts) ? data.cachedAccounts : [];
|
|
715
|
+
return {
|
|
716
|
+
wallets: cachedWallets.filter(isValidCachedWallet),
|
|
717
|
+
accounts: cachedAccounts.filter(isValidCachedAccount)
|
|
718
|
+
};
|
|
719
|
+
};
|
|
720
|
+
const setCachedState = (wallets, accounts) => {
|
|
721
|
+
store.mutate((prev) => ({
|
|
722
|
+
...prev,
|
|
723
|
+
cachedWallets: wallets,
|
|
724
|
+
cachedAccounts: accounts
|
|
725
|
+
}));
|
|
726
|
+
};
|
|
727
|
+
return {
|
|
728
|
+
observable: store.observable,
|
|
729
|
+
addEnabledWalletId,
|
|
730
|
+
removeEnabledWalletId,
|
|
731
|
+
getCachedState,
|
|
732
|
+
setCachedState
|
|
733
|
+
};
|
|
734
|
+
};
|
|
735
|
+
var DEFAULT_STORE_SYMBOL = /* @__PURE__ */ Symbol.for("kheopskit.defaultStore");
|
|
736
|
+
var getDefaultStore = () => {
|
|
737
|
+
const g = globalThis;
|
|
738
|
+
if (!g[DEFAULT_STORE_SYMBOL]) {
|
|
739
|
+
g[DEFAULT_STORE_SYMBOL] = createKheopskitStore();
|
|
740
|
+
}
|
|
741
|
+
return g[DEFAULT_STORE_SYMBOL];
|
|
742
|
+
};
|
|
743
|
+
|
|
367
744
|
// src/api/wallets.ts
|
|
368
745
|
import {
|
|
369
746
|
combineLatest as combineLatest3,
|
|
@@ -375,14 +752,42 @@ import {
|
|
|
375
752
|
shareReplay as shareReplay4,
|
|
376
753
|
take as take2
|
|
377
754
|
} from "rxjs";
|
|
378
|
-
|
|
379
|
-
|
|
755
|
+
|
|
756
|
+
// src/api/reconnectPolicy.ts
|
|
757
|
+
var createReconnectPolicy = ({
|
|
758
|
+
maxAttempts = 3
|
|
759
|
+
} = {}) => {
|
|
760
|
+
const reconnecting = /* @__PURE__ */ new Set();
|
|
761
|
+
const reconnected = /* @__PURE__ */ new Set();
|
|
762
|
+
const failedAttempts = /* @__PURE__ */ new Map();
|
|
763
|
+
const shouldAttempt = (walletId) => !reconnecting.has(walletId) && !reconnected.has(walletId) && (failedAttempts.get(walletId) ?? 0) < maxAttempts;
|
|
764
|
+
const attempt = async (walletId, connect) => {
|
|
765
|
+
if (!shouldAttempt(walletId)) return false;
|
|
766
|
+
reconnecting.add(walletId);
|
|
767
|
+
try {
|
|
768
|
+
await connect();
|
|
769
|
+
reconnected.add(walletId);
|
|
770
|
+
failedAttempts.delete(walletId);
|
|
771
|
+
return true;
|
|
772
|
+
} catch (err) {
|
|
773
|
+
failedAttempts.set(walletId, (failedAttempts.get(walletId) ?? 0) + 1);
|
|
774
|
+
throw err;
|
|
775
|
+
} finally {
|
|
776
|
+
reconnecting.delete(walletId);
|
|
777
|
+
}
|
|
778
|
+
};
|
|
779
|
+
return { shouldAttempt, attempt };
|
|
780
|
+
};
|
|
781
|
+
|
|
782
|
+
// src/api/wallets.ts
|
|
783
|
+
var getWallets$ = (config, store) => {
|
|
784
|
+
const autoReconnectWalletIds$ = store.observable.pipe(
|
|
380
785
|
map4((s) => s.autoReconnect ?? []),
|
|
381
786
|
take2(1),
|
|
382
787
|
shareReplay4({ bufferSize: 1, refCount: true })
|
|
383
788
|
);
|
|
384
789
|
return new Observable4((subscriber) => {
|
|
385
|
-
const ctx = { config, store
|
|
790
|
+
const ctx = { config, store };
|
|
386
791
|
const observables = config.platforms.map(
|
|
387
792
|
(plugin) => plugin.getWallets$(ctx)
|
|
388
793
|
);
|
|
@@ -397,32 +802,18 @@ var getWallets$ = (config, store2 = store) => {
|
|
|
397
802
|
})
|
|
398
803
|
// Note: No startWith([]) here - the hydration buffer handles initial state
|
|
399
804
|
);
|
|
400
|
-
const
|
|
401
|
-
const reconnectedWallets = /* @__PURE__ */ new Set();
|
|
402
|
-
const MAX_RECONNECT_ATTEMPTS = 3;
|
|
403
|
-
const failedAttempts = /* @__PURE__ */ new Map();
|
|
805
|
+
const reconnectPolicy = createReconnectPolicy();
|
|
404
806
|
const subAutoReconnect = combineLatest3([wallets$, autoReconnectWalletIds$]).pipe(
|
|
405
807
|
filter2(([, walletIds]) => config.autoReconnect && !!walletIds?.length),
|
|
406
808
|
mergeMap(
|
|
407
809
|
([wallets, walletIds]) => wallets.filter((wallet) => walletIds?.includes(wallet.id))
|
|
408
810
|
)
|
|
409
811
|
).subscribe(async (wallet) => {
|
|
410
|
-
if (wallet.isConnected
|
|
411
|
-
return;
|
|
412
|
-
}
|
|
413
|
-
reconnectingWallets.add(wallet.id);
|
|
812
|
+
if (wallet.isConnected) return;
|
|
414
813
|
try {
|
|
415
|
-
await wallet.connect();
|
|
416
|
-
reconnectedWallets.add(wallet.id);
|
|
417
|
-
failedAttempts.delete(wallet.id);
|
|
814
|
+
await reconnectPolicy.attempt(wallet.id, () => wallet.connect());
|
|
418
815
|
} catch (err) {
|
|
419
|
-
failedAttempts.set(
|
|
420
|
-
wallet.id,
|
|
421
|
-
(failedAttempts.get(wallet.id) ?? 0) + 1
|
|
422
|
-
);
|
|
423
816
|
console.error("Failed to reconnect wallet %s", wallet.id, { err });
|
|
424
|
-
} finally {
|
|
425
|
-
reconnectingWallets.delete(wallet.id);
|
|
426
817
|
}
|
|
427
818
|
});
|
|
428
819
|
const subWallets = wallets$.subscribe(subscriber);
|
|
@@ -437,25 +828,14 @@ var getWallets$ = (config, store2 = store) => {
|
|
|
437
828
|
var getKheopskit$ = (config, options = {}) => {
|
|
438
829
|
const { ssrCookies, store: existingStore } = options;
|
|
439
830
|
const kc = resolveConfig(config);
|
|
440
|
-
const
|
|
831
|
+
const store = existingStore ?? createKheopskitStore({ ssrCookies, storageKey: kc.storageKey });
|
|
441
832
|
if (kc.debug) console.debug("[kheopskit] config", kc);
|
|
442
833
|
if (kc.debug && typeof window === "undefined" && ssrCookies === void 0) {
|
|
443
834
|
console.warn(
|
|
444
835
|
"[kheopskit] Running on server without `ssrCookies`. Wallet state will not be hydrated. Pass cookies for SSR support."
|
|
445
836
|
);
|
|
446
837
|
}
|
|
447
|
-
const
|
|
448
|
-
const cachedWallets = cachedState.wallets.map((w) => {
|
|
449
|
-
const wallet = hydrateWallet(w);
|
|
450
|
-
if (!wallet.icon) {
|
|
451
|
-
const cachedIcon = getCachedIcon(wallet.id);
|
|
452
|
-
if (cachedIcon) {
|
|
453
|
-
return { ...wallet, icon: cachedIcon };
|
|
454
|
-
}
|
|
455
|
-
}
|
|
456
|
-
return wallet;
|
|
457
|
-
});
|
|
458
|
-
const cachedAccounts = cachedState.accounts.filter((cached) => acceptsCachedAccount(cached, kc.platforms)).map(hydrateAccount);
|
|
838
|
+
const { wallets: cachedWallets, accounts: cachedAccounts } = getHydratedSnapshot(store, kc.platforms, { icons: true });
|
|
459
839
|
if (kc.debug && cachedWallets.length > 0) {
|
|
460
840
|
console.debug("[kheopskit] hydrating from cache:", {
|
|
461
841
|
wallets: cachedWallets.length,
|
|
@@ -463,7 +843,7 @@ var getKheopskit$ = (config, options = {}) => {
|
|
|
463
843
|
});
|
|
464
844
|
}
|
|
465
845
|
return new Observable5((subscriber) => {
|
|
466
|
-
const liveWallets$ = getWallets$(kc,
|
|
846
|
+
const liveWallets$ = getWallets$(kc, store);
|
|
467
847
|
const liveAccounts$ = getAccounts$(kc, liveWallets$);
|
|
468
848
|
const bufferedWallets$ = createHydrationBuffer(
|
|
469
849
|
cachedWallets,
|
|
@@ -569,7 +949,7 @@ var getKheopskit$ = (config, options = {}) => {
|
|
|
569
949
|
accounts: relevantAccounts.length
|
|
570
950
|
});
|
|
571
951
|
}
|
|
572
|
-
|
|
952
|
+
store.setCachedState(
|
|
573
953
|
wallets.items.map(serializeWallet),
|
|
574
954
|
relevantAccounts.map(serializeAccount)
|
|
575
955
|
);
|
|
@@ -624,6 +1004,7 @@ export {
|
|
|
624
1004
|
clearAllCachedObservables,
|
|
625
1005
|
createKheopskitStore,
|
|
626
1006
|
getDefaultStore,
|
|
1007
|
+
getHydratedSnapshot,
|
|
627
1008
|
getKheopskit$,
|
|
628
1009
|
getWalletAccountId,
|
|
629
1010
|
getWalletId,
|