-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSelectWallet.tsx
212 lines (194 loc) · 7.57 KB
/
SelectWallet.tsx
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
import { isWalletObject } from "@starknet-io/get-starknet-core";
import { Image, Separator, StackSeparator, VStack, useDisclosure } from "@chakra-ui/react";
import {
DialogActionTrigger,
DialogBody,
DialogCloseTrigger,
DialogContent,
DialogFooter,
DialogHeader,
DialogRoot,
DialogTitle,
DialogTrigger,
} from "@/components/ui/dialog";
import { Button } from "@/components/ui/button";
import { useStoreWallet } from "../../Wallet/walletContext";
import { useFrontendProvider } from "../provider/providerContext";
import { useEffect } from "react";
import { useState } from "react";
import { WalletAccount, wallet, validateAndParseAddress, constants as SNconstants } from "starknet";
import { WALLET_API } from "@starknet-io/types-js";
import { compatibleApiVersions, myFrontendProviders } from "@/utils/constants";
import getStarknet from "@starknet-io/get-starknet-core"
import { resolveVirtualWallet } from "./virtualWallets";
// export interface StarknetWalletProvider extends StarknetWindowObject {}
type ValidWallet = {
wallet: WALLET_API.StarknetWindowObject;
isValid: boolean;
}
export async function scanObjectForWalletsCustom(
obj: Record<string, any>, // Browser window object
isWalletObject: (wallet: any) => boolean,
): Promise<ValidWallet[]> {
const wallets = await getStarknet.getAvailableWallets({});
console.log("List of starknet wallets", wallets);
const validWallets: ValidWallet[] = await Promise.all(wallets.map(
async (wallet: WALLET_API.StarknetWindowObject) => {
let isValid = await checkCompatibility(wallet);
// If not valid still check maybe its a virtual wallet ?
if (!isValid) {
try {
resolveVirtualWallet
wallet = await (wallet as any).loadWallet(window)
}
catch (e) {
console.log("Not a virtual wallet", e)
}
isValid = await checkCompatibility(wallet);
}
return { wallet: wallet, isValid: isValid } as ValidWallet;
}
))
console.log(validWallets);
return validWallets;
}
const checkCompatibility = async (myWalletSWO: WALLET_API.StarknetWindowObject) => {
let isCompatible: boolean = false;
try {
const permissions = (await myWalletSWO.request({ type: "wallet_getPermissions" })) as string[];
isCompatible = true;
} catch {
(err: any) => { console.log("Wallet compatibility failed.\n", err) };
}
return isCompatible;
}
export default function SelectWallet() {
const { open, onOpen, onClose } = useDisclosure()
const myWallet = useStoreWallet(state => state.StarknetWalletObject);
const setMyWallet = useStoreWallet(state => state.setMyStarknetWalletObject);
const myWalletAccount = useStoreWallet(state => state.myWalletAccount);
const setMyWalletAccount = useStoreWallet(state => state.setMyWalletAccount);
const myFrontendProviderIndex = useFrontendProvider(state => state.currentFrontendProviderIndex);
const setCurrentFrontendProviderIndex = useFrontendProvider(state => state.setCurrentFrontendProviderIndex);
const isConnected = useStoreWallet(state => state.isConnected);
const setConnected = useStoreWallet(state => state.setConnected);
const displaySelectWalletUI = useStoreWallet(state => state.displaySelectWalletUI);
const setSelectWalletUI = useStoreWallet(state => state.setSelectWalletUI);
const setWalletApi = useStoreWallet(state => state.setWalletApiList);
const setChain = useStoreWallet(state => state.setChain);
const setAddressAccount = useStoreWallet(state => state.setAddressAccount);
const [walletList, setWalletList] = useState<ValidWallet[]>([]);
const handleSelectedWallet = async (selectedWallet: WALLET_API.StarknetWindowObject) => {
setMyWallet(selectedWallet); // zustand
setSelectWalletUI(false);
console.log("Trying to connect wallet=", selectedWallet);
setMyWallet(selectedWallet); // zustand
const myWA=await WalletAccount.connect(myFrontendProviders[2], selectedWallet);
setMyWalletAccount(myWA);
console.log("WalletAccount created=",myWA);
const result = await wallet.requestAccounts(selectedWallet);
if (typeof (result) == "string") {
console.log("This Wallet is not compatible.");
setSelectWalletUI(false);
return;
}
console.log("Current account addr =", result);
if (Array.isArray(result)) {
const addr = validateAndParseAddress(result[0]);
setAddressAccount(addr); // zustand
}
const isConnectedWallet: boolean = await wallet.getPermissions(selectedWallet).then((res: any) => (res as WALLET_API.Permission[]).includes(WALLET_API.Permission.ACCOUNTS));
setConnected(isConnectedWallet); // zustand
if (isConnectedWallet) {
const chainId = (await wallet.requestChainId(selectedWallet)) as string;
setChain(chainId);
setCurrentFrontendProviderIndex(chainId === SNconstants.StarknetChainId.SN_MAIN ? 0 : 2);
console.log("change Provider index to :", myFrontendProviderIndex);
}
// ********** TODO : replace supportedSpecs by api versions when available in SNJS
setWalletApi(await wallet.supportedSpecs(selectedWallet));
setSelectWalletUI(false);
}
useEffect(
() => {
const fetchData = async () => {
const res = await scanObjectForWalletsCustom(window, isWalletObject);
return res
}
console.log("Launch select wallet window.");
fetchData().then((wallets) => setWalletList(wallets));
onOpen();
return () => { }
},
[]
)
return (
<DialogRoot
placement={"center"}
scrollBehavior={"inside"}
size={"md"}
open={open}
closeOnInteractOutside={true}
onOpenChange={() => {
setSelectWalletUI(false);
onClose()
}}
>
<DialogContent>
<DialogCloseTrigger />
<DialogHeader
fontSize='xl'
fontWeight='bold'
padding={"20px"}
marginBottom={"10px"}
>
Select a wallet:
</DialogHeader>
<DialogBody
px={"20px"}
>
<VStack
separator={<StackSeparator borderColor='gray.200' />}
gap={3}
marginBottom={"20px"}
align='stretch'
>
{
walletList.map((wallet: ValidWallet, index: number) => {
const iconW: string = typeof (wallet.wallet.icon) == "string" ? wallet.wallet.icon : wallet.wallet.icon.light;
return <>
{wallet.isValid ? <>
<Button id={"wId" + index.toString()}
// backgroundColor="gray.100"
// color={"black"}
variant="surface"
fontSize='lg'
fontWeight='bold'
onClick={() => {
handleSelectedWallet(wallet.wallet);
onClose();
}} >
<Image src={iconW} width={30} />
{wallet.wallet.name + ' ' + wallet.wallet.version}
</Button>
</> : <>
<Button id={"wId" + index.toString()}
fontSize='lg'
fontWeight='bold'
variant="surface"
backgroundColor="orange"
disabled={true}
>
<Image src={iconW} width={30} />
{wallet.wallet.name + ' ' + wallet.wallet.version + " not compatible!"}
</Button>
</>}
</>
})
}
</VStack>
</DialogBody>
</DialogContent>
</DialogRoot>
)
}