@kheopskit/core 0.1.0 → 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.mjs CHANGED
@@ -153,7 +153,8 @@ var noopStorage = {
153
153
  removeItem: () => {
154
154
  }
155
155
  };
156
- var safeLocalStorage = (() => {
156
+ var _safeLocalStorage = null;
157
+ var createSafeLocalStorage = () => {
157
158
  if (typeof window === "undefined") return noopStorage;
158
159
  try {
159
160
  const testKey = "__kheopskit_test__";
@@ -176,7 +177,23 @@ var safeLocalStorage = (() => {
176
177
  } catch {
177
178
  return noopStorage;
178
179
  }
179
- })();
180
+ };
181
+ var getSafeLocalStorage = () => {
182
+ if (_safeLocalStorage === null) {
183
+ _safeLocalStorage = createSafeLocalStorage();
184
+ }
185
+ return _safeLocalStorage;
186
+ };
187
+ var safeLocalStorage = {
188
+ getItem: (key) => getSafeLocalStorage().getItem(key),
189
+ setItem: (key, value) => getSafeLocalStorage().setItem(key, value),
190
+ removeItem: (key) => getSafeLocalStorage().removeItem(key),
191
+ subscribe: (key, callback) => {
192
+ const storage = getSafeLocalStorage();
193
+ return storage.subscribe?.(key, callback) ?? (() => {
194
+ });
195
+ }
196
+ };
180
197
  var parseCookie = (cookieString, key) => {
181
198
  if (!cookieString) return null;
182
199
  for (const cookie of cookieString.split(";")) {
@@ -298,16 +315,21 @@ var setCachedIcons = (icons) => {
298
315
  };
299
316
 
300
317
  // src/api/appKit.ts
301
- import { createAppKit } from "@reown/appkit/core";
302
318
  import {
303
319
  BehaviorSubject,
304
320
  combineLatest,
305
321
  distinctUntilChanged,
322
+ from,
306
323
  map,
307
324
  Observable,
308
325
  of,
309
- shareReplay
326
+ shareReplay,
327
+ switchMap
310
328
  } from "rxjs";
329
+ var loadAppKit = async () => {
330
+ const { createAppKit } = await import("@reown/appkit/core");
331
+ return createAppKit;
332
+ };
311
333
  var WALLET_CONNECT_ICON = "data:image/svg+xml;base64,PHN2ZyBmaWxsPS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1Yy0yLjEzOTYtMi4wODYtMi4xMzk2LTUuNDY5IDAtNy41NTVsMTUuNjc5NS0xNS4yOWMyLjEzOTYtMi4wODYgNS42MDg1LTIuMDg2IDcuNzQ4MSAwbDUwLjE3ODkgNDguOTMyYy41MzUuNTIyIDEuNDAyLjUyMiAxLjkzNyAwbDUwLjE3Ny00OC45MzJjMi4xMzktMi4wODcgNS42MDgtMi4wODcgNy43NDggMGw1MC4xNzkgNDguOTMyYy41MzUuNTIyIDEuNDAyLjUyMiAxLjkzNyAwbDUwLjE3OS00OC45MzFjMi4xMzktMi4wODcgNS42MDgtMi4wODcgNy43NDggMHoiIGZpbGw9IiNmZmYiLz48L2c+PC9zdmc+";
312
334
  var cachedAppKit = null;
313
335
  var resetAppKitCache = () => {
@@ -315,84 +337,90 @@ var resetAppKitCache = () => {
315
337
  };
316
338
  var getAppKitWallets$ = (config) => {
317
339
  if (!config.walletConnect) return of({});
340
+ if (typeof window === "undefined") return of({});
318
341
  const walletConnect = config.walletConnect;
319
342
  if (!cachedAppKit) {
320
- cachedAppKit = new Observable((subscriber) => {
321
- const appKit = createAppKit({
322
- projectId: walletConnect.projectId,
323
- metadata: walletConnect.metadata,
324
- networks: walletConnect.networks,
325
- themeMode: walletConnect.themeMode,
326
- themeVariables: walletConnect.themeVariables,
327
- universalProviderConfigOverride: {
328
- methods: {
329
- polkadot: ["polkadot_signTransaction", "polkadot_signMessage"]
330
- }
331
- },
332
- allWallets: "HIDE",
333
- debug: config.debug,
334
- allowUnsupportedChain: true
335
- });
336
- const status$ = new BehaviorSubject({
337
- isPolkadotConnected: false,
338
- isEthereumConnected: false
339
- });
340
- const unsubProviders = appKit.subscribeProviders((providers) => {
341
- status$.next({
342
- isPolkadotConnected: !!providers.polkadot,
343
- isEthereumConnected: !!providers.eip155
344
- });
345
- });
346
- const polkadotWallet$ = appKit.chainNamespaces.includes("polkadot") ? status$.pipe(
347
- map((s) => s.isPolkadotConnected),
348
- distinctUntilChanged(),
349
- map((isConnected) => {
350
- const walletInfo = appKit.getWalletInfo();
351
- return {
352
- id: getWalletId("polkadot", "walletconnect"),
353
- platform: "polkadot",
354
- type: "appKit",
355
- appKit,
356
- // todo maybe we dont want to expose the appKit instance
357
- name: walletInfo?.name ?? "WalletConnect",
358
- icon: walletInfo?.icon ?? WALLET_CONNECT_ICON,
359
- connect: async () => {
360
- if (!isConnected) await appKit.open();
343
+ cachedAppKit = from(loadAppKit()).pipe(
344
+ switchMap((createAppKit) => {
345
+ return new Observable((subscriber) => {
346
+ const appKit = createAppKit({
347
+ projectId: walletConnect.projectId,
348
+ metadata: walletConnect.metadata,
349
+ networks: walletConnect.networks,
350
+ themeMode: walletConnect.themeMode,
351
+ themeVariables: walletConnect.themeVariables,
352
+ universalProviderConfigOverride: {
353
+ methods: {
354
+ polkadot: ["polkadot_signTransaction", "polkadot_signMessage"]
355
+ }
361
356
  },
362
- disconnect: () => {
363
- if (isConnected) appKit.disconnect();
364
- },
365
- isConnected
366
- };
367
- })
368
- ) : of(void 0);
369
- const ethereumWallet$ = appKit.chainNamespaces.includes("eip155") ? status$.pipe(
370
- map((s) => s.isEthereumConnected),
371
- distinctUntilChanged(),
372
- map((isConnected) => {
373
- const walletInfo = appKit.getWalletInfo();
374
- return {
375
- id: getWalletId("ethereum", "walletconnect"),
376
- platform: "ethereum",
377
- type: "appKit",
378
- appKit,
379
- name: walletInfo?.name ?? "WalletConnect",
380
- icon: walletInfo?.icon ?? WALLET_CONNECT_ICON,
381
- connect: () => appKit.open(),
382
- disconnect: () => appKit.disconnect(),
383
- isConnected
357
+ allWallets: "HIDE",
358
+ debug: config.debug,
359
+ allowUnsupportedChain: true
360
+ });
361
+ const status$ = new BehaviorSubject({
362
+ isPolkadotConnected: false,
363
+ isEthereumConnected: false
364
+ });
365
+ const unsubProviders = appKit.subscribeProviders((providers) => {
366
+ status$.next({
367
+ isPolkadotConnected: !!providers.polkadot,
368
+ isEthereumConnected: !!providers.eip155
369
+ });
370
+ });
371
+ const polkadotWallet$ = appKit.chainNamespaces.includes("polkadot") ? status$.pipe(
372
+ map((s) => s.isPolkadotConnected),
373
+ distinctUntilChanged(),
374
+ map((isConnected) => {
375
+ const walletInfo = appKit.getWalletInfo();
376
+ return {
377
+ id: getWalletId("polkadot", "walletconnect"),
378
+ platform: "polkadot",
379
+ type: "appKit",
380
+ appKit,
381
+ // todo maybe we dont want to expose the appKit instance
382
+ name: walletInfo?.name ?? "WalletConnect",
383
+ icon: walletInfo?.icon ?? WALLET_CONNECT_ICON,
384
+ connect: async () => {
385
+ if (!isConnected) await appKit.open();
386
+ },
387
+ disconnect: () => {
388
+ if (isConnected) appKit.disconnect();
389
+ },
390
+ isConnected
391
+ };
392
+ })
393
+ ) : of(void 0);
394
+ const ethereumWallet$ = appKit.chainNamespaces.includes("eip155") ? status$.pipe(
395
+ map((s) => s.isEthereumConnected),
396
+ distinctUntilChanged(),
397
+ map((isConnected) => {
398
+ const walletInfo = appKit.getWalletInfo();
399
+ return {
400
+ id: getWalletId("ethereum", "walletconnect"),
401
+ platform: "ethereum",
402
+ type: "appKit",
403
+ appKit,
404
+ name: walletInfo?.name ?? "WalletConnect",
405
+ icon: walletInfo?.icon ?? WALLET_CONNECT_ICON,
406
+ connect: () => appKit.open(),
407
+ disconnect: () => appKit.disconnect(),
408
+ isConnected
409
+ };
410
+ })
411
+ ) : of(void 0);
412
+ const sub = combineLatest({
413
+ polkadot: polkadotWallet$,
414
+ ethereum: ethereumWallet$
415
+ }).subscribe(subscriber);
416
+ return () => {
417
+ sub.unsubscribe();
418
+ unsubProviders();
384
419
  };
385
- })
386
- ) : of(void 0);
387
- const sub = combineLatest({
388
- polkadot: polkadotWallet$,
389
- ethereum: ethereumWallet$
390
- }).subscribe(subscriber);
391
- return () => {
392
- sub.unsubscribe();
393
- unsubProviders();
394
- };
395
- }).pipe(shareReplay({ refCount: true, bufferSize: 1 }));
420
+ });
421
+ }),
422
+ shareReplay({ refCount: true, bufferSize: 1 })
423
+ );
396
424
  }
397
425
  return cachedAppKit;
398
426
  };
@@ -581,7 +609,7 @@ import {
581
609
  of as of2,
582
610
  ReplaySubject,
583
611
  shareReplay as shareReplay3,
584
- switchMap
612
+ switchMap as switchMap2
585
613
  } from "rxjs";
586
614
  import {
587
615
  createWalletClient,
@@ -772,7 +800,7 @@ var getAppKitAccounts$ = (wallet) => {
772
800
  var getEthereumAccounts$ = (ethereumWallets) => new Observable3((subscriber) => {
773
801
  const sub = ethereumWallets.pipe(
774
802
  map3((wallets) => wallets.filter((w) => w.isConnected)),
775
- switchMap((wallets) => {
803
+ switchMap2((wallets) => {
776
804
  return wallets.length ? combineLatest3([
777
805
  ...wallets.filter((w) => w.type === "injected").map(getInjectedWalletAccounts$),
778
806
  ...wallets.filter((w) => w.type === "appKit").map(getAppKitAccounts$)
@@ -805,7 +833,7 @@ import {
805
833
  Observable as Observable4,
806
834
  of as of3,
807
835
  shareReplay as shareReplay4,
808
- switchMap as switchMap2
836
+ switchMap as switchMap3
809
837
  } from "rxjs";
810
838
  var getInjectedWalletAccounts$2 = (wallet) => {
811
839
  if (!wallet.isConnected) return of3([]);
@@ -890,7 +918,7 @@ var getAppKitAccounts$2 = (wallet) => {
890
918
  var getPolkadotAccounts$ = (polkadotWallets$) => new Observable4((subscriber) => {
891
919
  const sub = polkadotWallets$.pipe(
892
920
  map4((wallets) => wallets.filter((w) => w.isConnected)),
893
- switchMap2(
921
+ switchMap3(
894
922
  (wallets) => wallets.length ? combineLatest4([
895
923
  ...wallets.filter((w) => w.type === "injected").map(getInjectedWalletAccounts$2),
896
924
  ...wallets.filter((w) => w.type === "appKit").map(getAppKitAccounts$2)
@@ -983,7 +1011,22 @@ var createKheopskitStore = (options = {}) => {
983
1011
  setCachedState
984
1012
  };
985
1013
  };
986
- var store = createKheopskitStore();
1014
+ var _defaultStore = null;
1015
+ var getDefaultStore = () => {
1016
+ if (_defaultStore === null) {
1017
+ _defaultStore = createKheopskitStore();
1018
+ }
1019
+ return _defaultStore;
1020
+ };
1021
+ var store = {
1022
+ get observable() {
1023
+ return getDefaultStore().observable;
1024
+ },
1025
+ addEnabledWalletId: (walletId) => getDefaultStore().addEnabledWalletId(walletId),
1026
+ removeEnabledWalletId: (walletId) => getDefaultStore().removeEnabledWalletId(walletId),
1027
+ getCachedState: () => getDefaultStore().getCachedState(),
1028
+ setCachedState: (wallets, accounts) => getDefaultStore().setCachedState(wallets, accounts)
1029
+ };
987
1030
  var isCompactStore = (value) => {
988
1031
  if (!value || typeof value !== "object" || Array.isArray(value)) return false;
989
1032
  if ("cachedWallets" in value || "cachedAccounts" in value) return false;
@@ -1531,7 +1574,9 @@ export {
1531
1574
  clearCachedObservable,
1532
1575
  createKheopskitStore,
1533
1576
  getCachedIcon,
1577
+ getDefaultStore,
1534
1578
  getKheopskit$,
1579
+ getSafeLocalStorage,
1535
1580
  hydrateAccount,
1536
1581
  hydrateWallet,
1537
1582
  resetAppKitCache,