|
48 | 48 | import { checkStatus } from "@requestnetwork/shared-utils/checkStatus"; |
49 | 49 | import { ethers } from "ethers"; |
50 | 50 |
|
| 51 | + interface CipherProvider extends CipherProviderTypes.ICipherProvider { |
| 52 | + getSessionSignatures: ( |
| 53 | + signer: ethers.Signer, |
| 54 | + walletAddress: `0x${string}`, |
| 55 | + domain: string, |
| 56 | + statement: string |
| 57 | + ) => Promise<any>; |
| 58 | + disconnectWallet: () => void; |
| 59 | + } |
| 60 | +
|
51 | 61 | export let config: IConfig; |
52 | 62 | export let wagmiConfig: WagmiConfig; |
53 | 63 | export let requestNetwork: RequestNetwork | null | undefined; |
54 | 64 | export let currencies: CurrencyTypes.CurrencyInput[] = []; |
55 | 65 |
|
56 | | - let cipherProvider: |
57 | | - | (CipherProviderTypes.ICipherProvider & { |
58 | | - getSessionSignatures: ( |
59 | | - signer: ethers.Signer, |
60 | | - walletAddress: `0x${string}` |
61 | | - ) => Promise<any>; |
62 | | - }) |
63 | | - | undefined; |
| 66 | + let cipherProvider: CipherProvider | undefined; |
64 | 67 |
|
65 | 68 | let sliderValueForDecryption = JSON.parse( |
66 | 69 | localStorage?.getItem("isDecryptionEnabled") ?? "false" |
|
72 | 75 | let activeConfig = config ? config : defaultConfig; |
73 | 76 | let mainColor = activeConfig.colors.main; |
74 | 77 | let secondaryColor = activeConfig.colors.secondary; |
75 | | - let account: GetAccountReturnType = wagmiConfig && getAccount(wagmiConfig); |
| 78 | + let account: GetAccountReturnType | undefined = wagmiConfig && getAccount(wagmiConfig); |
76 | 79 |
|
77 | 80 | let loading = false; |
78 | 81 | let searchQuery = ""; |
|
87 | 90 | }) |
88 | 91 | | undefined; |
89 | 92 | let currencyManager: CurrencyManager; |
90 | | - let previousWalletAddress: string | undefined; |
91 | | - let previousNetwork: string | undefined; |
92 | 93 |
|
93 | 94 | let columns = { |
94 | 95 | issuedAt: false, |
|
102 | 103 | let sortOrder = "desc"; |
103 | 104 | let sortColumn = "timestamp"; |
104 | 105 |
|
105 | | - let previousAddress: string | undefined; |
| 106 | + const handleWalletConnection = async () => { |
| 107 | + account = getAccount(wagmiConfig); |
| 108 | + await loadRequests(sliderValueForDecryption, account, requestNetwork); |
| 109 | + }; |
106 | 110 |
|
107 | | - $: { |
108 | | - if (wagmiConfig) { |
109 | | - const newAccount = getAccount(wagmiConfig); |
110 | | - if (newAccount?.address !== previousAddress) { |
111 | | - account = newAccount; |
112 | | - previousAddress = newAccount?.address; |
113 | | -
|
114 | | - if (newAccount?.address) { |
115 | | - tick().then(() => { |
116 | | - enableDecryption(); |
117 | | - getRequests(); |
118 | | - }); |
119 | | - } else { |
120 | | - requests = []; |
121 | | - activeRequest = undefined; |
122 | | - previousWalletAddress = undefined; |
123 | | - previousNetwork = undefined; |
124 | | - } |
125 | | - } |
126 | | - } |
127 | | - } |
| 111 | + const handleWalletDisconnection = () => { |
| 112 | + cipherProvider?.disconnectWallet(); |
| 113 | + requests = []; |
| 114 | + activeRequest = undefined; |
| 115 | + cipherProvider = undefined; |
| 116 | + account = undefined; |
| 117 | + }; |
128 | 118 |
|
129 | | - let unwatchAccount: WatchAccountReturnType | undefined; |
| 119 | + const handleWalletChange = (data: any) => { |
| 120 | + if (data?.address) { |
| 121 | + handleWalletConnection(); |
| 122 | + } else { |
| 123 | + handleWalletDisconnection(); |
| 124 | + } |
| 125 | + }; |
130 | 126 |
|
131 | 127 | onMount(() => { |
132 | 128 | unwatchAccount = watchAccount(wagmiConfig, { |
133 | 129 | onChange(data) { |
134 | | - if (data?.address !== previousAddress) { |
135 | | - account = data; |
136 | | - previousAddress = data?.address; |
137 | | -
|
138 | | - if (data?.address) { |
139 | | - getRequests(); |
140 | | - } else { |
141 | | - requests = []; |
142 | | - activeRequest = undefined; |
143 | | - previousWalletAddress = undefined; |
144 | | - previousNetwork = undefined; |
145 | | - } |
146 | | - } |
| 130 | + tick().then(() => { |
| 131 | + handleWalletChange(data); |
| 132 | + }); |
147 | 133 | }, |
148 | 134 | }); |
149 | 135 | }); |
150 | 136 |
|
| 137 | + let unwatchAccount: WatchAccountReturnType | undefined; |
| 138 | +
|
151 | 139 | onDestroy(() => { |
152 | 140 | if (typeof unwatchAccount === "function") unwatchAccount(); |
153 | 141 | }); |
154 | 142 |
|
155 | 143 | $: cipherProvider = |
156 | | - requestNetwork?.getCipherProvider() as CipherProviderTypes.ICipherProvider & { |
157 | | - getSessionSignatures: ( |
158 | | - signer: ethers.Signer, |
159 | | - walletAddress: `0x${string}` |
160 | | - ) => Promise<any>; |
161 | | - }; |
| 144 | + requestNetwork?.getCipherProvider() as CipherProvider; |
162 | 145 |
|
163 | 146 | $: { |
164 | 147 | signer = account?.address; |
|
170 | 153 | currencyManager = initializeCurrencyManager(currencies); |
171 | 154 | }); |
172 | 155 |
|
173 | | - const getRequests = async () => { |
| 156 | + const getRequests = async (account: GetAccountReturnType, requestNetwork: RequestNetwork | undefined | null) => { |
174 | 157 | if (!account?.address || !requestNetwork) return; |
175 | | - loading = true; |
176 | 158 |
|
177 | 159 | try { |
178 | 160 | const requestsData = await requestNetwork?.fromIdentity({ |
|
184 | 166 | .sort((a, b) => b.timestamp - a.timestamp); |
185 | 167 | } catch (error) { |
186 | 168 | console.error("Failed to fetch requests:", error); |
187 | | - } finally { |
188 | | - loading = false; |
189 | 169 | } |
190 | 170 | }; |
191 | 171 |
|
|
211 | 191 | let currentPage = 1; |
212 | 192 | let totalPages = 1; |
213 | 193 |
|
214 | | - $: { |
215 | | - const currentWalletAddress = account?.address; |
216 | | - const currentNetwork = account?.chainId?.toString(); |
217 | | -
|
218 | | - if ( |
219 | | - currentWalletAddress && |
220 | | - currentWalletAddress !== previousWalletAddress |
221 | | - ) { |
222 | | - getRequests(); |
223 | | - previousWalletAddress = currentWalletAddress; |
224 | | -
|
225 | | - activeRequest = undefined; |
226 | | - } |
227 | | -
|
228 | | - if (currentNetwork && currentNetwork !== previousNetwork) { |
229 | | - previousNetwork = currentNetwork; |
230 | | - } |
231 | | - } |
232 | | -
|
233 | 194 | $: { |
234 | 195 | if (sortColumn && sortOrder) { |
235 | 196 | requests = [...(requests ?? [])].sort((a, b) => { |
|
422 | 383 | activeRequest = undefined; |
423 | 384 | }; |
424 | 385 |
|
425 | | - const enableDecryption = async () => { |
| 386 | + const loadRequests = async (sliderValue: string, currentAccount: GetAccountReturnType, currentRequestNetwork: RequestNetwork | undefined | null) => { |
| 387 | + if (!currentAccount?.address || !currentRequestNetwork && !cipherProvider) return; |
| 388 | +
|
426 | 389 | loading = true; |
427 | | - if (sliderValueForDecryption === "on") { |
| 390 | + if (sliderValue === "on") { |
428 | 391 | try { |
429 | 392 | const signer = await getEthersSigner(wagmiConfig); |
430 | | - if (signer && account?.address) { |
431 | | - await cipherProvider?.getSessionSignatures(signer, account.address); |
| 393 | + if (signer && currentAccount?.address) { |
| 394 | + await cipherProvider?.getSessionSignatures(signer, currentAccount.address, window.location.host, "Sign in to Lit Protocol through Request Network"); |
432 | 395 | cipherProvider?.enableDecryption(true); |
433 | 396 | localStorage?.setItem("isDecryptionEnabled", JSON.stringify(true)); |
434 | 397 | } |
|
442 | 405 | cipherProvider?.enableDecryption(false); |
443 | 406 | localStorage?.setItem("isDecryptionEnabled", JSON.stringify(false)); |
444 | 407 | } |
445 | | - await getRequests(); |
| 408 | + await getRequests(currentAccount, currentRequestNetwork); |
446 | 409 | loading = false; |
447 | 410 | }; |
448 | | - $: sliderValueForDecryption, enableDecryption(); |
| 411 | + $: loadRequests(sliderValueForDecryption, account, requestNetwork); |
449 | 412 | </script> |
450 | 413 |
|
451 | 414 | <div |
|
0 commit comments