@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/dist/index.d.mts +6 -4
- package/dist/index.d.ts +6 -4
- package/dist/index.js +74 -39
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +122 -89
- package/dist/index.mjs.map +1 -1
- package/package.json +7 -3
- package/CHANGELOG.md +0 -123
- package/src/api/accounts.ts +0 -43
- package/src/api/appKit.ts +0 -122
- 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 -109
- 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,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,109 +0,0 @@
|
|
|
1
|
-
import type { WalletAccountId } from "@/utils";
|
|
2
|
-
import type { WalletId } from "@/utils/WalletId";
|
|
3
|
-
import type { AppKit } 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
|
-
};
|
|
31
|
-
debug: boolean;
|
|
32
|
-
};
|
|
33
|
-
|
|
34
|
-
export type PolkadotInjectedWallet = {
|
|
35
|
-
id: WalletId;
|
|
36
|
-
platform: "polkadot";
|
|
37
|
-
type: "injected";
|
|
38
|
-
extensionId: string;
|
|
39
|
-
extension: InjectedExtension | undefined;
|
|
40
|
-
name: string;
|
|
41
|
-
icon: string;
|
|
42
|
-
isConnected: boolean;
|
|
43
|
-
connect: () => Promise<void>;
|
|
44
|
-
disconnect: () => void;
|
|
45
|
-
};
|
|
46
|
-
|
|
47
|
-
export type PolkadotAppKitWallet = {
|
|
48
|
-
id: WalletId;
|
|
49
|
-
platform: "polkadot";
|
|
50
|
-
type: "appKit";
|
|
51
|
-
appKit: AppKit;
|
|
52
|
-
name: string;
|
|
53
|
-
icon: string;
|
|
54
|
-
isConnected: boolean;
|
|
55
|
-
connect: () => Promise<void>;
|
|
56
|
-
disconnect: () => void;
|
|
57
|
-
};
|
|
58
|
-
|
|
59
|
-
export type PolkadotWallet = PolkadotInjectedWallet | PolkadotAppKitWallet;
|
|
60
|
-
|
|
61
|
-
export type EthereumInjectedWallet = {
|
|
62
|
-
platform: "ethereum";
|
|
63
|
-
type: "injected";
|
|
64
|
-
id: WalletId;
|
|
65
|
-
providerId: string;
|
|
66
|
-
provider: EIP1193Provider;
|
|
67
|
-
name: string;
|
|
68
|
-
icon: string;
|
|
69
|
-
isConnected: boolean;
|
|
70
|
-
connect: () => Promise<void>;
|
|
71
|
-
disconnect: () => void;
|
|
72
|
-
};
|
|
73
|
-
|
|
74
|
-
export type EthereumAppKitWallet = {
|
|
75
|
-
platform: "ethereum";
|
|
76
|
-
type: "appKit";
|
|
77
|
-
id: WalletId;
|
|
78
|
-
appKit: AppKit;
|
|
79
|
-
name: string;
|
|
80
|
-
icon: string;
|
|
81
|
-
isConnected: boolean;
|
|
82
|
-
connect: () => Promise<void>;
|
|
83
|
-
disconnect: () => void;
|
|
84
|
-
};
|
|
85
|
-
|
|
86
|
-
export type EthereumWallet = EthereumInjectedWallet | EthereumAppKitWallet;
|
|
87
|
-
|
|
88
|
-
export type Wallet = PolkadotWallet | EthereumWallet;
|
|
89
|
-
|
|
90
|
-
export type WalletPlatform = Wallet["platform"];
|
|
91
|
-
|
|
92
|
-
export type PolkadotAccount = InjectedPolkadotAccount & {
|
|
93
|
-
id: WalletAccountId;
|
|
94
|
-
platform: "polkadot";
|
|
95
|
-
walletName: string;
|
|
96
|
-
walletId: string;
|
|
97
|
-
};
|
|
98
|
-
|
|
99
|
-
export type EthereumAccount = {
|
|
100
|
-
id: WalletAccountId;
|
|
101
|
-
platform: "ethereum";
|
|
102
|
-
client: WalletClient<CustomTransport, undefined, Account, undefined>; // let consumer knows chain is unknown
|
|
103
|
-
address: `0x${string}`;
|
|
104
|
-
walletName: string;
|
|
105
|
-
walletId: string;
|
|
106
|
-
isWalletDefault: boolean;
|
|
107
|
-
};
|
|
108
|
-
|
|
109
|
-
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
|
-
};
|
package/src/utils/index.ts
DELETED
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
// export all modules from this folder, except exports.ts
|
|
2
|
-
export * from "./createStore";
|
|
3
|
-
export * from "./getAccountAddressType";
|
|
4
|
-
export * from "./WalletAccountId";
|
|
5
|
-
export * from "./isEthereumAddress";
|
|
6
|
-
export * from "./isSs58Address";
|
|
7
|
-
export * from "./isValidAddress";
|
|
8
|
-
export * from "./sleep";
|
|
9
|
-
export * from "./throwAfter";
|
|
10
|
-
export * from "./isWalletPlatform";
|
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
import { AccountId, type SS58String } from "polkadot-api";
|
|
2
|
-
|
|
3
|
-
const accountIdEncoder = AccountId().enc;
|
|
4
|
-
|
|
5
|
-
export const isSs58Address = (
|
|
6
|
-
address: SS58String | string,
|
|
7
|
-
): address is SS58String => {
|
|
8
|
-
try {
|
|
9
|
-
if (!address) return false;
|
|
10
|
-
accountIdEncoder(address);
|
|
11
|
-
return true;
|
|
12
|
-
} catch {
|
|
13
|
-
return false;
|
|
14
|
-
}
|
|
15
|
-
};
|
|
@@ -1,8 +0,0 @@
|
|
|
1
|
-
import { isEthereumAddress } from "./isEthereumAddress";
|
|
2
|
-
import { isSs58Address } from "./isSs58Address";
|
|
3
|
-
|
|
4
|
-
export const isValidAddress = (address: string): boolean => {
|
|
5
|
-
return address.startsWith("0x")
|
|
6
|
-
? isEthereumAddress(address)
|
|
7
|
-
: isSs58Address(address);
|
|
8
|
-
};
|
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
import { type MonoTypeOperatorFunction, tap } from "rxjs";
|
|
2
|
-
|
|
3
|
-
type Opts = {
|
|
4
|
-
printValue?: boolean;
|
|
5
|
-
enabled?: boolean;
|
|
6
|
-
};
|
|
7
|
-
|
|
8
|
-
export const logObservable = <T>(
|
|
9
|
-
label: string,
|
|
10
|
-
opts?: Opts,
|
|
11
|
-
): MonoTypeOperatorFunction<T> =>
|
|
12
|
-
tap((value) => {
|
|
13
|
-
const { printValue = false, enabled = true } = opts || {};
|
|
14
|
-
|
|
15
|
-
if (!label || !enabled) return;
|
|
16
|
-
|
|
17
|
-
const text = `[kheopskit] observable ${label} emit`;
|
|
18
|
-
|
|
19
|
-
if (printValue) console.debug(text, value);
|
|
20
|
-
else console.debug(text);
|
|
21
|
-
});
|
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
export const POLKADOT_EXTENSIONS: Record<
|
|
2
|
-
string,
|
|
3
|
-
{ name: string; icon: string }
|
|
4
|
-
> = {
|
|
5
|
-
talisman: {
|
|
6
|
-
name: "Talisman",
|
|
7
|
-
icon: "data:image/svg+xml;base64,PHN2ZyB3aWR0aD0iMTI4IiBoZWlnaHQ9IjEyOCIgdmlld0JveD0iMCAwIDEyOCAxMjgiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyI+CiAgICA8cGF0aCBmaWxsPSIjZGRmZTc2IiBmaWxsLXJ1bGU9ImV2ZW5vZGQiIGQ9Ik0wIDcwLjI1YzAgMjEuMjU1IDAgMzEuODgzIDQuNDYzIDM5Ljg1MmEzNSAzNSAwIDAgMCAxMy40MzUgMTMuNDM1QzI1Ljg2NyAxMjggMzYuNDk1IDEyOCA1Ny43NSAxMjhoMTIuNWMyMS4yNTUgMCAzMS44ODMgMCAzOS44NTItNC40NjNhMzUgMzUgMCAwIDAgMTMuNDM1LTEzLjQzNUMxMjggMTAyLjEzMyAxMjggOTEuNTA1IDEyOCA3MC4yNXYtMTIuNWMwLTIxLjI1NSAwLTMxLjg4My00LjQ2My0zOS44NTJhMzUgMzUgMCAwIDAtMTMuNDM1LTEzLjQzNUMxMDIuMTMzIDAgOTEuNTA1IDAgNzAuMjUgMGgtMTIuNUMzNi40OTUgMCAyNS44NjcgMCA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{ WalletAccount } from "@/api";
|
|
2
|
-
|
|
3
|
-
export const sortAccounts = (a1: WalletAccount, a2: WalletAccount) => {
|
|
4
|
-
if (a1.platform === "polkadot") {
|
|
5
|
-
if (a2.platform === "polkadot") {
|
|
6
|
-
// sort by wallet name, fallback to address
|
|
7
|
-
if (a1.walletName !== a2.walletName) {
|
|
8
|
-
if (a1.walletName === "talisman") return -1;
|
|
9
|
-
if (a2.walletName === "talisman") return 1;
|
|
10
|
-
return a1.walletName.localeCompare(a2.walletName);
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
// sort by name, fallback to address
|
|
14
|
-
return a1.name !== a2.name
|
|
15
|
-
? (a1.name ?? "").localeCompare(a2.name ?? "")
|
|
16
|
-
: a1.address.localeCompare(a2.address);
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
return -1; // polkadot accounts first
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
if (a2.platform === "ethereum") {
|
|
23
|
-
// sort by wallet name, fallback to address
|
|
24
|
-
if (a1.walletName !== a2.walletName) {
|
|
25
|
-
if (a1.walletName === "Talisman") return -1;
|
|
26
|
-
if (a2.walletName === "Talisman") return 1;
|
|
27
|
-
return a1.walletName.localeCompare(a2.walletName);
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
// keep order as provider by the wallet
|
|
31
|
-
return 0;
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
// impossible case
|
|
35
|
-
return 0;
|
|
36
|
-
};
|