@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/src/api/appKit.ts DELETED
@@ -1,122 +0,0 @@
1
- import { getWalletId } from "@/utils/WalletId";
2
- import { createAppKit } from "@reown/appkit/core";
3
- import {
4
- BehaviorSubject,
5
- Observable,
6
- combineLatest,
7
- distinctUntilChanged,
8
- map,
9
- of,
10
- shareReplay,
11
- } from "rxjs";
12
- import type {
13
- EthereumAppKitWallet,
14
- KheopskitConfig,
15
- PolkadotAppKitWallet,
16
- } from "./types";
17
-
18
- const WALLET_CONNECT_ICON =
19
- "data:image/svg+xml;base64,PHN2ZyBmaWxsPSJub25lIiBoZWlnaHQ9IjQwMCIgdmlld0JveD0iMCAwIDQwMCA0MDAiIHdpZHRoPSI0MDAiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgeG1sbnM6eGxpbms9Imh0dHA6Ly93d3cudzMub3JnLzE5OTkveGxpbmsiPjxjbGlwUGF0aCBpZD0iYSI+PHBhdGggZD0ibTAgMGg0MDB2NDAwaC00MDB6Ii8+PC9jbGlwUGF0aD48ZyBjbGlwLXBhdGg9InVybCgjYSkiPjxjaXJjbGUgY3g9IjIwMCIgY3k9IjIwMCIgZmlsbD0iIzMzOTZmZiIgcj0iMTk5LjUiIHN0cm9rZT0iIzY2YjFmZiIvPjxwYXRoIGQ9Im0xMjIuNTE5IDE0OC45NjVjNDIuNzkxLTQxLjcyOSAxMTIuMTcxLTQxLjcyOSAxNTQuOTYyIDBsNS4xNSA1LjAyMmMyLjE0IDIuMDg2IDIuMTQgNS40NjkgMCA3LjU1NWwtMTcuNjE3IDE3LjE4Yy0xLjA3IDEuMDQzLTIuODA0IDEuMDQzLTMuODc0IDBsLTcuMDg3LTYuOTExYy0yOS44NTMtMjkuMTExLTc4LjI1My0yOS4xMTEtMTA4LjEwNiAwbC03LjU5IDcuNDAxYy0xLjA3IDEuMDQzLTIuODA0IDEuMDQzLTMuODc0IDBsLTE3LjYxNy0xNy4xOGMtMi4xNC0yLjA4Ni0yLjE0LTUuNDY5IDAtNy41NTV6bTE5MS4zOTcgMzUuNTI5IDE1LjY3OSAxNS4yOWMyLjE0IDIuMDg2IDIuMTQgNS40NjkgMCA3LjU1NWwtNzAuNyA2OC45NDRjLTIuMTM5IDIuMDg3LTUuNjA4IDIuMDg3LTcuNzQ4IDBsLTUwLjE3OC00OC45MzFjLS41MzUtLjUyMi0xLjQwMi0uNTIyLTEuOTM3IDBsLTUwLjE3OCA0OC45MzFjLTIuMTM5IDIuMDg3LTUuNjA4IDIuMDg3LTcuNzQ4IDBsLTcwLjcwMTUtNjguOTQ1Yy0yLjEzOTYtMi4wODYtMi4xMzk2LTUuNDY5IDAtNy41NTVsMTUuNjc5NS0xNS4yOWMyLjEzOTYtMi4wODYgNS42MDg1LTIuMDg2IDcuNzQ4MSAwbDUwLjE3ODkgNDguOTMyYy41MzUuNTIyIDEuNDAyLjUyMiAxLjkzNyAwbDUwLjE3Ny00OC45MzJjMi4xMzktMi4wODcgNS42MDgtMi4wODcgNy43NDggMGw1MC4xNzkgNDguOTMyYy41MzUuNTIyIDEuNDAyLjUyMiAxLjkzNyAwbDUwLjE3OS00OC45MzFjMi4xMzktMi4wODcgNS42MDgtMi4wODcgNy43NDggMHoiIGZpbGw9IiNmZmYiLz48L2c+PC9zdmc+";
20
-
21
- type AppKitWallets = {
22
- polkadot?: PolkadotAppKitWallet;
23
- ethereum?: EthereumAppKitWallet;
24
- };
25
-
26
- // once it exists, appKit object should never be recreated
27
- let cachedAppKit: Observable<AppKitWallets> | null = null;
28
-
29
- export const getAppKitWallets$ = (
30
- config: KheopskitConfig,
31
- ): Observable<AppKitWallets> => {
32
- if (!config.walletConnect) return of({}); // of(null).pipe(shareReplay({ refCount: true, bufferSize: 1 }));
33
-
34
- const walletConnect = config.walletConnect;
35
-
36
- if (!cachedAppKit) {
37
- cachedAppKit = new Observable<AppKitWallets>((subscriber) => {
38
- const appKit = createAppKit({
39
- projectId: walletConnect.projectId,
40
- metadata: walletConnect.metadata,
41
- networks: walletConnect.networks,
42
- universalProviderConfigOverride: {
43
- methods: {
44
- polkadot: ["polkadot_signTransaction", "polkadot_signMessage"],
45
- },
46
- },
47
- });
48
-
49
- const status$ = new BehaviorSubject({
50
- isPolkadotConnected: false,
51
- isEthereumConnected: false,
52
- });
53
-
54
- const unsubProviders = appKit.subscribeProviders((providers) => {
55
- status$.next({
56
- isPolkadotConnected: !!providers.polkadot,
57
- isEthereumConnected: !!providers.eip155,
58
- });
59
- });
60
-
61
- const polkadotWallet$ = appKit.chainNamespaces.includes("polkadot")
62
- ? status$.pipe(
63
- map((s) => s.isPolkadotConnected),
64
- distinctUntilChanged(),
65
- map((isConnected): PolkadotAppKitWallet => {
66
- const walletInfo = appKit.getWalletInfo();
67
-
68
- return {
69
- id: getWalletId("polkadot", "walletconnect"),
70
- platform: "polkadot",
71
- type: "appKit",
72
- appKit, // todo maybe we dont want to expose the appKit instance
73
- name: walletInfo?.name ?? "WalletConnect",
74
- icon: walletInfo?.icon ?? WALLET_CONNECT_ICON,
75
- connect: async () => {
76
- if (!isConnected) await appKit.open();
77
- },
78
- disconnect: () => {
79
- if (isConnected) appKit.disconnect();
80
- },
81
- isConnected,
82
- };
83
- }),
84
- )
85
- : of(undefined);
86
-
87
- const ethereumWallet$ = appKit.chainNamespaces.includes("eip155")
88
- ? status$.pipe(
89
- map((s) => s.isEthereumConnected),
90
- distinctUntilChanged(),
91
- map((isConnected): EthereumAppKitWallet => {
92
- const walletInfo = appKit.getWalletInfo();
93
-
94
- return {
95
- id: getWalletId("ethereum", "walletconnect"),
96
- platform: "ethereum",
97
- type: "appKit",
98
- appKit,
99
- name: walletInfo?.name ?? "WalletConnect",
100
- icon: walletInfo?.icon ?? WALLET_CONNECT_ICON,
101
- connect: () => appKit.open(),
102
- disconnect: () => appKit.disconnect(),
103
- isConnected,
104
- };
105
- }),
106
- )
107
- : of(undefined);
108
-
109
- const sub = combineLatest({
110
- polkadot: polkadotWallet$,
111
- ethereum: ethereumWallet$,
112
- }).subscribe(subscriber);
113
-
114
- return () => {
115
- sub.unsubscribe();
116
- unsubProviders();
117
- };
118
- }).pipe(shareReplay({ refCount: true, bufferSize: 1 }));
119
- }
120
-
121
- return cachedAppKit;
122
- };
package/src/api/config.ts DELETED
@@ -1,13 +0,0 @@
1
- import type { KheopskitConfig } from "./types";
2
-
3
- const DEFAULT_CONFIG: KheopskitConfig = {
4
- autoReconnect: true,
5
- platforms: ["polkadot"],
6
- debug: false,
7
- };
8
-
9
- export const resolveConfig = (
10
- config: Partial<KheopskitConfig> | undefined,
11
- ): KheopskitConfig => {
12
- return Object.assign({}, DEFAULT_CONFIG, config);
13
- };
@@ -1,207 +0,0 @@
1
- import type {
2
- EthereumAccount,
3
- EthereumAppKitWallet,
4
- EthereumInjectedWallet,
5
- EthereumWallet,
6
- } from "@/api/types";
7
- import { getWalletAccountId } from "@/utils";
8
- import { getCachedObservable$ } from "@/utils/getCachedObservable";
9
- import type UniversalProvider from "@walletconnect/universal-provider";
10
- import {
11
- Observable,
12
- ReplaySubject,
13
- combineLatest,
14
- distinctUntilChanged,
15
- map,
16
- of,
17
- shareReplay,
18
- switchMap,
19
- } from "rxjs";
20
- import {
21
- type EIP1193Provider,
22
- createWalletClient,
23
- custom,
24
- getAddress,
25
- } from "viem";
26
-
27
- const getInjectedWalletAccounts$ = (
28
- wallet: EthereumInjectedWallet,
29
- ): Observable<EthereumAccount[]> => {
30
- if (!wallet.isConnected) return of([]);
31
-
32
- return getCachedObservable$(`accounts:${wallet.id}`, () =>
33
- new Observable<EthereumAccount[]>((subscriber) => {
34
- const getAccount = (address: string, i: number): EthereumAccount => {
35
- // console.log("[Injected] createWalletClient", address);
36
- const client = createWalletClient({
37
- account: address as `0x${string}`,
38
- transport: custom(wallet.provider as EIP1193Provider),
39
- });
40
-
41
- return {
42
- id: getWalletAccountId(wallet.id, address),
43
- platform: "ethereum",
44
- client,
45
- address: getAddress(address),
46
- walletName: wallet.name,
47
- walletId: wallet.id,
48
- isWalletDefault: i === 0,
49
- };
50
- };
51
-
52
- const handleAccountsChanged = (addresses: string[]) => {
53
- subscriber.next(addresses.map(getAccount));
54
- };
55
-
56
- // subscribe to changes
57
- wallet.provider.on("accountsChanged", handleAccountsChanged);
58
-
59
- // initial value
60
- wallet.provider
61
- .request({ method: "eth_accounts" })
62
- .then((addresses: string[]) => {
63
- subscriber.next(addresses.map(getAccount));
64
- })
65
- .catch((err) => {
66
- console.error("Failed to get accounts", err);
67
- subscriber.next([]);
68
- });
69
-
70
- return () => {
71
- wallet.provider.removeListener(
72
- "accountsChanged",
73
- handleAccountsChanged,
74
- );
75
- };
76
- }).pipe(shareReplay({ refCount: true, bufferSize: 1 })),
77
- );
78
- };
79
-
80
- const wrapWalletConnectProvider = (
81
- provider: EIP1193Provider,
82
- sessionTopic: string,
83
- caipNetworkId: string,
84
- ): EIP1193Provider => {
85
- return new Proxy(provider, {
86
- get(target, prop, receiver) {
87
- if (prop !== "request") return Reflect.get(target, prop, receiver);
88
-
89
- // biome-ignore lint/suspicious/noExplicitAny: <explanation>
90
- return (args: any) => {
91
- // console.log("request", { target, prop, receiver, caipNetworkId, args });
92
-
93
- if (args && typeof args === "object" && args.method) {
94
- if (!args.topic) args.topic = sessionTopic;
95
- if (!args.chainId) args.chainId = caipNetworkId;
96
- }
97
- return target.request(args);
98
- };
99
- },
100
- });
101
- };
102
-
103
- const getAppKitAccounts$ = (
104
- wallet: EthereumAppKitWallet,
105
- ): Observable<EthereumAccount[]> => {
106
- const account = wallet.appKit.getAccount("eip155");
107
- const provider = wallet.appKit.getProvider<UniversalProvider>("eip155");
108
-
109
- if (
110
- !wallet.isConnected ||
111
- !wallet.appKit ||
112
- !account?.allAccounts.length ||
113
- !provider?.session
114
- )
115
- return of([]);
116
-
117
- return getCachedObservable$("accounts:appKit", () =>
118
- new Observable<EthereumAccount[]>((subscriber) => {
119
- const caipNetworkId$ = new ReplaySubject<string>(1);
120
-
121
- const handleChainChanged = (chainId: unknown) => {
122
- caipNetworkId$.next(`eip155:${chainId}`);
123
- };
124
-
125
- provider.on("chainChanged", handleChainChanged);
126
- provider.request({ method: "eth_chainId" }).then(handleChainChanged);
127
-
128
- const sub = caipNetworkId$
129
- .pipe(
130
- distinctUntilChanged(),
131
- map((caipNetworkId) =>
132
- custom(
133
- wrapWalletConnectProvider(
134
- provider as EIP1193Provider,
135
- // biome-ignore lint/style/noNonNullAssertion: <explanation>
136
- provider.session!.topic,
137
- caipNetworkId,
138
- ),
139
- ),
140
- ),
141
- map((transport) =>
142
- account.allAccounts.map((acc, i): EthereumAccount => {
143
- // console.log("[AppKit] createWalletClient", acc);
144
- const client = createWalletClient({
145
- account: acc.address as `0x${string}`,
146
- transport,
147
- });
148
-
149
- return {
150
- id: getWalletAccountId(wallet.id, acc.address),
151
- platform: "ethereum",
152
- walletName: wallet.name,
153
- walletId: wallet.id,
154
- address: acc.address as `0x${string}`,
155
- client,
156
- isWalletDefault: i === 0,
157
- };
158
- }),
159
- ),
160
- )
161
- .subscribe(subscriber);
162
-
163
- return () => {
164
- provider.off("chainChanged", handleChainChanged);
165
- sub.unsubscribe();
166
- };
167
- }).pipe(shareReplay({ refCount: true, bufferSize: 1 })),
168
- );
169
- };
170
-
171
- export const getEthereumAccounts$ = (
172
- ethereumWallets: Observable<EthereumWallet[]>,
173
- ) =>
174
- new Observable<EthereumAccount[]>((subscriber) => {
175
- const sub = ethereumWallets
176
- .pipe(
177
- map((wallets) => wallets.filter((w) => w.isConnected)),
178
- switchMap((wallets) => {
179
- return wallets.length
180
- ? combineLatest([
181
- ...wallets
182
- .filter((w) => w.type === "injected")
183
- .map(getInjectedWalletAccounts$),
184
- ...wallets
185
- .filter((w) => w.type === "appKit")
186
- .map(getAppKitAccounts$),
187
- // todo appkit
188
- ])
189
- : of([]);
190
- }),
191
- map((accounts) => accounts.flat()),
192
- distinctUntilChanged(isSameAccountsList),
193
- )
194
- .subscribe(subscriber);
195
-
196
- return () => {
197
- sub.unsubscribe();
198
- };
199
- }).pipe(
200
- // logObservable("ethereumAccounts$", true),
201
- shareReplay({ refCount: true, bufferSize: 1 }),
202
- );
203
-
204
- const isSameAccountsList = (a: EthereumAccount[], b: EthereumAccount[]) => {
205
- if (a.length !== b.length) return false;
206
- return a.every((account, i) => account.id === b[i]?.id);
207
- };
@@ -1,117 +0,0 @@
1
- import { store } from "@/api/store";
2
- import type {
3
- EthereumInjectedWallet,
4
- EthereumWallet,
5
- KheopskitConfig,
6
- } from "@/api/types";
7
- import { type WalletId, getWalletId } from "@/utils/WalletId";
8
- import { type EIP6963ProviderDetail, createStore } from "mipd";
9
- import {
10
- BehaviorSubject,
11
- Observable,
12
- combineLatest,
13
- map,
14
- shareReplay,
15
- } from "rxjs";
16
- import type { EIP1193Provider } from "viem";
17
- import { getAppKitWallets$ } from "../appKit";
18
-
19
- const providersDetails$ = new Observable<EIP6963ProviderDetail[]>(
20
- (subscriber) => {
21
- const store = createStore();
22
-
23
- const unsubscribe = store.subscribe((providerDetails) => {
24
- subscriber.next(providerDetails as EIP6963ProviderDetail[]);
25
- });
26
-
27
- const providerDetails = store.getProviders();
28
-
29
- subscriber.next(providerDetails as EIP6963ProviderDetail[]);
30
-
31
- return () => {
32
- unsubscribe();
33
- store.destroy();
34
- };
35
- },
36
- ).pipe(shareReplay({ refCount: true, bufferSize: 1 }));
37
-
38
- const ethereumInjectedWallets$ = new Observable<EthereumInjectedWallet[]>(
39
- (subscriber) => {
40
- const enabledWalletIds$ = new BehaviorSubject<Set<WalletId>>(new Set());
41
-
42
- const connectWallet = async (
43
- walletId: WalletId,
44
- provider: EIP1193Provider,
45
- ) => {
46
- if (enabledWalletIds$.value.has(walletId))
47
- throw new Error(`Extension ${walletId} already connected`);
48
-
49
- await provider.request({
50
- method: "eth_requestAccounts",
51
- });
52
-
53
- const newSet = new Set(enabledWalletIds$.value);
54
- newSet.add(walletId);
55
- enabledWalletIds$.next(newSet);
56
-
57
- store.addEnabledWalletId(walletId);
58
- };
59
-
60
- const disconnectWallet = async (walletId: WalletId) => {
61
- if (!enabledWalletIds$.value.has(walletId))
62
- throw new Error(`Extension ${walletId} is not connected`);
63
- const newSet = new Set(enabledWalletIds$.value);
64
- newSet.delete(walletId);
65
- enabledWalletIds$.next(newSet);
66
-
67
- store.removeEnabledWalletId(walletId);
68
- };
69
-
70
- const sub = combineLatest([providersDetails$, enabledWalletIds$])
71
- .pipe(
72
- map(([providerDetails, enabledWalletIds]) => {
73
- return providerDetails.map((pd): EthereumInjectedWallet => {
74
- const walletId = getWalletId("ethereum", pd.info.rdns);
75
- const provider = pd.provider as EIP1193Provider;
76
-
77
- return {
78
- platform: "ethereum",
79
- type: "injected",
80
- id: walletId,
81
- name: pd.info.name,
82
- icon: pd.info.icon,
83
- provider,
84
- isConnected: enabledWalletIds.has(walletId),
85
- providerId: pd.info.rdns,
86
- connect: () => connectWallet(walletId, provider),
87
- disconnect: () => disconnectWallet(walletId),
88
- };
89
- });
90
- }),
91
- )
92
- .subscribe(subscriber);
93
-
94
- return () => {
95
- sub.unsubscribe();
96
- };
97
- },
98
- ).pipe(shareReplay({ refCount: true, bufferSize: 1 }));
99
-
100
- export const getEthereumWallets$ = (config: KheopskitConfig) => {
101
- return new Observable<EthereumWallet[]>((subscriber) => {
102
- const subscription = combineLatest([
103
- ethereumInjectedWallets$,
104
- getAppKitWallets$(config)?.pipe(map((w) => w.ethereum)),
105
- ])
106
- .pipe(
107
- map(([injectedWallets, appKitWallet]) =>
108
- appKitWallet ? [...injectedWallets, appKitWallet] : injectedWallets,
109
- ),
110
- )
111
- .subscribe(subscriber);
112
-
113
- return () => {
114
- subscription.unsubscribe();
115
- };
116
- }).pipe(shareReplay({ refCount: true, bufferSize: 1 }));
117
- };
package/src/api/index.ts DELETED
@@ -1,3 +0,0 @@
1
- export * from "./kheopskit";
2
- export * from "./types";
3
- export { resolveConfig } from "./config";
@@ -1,45 +0,0 @@
1
- import { logObservable } from "@/utils/logObservable";
2
- import {
3
- Observable,
4
- combineLatest,
5
- map,
6
- shareReplay,
7
- throttleTime,
8
- } from "rxjs";
9
- import { getAccounts$ } from "./accounts";
10
- import { resolveConfig } from "./config";
11
- import type { KheopskitConfig, Wallet, WalletAccount } from "./types";
12
- import { getWallets$ } from "./wallets";
13
-
14
- export type { KheopskitConfig } from "./types";
15
-
16
- export type KheopskitState = {
17
- wallets: Wallet[];
18
- accounts: WalletAccount[];
19
- config: KheopskitConfig;
20
- };
21
-
22
- export const getKheopskit$ = (config?: Partial<KheopskitConfig>) => {
23
- const kc = resolveConfig(config);
24
-
25
- console.debug("[kheopskit] config", kc);
26
-
27
- return new Observable<KheopskitState>((subscriber) => {
28
- const wallets$ = getWallets$(kc);
29
-
30
- const subscription = combineLatest({
31
- wallets: wallets$,
32
- accounts: getAccounts$(kc, wallets$),
33
- })
34
- .pipe(map(({ wallets, accounts }) => ({ config: kc, wallets, accounts })))
35
- .subscribe(subscriber);
36
-
37
- return () => {
38
- subscription.unsubscribe();
39
- };
40
- }).pipe(
41
- throttleTime(50, undefined, { leading: true, trailing: true }),
42
- logObservable("kheopskit$", { enabled: kc.debug, printValue: true }),
43
- shareReplay({ bufferSize: 1, refCount: true }),
44
- );
45
- };
@@ -1,159 +0,0 @@
1
- import type {
2
- PolkadotAccount,
3
- PolkadotAppKitWallet,
4
- PolkadotInjectedWallet,
5
- PolkadotWallet,
6
- } from "@/api/types";
7
- import { getWalletAccountId } from "@/utils";
8
- import type { AppKit } from "@reown/appkit/core";
9
- import type UniversalProvider from "@walletconnect/universal-provider";
10
- import {
11
- type InjectedExtension,
12
- type InjectedPolkadotAccount,
13
- getPolkadotSignerFromPjs,
14
- } from "polkadot-api/pjs-signer";
15
- import {
16
- Observable,
17
- combineLatest,
18
- distinctUntilChanged,
19
- map,
20
- of,
21
- shareReplay,
22
- switchMap,
23
- } from "rxjs";
24
-
25
- const getInjectedWalletAccounts$ = (
26
- wallet: PolkadotInjectedWallet,
27
- ): Observable<PolkadotAccount[]> => {
28
- if (!wallet.isConnected) return of([]);
29
-
30
- return new Observable<PolkadotAccount[]>((subscriber) => {
31
- const getAccount = (account: InjectedPolkadotAccount): PolkadotAccount => ({
32
- id: getWalletAccountId(wallet.id, account.address),
33
- ...account,
34
- platform: "polkadot",
35
- walletName: wallet.name,
36
- walletId: wallet.id,
37
- });
38
-
39
- const extension = wallet.extension as InjectedExtension;
40
-
41
- // subscribe to changes
42
- const unsubscribe = extension.subscribe((accounts) => {
43
- subscriber.next(accounts.map(getAccount));
44
- });
45
-
46
- // initial value
47
- subscriber.next(extension.getAccounts().map(getAccount));
48
-
49
- return () => {
50
- return unsubscribe();
51
- };
52
- });
53
- };
54
-
55
- const getAppKitPolkadotSigner = (appKit: AppKit, address: string) => {
56
- const provider = appKit.getProvider<UniversalProvider>("polkadot");
57
- if (!provider) throw new Error("No provider found");
58
- if (!provider.session) throw new Error("No session found");
59
-
60
- return getPolkadotSignerFromPjs(
61
- address,
62
- (transactionPayload) => {
63
- if (!provider.session) throw new Error("No session found");
64
-
65
- return provider.client.request({
66
- topic: provider.session.topic,
67
- chainId: `polkadot:${transactionPayload.genesisHash.substring(2, 34)}`,
68
- request: {
69
- method: "polkadot_signTransaction",
70
- params: {
71
- address,
72
- transactionPayload,
73
- },
74
- },
75
- });
76
- },
77
- async ({ address, data }) => {
78
- if (!provider.session) throw new Error("No session found");
79
- const networks = appKit.getCaipNetworks("polkadot");
80
- const chainId = networks[0]?.caipNetworkId;
81
- if (!chainId) throw new Error("No chainId found");
82
-
83
- return provider.client.request({
84
- topic: provider.session.topic,
85
- chainId,
86
- request: {
87
- method: "polkadot_signMessage",
88
- params: {
89
- address,
90
- message: data,
91
- },
92
- },
93
- });
94
- },
95
- );
96
- };
97
-
98
- const getAppKitAccounts$ = (wallet: PolkadotAppKitWallet) => {
99
- const account = wallet.appKit.getAccount("polkadot");
100
- const provider = wallet.appKit.getProvider<UniversalProvider>("polkadot");
101
-
102
- if (
103
- !wallet.isConnected ||
104
- !wallet.appKit ||
105
- !account?.allAccounts.length ||
106
- !provider?.session
107
- )
108
- return of([]);
109
-
110
- return of(
111
- account.allAccounts.map(
112
- (acc): PolkadotAccount => ({
113
- id: getWalletAccountId(wallet.id, acc.address),
114
- platform: "polkadot",
115
- walletName: wallet.name,
116
- walletId: wallet.id,
117
- address: acc.address,
118
- polkadotSigner: getAppKitPolkadotSigner(wallet.appKit, acc.address),
119
- genesisHash: null,
120
- name: `${wallet.name} Polkadot`,
121
- type: "sr25519",
122
- }),
123
- ),
124
- );
125
- };
126
-
127
- export const getPolkadotAccounts$ = (
128
- polkadotWallets$: Observable<PolkadotWallet[]>,
129
- ) =>
130
- new Observable<PolkadotAccount[]>((subscriber) => {
131
- const sub = polkadotWallets$
132
- .pipe(
133
- map((wallets) => wallets.filter((w) => w.isConnected)),
134
- switchMap((wallets) =>
135
- wallets.length
136
- ? combineLatest([
137
- ...wallets
138
- .filter((w) => w.type === "injected")
139
- .map(getInjectedWalletAccounts$),
140
- ...wallets
141
- .filter((w) => w.type === "appKit")
142
- .map(getAppKitAccounts$),
143
- ])
144
- : of([]),
145
- ),
146
- map((accounts) => accounts.flat()),
147
- distinctUntilChanged(isSameAccountsList),
148
- )
149
- .subscribe(subscriber);
150
-
151
- return () => {
152
- sub.unsubscribe();
153
- };
154
- }).pipe(shareReplay({ refCount: true, bufferSize: 1 }));
155
-
156
- const isSameAccountsList = (a: PolkadotAccount[], b: PolkadotAccount[]) => {
157
- if (a.length !== b.length) return false;
158
- return a.every((account, i) => account.id === b[i]?.id);
159
- };