@kheopskit/core 0.0.21 → 0.1.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/dist/index.d.mts +146 -5
- package/dist/index.d.ts +146 -5
- package/dist/index.js +1055 -369
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +1081 -389
- package/dist/index.mjs.map +1 -1
- package/package.json +22 -17
- package/CHANGELOG.md +0 -131
- package/src/api/accounts.ts +0 -43
- package/src/api/appKit.ts +0 -127
- package/src/api/config.ts +0 -13
- package/src/api/ethereum/accounts.ts +0 -207
- package/src/api/ethereum/wallets.ts +0 -117
- package/src/api/index.ts +0 -3
- package/src/api/kheopskit.ts +0 -45
- package/src/api/polkadot/accounts.ts +0 -159
- package/src/api/polkadot/wallets.ts +0 -123
- package/src/api/store.ts +0 -36
- package/src/api/types.ts +0 -111
- package/src/api/wallets.ts +0 -72
- package/src/index.ts +0 -1
- package/src/utils/WalletAccountId.ts +0 -22
- package/src/utils/WalletId.ts +0 -21
- package/src/utils/createStore.ts +0 -45
- package/src/utils/getAccountAddressType.ts +0 -11
- package/src/utils/getCachedObservable.ts +0 -12
- package/src/utils/getQuery.ts +0 -72
- package/src/utils/index.ts +0 -10
- package/src/utils/isEthereumAddress.ts +0 -4
- package/src/utils/isSs58Address.ts +0 -15
- package/src/utils/isValidAddress.ts +0 -8
- package/src/utils/isWalletPlatform.ts +0 -7
- package/src/utils/logObservable.ts +0 -21
- package/src/utils/polkadotExtensions.ts +0 -21
- package/src/utils/sleep.ts +0 -2
- package/src/utils/sortAccounts.ts +0 -36
- package/src/utils/sortWallets.ts +0 -14
- package/src/utils/throwAfter.ts +0 -6
- package/tsconfig.json +0 -10
package/CHANGELOG.md
DELETED
|
@@ -1,131 +0,0 @@
|
|
|
1
|
-
# @kheopskit/core
|
|
2
|
-
|
|
3
|
-
## 0.0.21
|
|
4
|
-
|
|
5
|
-
### Patch Changes
|
|
6
|
-
|
|
7
|
-
- [`b3d5f98`](https://github.com/kheopskit/kheopskit/commit/b3d5f989a33f9c13f56833f71494df5091c4930c) Thanks [@0xKheops](https://github.com/0xKheops)! - feat: theme variables
|
|
8
|
-
|
|
9
|
-
- [`cd73eb1`](https://github.com/kheopskit/kheopskit/commit/cd73eb18840a69b49238629051450eca26b9e275) Thanks [@0xKheops](https://github.com/0xKheops)! - fix: hide all wallets
|
|
10
|
-
|
|
11
|
-
## 0.0.20
|
|
12
|
-
|
|
13
|
-
### Patch Changes
|
|
14
|
-
|
|
15
|
-
- [`87bbfb0`](https://github.com/kheopskit/kheopskit/commit/87bbfb016b15bd0378a62943ccbe1403a6e5d07e) Thanks [@0xKheops](https://github.com/0xKheops)! - test: changeset2
|
|
16
|
-
|
|
17
|
-
## 0.0.19
|
|
18
|
-
|
|
19
|
-
### Patch Changes
|
|
20
|
-
|
|
21
|
-
- [`e68aaa5`](https://github.com/kheopskit/kheopskit/commit/e68aaa5019630b03660d1cb5e95a9188cf972ebf) Thanks [@0xKheops](https://github.com/0xKheops)! - test: changeset
|
|
22
|
-
|
|
23
|
-
## 0.0.18
|
|
24
|
-
|
|
25
|
-
### Patch Changes
|
|
26
|
-
|
|
27
|
-
- [`2e04a98`](https://github.com/kheopskit/kheopskit/commit/2e04a9893795e6aa43c942dee61443b4700c3294) Thanks [@0xKheops](https://github.com/0xKheops)! - fix: sync changesets
|
|
28
|
-
|
|
29
|
-
## 0.0.15
|
|
30
|
-
|
|
31
|
-
### Patch Changes
|
|
32
|
-
|
|
33
|
-
- [`a540c06`](https://github.com/kheopskit/kheopskit/commit/a540c06e90816656a1a21df3d95d0328bff78455) Thanks [@0xKheops](https://github.com/0xKheops)! - test: one more changeset
|
|
34
|
-
|
|
35
|
-
## 0.0.14
|
|
36
|
-
|
|
37
|
-
### Patch Changes
|
|
38
|
-
|
|
39
|
-
- [`b8242ab`](https://github.com/kheopskit/kheopskit/commit/b8242abd31a6512b16399dd44ca5e5d82f6c70bf) Thanks [@0xKheops](https://github.com/0xKheops)! - test: changeset
|
|
40
|
-
|
|
41
|
-
## 0.0.13
|
|
42
|
-
|
|
43
|
-
### Patch Changes
|
|
44
|
-
|
|
45
|
-
- [`5e07091`](https://github.com/kheopskit/kheopskit/commit/5e070910229cb9202f5d2f1869bfb16c5180d273) Thanks [@0xKheops](https://github.com/0xKheops)! - test: changeset
|
|
46
|
-
|
|
47
|
-
## 0.0.12
|
|
48
|
-
|
|
49
|
-
### Patch Changes
|
|
50
|
-
|
|
51
|
-
- [`2f34f0c`](https://github.com/kheopskit/kheopskit/commit/2f34f0c16c7866a4187b4474a45f93e2ad07f5c7) Thanks [@0xKheops](https://github.com/0xKheops)! - test: changeset
|
|
52
|
-
|
|
53
|
-
## 0.0.11
|
|
54
|
-
|
|
55
|
-
### Patch Changes
|
|
56
|
-
|
|
57
|
-
- [`9d4f86e`](https://github.com/kheopskit/kheopskit/commit/9d4f86e7632843fad089ce930b209aee9b9e2b41) Thanks [@0xKheops](https://github.com/0xKheops)! - test changeset
|
|
58
|
-
|
|
59
|
-
## 0.0.10
|
|
60
|
-
|
|
61
|
-
### Patch Changes
|
|
62
|
-
|
|
63
|
-
- [`c7dea32`](https://github.com/kheopskit/kheopskit/commit/c7dea32e2921716cef82b53e1960c3cdb4c8e5ae) Thanks [@0xKheops](https://github.com/0xKheops)! - test changeset
|
|
64
|
-
|
|
65
|
-
## 0.0.9
|
|
66
|
-
|
|
67
|
-
### Patch Changes
|
|
68
|
-
|
|
69
|
-
- [`961f6a3`](https://github.com/kheopskit/kheopskit/commit/961f6a371c73d4065e9157c95ca4d996012098e7) Thanks [@0xKheops](https://github.com/0xKheops)! - test changeset
|
|
70
|
-
|
|
71
|
-
## 0.0.8
|
|
72
|
-
|
|
73
|
-
### Patch Changes
|
|
74
|
-
|
|
75
|
-
- [`b76185f`](https://github.com/kheopskit/kheopskit/commit/b76185f99a78c5a82a8b9aead65b0708f17b0bd5) Thanks [@0xKheops](https://github.com/0xKheops)! - test changeset
|
|
76
|
-
|
|
77
|
-
## 0.0.7
|
|
78
|
-
|
|
79
|
-
### Patch Changes
|
|
80
|
-
|
|
81
|
-
- [`07f8406`](https://github.com/kheopskit/kheopskit/commit/07f8406d3f176e6a8b3b06fe16396bf2b6b1db88) Thanks [@0xKheops](https://github.com/0xKheops)! - test changeset
|
|
82
|
-
|
|
83
|
-
## 0.0.6
|
|
84
|
-
|
|
85
|
-
### Patch Changes
|
|
86
|
-
|
|
87
|
-
- [`5319326`](https://github.com/kheopskit/kheopskit/commit/53193262e80fec9e242986f818d7f7b53f92357a) Thanks [@0xKheops](https://github.com/0xKheops)! - test changeset
|
|
88
|
-
|
|
89
|
-
## 0.0.5
|
|
90
|
-
|
|
91
|
-
### Patch Changes
|
|
92
|
-
|
|
93
|
-
- [`d865842`](https://github.com/kheopskit/kheopskit/commit/d86584236e51730e82baaa9068604fa7e703e9c2) Thanks [@0xKheops](https://github.com/0xKheops)! - test changeset
|
|
94
|
-
|
|
95
|
-
## 0.0.4
|
|
96
|
-
|
|
97
|
-
### Patch Changes
|
|
98
|
-
|
|
99
|
-
- [`4a61939`](https://github.com/kheopskit/kheopskit/commit/4a61939b9a5c4ea6fb119a0427704a5fc684343e) Thanks [@0xKheops](https://github.com/0xKheops)! - test changeset
|
|
100
|
-
|
|
101
|
-
## 0.0.3
|
|
102
|
-
|
|
103
|
-
### Patch Changes
|
|
104
|
-
|
|
105
|
-
- [`7a0ea89`](https://github.com/kheopskit/kheopskit/commit/7a0ea890982570ae89934fead69d319fff46dd98) Thanks [@0xKheops](https://github.com/0xKheops)! - test changeset
|
|
106
|
-
|
|
107
|
-
## 0.0.2
|
|
108
|
-
|
|
109
|
-
### Patch Changes
|
|
110
|
-
|
|
111
|
-
- [`806b8d3`](https://github.com/kheopskit/kheopskit/commit/806b8d394ba7c2576c76d9de72a15d7927bcff9e) - update peer deps
|
|
112
|
-
|
|
113
|
-
## 0.0.1
|
|
114
|
-
|
|
115
|
-
### Patch Changes
|
|
116
|
-
|
|
117
|
-
- [`e1cebed`](https://github.com/kheopskit/kheopskit/commit/e1cebed92d303f041070e0ae146ee34d9eb717bd) - refactor property names
|
|
118
|
-
|
|
119
|
-
- [`e1cebed`](https://github.com/kheopskit/kheopskit/commit/e1cebed92d303f041070e0ae146ee34d9eb717bd) - initial alpha release
|
|
120
|
-
|
|
121
|
-
## 0.0.1-alpha.1
|
|
122
|
-
|
|
123
|
-
### Patch Changes
|
|
124
|
-
|
|
125
|
-
- [`4f378f9`](https://github.com/0xKheops/kheopskit-alpha/commit/4f378f9b61e555b7b66ef3bfaf107ab8e6ac62b1) - refactor property names
|
|
126
|
-
|
|
127
|
-
## 0.0.1-alpha.0
|
|
128
|
-
|
|
129
|
-
### Patch Changes
|
|
130
|
-
|
|
131
|
-
- [`3216d3b`](https://github.com/0xKheops/kheopskit-alpha/commit/3216d3b4ca1f2fadbebe9a4275e7b864ac89d222) - initial alpha release
|
package/src/api/accounts.ts
DELETED
|
@@ -1,43 +0,0 @@
|
|
|
1
|
-
import { sortAccounts } from "@/utils/sortAccounts";
|
|
2
|
-
import { Observable, combineLatest, map, of, shareReplay } from "rxjs";
|
|
3
|
-
import { getEthereumAccounts$ } from "./ethereum/accounts";
|
|
4
|
-
import { getPolkadotAccounts$ } from "./polkadot/accounts";
|
|
5
|
-
import type { KheopskitConfig, Wallet, WalletAccount } from "./types";
|
|
6
|
-
|
|
7
|
-
export const getAccounts$ = (
|
|
8
|
-
config: KheopskitConfig,
|
|
9
|
-
wallets: Observable<Wallet[]>,
|
|
10
|
-
) => {
|
|
11
|
-
return new Observable<WalletAccount[]>((subscriber) => {
|
|
12
|
-
const sources = config.platforms.map<Observable<WalletAccount[]>>(
|
|
13
|
-
(platform) => {
|
|
14
|
-
switch (platform) {
|
|
15
|
-
case "polkadot":
|
|
16
|
-
return getPolkadotAccounts$(
|
|
17
|
-
wallets.pipe(
|
|
18
|
-
map((w) => w.filter((w) => w.platform === "polkadot")),
|
|
19
|
-
),
|
|
20
|
-
);
|
|
21
|
-
case "ethereum":
|
|
22
|
-
return getEthereumAccounts$(
|
|
23
|
-
wallets.pipe(
|
|
24
|
-
map((w) => w.filter((w) => w.platform === "ethereum")),
|
|
25
|
-
),
|
|
26
|
-
);
|
|
27
|
-
}
|
|
28
|
-
},
|
|
29
|
-
);
|
|
30
|
-
|
|
31
|
-
const accounts$ = sources.length
|
|
32
|
-
? combineLatest(sources).pipe(
|
|
33
|
-
map((accounts) => accounts.flat().sort(sortAccounts)),
|
|
34
|
-
)
|
|
35
|
-
: of([]);
|
|
36
|
-
|
|
37
|
-
const sub = accounts$.subscribe(subscriber);
|
|
38
|
-
|
|
39
|
-
return () => {
|
|
40
|
-
sub.unsubscribe();
|
|
41
|
-
};
|
|
42
|
-
}).pipe(shareReplay({ refCount: true, bufferSize: 1 }));
|
|
43
|
-
};
|
package/src/api/appKit.ts
DELETED
|
@@ -1,127 +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({});
|
|
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
|
-
themeMode: walletConnect.themeMode,
|
|
43
|
-
themeVariables: walletConnect.themeVariables,
|
|
44
|
-
universalProviderConfigOverride: {
|
|
45
|
-
methods: {
|
|
46
|
-
polkadot: ["polkadot_signTransaction", "polkadot_signMessage"],
|
|
47
|
-
},
|
|
48
|
-
},
|
|
49
|
-
allWallets: "HIDE",
|
|
50
|
-
debug: config.debug,
|
|
51
|
-
allowUnsupportedChain: true,
|
|
52
|
-
});
|
|
53
|
-
|
|
54
|
-
const status$ = new BehaviorSubject({
|
|
55
|
-
isPolkadotConnected: false,
|
|
56
|
-
isEthereumConnected: false,
|
|
57
|
-
});
|
|
58
|
-
|
|
59
|
-
const unsubProviders = appKit.subscribeProviders((providers) => {
|
|
60
|
-
status$.next({
|
|
61
|
-
isPolkadotConnected: !!providers.polkadot,
|
|
62
|
-
isEthereumConnected: !!providers.eip155,
|
|
63
|
-
});
|
|
64
|
-
});
|
|
65
|
-
|
|
66
|
-
const polkadotWallet$ = appKit.chainNamespaces.includes("polkadot")
|
|
67
|
-
? status$.pipe(
|
|
68
|
-
map((s) => s.isPolkadotConnected),
|
|
69
|
-
distinctUntilChanged(),
|
|
70
|
-
map((isConnected): PolkadotAppKitWallet => {
|
|
71
|
-
const walletInfo = appKit.getWalletInfo();
|
|
72
|
-
|
|
73
|
-
return {
|
|
74
|
-
id: getWalletId("polkadot", "walletconnect"),
|
|
75
|
-
platform: "polkadot",
|
|
76
|
-
type: "appKit",
|
|
77
|
-
appKit, // todo maybe we dont want to expose the appKit instance
|
|
78
|
-
name: walletInfo?.name ?? "WalletConnect",
|
|
79
|
-
icon: walletInfo?.icon ?? WALLET_CONNECT_ICON,
|
|
80
|
-
connect: async () => {
|
|
81
|
-
if (!isConnected) await appKit.open();
|
|
82
|
-
},
|
|
83
|
-
disconnect: () => {
|
|
84
|
-
if (isConnected) appKit.disconnect();
|
|
85
|
-
},
|
|
86
|
-
isConnected,
|
|
87
|
-
};
|
|
88
|
-
}),
|
|
89
|
-
)
|
|
90
|
-
: of(undefined);
|
|
91
|
-
|
|
92
|
-
const ethereumWallet$ = appKit.chainNamespaces.includes("eip155")
|
|
93
|
-
? status$.pipe(
|
|
94
|
-
map((s) => s.isEthereumConnected),
|
|
95
|
-
distinctUntilChanged(),
|
|
96
|
-
map((isConnected): EthereumAppKitWallet => {
|
|
97
|
-
const walletInfo = appKit.getWalletInfo();
|
|
98
|
-
|
|
99
|
-
return {
|
|
100
|
-
id: getWalletId("ethereum", "walletconnect"),
|
|
101
|
-
platform: "ethereum",
|
|
102
|
-
type: "appKit",
|
|
103
|
-
appKit,
|
|
104
|
-
name: walletInfo?.name ?? "WalletConnect",
|
|
105
|
-
icon: walletInfo?.icon ?? WALLET_CONNECT_ICON,
|
|
106
|
-
connect: () => appKit.open(),
|
|
107
|
-
disconnect: () => appKit.disconnect(),
|
|
108
|
-
isConnected,
|
|
109
|
-
};
|
|
110
|
-
}),
|
|
111
|
-
)
|
|
112
|
-
: of(undefined);
|
|
113
|
-
|
|
114
|
-
const sub = combineLatest({
|
|
115
|
-
polkadot: polkadotWallet$,
|
|
116
|
-
ethereum: ethereumWallet$,
|
|
117
|
-
}).subscribe(subscriber);
|
|
118
|
-
|
|
119
|
-
return () => {
|
|
120
|
-
sub.unsubscribe();
|
|
121
|
-
unsubProviders();
|
|
122
|
-
};
|
|
123
|
-
}).pipe(shareReplay({ refCount: true, bufferSize: 1 }));
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
return cachedAppKit;
|
|
127
|
-
};
|
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
package/src/api/kheopskit.ts
DELETED
|
@@ -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
|
-
};
|