@kheopskit/core 0.0.20 → 0.0.22

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.mjs CHANGED
@@ -1,3 +1,22 @@
1
+ // src/api/config.ts
2
+ var DEFAULT_CONFIG = {
3
+ autoReconnect: true,
4
+ platforms: ["polkadot"],
5
+ debug: false
6
+ };
7
+ var resolveConfig = (config) => {
8
+ return Object.assign({}, DEFAULT_CONFIG, config);
9
+ };
10
+
11
+ // src/api/kheopskit.ts
12
+ import {
13
+ combineLatest as combineLatest8,
14
+ map as map9,
15
+ Observable as Observable8,
16
+ shareReplay as shareReplay8,
17
+ throttleTime
18
+ } from "rxjs";
19
+
1
20
  // src/utils/logObservable.ts
2
21
  import { tap } from "rxjs";
3
22
  var logObservable = (label, opts) => tap((value) => {
@@ -8,14 +27,8 @@ var logObservable = (label, opts) => tap((value) => {
8
27
  else console.debug(text);
9
28
  });
10
29
 
11
- // src/api/kheopskit.ts
12
- import {
13
- Observable as Observable8,
14
- combineLatest as combineLatest8,
15
- map as map9,
16
- shareReplay as shareReplay8,
17
- throttleTime
18
- } from "rxjs";
30
+ // src/api/accounts.ts
31
+ import { combineLatest as combineLatest3, map as map4, Observable as Observable3, of as of3, shareReplay as shareReplay3 } from "rxjs";
19
32
 
20
33
  // src/utils/sortAccounts.ts
21
34
  var sortAccounts = (a1, a2) => {
@@ -41,8 +54,22 @@ var sortAccounts = (a1, a2) => {
41
54
  return 0;
42
55
  };
43
56
 
44
- // src/api/accounts.ts
45
- import { Observable as Observable3, combineLatest as combineLatest3, map as map4, of as of3, shareReplay as shareReplay3 } from "rxjs";
57
+ // src/api/ethereum/accounts.ts
58
+ import {
59
+ combineLatest,
60
+ distinctUntilChanged,
61
+ map as map2,
62
+ Observable,
63
+ of,
64
+ ReplaySubject,
65
+ shareReplay,
66
+ switchMap
67
+ } from "rxjs";
68
+ import {
69
+ createWalletClient,
70
+ custom,
71
+ getAddress
72
+ } from "viem";
46
73
 
47
74
  // src/utils/createStore.ts
48
75
  import { BehaviorSubject, filter, fromEvent, map } from "rxjs";
@@ -101,6 +128,9 @@ var isValidAddress = (address) => {
101
128
  return address.startsWith("0x") ? isEthereumAddress(address) : isSs58Address(address);
102
129
  };
103
130
 
131
+ // src/utils/isWalletPlatform.ts
132
+ var isWalletPlatform = (platform) => typeof platform === "string" && ["polkadot", "ethereum"].includes(platform);
133
+
104
134
  // src/utils/WalletAccountId.ts
105
135
  var getWalletAccountId = (walletId, address) => {
106
136
  if (!walletId) throw new Error("Missing walletId");
@@ -108,9 +138,6 @@ var getWalletAccountId = (walletId, address) => {
108
138
  return `${walletId}::${address}`;
109
139
  };
110
140
 
111
- // src/utils/isWalletPlatform.ts
112
- var isWalletPlatform = (platform) => typeof platform === "string" && ["polkadot", "ethereum"].includes(platform);
113
-
114
141
  // src/utils/getCachedObservable.ts
115
142
  var CACHE = /* @__PURE__ */ new Map();
116
143
  var getCachedObservable$ = (key, create) => {
@@ -119,21 +146,6 @@ var getCachedObservable$ = (key, create) => {
119
146
  };
120
147
 
121
148
  // src/api/ethereum/accounts.ts
122
- import {
123
- Observable,
124
- ReplaySubject,
125
- combineLatest,
126
- distinctUntilChanged,
127
- map as map2,
128
- of,
129
- shareReplay,
130
- switchMap
131
- } from "rxjs";
132
- import {
133
- createWalletClient,
134
- custom,
135
- getAddress
136
- } from "viem";
137
149
  var getInjectedWalletAccounts$ = (wallet) => {
138
150
  if (!wallet.isConnected) return of([]);
139
151
  return getCachedObservable$(
@@ -207,7 +219,7 @@ var getAppKitAccounts$ = (wallet) => {
207
219
  (caipNetworkId) => custom(
208
220
  wrapWalletConnectProvider(
209
221
  provider,
210
- // biome-ignore lint/style/noNonNullAssertion: <explanation>
222
+ // biome-ignore lint/style/noNonNullAssertion: legacy
211
223
  provider.session.topic,
212
224
  caipNetworkId
213
225
  )
@@ -268,10 +280,10 @@ import {
268
280
  getPolkadotSignerFromPjs
269
281
  } from "polkadot-api/pjs-signer";
270
282
  import {
271
- Observable as Observable2,
272
283
  combineLatest as combineLatest2,
273
284
  distinctUntilChanged as distinctUntilChanged2,
274
285
  map as map3,
286
+ Observable as Observable2,
275
287
  of as of2,
276
288
  shareReplay as shareReplay2,
277
289
  switchMap as switchMap2
@@ -408,15 +420,19 @@ var getAccounts$ = (config, wallets) => {
408
420
  }).pipe(shareReplay3({ refCount: true, bufferSize: 1 }));
409
421
  };
410
422
 
411
- // src/api/config.ts
412
- var DEFAULT_CONFIG = {
413
- autoReconnect: true,
414
- platforms: ["polkadot"],
415
- debug: false
416
- };
417
- var resolveConfig = (config) => {
418
- return Object.assign({}, DEFAULT_CONFIG, config);
419
- };
423
+ // src/api/wallets.ts
424
+ import {
425
+ combineLatest as combineLatest7,
426
+ distinct,
427
+ filter as filter2,
428
+ map as map8,
429
+ mergeMap,
430
+ Observable as Observable7,
431
+ of as of5,
432
+ shareReplay as shareReplay7,
433
+ startWith,
434
+ take
435
+ } from "rxjs";
420
436
 
421
437
  // src/utils/sortWallets.ts
422
438
  var sortWallets = (w1, w2) => {
@@ -428,19 +444,19 @@ var sortWallets = (w1, w2) => {
428
444
  return w1.name.localeCompare(w2.name);
429
445
  };
430
446
 
431
- // src/api/wallets.ts
447
+ // src/api/ethereum/wallets.ts
448
+ import { createStore as createStore2 } from "mipd";
432
449
  import {
433
- Observable as Observable7,
434
- combineLatest as combineLatest7,
435
- distinct,
436
- filter as filter2,
437
- map as map8,
438
- mergeMap as mergeMap2,
439
- of as of6,
440
- shareReplay as shareReplay7,
441
- take
450
+ BehaviorSubject as BehaviorSubject3,
451
+ combineLatest as combineLatest5,
452
+ map as map6,
453
+ Observable as Observable5,
454
+ shareReplay as shareReplay5
442
455
  } from "rxjs";
443
456
 
457
+ // src/api/store.ts
458
+ import { uniq } from "lodash";
459
+
444
460
  // src/utils/WalletId.ts
445
461
  var getWalletId = (platform, identifier) => {
446
462
  if (!isWalletPlatform(platform)) throw new Error("Invalid platform");
@@ -456,7 +472,6 @@ var parseWalletId = (walletId) => {
456
472
  };
457
473
 
458
474
  // src/api/store.ts
459
- import { uniq } from "lodash";
460
475
  var LOCAL_STORAGE_KEY = "kheopskit";
461
476
  var DEFAULT_SETTINGS = {};
462
477
  var storage = createStore(LOCAL_STORAGE_KEY, DEFAULT_SETTINGS);
@@ -481,24 +496,14 @@ var store = {
481
496
  removeEnabledWalletId
482
497
  };
483
498
 
484
- // src/api/ethereum/wallets.ts
485
- import { createStore as createStore2 } from "mipd";
486
- import {
487
- BehaviorSubject as BehaviorSubject3,
488
- Observable as Observable5,
489
- combineLatest as combineLatest5,
490
- map as map6,
491
- shareReplay as shareReplay5
492
- } from "rxjs";
493
-
494
499
  // src/api/appKit.ts
495
500
  import { createAppKit } from "@reown/appkit/core";
496
501
  import {
497
502
  BehaviorSubject as BehaviorSubject2,
498
- Observable as Observable4,
499
503
  combineLatest as combineLatest4,
500
504
  distinctUntilChanged as distinctUntilChanged3,
501
505
  map as map5,
506
+ Observable as Observable4,
502
507
  of as of4,
503
508
  shareReplay as shareReplay4
504
509
  } from "rxjs";
@@ -513,11 +518,16 @@ var getAppKitWallets$ = (config) => {
513
518
  projectId: walletConnect.projectId,
514
519
  metadata: walletConnect.metadata,
515
520
  networks: walletConnect.networks,
521
+ themeMode: walletConnect.themeMode,
522
+ themeVariables: walletConnect.themeVariables,
516
523
  universalProviderConfigOverride: {
517
524
  methods: {
518
525
  polkadot: ["polkadot_signTransaction", "polkadot_signMessage"]
519
526
  }
520
- }
527
+ },
528
+ allWallets: "HIDE",
529
+ debug: config.debug,
530
+ allowUnsupportedChain: true
521
531
  });
522
532
  const status$ = new BehaviorSubject2({
523
533
  isPolkadotConnected: false,
@@ -661,6 +671,21 @@ var getEthereumWallets$ = (config) => {
661
671
  }).pipe(shareReplay5({ refCount: true, bufferSize: 1 }));
662
672
  };
663
673
 
674
+ // src/api/polkadot/wallets.ts
675
+ import { isEqual } from "lodash";
676
+ import {
677
+ connectInjectedExtension,
678
+ getInjectedExtensions
679
+ } from "polkadot-api/pjs-signer";
680
+ import {
681
+ BehaviorSubject as BehaviorSubject4,
682
+ combineLatest as combineLatest6,
683
+ distinctUntilChanged as distinctUntilChanged4,
684
+ map as map7,
685
+ Observable as Observable6,
686
+ shareReplay as shareReplay6
687
+ } from "rxjs";
688
+
664
689
  // src/utils/polkadotExtensions.ts
665
690
  var POLKADOT_EXTENSIONS = {
666
691
  talisman: {
@@ -682,23 +707,29 @@ var POLKADOT_EXTENSIONS = {
682
707
  };
683
708
 
684
709
  // src/api/polkadot/wallets.ts
685
- import { isEqual } from "lodash";
686
- import {
687
- connectInjectedExtension,
688
- getInjectedExtensions
689
- } from "polkadot-api/pjs-signer";
690
- import {
691
- BehaviorSubject as BehaviorSubject4,
692
- Observable as Observable6,
693
- combineLatest as combineLatest6,
694
- distinctUntilChanged as distinctUntilChanged4,
695
- map as map7,
696
- mergeMap,
697
- of as of5,
698
- shareReplay as shareReplay6,
699
- timer
700
- } from "rxjs";
701
710
  var getInjectedWalletsIds = () => getInjectedExtensions().map((name) => getWalletId("polkadot", name));
711
+ var createWalletIdsPoller$ = () => {
712
+ return new Observable6((subscriber) => {
713
+ subscriber.next(getInjectedWalletsIds());
714
+ const intervals = [100, 200, 300, 500];
715
+ let index = 0;
716
+ const poll = () => {
717
+ subscriber.next(getInjectedWalletsIds());
718
+ if (index < intervals.length) {
719
+ const delay = intervals[index++];
720
+ setTimeout(poll, delay);
721
+ }
722
+ };
723
+ if (intervals.length > 0) {
724
+ setTimeout(poll, intervals[index++] ?? 100);
725
+ }
726
+ return () => {
727
+ };
728
+ }).pipe(
729
+ distinctUntilChanged4(isEqual),
730
+ shareReplay6({ refCount: true, bufferSize: 1 })
731
+ );
732
+ };
702
733
  var polkadotInjectedWallets$ = new Observable6((subscriber) => {
703
734
  const enabledExtensions$ = new BehaviorSubject4(/* @__PURE__ */ new Map());
704
735
  const connect = async (walletId) => {
@@ -719,11 +750,7 @@ var polkadotInjectedWallets$ = new Observable6((subscriber) => {
719
750
  enabledExtensions$.next(newMap);
720
751
  store.removeEnabledWalletId(walletId);
721
752
  };
722
- const walletIds$ = of5(0, 200, 500, 1e3).pipe(
723
- mergeMap((time) => timer(time)),
724
- map7(() => getInjectedWalletsIds()),
725
- distinctUntilChanged4(isEqual)
726
- );
753
+ const walletIds$ = createWalletIdsPoller$();
727
754
  const subscription = combineLatest6([walletIds$, enabledExtensions$]).pipe(
728
755
  map7(([walletIds, enabledExtensions]) => {
729
756
  return walletIds.map((id) => {
@@ -790,23 +817,28 @@ var getWallets$ = (config) => {
790
817
  }
791
818
  );
792
819
  const wallets$ = observables.length ? combineLatest7(observables).pipe(
793
- map8((wallets) => wallets.flat().sort(sortWallets))
794
- ) : of6([]);
820
+ map8((wallets) => wallets.flat().sort(sortWallets)),
821
+ // Emit empty array immediately so UI doesn't wait
822
+ startWith([])
823
+ ) : of5([]);
824
+ const reconnectingWallets = /* @__PURE__ */ new Set();
795
825
  const subAutoReconnect = combineLatest7([wallets$, autoReconnectWalletIds$]).pipe(
796
826
  filter2(([, walletIds]) => config.autoReconnect && !!walletIds?.length),
797
- mergeMap2(
827
+ mergeMap(
798
828
  ([wallets, walletIds]) => wallets.filter((wallet) => walletIds?.includes(wallet.id))
799
829
  ),
800
830
  distinct((w) => w.id)
801
831
  ).subscribe(async (wallet) => {
802
- if (wallet.isConnected) {
803
- console.warn("Wallet %s already connected", wallet.id);
832
+ if (wallet.isConnected || reconnectingWallets.has(wallet.id)) {
804
833
  return;
805
834
  }
835
+ reconnectingWallets.add(wallet.id);
806
836
  try {
807
837
  await wallet.connect();
808
838
  } catch (err) {
809
839
  console.error("Failed to reconnect wallet %s", wallet.id, { err });
840
+ } finally {
841
+ reconnectingWallets.delete(wallet.id);
810
842
  }
811
843
  });
812
844
  const subWallets = wallets$.subscribe(subscriber);
@@ -831,7 +863,8 @@ var getKheopskit$ = (config) => {
831
863
  subscription.unsubscribe();
832
864
  };
833
865
  }).pipe(
834
- throttleTime(50, void 0, { leading: true, trailing: true }),
866
+ throttleTime(16, void 0, { leading: true, trailing: true }),
867
+ // ~1 frame at 60fps
835
868
  logObservable("kheopskit$", { enabled: kc.debug, printValue: true }),
836
869
  shareReplay8({ bufferSize: 1, refCount: true })
837
870
  );