@kheopskit/core 1.0.1 → 5.0.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/MIGRATING_TO_V4.md +259 -0
- package/README.md +67 -0
- package/dist/chunk-4ENHC7G4.js +210 -0
- package/dist/chunk-4ENHC7G4.js.map +1 -0
- package/dist/chunk-6XAZANB5.mjs +450 -0
- package/dist/chunk-6XAZANB5.mjs.map +1 -0
- package/dist/chunk-7QSGAJ4A.mjs +210 -0
- package/dist/chunk-7QSGAJ4A.mjs.map +1 -0
- package/dist/chunk-B4L6GAYD.js +179 -0
- package/dist/chunk-B4L6GAYD.js.map +1 -0
- package/dist/chunk-BWUUHUDK.mjs +24 -0
- package/dist/chunk-BWUUHUDK.mjs.map +1 -0
- package/dist/chunk-D3EQMFZ2.js +24 -0
- package/dist/chunk-D3EQMFZ2.js.map +1 -0
- package/dist/chunk-XQWJM3KC.js +450 -0
- package/dist/chunk-XQWJM3KC.js.map +1 -0
- package/dist/chunk-YDLCHYHH.mjs +179 -0
- package/dist/chunk-YDLCHYHH.mjs.map +1 -0
- package/dist/ethereum.d.mts +61 -0
- package/dist/ethereum.d.ts +61 -0
- package/dist/ethereum.js +351 -0
- package/dist/ethereum.js.map +1 -0
- package/dist/ethereum.mjs +351 -0
- package/dist/ethereum.mjs.map +1 -0
- package/dist/getCachedObservable-C4E8dfMp.d.mts +20 -0
- package/dist/getCachedObservable-C4E8dfMp.d.ts +20 -0
- package/dist/index.d.mts +55 -267
- package/dist/index.d.ts +55 -267
- package/dist/index.js +327 -1355
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +263 -1325
- package/dist/index.mjs.map +1 -1
- package/dist/internal.d.mts +86 -0
- package/dist/internal.d.ts +86 -0
- package/dist/internal.js +32 -0
- package/dist/internal.js.map +1 -0
- package/dist/internal.mjs +32 -0
- package/dist/internal.mjs.map +1 -0
- package/dist/polkadot.d.mts +70 -0
- package/dist/polkadot.d.ts +70 -0
- package/dist/polkadot.js +303 -0
- package/dist/polkadot.js.map +1 -0
- package/dist/polkadot.mjs +303 -0
- package/dist/polkadot.mjs.map +1 -0
- package/dist/solana.d.mts +98 -0
- package/dist/solana.d.ts +98 -0
- package/dist/solana.js +461 -0
- package/dist/solana.js.map +1 -0
- package/dist/solana.mjs +461 -0
- package/dist/solana.mjs.map +1 -0
- package/dist/types-C7V7DGlg.d.mts +349 -0
- package/dist/types-C7V7DGlg.d.ts +349 -0
- package/package.json +104 -18
|
@@ -0,0 +1,349 @@
|
|
|
1
|
+
import * as rxjs from 'rxjs';
|
|
2
|
+
import { Observable } from 'rxjs';
|
|
3
|
+
|
|
4
|
+
type WalletAccountId = string;
|
|
5
|
+
declare const getWalletAccountId: (walletId: string, address: string) => WalletAccountId;
|
|
6
|
+
declare const parseWalletAccountId: (accountId: string) => {
|
|
7
|
+
walletId: string;
|
|
8
|
+
address: string;
|
|
9
|
+
};
|
|
10
|
+
|
|
11
|
+
type WalletId = string;
|
|
12
|
+
declare const getWalletId: (platform: WalletPlatform, identifier: string) => WalletId;
|
|
13
|
+
declare const parseWalletId: (walletId: string) => {
|
|
14
|
+
platform: WalletPlatform;
|
|
15
|
+
identifier: string;
|
|
16
|
+
};
|
|
17
|
+
/**
|
|
18
|
+
* Non-throwing variant of {@link parseWalletId}. Use when validating untrusted
|
|
19
|
+
* input (e.g. cached state persisted by an older version) before parsing.
|
|
20
|
+
*/
|
|
21
|
+
declare const isValidWalletId: (walletId: unknown) => walletId is WalletId;
|
|
22
|
+
|
|
23
|
+
type KheopskitStoreData = {
|
|
24
|
+
autoReconnect?: WalletId[];
|
|
25
|
+
/** Cached wallet state for SSR hydration to prevent UI flash */
|
|
26
|
+
cachedWallets?: CachedWallet[];
|
|
27
|
+
/** Cached account state for SSR hydration to prevent UI flash */
|
|
28
|
+
cachedAccounts?: CachedAccount[];
|
|
29
|
+
};
|
|
30
|
+
type CreateKheopskitStoreOptions = {
|
|
31
|
+
/**
|
|
32
|
+
* Cookie string for SSR hydration.
|
|
33
|
+
* When provided, uses cookieStorage instead of localStorage.
|
|
34
|
+
*/
|
|
35
|
+
ssrCookies?: string;
|
|
36
|
+
/**
|
|
37
|
+
* Custom storage key to namespace the stored data.
|
|
38
|
+
* @default "kheopskit"
|
|
39
|
+
*/
|
|
40
|
+
storageKey?: string;
|
|
41
|
+
};
|
|
42
|
+
/**
|
|
43
|
+
* Creates a kheopskit store with the appropriate storage backend.
|
|
44
|
+
* Uses cookieStorage when ssrCookies is provided (for SSR hydration),
|
|
45
|
+
* otherwise falls back to safeLocalStorage.
|
|
46
|
+
*
|
|
47
|
+
* @param options - Configuration options for the store
|
|
48
|
+
*/
|
|
49
|
+
declare const createKheopskitStore: (options?: CreateKheopskitStoreOptions) => {
|
|
50
|
+
observable: rxjs.Observable<KheopskitStoreData>;
|
|
51
|
+
addEnabledWalletId: (walletId: WalletId) => void;
|
|
52
|
+
removeEnabledWalletId: (walletId: WalletId) => void;
|
|
53
|
+
getCachedState: () => {
|
|
54
|
+
wallets: CachedWallet[];
|
|
55
|
+
accounts: CachedAccount[];
|
|
56
|
+
};
|
|
57
|
+
setCachedState: (wallets: CachedWallet[], accounts: CachedAccount[]) => void;
|
|
58
|
+
};
|
|
59
|
+
type KheopskitStore = ReturnType<typeof createKheopskitStore>;
|
|
60
|
+
/**
|
|
61
|
+
* Gets the default store, creating it on first access.
|
|
62
|
+
* Uses localStorage on client, noop on server.
|
|
63
|
+
* Lazily initialized to avoid SSR issues with module-level code.
|
|
64
|
+
*/
|
|
65
|
+
declare const getDefaultStore: () => KheopskitStore;
|
|
66
|
+
|
|
67
|
+
type WalletPlatform = "polkadot" | "ethereum" | "solana";
|
|
68
|
+
/**
|
|
69
|
+
* Minimal structural view of a WalletConnect `UniversalProvider` — the subset
|
|
70
|
+
* kheopskit reads. Declared locally so core never depends on
|
|
71
|
+
* `@walletconnect/universal-provider`. The concrete instance comes from
|
|
72
|
+
* `@reown/appkit` at runtime.
|
|
73
|
+
*/
|
|
74
|
+
type WalletConnectProvider = {
|
|
75
|
+
session?: {
|
|
76
|
+
topic: string;
|
|
77
|
+
namespaces: Record<string, {
|
|
78
|
+
accounts?: string[];
|
|
79
|
+
}>;
|
|
80
|
+
};
|
|
81
|
+
client: {
|
|
82
|
+
request<T = unknown>(args: {
|
|
83
|
+
topic: string;
|
|
84
|
+
chainId: string;
|
|
85
|
+
request: {
|
|
86
|
+
method: string;
|
|
87
|
+
params: unknown;
|
|
88
|
+
};
|
|
89
|
+
}): Promise<T>;
|
|
90
|
+
};
|
|
91
|
+
request(args: {
|
|
92
|
+
method: string;
|
|
93
|
+
params?: unknown;
|
|
94
|
+
}): Promise<unknown>;
|
|
95
|
+
on(event: string, listener: (...args: unknown[]) => void): void;
|
|
96
|
+
off(event: string, listener: (...args: unknown[]) => void): void;
|
|
97
|
+
};
|
|
98
|
+
/**
|
|
99
|
+
* Minimal structural view of the Reown AppKit instance — the subset kheopskit's
|
|
100
|
+
* account factories use. Exposed as the `appKit` escape hatch on AppKit wallets;
|
|
101
|
+
* cast it to `@reown/appkit`'s `AppKit` type for the full API. Declared locally
|
|
102
|
+
* so core never depends on `@reown/appkit`'s types (it's an optional peer).
|
|
103
|
+
*/
|
|
104
|
+
type AppKitInstance = {
|
|
105
|
+
getProvider<T = WalletConnectProvider>(namespace: string): T | undefined;
|
|
106
|
+
getAccount(namespace: string): {
|
|
107
|
+
allAccounts: {
|
|
108
|
+
address: string;
|
|
109
|
+
}[];
|
|
110
|
+
} | undefined;
|
|
111
|
+
getCaipNetworks(namespace: string): {
|
|
112
|
+
caipNetworkId?: string;
|
|
113
|
+
}[];
|
|
114
|
+
};
|
|
115
|
+
type WalletType = "injected" | "walletconnect";
|
|
116
|
+
type PolkadotAccountType = "sr25519" | "ed25519" | "ecdsa" | "ethereum";
|
|
117
|
+
/**
|
|
118
|
+
* SDK-free fields common to every wallet, regardless of platform. Platform
|
|
119
|
+
* packages (`@kheopskit/core/<platform>`) extend this with SDK-typed fields
|
|
120
|
+
* (the injected provider/extension/standard-wallet handle).
|
|
121
|
+
*/
|
|
122
|
+
type BaseWallet = {
|
|
123
|
+
id: WalletId;
|
|
124
|
+
platform: WalletPlatform;
|
|
125
|
+
type: WalletType;
|
|
126
|
+
name: string;
|
|
127
|
+
icon: string;
|
|
128
|
+
isConnected: boolean;
|
|
129
|
+
connect: () => Promise<void>;
|
|
130
|
+
/**
|
|
131
|
+
* Disconnect the wallet. Resolves once the underlying provider/extension
|
|
132
|
+
* disconnect completes; rejects if it fails so callers can surface or retry.
|
|
133
|
+
*/
|
|
134
|
+
disconnect: () => Promise<void>;
|
|
135
|
+
};
|
|
136
|
+
/**
|
|
137
|
+
* SDK-free fields common to every account, regardless of platform. Platform
|
|
138
|
+
* packages extend this with their SDK-typed signer/client.
|
|
139
|
+
*/
|
|
140
|
+
type BaseWalletAccount = {
|
|
141
|
+
id: WalletAccountId;
|
|
142
|
+
platform: WalletPlatform;
|
|
143
|
+
/** Base58 (Solana), SS58 (Polkadot) or 0x-hex (Ethereum) address. */
|
|
144
|
+
address: string;
|
|
145
|
+
/** Friendly account name, when the wallet exposes one (e.g. Polkadot). */
|
|
146
|
+
name?: string;
|
|
147
|
+
walletName: string;
|
|
148
|
+
walletId: WalletId;
|
|
149
|
+
};
|
|
150
|
+
/**
|
|
151
|
+
* The single WalletConnect connector, shared across every platform.
|
|
152
|
+
*
|
|
153
|
+
* Unlike injected wallets, it is **not tied to a platform**: one WalletConnect
|
|
154
|
+
* session spans whichever namespaces the remote wallet approves in a single
|
|
155
|
+
* pairing (a namespace can't be added to a live session afterwards). So there is
|
|
156
|
+
* exactly one of these in `wallets`, discriminated by `type: "walletconnect"`.
|
|
157
|
+
* Its accounts appear in `accounts`, each carrying its own `platform`.
|
|
158
|
+
*/
|
|
159
|
+
type WalletConnectWallet = {
|
|
160
|
+
id: WalletId;
|
|
161
|
+
type: "walletconnect";
|
|
162
|
+
/**
|
|
163
|
+
* Platforms (namespaces) the live session currently carries. Empty until
|
|
164
|
+
* connected; populated with whatever the wallet approved (e.g. just
|
|
165
|
+
* `["ethereum"]`, or `["polkadot", "ethereum"]`).
|
|
166
|
+
*/
|
|
167
|
+
platforms: WalletPlatform[];
|
|
168
|
+
/**
|
|
169
|
+
* Raw Reown AppKit instance, exposed as an escape hatch for advanced use
|
|
170
|
+
* (custom modal control, reading providers directly). Most consumers should
|
|
171
|
+
* use `connect`/`disconnect` and the derived accounts instead.
|
|
172
|
+
*/
|
|
173
|
+
appKit: AppKitInstance;
|
|
174
|
+
name: string;
|
|
175
|
+
icon: string;
|
|
176
|
+
isConnected: boolean;
|
|
177
|
+
connect: () => Promise<void>;
|
|
178
|
+
disconnect: () => Promise<void>;
|
|
179
|
+
};
|
|
180
|
+
/** Narrows a wallet to the platform-less {@link WalletConnectWallet}. */
|
|
181
|
+
declare const isWalletConnectWallet: (wallet: BaseWallet | WalletConnectWallet) => wallet is WalletConnectWallet;
|
|
182
|
+
/**
|
|
183
|
+
* Narrows a wallet to an injected (browser-extension / Wallet Standard) wallet —
|
|
184
|
+
* the complement of {@link isWalletConnectWallet}. `state.wallets` is
|
|
185
|
+
* `(InjectedWallet | WalletConnectWallet)[]`, so use this to recover the
|
|
186
|
+
* injected-only fields when iterating: `platform`, `sourceId`, and the SDK
|
|
187
|
+
* handle (`provider` on Ethereum, `extension` on Polkadot, `wallet` on Solana).
|
|
188
|
+
*
|
|
189
|
+
* @example
|
|
190
|
+
* ```ts
|
|
191
|
+
* for (const wallet of wallets.filter(isInjectedWallet)) {
|
|
192
|
+
* console.log(wallet.platform, wallet.sourceId); // typed, no WC widening
|
|
193
|
+
* }
|
|
194
|
+
* ```
|
|
195
|
+
*/
|
|
196
|
+
declare const isInjectedWallet: <W extends BaseWallet | WalletConnectWallet>(wallet: W) => wallet is Exclude<W, WalletConnectWallet>;
|
|
197
|
+
/**
|
|
198
|
+
* Dapp metadata shown in the WalletConnect modal. Mirrors WalletConnect's
|
|
199
|
+
* `Metadata`, declared locally so core doesn't depend on
|
|
200
|
+
* `@walletconnect/universal-provider`.
|
|
201
|
+
*/
|
|
202
|
+
type WalletConnectMetadata = {
|
|
203
|
+
name: string;
|
|
204
|
+
description: string;
|
|
205
|
+
url: string;
|
|
206
|
+
icons: string[];
|
|
207
|
+
};
|
|
208
|
+
type WalletConnectConfig = {
|
|
209
|
+
projectId: string;
|
|
210
|
+
metadata: WalletConnectMetadata;
|
|
211
|
+
/** Defaults to wss://relay.walletconnect.com */
|
|
212
|
+
relayUrl?: string;
|
|
213
|
+
/**
|
|
214
|
+
* Networks AppKit should enable. Pass `AppKitNetwork[]` from
|
|
215
|
+
* `@reown/appkit/networks` (see
|
|
216
|
+
* https://docs.reown.com/advanced/multichain/polkadot/dapp-integration-guide#walletconnect-code%2Fcomponent-setup).
|
|
217
|
+
* Loosely typed (`unknown`) so core doesn't depend on `@reown/appkit`'s
|
|
218
|
+
* types — the value is forwarded to AppKit as-is.
|
|
219
|
+
*/
|
|
220
|
+
networks: [unknown, ...unknown[]];
|
|
221
|
+
themeMode?: "light" | "dark";
|
|
222
|
+
themeVariables?: Record<string, string | number>;
|
|
223
|
+
};
|
|
224
|
+
/**
|
|
225
|
+
* Context passed to a platform plugin's `getWallets$`. Carries the shared store
|
|
226
|
+
* and the resolved core config (for WalletConnect / debug).
|
|
227
|
+
*/
|
|
228
|
+
type PlatformContext = {
|
|
229
|
+
store: KheopskitStore;
|
|
230
|
+
config: KheopskitConfig;
|
|
231
|
+
};
|
|
232
|
+
/**
|
|
233
|
+
* A platform plugin. Created by the per-platform factories exported from
|
|
234
|
+
* `@kheopskit/core/polkadot`, `/ethereum`, `/solana`. Core iterates plugins
|
|
235
|
+
* generically and never imports a platform SDK itself.
|
|
236
|
+
*
|
|
237
|
+
* @typeParam TPlatform - the platform discriminant
|
|
238
|
+
* @typeParam TWallet - the platform's wallet type (extends {@link BaseWallet})
|
|
239
|
+
* @typeParam TAccount - the platform's account type (extends {@link BaseWalletAccount})
|
|
240
|
+
*/
|
|
241
|
+
type KheopskitPlatform<TPlatform extends WalletPlatform = WalletPlatform, TWallet extends BaseWallet = BaseWallet, TAccount extends BaseWalletAccount = BaseWalletAccount> = {
|
|
242
|
+
readonly platform: TPlatform;
|
|
243
|
+
getWallets$(ctx: PlatformContext): Observable<TWallet[]>;
|
|
244
|
+
getAccounts$(wallets$: Observable<(TWallet | WalletConnectWallet)[]>): Observable<TAccount[]>;
|
|
245
|
+
/**
|
|
246
|
+
* Optional hydration filter. Cached accounts for which this returns false are
|
|
247
|
+
* dropped during SSR hydration (Polkadot uses it to honour `accountTypes`).
|
|
248
|
+
*/
|
|
249
|
+
acceptsCachedAccount?(cached: CachedAccount): boolean;
|
|
250
|
+
};
|
|
251
|
+
type ElementOf<T> = T extends readonly (infer E)[] ? E : never;
|
|
252
|
+
/** The account type produced by a plugin (inferred from its `getAccounts$`). */
|
|
253
|
+
type AccountOf<T> = T extends {
|
|
254
|
+
getAccounts$: (wallets$: never) => Observable<infer R>;
|
|
255
|
+
} ? ElementOf<R> : never;
|
|
256
|
+
/** The wallet type produced by a plugin (inferred from its `getWallets$`). */
|
|
257
|
+
type WalletOf<T> = T extends {
|
|
258
|
+
getWallets$: (ctx: never) => Observable<infer R>;
|
|
259
|
+
} ? ElementOf<R> : never;
|
|
260
|
+
type KheopskitConfig<P extends readonly KheopskitPlatform[] = readonly KheopskitPlatform[]> = {
|
|
261
|
+
autoReconnect: boolean;
|
|
262
|
+
/**
|
|
263
|
+
* Platform plugins to enable, e.g. `[polkadot(), solana({ chain })]`.
|
|
264
|
+
* Import factories from `@kheopskit/core/<platform>`.
|
|
265
|
+
*/
|
|
266
|
+
platforms: P;
|
|
267
|
+
walletConnect?: WalletConnectConfig;
|
|
268
|
+
debug: boolean;
|
|
269
|
+
/**
|
|
270
|
+
* Custom storage key for persisting wallet connection state.
|
|
271
|
+
* Useful when running multiple kheopskit instances on the same domain
|
|
272
|
+
* to prevent state conflicts between different dapps.
|
|
273
|
+
*
|
|
274
|
+
* @default "kheopskit"
|
|
275
|
+
*/
|
|
276
|
+
storageKey: string;
|
|
277
|
+
/**
|
|
278
|
+
* Grace period in milliseconds to wait for wallets to inject before
|
|
279
|
+
* syncing to actual state. During this period, cached wallet/account
|
|
280
|
+
* state from storage is preserved to prevent UI flashing.
|
|
281
|
+
*
|
|
282
|
+
* Set to 0 to disable hydration buffering.
|
|
283
|
+
*
|
|
284
|
+
* @default 500
|
|
285
|
+
*/
|
|
286
|
+
hydrationGracePeriod: number;
|
|
287
|
+
};
|
|
288
|
+
/**
|
|
289
|
+
* The current kheopskit state.
|
|
290
|
+
*
|
|
291
|
+
* @remarks
|
|
292
|
+
* While {@link KheopskitState.isHydrating} is `true`, `wallets` and `accounts`
|
|
293
|
+
* may contain cached placeholders restored from storage. The **SDK handles** the
|
|
294
|
+
* platform types advertise (e.g. `account.signer` / `getSigner` on Solana,
|
|
295
|
+
* `account.client` on Ethereum, `wallet.provider` / `extension`) are **absent at
|
|
296
|
+
* runtime even though the types claim them**, and placeholder wallets throw if
|
|
297
|
+
* `connect`/`disconnect` is called. Guard all access to those behind
|
|
298
|
+
* `!isHydrating`.
|
|
299
|
+
*
|
|
300
|
+
* The plain, serializable platform data that is persisted in the cache IS
|
|
301
|
+
* restored on the placeholders (Ethereum `chainId`, Polkadot key `type`), so it
|
|
302
|
+
* renders immediately on reload without flashing. Solana `chains` is not cached,
|
|
303
|
+
* so it remains absent until the live account loads.
|
|
304
|
+
*/
|
|
305
|
+
type KheopskitState<P extends readonly KheopskitPlatform[] = readonly KheopskitPlatform[]> = {
|
|
306
|
+
wallets: (WalletOf<P[number]> | WalletConnectWallet)[];
|
|
307
|
+
accounts: AccountOf<P[number]>[];
|
|
308
|
+
config: KheopskitConfig<P>;
|
|
309
|
+
/**
|
|
310
|
+
* Whether the state is still being hydrated from cache.
|
|
311
|
+
*
|
|
312
|
+
* During hydration, cached wallets/accounts may be displayed before the
|
|
313
|
+
* actual wallet extensions have injected. See the type-level remarks: while
|
|
314
|
+
* this is `true`, SDK-typed fields (signer/client/provider/extension) are not
|
|
315
|
+
* present at runtime — guard all access behind `!isHydrating`.
|
|
316
|
+
*/
|
|
317
|
+
isHydrating: boolean;
|
|
318
|
+
};
|
|
319
|
+
/**
|
|
320
|
+
* Serializable wallet data for SSR hydration cache.
|
|
321
|
+
* Contains only the data needed to render wallet UI without flash.
|
|
322
|
+
* Note: icon is NOT stored to save cookie space - it's looked up at hydration time.
|
|
323
|
+
*/
|
|
324
|
+
type CachedWallet = {
|
|
325
|
+
id: WalletId;
|
|
326
|
+
/** Absent for the platform-less WalletConnect connector. */
|
|
327
|
+
platform?: WalletPlatform;
|
|
328
|
+
type: WalletType;
|
|
329
|
+
name: string;
|
|
330
|
+
isConnected: boolean;
|
|
331
|
+
};
|
|
332
|
+
/**
|
|
333
|
+
* Serializable account data for SSR hydration cache.
|
|
334
|
+
* Contains only the data needed to render account UI without flash.
|
|
335
|
+
*/
|
|
336
|
+
type CachedAccount = {
|
|
337
|
+
id: string;
|
|
338
|
+
platform: WalletPlatform;
|
|
339
|
+
address: string;
|
|
340
|
+
name?: string;
|
|
341
|
+
/** Cached chain ID for Ethereum accounts. */
|
|
342
|
+
chainId?: number;
|
|
343
|
+
/** Cached key type for Polkadot accounts. */
|
|
344
|
+
polkadotAccountType?: PolkadotAccountType;
|
|
345
|
+
walletId: WalletId;
|
|
346
|
+
walletName: string;
|
|
347
|
+
};
|
|
348
|
+
|
|
349
|
+
export { type AccountOf as A, type BaseWallet as B, type CachedAccount as C, type KheopskitPlatform as K, type PlatformContext as P, type WalletAccountId as W, type WalletId as a, type WalletConnectWallet as b, isWalletConnectWallet as c, type KheopskitConfig as d, createKheopskitStore as e, type KheopskitState as f, type AppKitInstance as g, type BaseWalletAccount as h, isInjectedWallet as i, type CachedWallet as j, type PolkadotAccountType as k, type WalletConnectConfig as l, type WalletConnectMetadata as m, type WalletConnectProvider as n, type WalletOf as o, type WalletPlatform as p, type WalletType as q, getDefaultStore as r, getWalletAccountId as s, getWalletId as t, isValidWalletId as u, parseWalletAccountId as v, parseWalletId as w };
|
|
@@ -0,0 +1,349 @@
|
|
|
1
|
+
import * as rxjs from 'rxjs';
|
|
2
|
+
import { Observable } from 'rxjs';
|
|
3
|
+
|
|
4
|
+
type WalletAccountId = string;
|
|
5
|
+
declare const getWalletAccountId: (walletId: string, address: string) => WalletAccountId;
|
|
6
|
+
declare const parseWalletAccountId: (accountId: string) => {
|
|
7
|
+
walletId: string;
|
|
8
|
+
address: string;
|
|
9
|
+
};
|
|
10
|
+
|
|
11
|
+
type WalletId = string;
|
|
12
|
+
declare const getWalletId: (platform: WalletPlatform, identifier: string) => WalletId;
|
|
13
|
+
declare const parseWalletId: (walletId: string) => {
|
|
14
|
+
platform: WalletPlatform;
|
|
15
|
+
identifier: string;
|
|
16
|
+
};
|
|
17
|
+
/**
|
|
18
|
+
* Non-throwing variant of {@link parseWalletId}. Use when validating untrusted
|
|
19
|
+
* input (e.g. cached state persisted by an older version) before parsing.
|
|
20
|
+
*/
|
|
21
|
+
declare const isValidWalletId: (walletId: unknown) => walletId is WalletId;
|
|
22
|
+
|
|
23
|
+
type KheopskitStoreData = {
|
|
24
|
+
autoReconnect?: WalletId[];
|
|
25
|
+
/** Cached wallet state for SSR hydration to prevent UI flash */
|
|
26
|
+
cachedWallets?: CachedWallet[];
|
|
27
|
+
/** Cached account state for SSR hydration to prevent UI flash */
|
|
28
|
+
cachedAccounts?: CachedAccount[];
|
|
29
|
+
};
|
|
30
|
+
type CreateKheopskitStoreOptions = {
|
|
31
|
+
/**
|
|
32
|
+
* Cookie string for SSR hydration.
|
|
33
|
+
* When provided, uses cookieStorage instead of localStorage.
|
|
34
|
+
*/
|
|
35
|
+
ssrCookies?: string;
|
|
36
|
+
/**
|
|
37
|
+
* Custom storage key to namespace the stored data.
|
|
38
|
+
* @default "kheopskit"
|
|
39
|
+
*/
|
|
40
|
+
storageKey?: string;
|
|
41
|
+
};
|
|
42
|
+
/**
|
|
43
|
+
* Creates a kheopskit store with the appropriate storage backend.
|
|
44
|
+
* Uses cookieStorage when ssrCookies is provided (for SSR hydration),
|
|
45
|
+
* otherwise falls back to safeLocalStorage.
|
|
46
|
+
*
|
|
47
|
+
* @param options - Configuration options for the store
|
|
48
|
+
*/
|
|
49
|
+
declare const createKheopskitStore: (options?: CreateKheopskitStoreOptions) => {
|
|
50
|
+
observable: rxjs.Observable<KheopskitStoreData>;
|
|
51
|
+
addEnabledWalletId: (walletId: WalletId) => void;
|
|
52
|
+
removeEnabledWalletId: (walletId: WalletId) => void;
|
|
53
|
+
getCachedState: () => {
|
|
54
|
+
wallets: CachedWallet[];
|
|
55
|
+
accounts: CachedAccount[];
|
|
56
|
+
};
|
|
57
|
+
setCachedState: (wallets: CachedWallet[], accounts: CachedAccount[]) => void;
|
|
58
|
+
};
|
|
59
|
+
type KheopskitStore = ReturnType<typeof createKheopskitStore>;
|
|
60
|
+
/**
|
|
61
|
+
* Gets the default store, creating it on first access.
|
|
62
|
+
* Uses localStorage on client, noop on server.
|
|
63
|
+
* Lazily initialized to avoid SSR issues with module-level code.
|
|
64
|
+
*/
|
|
65
|
+
declare const getDefaultStore: () => KheopskitStore;
|
|
66
|
+
|
|
67
|
+
type WalletPlatform = "polkadot" | "ethereum" | "solana";
|
|
68
|
+
/**
|
|
69
|
+
* Minimal structural view of a WalletConnect `UniversalProvider` — the subset
|
|
70
|
+
* kheopskit reads. Declared locally so core never depends on
|
|
71
|
+
* `@walletconnect/universal-provider`. The concrete instance comes from
|
|
72
|
+
* `@reown/appkit` at runtime.
|
|
73
|
+
*/
|
|
74
|
+
type WalletConnectProvider = {
|
|
75
|
+
session?: {
|
|
76
|
+
topic: string;
|
|
77
|
+
namespaces: Record<string, {
|
|
78
|
+
accounts?: string[];
|
|
79
|
+
}>;
|
|
80
|
+
};
|
|
81
|
+
client: {
|
|
82
|
+
request<T = unknown>(args: {
|
|
83
|
+
topic: string;
|
|
84
|
+
chainId: string;
|
|
85
|
+
request: {
|
|
86
|
+
method: string;
|
|
87
|
+
params: unknown;
|
|
88
|
+
};
|
|
89
|
+
}): Promise<T>;
|
|
90
|
+
};
|
|
91
|
+
request(args: {
|
|
92
|
+
method: string;
|
|
93
|
+
params?: unknown;
|
|
94
|
+
}): Promise<unknown>;
|
|
95
|
+
on(event: string, listener: (...args: unknown[]) => void): void;
|
|
96
|
+
off(event: string, listener: (...args: unknown[]) => void): void;
|
|
97
|
+
};
|
|
98
|
+
/**
|
|
99
|
+
* Minimal structural view of the Reown AppKit instance — the subset kheopskit's
|
|
100
|
+
* account factories use. Exposed as the `appKit` escape hatch on AppKit wallets;
|
|
101
|
+
* cast it to `@reown/appkit`'s `AppKit` type for the full API. Declared locally
|
|
102
|
+
* so core never depends on `@reown/appkit`'s types (it's an optional peer).
|
|
103
|
+
*/
|
|
104
|
+
type AppKitInstance = {
|
|
105
|
+
getProvider<T = WalletConnectProvider>(namespace: string): T | undefined;
|
|
106
|
+
getAccount(namespace: string): {
|
|
107
|
+
allAccounts: {
|
|
108
|
+
address: string;
|
|
109
|
+
}[];
|
|
110
|
+
} | undefined;
|
|
111
|
+
getCaipNetworks(namespace: string): {
|
|
112
|
+
caipNetworkId?: string;
|
|
113
|
+
}[];
|
|
114
|
+
};
|
|
115
|
+
type WalletType = "injected" | "walletconnect";
|
|
116
|
+
type PolkadotAccountType = "sr25519" | "ed25519" | "ecdsa" | "ethereum";
|
|
117
|
+
/**
|
|
118
|
+
* SDK-free fields common to every wallet, regardless of platform. Platform
|
|
119
|
+
* packages (`@kheopskit/core/<platform>`) extend this with SDK-typed fields
|
|
120
|
+
* (the injected provider/extension/standard-wallet handle).
|
|
121
|
+
*/
|
|
122
|
+
type BaseWallet = {
|
|
123
|
+
id: WalletId;
|
|
124
|
+
platform: WalletPlatform;
|
|
125
|
+
type: WalletType;
|
|
126
|
+
name: string;
|
|
127
|
+
icon: string;
|
|
128
|
+
isConnected: boolean;
|
|
129
|
+
connect: () => Promise<void>;
|
|
130
|
+
/**
|
|
131
|
+
* Disconnect the wallet. Resolves once the underlying provider/extension
|
|
132
|
+
* disconnect completes; rejects if it fails so callers can surface or retry.
|
|
133
|
+
*/
|
|
134
|
+
disconnect: () => Promise<void>;
|
|
135
|
+
};
|
|
136
|
+
/**
|
|
137
|
+
* SDK-free fields common to every account, regardless of platform. Platform
|
|
138
|
+
* packages extend this with their SDK-typed signer/client.
|
|
139
|
+
*/
|
|
140
|
+
type BaseWalletAccount = {
|
|
141
|
+
id: WalletAccountId;
|
|
142
|
+
platform: WalletPlatform;
|
|
143
|
+
/** Base58 (Solana), SS58 (Polkadot) or 0x-hex (Ethereum) address. */
|
|
144
|
+
address: string;
|
|
145
|
+
/** Friendly account name, when the wallet exposes one (e.g. Polkadot). */
|
|
146
|
+
name?: string;
|
|
147
|
+
walletName: string;
|
|
148
|
+
walletId: WalletId;
|
|
149
|
+
};
|
|
150
|
+
/**
|
|
151
|
+
* The single WalletConnect connector, shared across every platform.
|
|
152
|
+
*
|
|
153
|
+
* Unlike injected wallets, it is **not tied to a platform**: one WalletConnect
|
|
154
|
+
* session spans whichever namespaces the remote wallet approves in a single
|
|
155
|
+
* pairing (a namespace can't be added to a live session afterwards). So there is
|
|
156
|
+
* exactly one of these in `wallets`, discriminated by `type: "walletconnect"`.
|
|
157
|
+
* Its accounts appear in `accounts`, each carrying its own `platform`.
|
|
158
|
+
*/
|
|
159
|
+
type WalletConnectWallet = {
|
|
160
|
+
id: WalletId;
|
|
161
|
+
type: "walletconnect";
|
|
162
|
+
/**
|
|
163
|
+
* Platforms (namespaces) the live session currently carries. Empty until
|
|
164
|
+
* connected; populated with whatever the wallet approved (e.g. just
|
|
165
|
+
* `["ethereum"]`, or `["polkadot", "ethereum"]`).
|
|
166
|
+
*/
|
|
167
|
+
platforms: WalletPlatform[];
|
|
168
|
+
/**
|
|
169
|
+
* Raw Reown AppKit instance, exposed as an escape hatch for advanced use
|
|
170
|
+
* (custom modal control, reading providers directly). Most consumers should
|
|
171
|
+
* use `connect`/`disconnect` and the derived accounts instead.
|
|
172
|
+
*/
|
|
173
|
+
appKit: AppKitInstance;
|
|
174
|
+
name: string;
|
|
175
|
+
icon: string;
|
|
176
|
+
isConnected: boolean;
|
|
177
|
+
connect: () => Promise<void>;
|
|
178
|
+
disconnect: () => Promise<void>;
|
|
179
|
+
};
|
|
180
|
+
/** Narrows a wallet to the platform-less {@link WalletConnectWallet}. */
|
|
181
|
+
declare const isWalletConnectWallet: (wallet: BaseWallet | WalletConnectWallet) => wallet is WalletConnectWallet;
|
|
182
|
+
/**
|
|
183
|
+
* Narrows a wallet to an injected (browser-extension / Wallet Standard) wallet —
|
|
184
|
+
* the complement of {@link isWalletConnectWallet}. `state.wallets` is
|
|
185
|
+
* `(InjectedWallet | WalletConnectWallet)[]`, so use this to recover the
|
|
186
|
+
* injected-only fields when iterating: `platform`, `sourceId`, and the SDK
|
|
187
|
+
* handle (`provider` on Ethereum, `extension` on Polkadot, `wallet` on Solana).
|
|
188
|
+
*
|
|
189
|
+
* @example
|
|
190
|
+
* ```ts
|
|
191
|
+
* for (const wallet of wallets.filter(isInjectedWallet)) {
|
|
192
|
+
* console.log(wallet.platform, wallet.sourceId); // typed, no WC widening
|
|
193
|
+
* }
|
|
194
|
+
* ```
|
|
195
|
+
*/
|
|
196
|
+
declare const isInjectedWallet: <W extends BaseWallet | WalletConnectWallet>(wallet: W) => wallet is Exclude<W, WalletConnectWallet>;
|
|
197
|
+
/**
|
|
198
|
+
* Dapp metadata shown in the WalletConnect modal. Mirrors WalletConnect's
|
|
199
|
+
* `Metadata`, declared locally so core doesn't depend on
|
|
200
|
+
* `@walletconnect/universal-provider`.
|
|
201
|
+
*/
|
|
202
|
+
type WalletConnectMetadata = {
|
|
203
|
+
name: string;
|
|
204
|
+
description: string;
|
|
205
|
+
url: string;
|
|
206
|
+
icons: string[];
|
|
207
|
+
};
|
|
208
|
+
type WalletConnectConfig = {
|
|
209
|
+
projectId: string;
|
|
210
|
+
metadata: WalletConnectMetadata;
|
|
211
|
+
/** Defaults to wss://relay.walletconnect.com */
|
|
212
|
+
relayUrl?: string;
|
|
213
|
+
/**
|
|
214
|
+
* Networks AppKit should enable. Pass `AppKitNetwork[]` from
|
|
215
|
+
* `@reown/appkit/networks` (see
|
|
216
|
+
* https://docs.reown.com/advanced/multichain/polkadot/dapp-integration-guide#walletconnect-code%2Fcomponent-setup).
|
|
217
|
+
* Loosely typed (`unknown`) so core doesn't depend on `@reown/appkit`'s
|
|
218
|
+
* types — the value is forwarded to AppKit as-is.
|
|
219
|
+
*/
|
|
220
|
+
networks: [unknown, ...unknown[]];
|
|
221
|
+
themeMode?: "light" | "dark";
|
|
222
|
+
themeVariables?: Record<string, string | number>;
|
|
223
|
+
};
|
|
224
|
+
/**
|
|
225
|
+
* Context passed to a platform plugin's `getWallets$`. Carries the shared store
|
|
226
|
+
* and the resolved core config (for WalletConnect / debug).
|
|
227
|
+
*/
|
|
228
|
+
type PlatformContext = {
|
|
229
|
+
store: KheopskitStore;
|
|
230
|
+
config: KheopskitConfig;
|
|
231
|
+
};
|
|
232
|
+
/**
|
|
233
|
+
* A platform plugin. Created by the per-platform factories exported from
|
|
234
|
+
* `@kheopskit/core/polkadot`, `/ethereum`, `/solana`. Core iterates plugins
|
|
235
|
+
* generically and never imports a platform SDK itself.
|
|
236
|
+
*
|
|
237
|
+
* @typeParam TPlatform - the platform discriminant
|
|
238
|
+
* @typeParam TWallet - the platform's wallet type (extends {@link BaseWallet})
|
|
239
|
+
* @typeParam TAccount - the platform's account type (extends {@link BaseWalletAccount})
|
|
240
|
+
*/
|
|
241
|
+
type KheopskitPlatform<TPlatform extends WalletPlatform = WalletPlatform, TWallet extends BaseWallet = BaseWallet, TAccount extends BaseWalletAccount = BaseWalletAccount> = {
|
|
242
|
+
readonly platform: TPlatform;
|
|
243
|
+
getWallets$(ctx: PlatformContext): Observable<TWallet[]>;
|
|
244
|
+
getAccounts$(wallets$: Observable<(TWallet | WalletConnectWallet)[]>): Observable<TAccount[]>;
|
|
245
|
+
/**
|
|
246
|
+
* Optional hydration filter. Cached accounts for which this returns false are
|
|
247
|
+
* dropped during SSR hydration (Polkadot uses it to honour `accountTypes`).
|
|
248
|
+
*/
|
|
249
|
+
acceptsCachedAccount?(cached: CachedAccount): boolean;
|
|
250
|
+
};
|
|
251
|
+
type ElementOf<T> = T extends readonly (infer E)[] ? E : never;
|
|
252
|
+
/** The account type produced by a plugin (inferred from its `getAccounts$`). */
|
|
253
|
+
type AccountOf<T> = T extends {
|
|
254
|
+
getAccounts$: (wallets$: never) => Observable<infer R>;
|
|
255
|
+
} ? ElementOf<R> : never;
|
|
256
|
+
/** The wallet type produced by a plugin (inferred from its `getWallets$`). */
|
|
257
|
+
type WalletOf<T> = T extends {
|
|
258
|
+
getWallets$: (ctx: never) => Observable<infer R>;
|
|
259
|
+
} ? ElementOf<R> : never;
|
|
260
|
+
type KheopskitConfig<P extends readonly KheopskitPlatform[] = readonly KheopskitPlatform[]> = {
|
|
261
|
+
autoReconnect: boolean;
|
|
262
|
+
/**
|
|
263
|
+
* Platform plugins to enable, e.g. `[polkadot(), solana({ chain })]`.
|
|
264
|
+
* Import factories from `@kheopskit/core/<platform>`.
|
|
265
|
+
*/
|
|
266
|
+
platforms: P;
|
|
267
|
+
walletConnect?: WalletConnectConfig;
|
|
268
|
+
debug: boolean;
|
|
269
|
+
/**
|
|
270
|
+
* Custom storage key for persisting wallet connection state.
|
|
271
|
+
* Useful when running multiple kheopskit instances on the same domain
|
|
272
|
+
* to prevent state conflicts between different dapps.
|
|
273
|
+
*
|
|
274
|
+
* @default "kheopskit"
|
|
275
|
+
*/
|
|
276
|
+
storageKey: string;
|
|
277
|
+
/**
|
|
278
|
+
* Grace period in milliseconds to wait for wallets to inject before
|
|
279
|
+
* syncing to actual state. During this period, cached wallet/account
|
|
280
|
+
* state from storage is preserved to prevent UI flashing.
|
|
281
|
+
*
|
|
282
|
+
* Set to 0 to disable hydration buffering.
|
|
283
|
+
*
|
|
284
|
+
* @default 500
|
|
285
|
+
*/
|
|
286
|
+
hydrationGracePeriod: number;
|
|
287
|
+
};
|
|
288
|
+
/**
|
|
289
|
+
* The current kheopskit state.
|
|
290
|
+
*
|
|
291
|
+
* @remarks
|
|
292
|
+
* While {@link KheopskitState.isHydrating} is `true`, `wallets` and `accounts`
|
|
293
|
+
* may contain cached placeholders restored from storage. The **SDK handles** the
|
|
294
|
+
* platform types advertise (e.g. `account.signer` / `getSigner` on Solana,
|
|
295
|
+
* `account.client` on Ethereum, `wallet.provider` / `extension`) are **absent at
|
|
296
|
+
* runtime even though the types claim them**, and placeholder wallets throw if
|
|
297
|
+
* `connect`/`disconnect` is called. Guard all access to those behind
|
|
298
|
+
* `!isHydrating`.
|
|
299
|
+
*
|
|
300
|
+
* The plain, serializable platform data that is persisted in the cache IS
|
|
301
|
+
* restored on the placeholders (Ethereum `chainId`, Polkadot key `type`), so it
|
|
302
|
+
* renders immediately on reload without flashing. Solana `chains` is not cached,
|
|
303
|
+
* so it remains absent until the live account loads.
|
|
304
|
+
*/
|
|
305
|
+
type KheopskitState<P extends readonly KheopskitPlatform[] = readonly KheopskitPlatform[]> = {
|
|
306
|
+
wallets: (WalletOf<P[number]> | WalletConnectWallet)[];
|
|
307
|
+
accounts: AccountOf<P[number]>[];
|
|
308
|
+
config: KheopskitConfig<P>;
|
|
309
|
+
/**
|
|
310
|
+
* Whether the state is still being hydrated from cache.
|
|
311
|
+
*
|
|
312
|
+
* During hydration, cached wallets/accounts may be displayed before the
|
|
313
|
+
* actual wallet extensions have injected. See the type-level remarks: while
|
|
314
|
+
* this is `true`, SDK-typed fields (signer/client/provider/extension) are not
|
|
315
|
+
* present at runtime — guard all access behind `!isHydrating`.
|
|
316
|
+
*/
|
|
317
|
+
isHydrating: boolean;
|
|
318
|
+
};
|
|
319
|
+
/**
|
|
320
|
+
* Serializable wallet data for SSR hydration cache.
|
|
321
|
+
* Contains only the data needed to render wallet UI without flash.
|
|
322
|
+
* Note: icon is NOT stored to save cookie space - it's looked up at hydration time.
|
|
323
|
+
*/
|
|
324
|
+
type CachedWallet = {
|
|
325
|
+
id: WalletId;
|
|
326
|
+
/** Absent for the platform-less WalletConnect connector. */
|
|
327
|
+
platform?: WalletPlatform;
|
|
328
|
+
type: WalletType;
|
|
329
|
+
name: string;
|
|
330
|
+
isConnected: boolean;
|
|
331
|
+
};
|
|
332
|
+
/**
|
|
333
|
+
* Serializable account data for SSR hydration cache.
|
|
334
|
+
* Contains only the data needed to render account UI without flash.
|
|
335
|
+
*/
|
|
336
|
+
type CachedAccount = {
|
|
337
|
+
id: string;
|
|
338
|
+
platform: WalletPlatform;
|
|
339
|
+
address: string;
|
|
340
|
+
name?: string;
|
|
341
|
+
/** Cached chain ID for Ethereum accounts. */
|
|
342
|
+
chainId?: number;
|
|
343
|
+
/** Cached key type for Polkadot accounts. */
|
|
344
|
+
polkadotAccountType?: PolkadotAccountType;
|
|
345
|
+
walletId: WalletId;
|
|
346
|
+
walletName: string;
|
|
347
|
+
};
|
|
348
|
+
|
|
349
|
+
export { type AccountOf as A, type BaseWallet as B, type CachedAccount as C, type KheopskitPlatform as K, type PlatformContext as P, type WalletAccountId as W, type WalletId as a, type WalletConnectWallet as b, isWalletConnectWallet as c, type KheopskitConfig as d, createKheopskitStore as e, type KheopskitState as f, type AppKitInstance as g, type BaseWalletAccount as h, isInjectedWallet as i, type CachedWallet as j, type PolkadotAccountType as k, type WalletConnectConfig as l, type WalletConnectMetadata as m, type WalletConnectProvider as n, type WalletOf as o, type WalletPlatform as p, type WalletType as q, getDefaultStore as r, getWalletAccountId as s, getWalletId as t, isValidWalletId as u, parseWalletAccountId as v, parseWalletId as w };
|