-
Notifications
You must be signed in to change notification settings - Fork 578
/
Copy pathkeplr.ts
61 lines (52 loc) · 1.55 KB
/
keplr.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import { ChainInfo, Keplr } from "@keplr-wallet/types";
import { Wallet } from "./types";
export const getKeplrFromWindow: () => Promise<
Keplr | undefined
> = async () => {
if (typeof window === "undefined") {
return undefined;
}
if (window.keplr) {
return window.keplr;
}
if (document.readyState === "complete") {
return window.keplr;
}
return new Promise((resolve) => {
const documentStateChange = (event: Event) => {
if (
event.target &&
(event.target as Document).readyState === "complete"
) {
resolve(window.keplr);
document.removeEventListener("readystatechange", documentStateChange);
}
};
document.addEventListener("readystatechange", documentStateChange);
});
};
export class KeplrWallet implements Wallet {
constructor(public readonly keplr: Keplr) {}
getChainInfosWithoutEndpoints(): Promise<
(Pick<ChainInfo, "chainId" | "chainName" | "bech32Config"> & {
readonly isEthermintLike?: boolean;
})[]
> {
return this.keplr.getChainInfosWithoutEndpoints().then((chainInfos) => {
return chainInfos.map((chainInfo) => {
return {
...chainInfo,
isEthermintLike:
chainInfo.features?.includes("eth-address-gen") ||
chainInfo.features?.includes("eth-key-sign"),
};
});
});
}
suggestChain(chainInfo: ChainInfo): Promise<void> {
return this.keplr.experimentalSuggestChain(chainInfo);
}
init(chainIds: string[]): Promise<void> {
return this.keplr.enable(chainIds);
}
}