@kheopskit/core 0.0.1-alpha.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/CHANGELOG.md ADDED
@@ -0,0 +1,7 @@
1
+ # @kheopskit/core
2
+
3
+ ## 0.0.1-alpha.0
4
+
5
+ ### Patch Changes
6
+
7
+ - [`3216d3b`](https://github.com/0xKheops/kheopskit-alpha/commit/3216d3b4ca1f2fadbebe9a4275e7b864ac89d222) Thanks [@0xKheops](https://github.com/0xKheops)! - initial alpha release
@@ -0,0 +1,94 @@
1
+ import { Observable } from 'rxjs';
2
+ import { EIP1193Provider } from 'viem';
3
+ import { InjectedPolkadotAccount, InjectedExtension, InjectedAccount, PolkadotSigner } from 'polkadot-api/pjs-signer';
4
+
5
+ type WalletId = string;
6
+
7
+ type AccountId = string;
8
+
9
+ type PolkadotAccount = InjectedPolkadotAccount & {
10
+ id: AccountId;
11
+ platform: "polkadot";
12
+ walletName: string;
13
+ walletId: string;
14
+ };
15
+
16
+ type EthereumAccount = {
17
+ id: AccountId;
18
+ platform: "ethereum";
19
+ provider: EIP1193Provider;
20
+ address: `0x${string}`;
21
+ walletName: string;
22
+ walletId: string;
23
+ isWalletDefault: boolean;
24
+ };
25
+
26
+ type AccountStorageBase = {
27
+ wallet: string;
28
+ address: string;
29
+ };
30
+ type EthereumAccountStorage = AccountStorageBase & {
31
+ platform: "ethereum";
32
+ };
33
+ type PolkadotAccountStorage = AccountStorageBase & {
34
+ platform: "polkadot";
35
+ name: InjectedAccount["name"];
36
+ type: InjectedAccount["type"];
37
+ genesisHash: InjectedAccount["genesisHash"];
38
+ };
39
+ type AccountStorage = PolkadotAccountStorage | EthereumAccountStorage;
40
+ type Account<T extends AccountStorage> = T & {
41
+ id: string;
42
+ signer: T extends PolkadotAccountStorage ? PolkadotSigner : null;
43
+ };
44
+ type PlatformData<T extends AccountStorageBase> = {
45
+ enabledExtensionIds: string[];
46
+ accounts: T[];
47
+ defaultAccountId: string | null;
48
+ };
49
+ type KheopskitStoreData = {
50
+ autoReconnect?: WalletId[];
51
+ };
52
+ type KheopskitConfig = {
53
+ autoReconnect?: boolean;
54
+ platforms: WalletPlatform[];
55
+ };
56
+ type PolkadotDisabledInjectedWallet = {
57
+ id: WalletId;
58
+ platform: "polkadot";
59
+ extensionId: string;
60
+ name: string;
61
+ isEnabled: false;
62
+ connect: () => Promise<void>;
63
+ };
64
+ type PolkadotEnabledInjectedWallet = {
65
+ id: WalletId;
66
+ platform: "polkadot";
67
+ extensionId: string;
68
+ extension: InjectedExtension;
69
+ name: string;
70
+ isEnabled: true;
71
+ disconnect: () => void;
72
+ };
73
+ type PolkadotWallet = PolkadotDisabledInjectedWallet | PolkadotEnabledInjectedWallet;
74
+ type EthereumWallet = {
75
+ platform: "ethereum";
76
+ id: WalletId;
77
+ providerId: string;
78
+ provider: EIP1193Provider;
79
+ name: string;
80
+ icon: string;
81
+ isEnabled: boolean;
82
+ connect: () => Promise<void>;
83
+ disconnect: () => void;
84
+ };
85
+ type Wallet = PolkadotWallet | EthereumWallet;
86
+ type WalletPlatform = Wallet["platform"];
87
+ type WalletAccount = PolkadotAccount | EthereumAccount;
88
+
89
+ declare const getKheopskit$: (config: KheopskitConfig) => Observable<{
90
+ wallets: Wallet[];
91
+ accounts: WalletAccount[];
92
+ }>;
93
+
94
+ export { type Account, type AccountStorage, type EthereumAccountStorage, type EthereumWallet, type KheopskitConfig, type KheopskitStoreData, type PlatformData, type PolkadotAccountStorage, type PolkadotDisabledInjectedWallet, type PolkadotEnabledInjectedWallet, type PolkadotWallet, type Wallet, type WalletAccount, type WalletPlatform, getKheopskit$ };
@@ -0,0 +1,94 @@
1
+ import { Observable } from 'rxjs';
2
+ import { EIP1193Provider } from 'viem';
3
+ import { InjectedPolkadotAccount, InjectedExtension, InjectedAccount, PolkadotSigner } from 'polkadot-api/pjs-signer';
4
+
5
+ type WalletId = string;
6
+
7
+ type AccountId = string;
8
+
9
+ type PolkadotAccount = InjectedPolkadotAccount & {
10
+ id: AccountId;
11
+ platform: "polkadot";
12
+ walletName: string;
13
+ walletId: string;
14
+ };
15
+
16
+ type EthereumAccount = {
17
+ id: AccountId;
18
+ platform: "ethereum";
19
+ provider: EIP1193Provider;
20
+ address: `0x${string}`;
21
+ walletName: string;
22
+ walletId: string;
23
+ isWalletDefault: boolean;
24
+ };
25
+
26
+ type AccountStorageBase = {
27
+ wallet: string;
28
+ address: string;
29
+ };
30
+ type EthereumAccountStorage = AccountStorageBase & {
31
+ platform: "ethereum";
32
+ };
33
+ type PolkadotAccountStorage = AccountStorageBase & {
34
+ platform: "polkadot";
35
+ name: InjectedAccount["name"];
36
+ type: InjectedAccount["type"];
37
+ genesisHash: InjectedAccount["genesisHash"];
38
+ };
39
+ type AccountStorage = PolkadotAccountStorage | EthereumAccountStorage;
40
+ type Account<T extends AccountStorage> = T & {
41
+ id: string;
42
+ signer: T extends PolkadotAccountStorage ? PolkadotSigner : null;
43
+ };
44
+ type PlatformData<T extends AccountStorageBase> = {
45
+ enabledExtensionIds: string[];
46
+ accounts: T[];
47
+ defaultAccountId: string | null;
48
+ };
49
+ type KheopskitStoreData = {
50
+ autoReconnect?: WalletId[];
51
+ };
52
+ type KheopskitConfig = {
53
+ autoReconnect?: boolean;
54
+ platforms: WalletPlatform[];
55
+ };
56
+ type PolkadotDisabledInjectedWallet = {
57
+ id: WalletId;
58
+ platform: "polkadot";
59
+ extensionId: string;
60
+ name: string;
61
+ isEnabled: false;
62
+ connect: () => Promise<void>;
63
+ };
64
+ type PolkadotEnabledInjectedWallet = {
65
+ id: WalletId;
66
+ platform: "polkadot";
67
+ extensionId: string;
68
+ extension: InjectedExtension;
69
+ name: string;
70
+ isEnabled: true;
71
+ disconnect: () => void;
72
+ };
73
+ type PolkadotWallet = PolkadotDisabledInjectedWallet | PolkadotEnabledInjectedWallet;
74
+ type EthereumWallet = {
75
+ platform: "ethereum";
76
+ id: WalletId;
77
+ providerId: string;
78
+ provider: EIP1193Provider;
79
+ name: string;
80
+ icon: string;
81
+ isEnabled: boolean;
82
+ connect: () => Promise<void>;
83
+ disconnect: () => void;
84
+ };
85
+ type Wallet = PolkadotWallet | EthereumWallet;
86
+ type WalletPlatform = Wallet["platform"];
87
+ type WalletAccount = PolkadotAccount | EthereumAccount;
88
+
89
+ declare const getKheopskit$: (config: KheopskitConfig) => Observable<{
90
+ wallets: Wallet[];
91
+ accounts: WalletAccount[];
92
+ }>;
93
+
94
+ export { type Account, type AccountStorage, type EthereumAccountStorage, type EthereumWallet, type KheopskitConfig, type KheopskitStoreData, type PlatformData, type PolkadotAccountStorage, type PolkadotDisabledInjectedWallet, type PolkadotEnabledInjectedWallet, type PolkadotWallet, type Wallet, type WalletAccount, type WalletPlatform, getKheopskit$ };
package/dist/index.js ADDED
@@ -0,0 +1,458 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/index.ts
21
+ var index_exports = {};
22
+ __export(index_exports, {
23
+ getKheopskit$: () => getKheopskit$
24
+ });
25
+ module.exports = __toCommonJS(index_exports);
26
+
27
+ // src/api/kheopskit.ts
28
+ var import_rxjs8 = require("rxjs");
29
+
30
+ // src/api/accounts.ts
31
+ var import_rxjs6 = require("rxjs");
32
+
33
+ // src/utils/createStore.ts
34
+ var import_rxjs = require("rxjs");
35
+ var createStore = (key, defaultValue) => {
36
+ const subject = new import_rxjs.BehaviorSubject(getStoredData(key, defaultValue));
37
+ (0, import_rxjs.fromEvent)(window, "storage").pipe(
38
+ (0, import_rxjs.filter)((event) => event.key === key),
39
+ (0, import_rxjs.map)((event) => parseData(event.newValue, defaultValue))
40
+ ).subscribe((newValue) => subject.next(newValue));
41
+ const update = (val) => {
42
+ setStoredData(key, val);
43
+ subject.next(val);
44
+ };
45
+ return {
46
+ observable: subject.asObservable(),
47
+ set: (val) => update(val),
48
+ mutate: (transform) => update(transform(subject.getValue())),
49
+ get: () => structuredClone(subject.getValue())
50
+ };
51
+ };
52
+ var parseData = (str, defaultValue) => {
53
+ try {
54
+ if (str) return JSON.parse(str);
55
+ } catch {
56
+ }
57
+ return defaultValue;
58
+ };
59
+ var getStoredData = (key, defaultValue) => {
60
+ const str = localStorage.getItem(key);
61
+ return parseData(str, defaultValue);
62
+ };
63
+ var setStoredData = (key, val) => {
64
+ const str = JSON.stringify(val);
65
+ localStorage.setItem(key, str);
66
+ };
67
+
68
+ // src/utils/isEthereumAddress.ts
69
+ var isEthereumAddress = (address) => /^0x[a-fA-F0-9]{40}$/.test(address);
70
+
71
+ // src/utils/isSs58Address.ts
72
+ var import_polkadot_api = require("polkadot-api");
73
+ var accountIdEncoder = (0, import_polkadot_api.AccountId)().enc;
74
+ var isSs58Address = (address) => {
75
+ try {
76
+ if (!address) return false;
77
+ accountIdEncoder(address);
78
+ return true;
79
+ } catch (_err) {
80
+ return false;
81
+ }
82
+ };
83
+
84
+ // src/utils/isValidAddress.ts
85
+ var isValidAddress = (address) => {
86
+ return address.startsWith("0x") ? isEthereumAddress(address) : isSs58Address(address);
87
+ };
88
+
89
+ // src/utils/AccountId.ts
90
+ var getAccountId = (walletId, address) => {
91
+ if (!walletId) throw new Error("Missing walletId");
92
+ if (!isValidAddress(address)) throw new Error("Invalid address");
93
+ return `${walletId}::${address}`;
94
+ };
95
+
96
+ // src/utils/isWalletPlatform.ts
97
+ var isWalletPlatform = (platform) => typeof platform === "string" && ["polkadot", "ethereum"].includes(platform);
98
+
99
+ // src/api/ethereum/accounts.ts
100
+ var import_rxjs3 = require("rxjs");
101
+ var import_viem = require("viem");
102
+
103
+ // src/api/store.ts
104
+ var import_lodash = require("lodash");
105
+
106
+ // src/utils/WalletId.ts
107
+ var getWalletId = (platform, identifier) => {
108
+ if (!isWalletPlatform(platform)) throw new Error("Invalid platform");
109
+ if (!identifier) throw new Error("Invalid name");
110
+ return `${platform}:${identifier}`;
111
+ };
112
+ var parseWalletId = (walletId) => {
113
+ if (!walletId) throw new Error("Invalid walletId");
114
+ const [platform, identifier] = walletId.split(":");
115
+ if (!isWalletPlatform(platform)) throw new Error("Invalid platform");
116
+ if (!identifier) throw new Error("Invalid address");
117
+ return { platform, identifier };
118
+ };
119
+
120
+ // src/api/store.ts
121
+ var LOCAL_STORAGE_KEY = "kheopskit";
122
+ var DEFAULT_SETTINGS = {};
123
+ var storage = createStore(LOCAL_STORAGE_KEY, DEFAULT_SETTINGS);
124
+ var addEnabledWalletId = (walletId) => {
125
+ parseWalletId(walletId);
126
+ storage.mutate((prev) => ({
127
+ ...prev,
128
+ autoReconnect: (0, import_lodash.uniq)((prev.autoReconnect ?? []).concat(walletId))
129
+ }));
130
+ };
131
+ var removeEnabledWalletId = (walletId) => {
132
+ storage.mutate((prev) => ({
133
+ ...prev,
134
+ autoReconnect: (0, import_lodash.uniq)(
135
+ (prev.autoReconnect ?? []).filter((id) => id !== walletId)
136
+ )
137
+ }));
138
+ };
139
+ var store = {
140
+ observable: storage.observable,
141
+ addEnabledWalletId,
142
+ removeEnabledWalletId
143
+ };
144
+
145
+ // src/api/ethereum/wallets.ts
146
+ var import_mipd = require("mipd");
147
+ var import_rxjs2 = require("rxjs");
148
+ var providersDetails$ = new import_rxjs2.Observable(
149
+ (subscriber) => {
150
+ const store2 = (0, import_mipd.createStore)();
151
+ const unsubscribe = store2.subscribe((providerDetails2) => {
152
+ subscriber.next(providerDetails2);
153
+ });
154
+ const providerDetails = store2.getProviders();
155
+ subscriber.next(providerDetails);
156
+ return () => {
157
+ unsubscribe();
158
+ store2.destroy();
159
+ };
160
+ }
161
+ ).pipe((0, import_rxjs2.shareReplay)({ refCount: true, bufferSize: 1 }));
162
+ providersDetails$.subscribe(() => {
163
+ console.count("[kheopskit] providers$ emit");
164
+ });
165
+ var ethereumWallets$ = new import_rxjs2.Observable(
166
+ (subscriber) => {
167
+ const enabledWalletIds$ = new import_rxjs2.BehaviorSubject(/* @__PURE__ */ new Set());
168
+ const connectWallet = async (walletId, provider) => {
169
+ if (enabledWalletIds$.value.has(walletId))
170
+ throw new Error(`Extension ${walletId} already connected`);
171
+ provider.request({
172
+ method: "eth_requestAccounts"
173
+ });
174
+ const newSet = new Set(enabledWalletIds$.value);
175
+ newSet.add(walletId);
176
+ enabledWalletIds$.next(newSet);
177
+ store.addEnabledWalletId(walletId);
178
+ };
179
+ const disconnectWallet = async (walletId, _provider) => {
180
+ if (!enabledWalletIds$.value.has(walletId))
181
+ throw new Error(`Extension ${walletId} is not connected`);
182
+ const newSet = new Set(enabledWalletIds$.value);
183
+ newSet.delete(walletId);
184
+ enabledWalletIds$.next(newSet);
185
+ store.removeEnabledWalletId(walletId);
186
+ };
187
+ const sub = (0, import_rxjs2.combineLatest)([providersDetails$, enabledWalletIds$]).pipe(
188
+ (0, import_rxjs2.map)(([providerDetails, enabledWalletIds]) => {
189
+ return providerDetails.map((pd) => {
190
+ const walletId = getWalletId("ethereum", pd.info.rdns);
191
+ const provider = pd.provider;
192
+ return {
193
+ platform: "ethereum",
194
+ id: walletId,
195
+ name: pd.info.name,
196
+ icon: pd.info.icon,
197
+ provider,
198
+ isEnabled: enabledWalletIds.has(walletId),
199
+ providerId: pd.info.rdns,
200
+ connect: () => connectWallet(walletId, provider),
201
+ disconnect: () => disconnectWallet(walletId, provider)
202
+ };
203
+ });
204
+ })
205
+ ).subscribe(subscriber);
206
+ return () => {
207
+ sub.unsubscribe();
208
+ };
209
+ }
210
+ ).pipe((0, import_rxjs2.shareReplay)({ refCount: true, bufferSize: 1 }));
211
+ ethereumWallets$.subscribe(() => {
212
+ console.count("[kheopskit] ethereumWallets$ emit");
213
+ });
214
+
215
+ // src/api/ethereum/accounts.ts
216
+ var getWalletAccounts$ = (wallet) => {
217
+ if (!wallet.isEnabled) return (0, import_rxjs3.of)([]);
218
+ return new import_rxjs3.Observable((subscriber) => {
219
+ const getAccount = (address, i) => ({
220
+ id: getAccountId(wallet.id, address),
221
+ platform: "ethereum",
222
+ provider: wallet.provider,
223
+ address: (0, import_viem.getAddress)(address),
224
+ walletName: wallet.name,
225
+ walletId: wallet.id,
226
+ isWalletDefault: i === 0
227
+ });
228
+ const listener = (addresses) => {
229
+ subscriber.next(addresses.map(getAccount));
230
+ };
231
+ wallet.provider.on("accountsChanged", listener);
232
+ wallet.provider.request({ method: "eth_accounts" }).then((addresses) => {
233
+ subscriber.next(addresses.map(getAccount));
234
+ }).catch((err) => {
235
+ console.error("Failed to get accounts", err);
236
+ subscriber.next([]);
237
+ });
238
+ return () => {
239
+ wallet.provider.removeListener("accountsChanged", listener);
240
+ };
241
+ });
242
+ };
243
+ var ethereumAccounts$ = new import_rxjs3.Observable(
244
+ (subscriber) => {
245
+ const sub = ethereumWallets$.pipe(
246
+ (0, import_rxjs3.map)((wallets) => wallets.filter((w) => w.isEnabled)),
247
+ (0, import_rxjs3.switchMap)(
248
+ (wallets) => wallets.length ? (0, import_rxjs3.combineLatest)(wallets.map(getWalletAccounts$)) : (0, import_rxjs3.of)([])
249
+ ),
250
+ (0, import_rxjs3.map)((accounts) => accounts.flat())
251
+ ).subscribe(subscriber);
252
+ return () => {
253
+ sub.unsubscribe();
254
+ };
255
+ }
256
+ ).pipe((0, import_rxjs3.shareReplay)({ refCount: true, bufferSize: 1 }));
257
+ ethereumAccounts$.subscribe(() => {
258
+ console.count("[kheopskit] ethereumAccounts$ emit");
259
+ });
260
+
261
+ // src/api/polkadot/accounts.ts
262
+ var import_rxjs5 = require("rxjs");
263
+
264
+ // src/api/polkadot/wallets.ts
265
+ var import_lodash2 = require("lodash");
266
+ var import_pjs_signer = require("polkadot-api/pjs-signer");
267
+ var import_rxjs4 = require("rxjs");
268
+ var getInjectedWalletsIds = () => (0, import_pjs_signer.getInjectedExtensions)().map((name) => getWalletId("polkadot", name));
269
+ var polkadotInjectedWallets$ = new import_rxjs4.Observable(
270
+ (subscriber) => {
271
+ const enabledExtensions$ = new import_rxjs4.BehaviorSubject(/* @__PURE__ */ new Map());
272
+ const connect = async (walletId) => {
273
+ if (enabledExtensions$.value.has(walletId))
274
+ throw new Error(`Extension ${walletId} already connected`);
275
+ const { identifier } = parseWalletId(walletId);
276
+ const extension = await (0, import_pjs_signer.connectInjectedExtension)(identifier);
277
+ const newMap = new Map(enabledExtensions$.value);
278
+ newMap.set(walletId, extension);
279
+ enabledExtensions$.next(newMap);
280
+ store.addEnabledWalletId(walletId);
281
+ };
282
+ const disconnect = (walletId) => {
283
+ if (!enabledExtensions$.value.has(walletId))
284
+ throw new Error(`Extension ${walletId} is not connected`);
285
+ const newMap = new Map(enabledExtensions$.value);
286
+ newMap.delete(walletId);
287
+ enabledExtensions$.next(newMap);
288
+ store.removeEnabledWalletId(walletId);
289
+ };
290
+ const walletIds$ = (0, import_rxjs4.of)(0, 200, 500, 1e3).pipe(
291
+ (0, import_rxjs4.mergeMap)((time) => (0, import_rxjs4.timer)(time)),
292
+ (0, import_rxjs4.map)(() => getInjectedWalletsIds()),
293
+ (0, import_rxjs4.distinctUntilChanged)(import_lodash2.isEqual)
294
+ );
295
+ const subscription = (0, import_rxjs4.combineLatest)([walletIds$, enabledExtensions$]).pipe(
296
+ (0, import_rxjs4.map)(([walletIds, enabledExtensions]) => {
297
+ return walletIds.map((id) => {
298
+ const { identifier } = parseWalletId(id);
299
+ const extension = enabledExtensions.get(id);
300
+ return extension ? {
301
+ id,
302
+ platform: "polkadot",
303
+ name: identifier,
304
+ extensionId: identifier,
305
+ isEnabled: true,
306
+ extension,
307
+ disconnect: () => disconnect(id)
308
+ } : {
309
+ id,
310
+ platform: "polkadot",
311
+ name: identifier,
312
+ extensionId: identifier,
313
+ isEnabled: false,
314
+ connect: () => connect(id)
315
+ };
316
+ });
317
+ })
318
+ ).subscribe(subscriber);
319
+ return () => {
320
+ subscription.unsubscribe();
321
+ };
322
+ }
323
+ ).pipe((0, import_rxjs4.shareReplay)({ refCount: true, bufferSize: 1 }));
324
+ var polkadotWallets$ = polkadotInjectedWallets$;
325
+ polkadotWallets$.subscribe(() => {
326
+ console.count("[kheopskit] polkadotWallets$ emit");
327
+ });
328
+
329
+ // src/api/polkadot/accounts.ts
330
+ var getWalletAccounts$2 = (wallet) => {
331
+ if (!wallet.isEnabled) return (0, import_rxjs5.of)([]);
332
+ return new import_rxjs5.Observable((subscriber) => {
333
+ const getAccount = (account) => ({
334
+ id: getAccountId(wallet.id, account.address),
335
+ ...account,
336
+ platform: "polkadot",
337
+ walletName: wallet.name,
338
+ walletId: wallet.id
339
+ });
340
+ const unsubscribe = wallet.extension.subscribe((accounts) => {
341
+ subscriber.next(accounts.map(getAccount));
342
+ });
343
+ subscriber.next(wallet.extension.getAccounts().map(getAccount));
344
+ return () => {
345
+ return unsubscribe();
346
+ };
347
+ });
348
+ };
349
+ var polkadotAccounts$ = new import_rxjs5.Observable(
350
+ (subscriber) => {
351
+ const sub = polkadotWallets$.pipe(
352
+ (0, import_rxjs5.map)((wallets) => wallets.filter((w) => w.isEnabled)),
353
+ (0, import_rxjs5.switchMap)(
354
+ (wallets) => wallets.length ? (0, import_rxjs5.combineLatest)(wallets.map(getWalletAccounts$2)) : (0, import_rxjs5.of)([])
355
+ ),
356
+ (0, import_rxjs5.map)((accounts) => accounts.flat())
357
+ ).subscribe(subscriber);
358
+ return () => {
359
+ sub.unsubscribe();
360
+ };
361
+ }
362
+ ).pipe((0, import_rxjs5.shareReplay)({ refCount: true, bufferSize: 1 }));
363
+ polkadotAccounts$.subscribe(() => {
364
+ console.count("[kheopskit] polkadotAccounts$ emit");
365
+ });
366
+
367
+ // src/api/accounts.ts
368
+ var getAccounts$ = (config) => {
369
+ return new import_rxjs6.Observable((subscriber) => {
370
+ const observables = config.platforms.map(
371
+ (platform) => {
372
+ switch (platform) {
373
+ case "polkadot":
374
+ return polkadotAccounts$;
375
+ case "ethereum":
376
+ return ethereumAccounts$;
377
+ }
378
+ }
379
+ );
380
+ const accounts$ = observables.length ? (0, import_rxjs6.combineLatest)(observables).pipe((0, import_rxjs6.map)((accounts) => accounts.flat())) : (0, import_rxjs6.of)([]);
381
+ const sub = accounts$.subscribe(subscriber);
382
+ return () => {
383
+ sub.unsubscribe();
384
+ };
385
+ }).pipe((0, import_rxjs6.shareReplay)({ refCount: true, bufferSize: 1 }));
386
+ };
387
+
388
+ // src/api/config.ts
389
+ var DEFAULT_CONFIG = {
390
+ autoReconnect: true,
391
+ platforms: ["polkadot"]
392
+ };
393
+ var resolveConfig = (config) => {
394
+ return Object.assign({}, DEFAULT_CONFIG, config);
395
+ };
396
+
397
+ // src/api/wallets.ts
398
+ var import_rxjs7 = require("rxjs");
399
+ var autoReconnectWalletIds$ = store.observable.pipe(
400
+ (0, import_rxjs7.map)((s) => s.autoReconnect ?? []),
401
+ (0, import_rxjs7.take)(1),
402
+ (0, import_rxjs7.shareReplay)(1)
403
+ );
404
+ var getWallets$ = (config) => {
405
+ return new import_rxjs7.Observable((subscriber) => {
406
+ const observables = config.platforms.map(
407
+ (platform) => {
408
+ switch (platform) {
409
+ case "polkadot":
410
+ return polkadotWallets$;
411
+ case "ethereum":
412
+ return ethereumWallets$;
413
+ }
414
+ }
415
+ );
416
+ const wallets$ = observables.length ? (0, import_rxjs7.combineLatest)(observables).pipe((0, import_rxjs7.map)((wallets) => wallets.flat())) : (0, import_rxjs7.of)([]);
417
+ const subAutoReconnect = (0, import_rxjs7.combineLatest)([wallets$, autoReconnectWalletIds$]).pipe(
418
+ (0, import_rxjs7.filter)(([, walletIds]) => config.autoReconnect && !!walletIds?.length),
419
+ (0, import_rxjs7.mergeMap)(
420
+ ([wallets, walletIds]) => wallets.filter((wallet) => walletIds?.includes(wallet.id))
421
+ ),
422
+ (0, import_rxjs7.distinct)((w) => w.id)
423
+ ).subscribe(async (wallet) => {
424
+ if (wallet.isEnabled) {
425
+ console.warn("Wallet %s already connected", wallet.id);
426
+ return;
427
+ }
428
+ try {
429
+ await wallet.connect();
430
+ } catch (err) {
431
+ console.error("Failed to reconnect wallet %s", wallet.id, { err });
432
+ }
433
+ });
434
+ const subWallets = wallets$.subscribe(subscriber);
435
+ return () => {
436
+ subAutoReconnect.unsubscribe();
437
+ subWallets.unsubscribe();
438
+ };
439
+ }).pipe((0, import_rxjs7.shareReplay)({ refCount: true, bufferSize: 1 }));
440
+ };
441
+
442
+ // src/api/kheopskit.ts
443
+ var getKheopskit$ = (config) => {
444
+ const c = resolveConfig(config);
445
+ return new import_rxjs8.Observable(
446
+ (subscriber) => {
447
+ const subscription = (0, import_rxjs8.combineLatest)([getWallets$(c), getAccounts$(c)]).pipe((0, import_rxjs8.map)(([wallets, accounts]) => ({ wallets, accounts }))).subscribe(subscriber);
448
+ return () => {
449
+ subscription.unsubscribe();
450
+ };
451
+ }
452
+ ).pipe((0, import_rxjs8.shareReplay)({ bufferSize: 1, refCount: true }));
453
+ };
454
+ // Annotate the CommonJS export names for ESM import in node:
455
+ 0 && (module.exports = {
456
+ getKheopskit$
457
+ });
458
+ //# sourceMappingURL=index.js.map