@kheopskit/core 0.0.22 → 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.d.mts CHANGED
@@ -3,8 +3,44 @@ import { AppKitNetwork } from '@reown/appkit/networks';
3
3
  import { Metadata } from '@walletconnect/universal-provider';
4
4
  import { InjectedExtension, InjectedPolkadotAccount } from 'polkadot-api/pjs-signer';
5
5
  import { EIP1193Provider, WalletClient, CustomTransport, Account } from 'viem';
6
+ import * as rxjs from 'rxjs';
6
7
  import { Observable } from 'rxjs';
7
8
 
9
+ /**
10
+ * Clears an observable from the cache.
11
+ * Use when a wallet disconnects or configuration changes.
12
+ */
13
+ declare const clearCachedObservable: (key: string) => void;
14
+ /**
15
+ * Clears all cached observables.
16
+ * Use when resetting the entire kheopskit state.
17
+ */
18
+ declare const clearAllCachedObservables: () => void;
19
+
20
+ type Storage = {
21
+ getItem: (key: string) => string | null;
22
+ setItem: (key: string, value: string) => void;
23
+ removeItem: (key: string) => void;
24
+ };
25
+ /**
26
+ * Extended storage interface with cross-tab sync support.
27
+ */
28
+ type SyncableStorage = Storage & {
29
+ /**
30
+ * Subscribe to storage changes from other tabs.
31
+ * Returns an unsubscribe function.
32
+ */
33
+ subscribe?: (key: string, callback: (value: string | null) => void) => () => void;
34
+ };
35
+ /**
36
+ * A safe localStorage wrapper that falls back to noopStorage
37
+ * when localStorage is not available (e.g., during SSR).
38
+ * Includes cross-tab sync via the native 'storage' event.
39
+ *
40
+ * Lazily initialized on first access to be SSR-safe.
41
+ */
42
+ declare const getSafeLocalStorage: () => SyncableStorage;
43
+
8
44
  type WalletAccountId = string;
9
45
 
10
46
  type WalletId = string;
@@ -26,6 +62,30 @@ type KheopskitConfig = {
26
62
  themeVariables?: ThemeVariables;
27
63
  };
28
64
  debug: boolean;
65
+ /**
66
+ * Custom storage key for persisting wallet connection state.
67
+ * Useful when running multiple kheopskit instances on the same domain
68
+ * to prevent state conflicts between different dapps.
69
+ *
70
+ * @default "kheopskit"
71
+ *
72
+ * @example
73
+ * ```ts
74
+ * // For app "MyDapp" to avoid conflicts
75
+ * { storageKey: "kheopskit-mydapp" }
76
+ * ```
77
+ */
78
+ storageKey: string;
79
+ /**
80
+ * Grace period in milliseconds to wait for wallets to inject before
81
+ * syncing to actual state. During this period, cached wallet/account
82
+ * state from storage is preserved to prevent UI flashing.
83
+ *
84
+ * Set to 0 to disable hydration buffering.
85
+ *
86
+ * @default 500
87
+ */
88
+ hydrationGracePeriod: number;
29
89
  };
30
90
  type PolkadotInjectedWallet = {
31
91
  id: WalletId;
@@ -93,14 +153,126 @@ type EthereumAccount = {
93
153
  isWalletDefault: boolean;
94
154
  };
95
155
  type WalletAccount = PolkadotAccount | EthereumAccount;
156
+ /**
157
+ * Serializable wallet data for SSR hydration cache.
158
+ * Contains only the data needed to render wallet UI without flash.
159
+ * Note: icon is NOT stored to save cookie space - it's looked up at hydration time.
160
+ */
161
+ type CachedWallet = {
162
+ id: WalletId;
163
+ platform: WalletPlatform;
164
+ type: "injected" | "appKit";
165
+ name: string;
166
+ isConnected: boolean;
167
+ };
168
+ /**
169
+ * Serializable account data for SSR hydration cache.
170
+ * Contains only the data needed to render account UI without flash.
171
+ */
172
+ type CachedAccount = {
173
+ id: string;
174
+ platform: WalletPlatform;
175
+ address: string;
176
+ name?: string;
177
+ walletId: WalletId;
178
+ walletName: string;
179
+ };
180
+
181
+ /**
182
+ * Converts a CachedWallet to a placeholder Wallet object.
183
+ * The placeholder has the same display properties but connect/disconnect throw errors.
184
+ *
185
+ * @param cached - The cached wallet data from storage
186
+ * @returns A placeholder Wallet object that can be displayed but not interacted with
187
+ */
188
+ declare const hydrateWallet: (cached: CachedWallet) => Wallet;
189
+ /**
190
+ * Converts a CachedAccount to a placeholder WalletAccount object.
191
+ *
192
+ * @param cached - The cached account data from storage
193
+ * @returns A placeholder WalletAccount object that can be displayed
194
+ */
195
+ declare const hydrateAccount: (cached: CachedAccount) => WalletAccount;
196
+
197
+ /**
198
+ * Gets a cached icon for a wallet.
199
+ * @param walletId - The wallet ID (e.g., "ethereum:io.talisman")
200
+ * @returns The cached icon data URI, or undefined if not cached
201
+ */
202
+ declare const getCachedIcon: (walletId: string) => string | undefined;
96
203
 
204
+ /**
205
+ * Clears the cached AppKit observable.
206
+ * Use when configuration changes or for testing purposes.
207
+ * Note: This does NOT destroy the appKit instance created by Reown.
208
+ */
209
+ declare const resetAppKitCache: () => void;
210
+
211
+ /**
212
+ * Default storage key for persisting wallet connection state.
213
+ * Can be overridden via config.storageKey to avoid conflicts
214
+ * when running multiple dapps on the same domain.
215
+ */
216
+ declare const DEFAULT_STORAGE_KEY = "kheopskit";
97
217
  declare const resolveConfig: (config: Partial<KheopskitConfig> | undefined) => KheopskitConfig;
98
218
 
219
+ type KheopskitStoreData = {
220
+ autoReconnect?: WalletId[];
221
+ /** Cached wallet state for SSR hydration to prevent UI flash */
222
+ cachedWallets?: CachedWallet[];
223
+ /** Cached account state for SSR hydration to prevent UI flash */
224
+ cachedAccounts?: CachedAccount[];
225
+ };
226
+ type CreateKheopskitStoreOptions = {
227
+ /**
228
+ * Cookie string for SSR hydration.
229
+ * When provided, uses cookieStorage instead of localStorage.
230
+ */
231
+ ssrCookies?: string;
232
+ /**
233
+ * Custom storage key to namespace the stored data.
234
+ * @default "kheopskit"
235
+ */
236
+ storageKey?: string;
237
+ };
238
+ /**
239
+ * Creates a kheopskit store with the appropriate storage backend.
240
+ * Uses cookieStorage when ssrCookies is provided (for SSR hydration),
241
+ * otherwise falls back to safeLocalStorage.
242
+ *
243
+ * @param options - Configuration options for the store
244
+ */
245
+ declare const createKheopskitStore: (options?: CreateKheopskitStoreOptions) => {
246
+ observable: rxjs.Observable<KheopskitStoreData>;
247
+ addEnabledWalletId: (walletId: WalletId) => void;
248
+ removeEnabledWalletId: (walletId: WalletId) => void;
249
+ getCachedState: () => {
250
+ wallets: CachedWallet[];
251
+ accounts: CachedAccount[];
252
+ };
253
+ setCachedState: (wallets: CachedWallet[], accounts: CachedAccount[]) => void;
254
+ };
255
+ type KheopskitStore = ReturnType<typeof createKheopskitStore>;
256
+ /**
257
+ * Gets the default store, creating it on first access.
258
+ * Uses localStorage on client, noop on server.
259
+ * Lazily initialized to avoid SSR issues with module-level code.
260
+ */
261
+ declare const getDefaultStore: () => KheopskitStore;
262
+
99
263
  type KheopskitState = {
100
264
  wallets: Wallet[];
101
265
  accounts: WalletAccount[];
102
266
  config: KheopskitConfig;
267
+ /**
268
+ * Whether the state is still being hydrated from cache.
269
+ * During hydration, cached wallets/accounts may be displayed
270
+ * before the actual wallet extensions have injected.
271
+ *
272
+ * Use this to show loading indicators or disable certain actions.
273
+ */
274
+ isHydrating: boolean;
103
275
  };
104
- declare const getKheopskit$: (config?: Partial<KheopskitConfig>) => Observable<KheopskitState>;
276
+ declare const getKheopskit$: (config?: Partial<KheopskitConfig>, ssrCookies?: string, existingStore?: ReturnType<typeof createKheopskitStore>) => Observable<KheopskitState>;
105
277
 
106
- export { type EthereumAccount, type EthereumAppKitWallet, type EthereumInjectedWallet, type EthereumWallet, type KheopskitConfig, type KheopskitState, type PolkadotAccount, type PolkadotAppKitWallet, type PolkadotInjectedWallet, type PolkadotWallet, type Wallet, type WalletAccount, type WalletPlatform, getKheopskit$, resolveConfig };
278
+ export { type CachedAccount, type CachedWallet, DEFAULT_STORAGE_KEY, type EthereumAccount, type EthereumAppKitWallet, type EthereumInjectedWallet, type EthereumWallet, type KheopskitConfig, type KheopskitState, type PolkadotAccount, type PolkadotAppKitWallet, type PolkadotInjectedWallet, type PolkadotWallet, type Wallet, type WalletAccount, type WalletPlatform, clearAllCachedObservables, clearCachedObservable, createKheopskitStore, getCachedIcon, getDefaultStore, getKheopskit$, getSafeLocalStorage, hydrateAccount, hydrateWallet, resetAppKitCache, resolveConfig };
package/dist/index.d.ts CHANGED
@@ -3,8 +3,44 @@ import { AppKitNetwork } from '@reown/appkit/networks';
3
3
  import { Metadata } from '@walletconnect/universal-provider';
4
4
  import { InjectedExtension, InjectedPolkadotAccount } from 'polkadot-api/pjs-signer';
5
5
  import { EIP1193Provider, WalletClient, CustomTransport, Account } from 'viem';
6
+ import * as rxjs from 'rxjs';
6
7
  import { Observable } from 'rxjs';
7
8
 
9
+ /**
10
+ * Clears an observable from the cache.
11
+ * Use when a wallet disconnects or configuration changes.
12
+ */
13
+ declare const clearCachedObservable: (key: string) => void;
14
+ /**
15
+ * Clears all cached observables.
16
+ * Use when resetting the entire kheopskit state.
17
+ */
18
+ declare const clearAllCachedObservables: () => void;
19
+
20
+ type Storage = {
21
+ getItem: (key: string) => string | null;
22
+ setItem: (key: string, value: string) => void;
23
+ removeItem: (key: string) => void;
24
+ };
25
+ /**
26
+ * Extended storage interface with cross-tab sync support.
27
+ */
28
+ type SyncableStorage = Storage & {
29
+ /**
30
+ * Subscribe to storage changes from other tabs.
31
+ * Returns an unsubscribe function.
32
+ */
33
+ subscribe?: (key: string, callback: (value: string | null) => void) => () => void;
34
+ };
35
+ /**
36
+ * A safe localStorage wrapper that falls back to noopStorage
37
+ * when localStorage is not available (e.g., during SSR).
38
+ * Includes cross-tab sync via the native 'storage' event.
39
+ *
40
+ * Lazily initialized on first access to be SSR-safe.
41
+ */
42
+ declare const getSafeLocalStorage: () => SyncableStorage;
43
+
8
44
  type WalletAccountId = string;
9
45
 
10
46
  type WalletId = string;
@@ -26,6 +62,30 @@ type KheopskitConfig = {
26
62
  themeVariables?: ThemeVariables;
27
63
  };
28
64
  debug: boolean;
65
+ /**
66
+ * Custom storage key for persisting wallet connection state.
67
+ * Useful when running multiple kheopskit instances on the same domain
68
+ * to prevent state conflicts between different dapps.
69
+ *
70
+ * @default "kheopskit"
71
+ *
72
+ * @example
73
+ * ```ts
74
+ * // For app "MyDapp" to avoid conflicts
75
+ * { storageKey: "kheopskit-mydapp" }
76
+ * ```
77
+ */
78
+ storageKey: string;
79
+ /**
80
+ * Grace period in milliseconds to wait for wallets to inject before
81
+ * syncing to actual state. During this period, cached wallet/account
82
+ * state from storage is preserved to prevent UI flashing.
83
+ *
84
+ * Set to 0 to disable hydration buffering.
85
+ *
86
+ * @default 500
87
+ */
88
+ hydrationGracePeriod: number;
29
89
  };
30
90
  type PolkadotInjectedWallet = {
31
91
  id: WalletId;
@@ -93,14 +153,126 @@ type EthereumAccount = {
93
153
  isWalletDefault: boolean;
94
154
  };
95
155
  type WalletAccount = PolkadotAccount | EthereumAccount;
156
+ /**
157
+ * Serializable wallet data for SSR hydration cache.
158
+ * Contains only the data needed to render wallet UI without flash.
159
+ * Note: icon is NOT stored to save cookie space - it's looked up at hydration time.
160
+ */
161
+ type CachedWallet = {
162
+ id: WalletId;
163
+ platform: WalletPlatform;
164
+ type: "injected" | "appKit";
165
+ name: string;
166
+ isConnected: boolean;
167
+ };
168
+ /**
169
+ * Serializable account data for SSR hydration cache.
170
+ * Contains only the data needed to render account UI without flash.
171
+ */
172
+ type CachedAccount = {
173
+ id: string;
174
+ platform: WalletPlatform;
175
+ address: string;
176
+ name?: string;
177
+ walletId: WalletId;
178
+ walletName: string;
179
+ };
180
+
181
+ /**
182
+ * Converts a CachedWallet to a placeholder Wallet object.
183
+ * The placeholder has the same display properties but connect/disconnect throw errors.
184
+ *
185
+ * @param cached - The cached wallet data from storage
186
+ * @returns A placeholder Wallet object that can be displayed but not interacted with
187
+ */
188
+ declare const hydrateWallet: (cached: CachedWallet) => Wallet;
189
+ /**
190
+ * Converts a CachedAccount to a placeholder WalletAccount object.
191
+ *
192
+ * @param cached - The cached account data from storage
193
+ * @returns A placeholder WalletAccount object that can be displayed
194
+ */
195
+ declare const hydrateAccount: (cached: CachedAccount) => WalletAccount;
196
+
197
+ /**
198
+ * Gets a cached icon for a wallet.
199
+ * @param walletId - The wallet ID (e.g., "ethereum:io.talisman")
200
+ * @returns The cached icon data URI, or undefined if not cached
201
+ */
202
+ declare const getCachedIcon: (walletId: string) => string | undefined;
96
203
 
204
+ /**
205
+ * Clears the cached AppKit observable.
206
+ * Use when configuration changes or for testing purposes.
207
+ * Note: This does NOT destroy the appKit instance created by Reown.
208
+ */
209
+ declare const resetAppKitCache: () => void;
210
+
211
+ /**
212
+ * Default storage key for persisting wallet connection state.
213
+ * Can be overridden via config.storageKey to avoid conflicts
214
+ * when running multiple dapps on the same domain.
215
+ */
216
+ declare const DEFAULT_STORAGE_KEY = "kheopskit";
97
217
  declare const resolveConfig: (config: Partial<KheopskitConfig> | undefined) => KheopskitConfig;
98
218
 
219
+ type KheopskitStoreData = {
220
+ autoReconnect?: WalletId[];
221
+ /** Cached wallet state for SSR hydration to prevent UI flash */
222
+ cachedWallets?: CachedWallet[];
223
+ /** Cached account state for SSR hydration to prevent UI flash */
224
+ cachedAccounts?: CachedAccount[];
225
+ };
226
+ type CreateKheopskitStoreOptions = {
227
+ /**
228
+ * Cookie string for SSR hydration.
229
+ * When provided, uses cookieStorage instead of localStorage.
230
+ */
231
+ ssrCookies?: string;
232
+ /**
233
+ * Custom storage key to namespace the stored data.
234
+ * @default "kheopskit"
235
+ */
236
+ storageKey?: string;
237
+ };
238
+ /**
239
+ * Creates a kheopskit store with the appropriate storage backend.
240
+ * Uses cookieStorage when ssrCookies is provided (for SSR hydration),
241
+ * otherwise falls back to safeLocalStorage.
242
+ *
243
+ * @param options - Configuration options for the store
244
+ */
245
+ declare const createKheopskitStore: (options?: CreateKheopskitStoreOptions) => {
246
+ observable: rxjs.Observable<KheopskitStoreData>;
247
+ addEnabledWalletId: (walletId: WalletId) => void;
248
+ removeEnabledWalletId: (walletId: WalletId) => void;
249
+ getCachedState: () => {
250
+ wallets: CachedWallet[];
251
+ accounts: CachedAccount[];
252
+ };
253
+ setCachedState: (wallets: CachedWallet[], accounts: CachedAccount[]) => void;
254
+ };
255
+ type KheopskitStore = ReturnType<typeof createKheopskitStore>;
256
+ /**
257
+ * Gets the default store, creating it on first access.
258
+ * Uses localStorage on client, noop on server.
259
+ * Lazily initialized to avoid SSR issues with module-level code.
260
+ */
261
+ declare const getDefaultStore: () => KheopskitStore;
262
+
99
263
  type KheopskitState = {
100
264
  wallets: Wallet[];
101
265
  accounts: WalletAccount[];
102
266
  config: KheopskitConfig;
267
+ /**
268
+ * Whether the state is still being hydrated from cache.
269
+ * During hydration, cached wallets/accounts may be displayed
270
+ * before the actual wallet extensions have injected.
271
+ *
272
+ * Use this to show loading indicators or disable certain actions.
273
+ */
274
+ isHydrating: boolean;
103
275
  };
104
- declare const getKheopskit$: (config?: Partial<KheopskitConfig>) => Observable<KheopskitState>;
276
+ declare const getKheopskit$: (config?: Partial<KheopskitConfig>, ssrCookies?: string, existingStore?: ReturnType<typeof createKheopskitStore>) => Observable<KheopskitState>;
105
277
 
106
- export { type EthereumAccount, type EthereumAppKitWallet, type EthereumInjectedWallet, type EthereumWallet, type KheopskitConfig, type KheopskitState, type PolkadotAccount, type PolkadotAppKitWallet, type PolkadotInjectedWallet, type PolkadotWallet, type Wallet, type WalletAccount, type WalletPlatform, getKheopskit$, resolveConfig };
278
+ export { type CachedAccount, type CachedWallet, DEFAULT_STORAGE_KEY, type EthereumAccount, type EthereumAppKitWallet, type EthereumInjectedWallet, type EthereumWallet, type KheopskitConfig, type KheopskitState, type PolkadotAccount, type PolkadotAppKitWallet, type PolkadotInjectedWallet, type PolkadotWallet, type Wallet, type WalletAccount, type WalletPlatform, clearAllCachedObservables, clearCachedObservable, createKheopskitStore, getCachedIcon, getDefaultStore, getKheopskit$, getSafeLocalStorage, hydrateAccount, hydrateWallet, resetAppKitCache, resolveConfig };