@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
|
@@ -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
|
-
};
|
|
@@ -1,123 +0,0 @@
|
|
|
1
|
-
import { store } from "@/api/store";
|
|
2
|
-
import type {
|
|
3
|
-
KheopskitConfig,
|
|
4
|
-
PolkadotInjectedWallet,
|
|
5
|
-
PolkadotWallet,
|
|
6
|
-
} from "@/api/types";
|
|
7
|
-
import { type WalletId, getWalletId, parseWalletId } from "@/utils/WalletId";
|
|
8
|
-
import { POLKADOT_EXTENSIONS } from "@/utils/polkadotExtensions";
|
|
9
|
-
import { isEqual } from "lodash";
|
|
10
|
-
import {
|
|
11
|
-
type InjectedExtension,
|
|
12
|
-
connectInjectedExtension,
|
|
13
|
-
getInjectedExtensions,
|
|
14
|
-
} from "polkadot-api/pjs-signer";
|
|
15
|
-
import {
|
|
16
|
-
BehaviorSubject,
|
|
17
|
-
Observable,
|
|
18
|
-
combineLatest,
|
|
19
|
-
distinctUntilChanged,
|
|
20
|
-
map,
|
|
21
|
-
mergeMap,
|
|
22
|
-
of,
|
|
23
|
-
shareReplay,
|
|
24
|
-
timer,
|
|
25
|
-
} from "rxjs";
|
|
26
|
-
import { getAppKitWallets$ } from "../appKit";
|
|
27
|
-
|
|
28
|
-
const getInjectedWalletsIds = () =>
|
|
29
|
-
getInjectedExtensions().map((name) => getWalletId("polkadot", name));
|
|
30
|
-
|
|
31
|
-
export const polkadotInjectedWallets$ = new Observable<
|
|
32
|
-
PolkadotInjectedWallet[]
|
|
33
|
-
>((subscriber) => {
|
|
34
|
-
const enabledExtensions$ = new BehaviorSubject<
|
|
35
|
-
Map<WalletId, InjectedExtension>
|
|
36
|
-
>(new Map());
|
|
37
|
-
|
|
38
|
-
const connect = async (walletId: WalletId) => {
|
|
39
|
-
if (enabledExtensions$.value.has(walletId))
|
|
40
|
-
throw new Error(`Extension ${walletId} already connected`);
|
|
41
|
-
const { identifier } = parseWalletId(walletId);
|
|
42
|
-
const extension = await connectInjectedExtension(identifier);
|
|
43
|
-
|
|
44
|
-
const newMap = new Map(enabledExtensions$.value);
|
|
45
|
-
newMap.set(walletId, extension);
|
|
46
|
-
enabledExtensions$.next(newMap);
|
|
47
|
-
|
|
48
|
-
store.addEnabledWalletId(walletId);
|
|
49
|
-
};
|
|
50
|
-
|
|
51
|
-
const disconnect = (walletId: WalletId) => {
|
|
52
|
-
if (!enabledExtensions$.value.has(walletId))
|
|
53
|
-
throw new Error(`Extension ${walletId} is not connected`);
|
|
54
|
-
|
|
55
|
-
const newMap = new Map(enabledExtensions$.value);
|
|
56
|
-
newMap.delete(walletId);
|
|
57
|
-
enabledExtensions$.next(newMap);
|
|
58
|
-
|
|
59
|
-
store.removeEnabledWalletId(walletId);
|
|
60
|
-
};
|
|
61
|
-
|
|
62
|
-
const walletIds$ = of(0, 200, 500, 1000) // poll for wallets that inject after page load
|
|
63
|
-
.pipe(
|
|
64
|
-
mergeMap((time) => timer(time)),
|
|
65
|
-
map(() => getInjectedWalletsIds()),
|
|
66
|
-
distinctUntilChanged<WalletId[]>(isEqual),
|
|
67
|
-
);
|
|
68
|
-
|
|
69
|
-
const subscription = combineLatest([walletIds$, enabledExtensions$])
|
|
70
|
-
.pipe(
|
|
71
|
-
map(([walletIds, enabledExtensions]) => {
|
|
72
|
-
return walletIds.map((id): PolkadotInjectedWallet => {
|
|
73
|
-
const { identifier } = parseWalletId(id);
|
|
74
|
-
const extension = enabledExtensions.get(id);
|
|
75
|
-
const extInfo = POLKADOT_EXTENSIONS[identifier];
|
|
76
|
-
|
|
77
|
-
return {
|
|
78
|
-
id,
|
|
79
|
-
type: "injected",
|
|
80
|
-
platform: "polkadot",
|
|
81
|
-
name: extInfo?.name ?? identifier,
|
|
82
|
-
icon: extInfo?.icon ?? "",
|
|
83
|
-
extensionId: identifier,
|
|
84
|
-
extension,
|
|
85
|
-
isConnected: !!extension,
|
|
86
|
-
connect: () => connect(id),
|
|
87
|
-
disconnect: () => disconnect(id),
|
|
88
|
-
};
|
|
89
|
-
});
|
|
90
|
-
}),
|
|
91
|
-
)
|
|
92
|
-
.subscribe(subscriber);
|
|
93
|
-
|
|
94
|
-
return () => {
|
|
95
|
-
// console.log("Unsubscribing from polkadotInjectedWallets$");
|
|
96
|
-
subscription.unsubscribe();
|
|
97
|
-
};
|
|
98
|
-
}).pipe(
|
|
99
|
-
// logObservable("polkadotInjectedWallets$"),
|
|
100
|
-
shareReplay({ refCount: true, bufferSize: 1 }),
|
|
101
|
-
);
|
|
102
|
-
|
|
103
|
-
export const getPolkadotWallets$ = (config: KheopskitConfig) => {
|
|
104
|
-
return new Observable<PolkadotWallet[]>((subscriber) => {
|
|
105
|
-
const subscription = combineLatest([
|
|
106
|
-
polkadotInjectedWallets$,
|
|
107
|
-
getAppKitWallets$(config)?.pipe(map((w) => w.polkadot)),
|
|
108
|
-
])
|
|
109
|
-
.pipe(
|
|
110
|
-
map(([injectedWallets, appKitWallet]) =>
|
|
111
|
-
appKitWallet ? [...injectedWallets, appKitWallet] : injectedWallets,
|
|
112
|
-
),
|
|
113
|
-
)
|
|
114
|
-
.subscribe(subscriber);
|
|
115
|
-
|
|
116
|
-
return () => {
|
|
117
|
-
subscription.unsubscribe();
|
|
118
|
-
};
|
|
119
|
-
}).pipe(
|
|
120
|
-
// logObservable("getPolkadotWallets$"),
|
|
121
|
-
shareReplay({ refCount: true, bufferSize: 1 }),
|
|
122
|
-
);
|
|
123
|
-
};
|
package/src/api/store.ts
DELETED
|
@@ -1,36 +0,0 @@
|
|
|
1
|
-
import { type WalletId, parseWalletId } from "@/utils/WalletId";
|
|
2
|
-
import { createStore } from "@/utils/createStore";
|
|
3
|
-
import { uniq } from "lodash";
|
|
4
|
-
|
|
5
|
-
const LOCAL_STORAGE_KEY = "kheopskit";
|
|
6
|
-
|
|
7
|
-
export type KheopskitStoreData = {
|
|
8
|
-
autoReconnect?: WalletId[];
|
|
9
|
-
};
|
|
10
|
-
|
|
11
|
-
const DEFAULT_SETTINGS: KheopskitStoreData = {};
|
|
12
|
-
|
|
13
|
-
const storage = createStore(LOCAL_STORAGE_KEY, DEFAULT_SETTINGS);
|
|
14
|
-
|
|
15
|
-
export const addEnabledWalletId = (walletId: WalletId) => {
|
|
16
|
-
parseWalletId(walletId); // validate walletId
|
|
17
|
-
storage.mutate((prev) => ({
|
|
18
|
-
...prev,
|
|
19
|
-
autoReconnect: uniq((prev.autoReconnect ?? []).concat(walletId)),
|
|
20
|
-
}));
|
|
21
|
-
};
|
|
22
|
-
|
|
23
|
-
export const removeEnabledWalletId = (walletId: WalletId) => {
|
|
24
|
-
storage.mutate((prev) => ({
|
|
25
|
-
...prev,
|
|
26
|
-
autoReconnect: uniq(
|
|
27
|
-
(prev.autoReconnect ?? []).filter((id) => id !== walletId),
|
|
28
|
-
),
|
|
29
|
-
}));
|
|
30
|
-
};
|
|
31
|
-
|
|
32
|
-
export const store = {
|
|
33
|
-
observable: storage.observable,
|
|
34
|
-
addEnabledWalletId,
|
|
35
|
-
removeEnabledWalletId,
|
|
36
|
-
};
|
package/src/api/types.ts
DELETED
|
@@ -1,111 +0,0 @@
|
|
|
1
|
-
import type { WalletAccountId } from "@/utils";
|
|
2
|
-
import type { WalletId } from "@/utils/WalletId";
|
|
3
|
-
import type { AppKit, ThemeMode, ThemeVariables } from "@reown/appkit/core";
|
|
4
|
-
import type { AppKitNetwork } from "@reown/appkit/networks";
|
|
5
|
-
import type { Metadata } from "@walletconnect/universal-provider";
|
|
6
|
-
import type {
|
|
7
|
-
InjectedExtension,
|
|
8
|
-
InjectedPolkadotAccount,
|
|
9
|
-
} from "polkadot-api/pjs-signer";
|
|
10
|
-
import type {
|
|
11
|
-
Account,
|
|
12
|
-
CustomTransport,
|
|
13
|
-
EIP1193Provider,
|
|
14
|
-
WalletClient,
|
|
15
|
-
} from "viem";
|
|
16
|
-
|
|
17
|
-
export type KheopskitConfig = {
|
|
18
|
-
autoReconnect: boolean;
|
|
19
|
-
platforms: WalletPlatform[];
|
|
20
|
-
walletConnect?: {
|
|
21
|
-
projectId: string;
|
|
22
|
-
metadata: Metadata;
|
|
23
|
-
/** Defaults to wss://relay.walletconnect.com */
|
|
24
|
-
relayUrl?: string;
|
|
25
|
-
/**
|
|
26
|
-
* list of CAIP-13 ids of polkadot-sdk chains
|
|
27
|
-
* see https://docs.reown.com/advanced/multichain/polkadot/dapp-integration-guide#walletconnect-code%2Fcomponent-setup
|
|
28
|
-
*/
|
|
29
|
-
networks: [AppKitNetwork, ...AppKitNetwork[]];
|
|
30
|
-
themeMode?: ThemeMode;
|
|
31
|
-
themeVariables?: ThemeVariables;
|
|
32
|
-
};
|
|
33
|
-
debug: boolean;
|
|
34
|
-
};
|
|
35
|
-
|
|
36
|
-
export type PolkadotInjectedWallet = {
|
|
37
|
-
id: WalletId;
|
|
38
|
-
platform: "polkadot";
|
|
39
|
-
type: "injected";
|
|
40
|
-
extensionId: string;
|
|
41
|
-
extension: InjectedExtension | undefined;
|
|
42
|
-
name: string;
|
|
43
|
-
icon: string;
|
|
44
|
-
isConnected: boolean;
|
|
45
|
-
connect: () => Promise<void>;
|
|
46
|
-
disconnect: () => void;
|
|
47
|
-
};
|
|
48
|
-
|
|
49
|
-
export type PolkadotAppKitWallet = {
|
|
50
|
-
id: WalletId;
|
|
51
|
-
platform: "polkadot";
|
|
52
|
-
type: "appKit";
|
|
53
|
-
appKit: AppKit;
|
|
54
|
-
name: string;
|
|
55
|
-
icon: string;
|
|
56
|
-
isConnected: boolean;
|
|
57
|
-
connect: () => Promise<void>;
|
|
58
|
-
disconnect: () => void;
|
|
59
|
-
};
|
|
60
|
-
|
|
61
|
-
export type PolkadotWallet = PolkadotInjectedWallet | PolkadotAppKitWallet;
|
|
62
|
-
|
|
63
|
-
export type EthereumInjectedWallet = {
|
|
64
|
-
platform: "ethereum";
|
|
65
|
-
type: "injected";
|
|
66
|
-
id: WalletId;
|
|
67
|
-
providerId: string;
|
|
68
|
-
provider: EIP1193Provider;
|
|
69
|
-
name: string;
|
|
70
|
-
icon: string;
|
|
71
|
-
isConnected: boolean;
|
|
72
|
-
connect: () => Promise<void>;
|
|
73
|
-
disconnect: () => void;
|
|
74
|
-
};
|
|
75
|
-
|
|
76
|
-
export type EthereumAppKitWallet = {
|
|
77
|
-
platform: "ethereum";
|
|
78
|
-
type: "appKit";
|
|
79
|
-
id: WalletId;
|
|
80
|
-
appKit: AppKit;
|
|
81
|
-
name: string;
|
|
82
|
-
icon: string;
|
|
83
|
-
isConnected: boolean;
|
|
84
|
-
connect: () => Promise<void>;
|
|
85
|
-
disconnect: () => void;
|
|
86
|
-
};
|
|
87
|
-
|
|
88
|
-
export type EthereumWallet = EthereumInjectedWallet | EthereumAppKitWallet;
|
|
89
|
-
|
|
90
|
-
export type Wallet = PolkadotWallet | EthereumWallet;
|
|
91
|
-
|
|
92
|
-
export type WalletPlatform = Wallet["platform"];
|
|
93
|
-
|
|
94
|
-
export type PolkadotAccount = InjectedPolkadotAccount & {
|
|
95
|
-
id: WalletAccountId;
|
|
96
|
-
platform: "polkadot";
|
|
97
|
-
walletName: string;
|
|
98
|
-
walletId: string;
|
|
99
|
-
};
|
|
100
|
-
|
|
101
|
-
export type EthereumAccount = {
|
|
102
|
-
id: WalletAccountId;
|
|
103
|
-
platform: "ethereum";
|
|
104
|
-
client: WalletClient<CustomTransport, undefined, Account, undefined>; // let consumer knows chain is unknown
|
|
105
|
-
address: `0x${string}`;
|
|
106
|
-
walletName: string;
|
|
107
|
-
walletId: string;
|
|
108
|
-
isWalletDefault: boolean;
|
|
109
|
-
};
|
|
110
|
-
|
|
111
|
-
export type WalletAccount = PolkadotAccount | EthereumAccount;
|
package/src/api/wallets.ts
DELETED
|
@@ -1,72 +0,0 @@
|
|
|
1
|
-
import { sortWallets } from "@/utils/sortWallets";
|
|
2
|
-
import {
|
|
3
|
-
Observable,
|
|
4
|
-
combineLatest,
|
|
5
|
-
distinct,
|
|
6
|
-
filter,
|
|
7
|
-
map,
|
|
8
|
-
mergeMap,
|
|
9
|
-
of,
|
|
10
|
-
shareReplay,
|
|
11
|
-
take,
|
|
12
|
-
} from "rxjs";
|
|
13
|
-
import { getEthereumWallets$ } from "./ethereum/wallets";
|
|
14
|
-
import { getPolkadotWallets$ } from "./polkadot/wallets";
|
|
15
|
-
import { store } from "./store";
|
|
16
|
-
import type { KheopskitConfig, Wallet } from "./types";
|
|
17
|
-
|
|
18
|
-
// lock the list of wallets to auto reconnect on first call
|
|
19
|
-
const autoReconnectWalletIds$ = store.observable.pipe(
|
|
20
|
-
map((s) => s.autoReconnect ?? []),
|
|
21
|
-
take(1),
|
|
22
|
-
shareReplay(1),
|
|
23
|
-
);
|
|
24
|
-
|
|
25
|
-
export const getWallets$ = (config: KheopskitConfig) => {
|
|
26
|
-
return new Observable<Wallet[]>((subscriber) => {
|
|
27
|
-
const observables = config.platforms.map<Observable<Wallet[]>>(
|
|
28
|
-
(platform) => {
|
|
29
|
-
switch (platform) {
|
|
30
|
-
case "polkadot":
|
|
31
|
-
return getPolkadotWallets$(config);
|
|
32
|
-
case "ethereum":
|
|
33
|
-
return getEthereumWallets$(config);
|
|
34
|
-
}
|
|
35
|
-
},
|
|
36
|
-
);
|
|
37
|
-
|
|
38
|
-
const wallets$ = observables.length
|
|
39
|
-
? combineLatest(observables).pipe(
|
|
40
|
-
map((wallets) => wallets.flat().sort(sortWallets)),
|
|
41
|
-
)
|
|
42
|
-
: of([]);
|
|
43
|
-
|
|
44
|
-
const subAutoReconnect = combineLatest([wallets$, autoReconnectWalletIds$])
|
|
45
|
-
.pipe(
|
|
46
|
-
filter(([, walletIds]) => config.autoReconnect && !!walletIds?.length),
|
|
47
|
-
mergeMap(([wallets, walletIds]) =>
|
|
48
|
-
wallets.filter((wallet) => walletIds?.includes(wallet.id)),
|
|
49
|
-
),
|
|
50
|
-
distinct((w) => w.id),
|
|
51
|
-
)
|
|
52
|
-
.subscribe(async (wallet) => {
|
|
53
|
-
if (wallet.isConnected) {
|
|
54
|
-
console.warn("Wallet %s already connected", wallet.id);
|
|
55
|
-
return;
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
try {
|
|
59
|
-
await wallet.connect();
|
|
60
|
-
} catch (err) {
|
|
61
|
-
console.error("Failed to reconnect wallet %s", wallet.id, { err });
|
|
62
|
-
}
|
|
63
|
-
});
|
|
64
|
-
|
|
65
|
-
const subWallets = wallets$.subscribe(subscriber);
|
|
66
|
-
|
|
67
|
-
return () => {
|
|
68
|
-
subAutoReconnect.unsubscribe();
|
|
69
|
-
subWallets.unsubscribe();
|
|
70
|
-
};
|
|
71
|
-
}).pipe(shareReplay({ refCount: true, bufferSize: 1 }));
|
|
72
|
-
};
|
package/src/index.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export * from "./api";
|
|
@@ -1,22 +0,0 @@
|
|
|
1
|
-
import type { SS58String } from "polkadot-api";
|
|
2
|
-
|
|
3
|
-
import { isValidAddress } from "./isValidAddress";
|
|
4
|
-
|
|
5
|
-
export type WalletAccountId = string;
|
|
6
|
-
|
|
7
|
-
export const getWalletAccountId = (
|
|
8
|
-
walletId: string,
|
|
9
|
-
address: SS58String,
|
|
10
|
-
): WalletAccountId => {
|
|
11
|
-
if (!walletId) throw new Error("Missing walletId");
|
|
12
|
-
if (!isValidAddress(address)) throw new Error("Invalid address");
|
|
13
|
-
return `${walletId}::${address}`;
|
|
14
|
-
};
|
|
15
|
-
|
|
16
|
-
export const parseWalletAccountId = (accountId: string) => {
|
|
17
|
-
if (!accountId) throw new Error("Invalid walletAccountId");
|
|
18
|
-
const [walletId, address] = accountId.split("::");
|
|
19
|
-
if (!walletId) throw new Error("Missing walletId");
|
|
20
|
-
if (!address || !isValidAddress(address)) throw new Error("Invalid address");
|
|
21
|
-
return { walletId, address };
|
|
22
|
-
};
|
package/src/utils/WalletId.ts
DELETED
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
import type { WalletPlatform } from "@/api/types";
|
|
2
|
-
import { isWalletPlatform } from "./isWalletPlatform";
|
|
3
|
-
|
|
4
|
-
export type WalletId = string;
|
|
5
|
-
|
|
6
|
-
export const getWalletId = (
|
|
7
|
-
platform: WalletPlatform,
|
|
8
|
-
identifier: string,
|
|
9
|
-
): WalletId => {
|
|
10
|
-
if (!isWalletPlatform(platform)) throw new Error("Invalid platform");
|
|
11
|
-
if (!identifier) throw new Error("Invalid name");
|
|
12
|
-
return `${platform}:${identifier}`;
|
|
13
|
-
};
|
|
14
|
-
|
|
15
|
-
export const parseWalletId = (walletId: string) => {
|
|
16
|
-
if (!walletId) throw new Error("Invalid walletId");
|
|
17
|
-
const [platform, identifier] = walletId.split(":");
|
|
18
|
-
if (!isWalletPlatform(platform)) throw new Error("Invalid platform");
|
|
19
|
-
if (!identifier) throw new Error("Invalid address");
|
|
20
|
-
return { platform, identifier };
|
|
21
|
-
};
|
package/src/utils/createStore.ts
DELETED
|
@@ -1,45 +0,0 @@
|
|
|
1
|
-
import { BehaviorSubject, filter, fromEvent, map } from "rxjs";
|
|
2
|
-
|
|
3
|
-
export const createStore = <T>(key: string, defaultValue: T) => {
|
|
4
|
-
const subject = new BehaviorSubject<T>(getStoredData(key, defaultValue));
|
|
5
|
-
|
|
6
|
-
// Cross-tab sync via 'storage' event (won't fire if key is updated from same tab)
|
|
7
|
-
fromEvent<StorageEvent>(window, "storage")
|
|
8
|
-
.pipe(
|
|
9
|
-
filter((event) => event.key === key),
|
|
10
|
-
map((event) => parseData(event.newValue, defaultValue)),
|
|
11
|
-
)
|
|
12
|
-
.subscribe((newValue) => subject.next(newValue));
|
|
13
|
-
|
|
14
|
-
const update = (val: T) => {
|
|
15
|
-
setStoredData(key, val);
|
|
16
|
-
subject.next(val);
|
|
17
|
-
};
|
|
18
|
-
|
|
19
|
-
return {
|
|
20
|
-
observable: subject.asObservable(),
|
|
21
|
-
set: (val: T) => update(val),
|
|
22
|
-
mutate: (transform: (prev: T) => T) =>
|
|
23
|
-
update(transform(subject.getValue())),
|
|
24
|
-
get: () => structuredClone(subject.getValue()),
|
|
25
|
-
};
|
|
26
|
-
};
|
|
27
|
-
|
|
28
|
-
const parseData = <T>(str: string | null, defaultValue: T): T => {
|
|
29
|
-
try {
|
|
30
|
-
if (str) return JSON.parse(str);
|
|
31
|
-
} catch {
|
|
32
|
-
// invalid data
|
|
33
|
-
}
|
|
34
|
-
return defaultValue;
|
|
35
|
-
};
|
|
36
|
-
|
|
37
|
-
const getStoredData = <T>(key: string, defaultValue: T): T => {
|
|
38
|
-
const str = localStorage.getItem(key);
|
|
39
|
-
return parseData(str, defaultValue);
|
|
40
|
-
};
|
|
41
|
-
|
|
42
|
-
const setStoredData = <T>(key: string, val: T) => {
|
|
43
|
-
const str = JSON.stringify(val);
|
|
44
|
-
localStorage.setItem(key, str);
|
|
45
|
-
};
|
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
import { isEthereumAddress } from "./isEthereumAddress";
|
|
2
|
-
import { isSs58Address } from "./isSs58Address";
|
|
3
|
-
|
|
4
|
-
export type AccountAddressType = "ss58" | "ethereum";
|
|
5
|
-
|
|
6
|
-
export const getAccountAddressType = (address: string): AccountAddressType => {
|
|
7
|
-
if (address.startsWith("0x")) {
|
|
8
|
-
if (isEthereumAddress(address)) return "ethereum";
|
|
9
|
-
} else if (isSs58Address(address)) return "ss58";
|
|
10
|
-
throw new Error("Invalid address");
|
|
11
|
-
};
|
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
import type { Observable } from "rxjs";
|
|
2
|
-
|
|
3
|
-
const CACHE = new Map<string, Observable<unknown>>();
|
|
4
|
-
|
|
5
|
-
export const getCachedObservable$ = <T, Obs = Observable<T>>(
|
|
6
|
-
key: string,
|
|
7
|
-
create: () => Obs,
|
|
8
|
-
): Obs => {
|
|
9
|
-
if (!CACHE.has(key)) CACHE.set(key, create() as Observable<unknown>);
|
|
10
|
-
|
|
11
|
-
return CACHE.get(key) as Obs;
|
|
12
|
-
};
|
package/src/utils/getQuery.ts
DELETED
|
@@ -1,72 +0,0 @@
|
|
|
1
|
-
import { isEqual } from "lodash";
|
|
2
|
-
import {
|
|
3
|
-
BehaviorSubject,
|
|
4
|
-
Observable,
|
|
5
|
-
distinctUntilChanged,
|
|
6
|
-
shareReplay,
|
|
7
|
-
} from "rxjs";
|
|
8
|
-
|
|
9
|
-
import { getCachedObservable$ } from "./getCachedObservable";
|
|
10
|
-
|
|
11
|
-
export type QueryStatus = "loading" | "loaded" | "error";
|
|
12
|
-
|
|
13
|
-
export type QueryResult<
|
|
14
|
-
T,
|
|
15
|
-
S extends QueryStatus = "loading" | "loaded" | "error",
|
|
16
|
-
> = S extends "loading"
|
|
17
|
-
? { status: "loading"; data: T | undefined; error: undefined }
|
|
18
|
-
: S extends "loaded"
|
|
19
|
-
? { status: "loaded"; data: T; error: undefined }
|
|
20
|
-
: { status: "error"; data: undefined; error: unknown };
|
|
21
|
-
|
|
22
|
-
type QueryOptions<T> = {
|
|
23
|
-
queryKey: string;
|
|
24
|
-
queryFn: () => Promise<T>;
|
|
25
|
-
defaultValue?: T;
|
|
26
|
-
refreshInterval?: number;
|
|
27
|
-
};
|
|
28
|
-
|
|
29
|
-
export const getQuery$ = <T>({
|
|
30
|
-
queryKey,
|
|
31
|
-
queryFn,
|
|
32
|
-
defaultValue,
|
|
33
|
-
refreshInterval,
|
|
34
|
-
}: QueryOptions<T>): Observable<QueryResult<T>> => {
|
|
35
|
-
return getCachedObservable$(queryKey, () =>
|
|
36
|
-
new Observable<QueryResult<T>>((subscriber) => {
|
|
37
|
-
const result = new BehaviorSubject<QueryResult<T>>({
|
|
38
|
-
status: "loading",
|
|
39
|
-
data: defaultValue,
|
|
40
|
-
error: undefined,
|
|
41
|
-
});
|
|
42
|
-
|
|
43
|
-
// result subscription
|
|
44
|
-
const sub = result
|
|
45
|
-
.pipe(distinctUntilChanged<QueryResult<T>>(isEqual))
|
|
46
|
-
.subscribe(subscriber);
|
|
47
|
-
|
|
48
|
-
let timeout: ReturnType<typeof setTimeout> | undefined = undefined;
|
|
49
|
-
|
|
50
|
-
// fetch result subscription
|
|
51
|
-
const run = () => {
|
|
52
|
-
queryFn()
|
|
53
|
-
.then((data) => {
|
|
54
|
-
result.next({ status: "loaded", data, error: undefined });
|
|
55
|
-
})
|
|
56
|
-
.catch((error) => {
|
|
57
|
-
result.next({ status: "error", data: undefined, error });
|
|
58
|
-
})
|
|
59
|
-
.finally(() => {
|
|
60
|
-
if (refreshInterval) timeout = setTimeout(run, refreshInterval);
|
|
61
|
-
});
|
|
62
|
-
};
|
|
63
|
-
|
|
64
|
-
run();
|
|
65
|
-
|
|
66
|
-
return () => {
|
|
67
|
-
sub.unsubscribe();
|
|
68
|
-
if (timeout) clearTimeout(timeout);
|
|
69
|
-
};
|
|
70
|
-
}).pipe(shareReplay({ refCount: true, bufferSize: 1 })),
|
|
71
|
-
);
|
|
72
|
-
};
|