From cad019036a54dac086fe1b22d8a598aabdaf33f3 Mon Sep 17 00:00:00 2001 From: Karandeep Singh Date: Mon, 9 Sep 2024 22:10:19 -0400 Subject: [PATCH] wrap onApprove code with try/catch --- .../src/views/AuthRequestModal.tsx | 32 ++++++++------ .../src/views/SessionAuthenticateModal.tsx | 43 +++++++++++-------- .../views/SessionGrantPermissionsModal.tsx | 15 +++---- .../src/views/SessionProposalModal.tsx | 17 ++++---- .../src/views/SessionSendCallsModal.tsx | 13 +++--- .../src/views/SessionSendTransactionModal.tsx | 13 +++--- .../src/views/SessionSignCosmosModal.tsx | 15 +++---- .../src/views/SessionSignKadenaModal.tsx | 15 ++++--- .../src/views/SessionSignModal.tsx | 15 +++---- .../src/views/SessionSignMultiversxModal.tsx | 15 +++---- .../src/views/SessionSignNearModal.tsx | 15 +++---- .../src/views/SessionSignPolkadotModal.tsx | 15 +++---- .../src/views/SessionSignSolanaModal.tsx | 15 +++---- .../src/views/SessionSignTezosModal.tsx | 15 +++---- .../src/views/SessionSignTronModal.tsx | 15 +++---- .../src/views/SessionSignTypedDataModal.tsx | 15 +++---- 16 files changed, 141 insertions(+), 142 deletions(-) diff --git a/advanced/wallets/react-wallet-v2/src/views/AuthRequestModal.tsx b/advanced/wallets/react-wallet-v2/src/views/AuthRequestModal.tsx index ac13a1255..fa82b7531 100644 --- a/advanced/wallets/react-wallet-v2/src/views/AuthRequestModal.tsx +++ b/advanced/wallets/react-wallet-v2/src/views/AuthRequestModal.tsx @@ -9,6 +9,7 @@ import SettingsStore from '@/store/SettingsStore' import { eip155Addresses, eip155Wallets } from '@/utils/EIP155WalletUtil' import { web3wallet } from '@/utils/WalletConnectUtil' import RequestModal from './RequestModal' +import { styledToast } from '@/utils/HelperUtil' export default function AuthRequestModal() { const { account } = useSnapshot(SettingsStore.state) @@ -32,19 +33,24 @@ export default function AuthRequestModal() { // Handle approve action (logic varies based on request method) const onApprove = useCallback(async () => { - if (request) { - setIsLoadingApprove(true) - const signature = await eip155Wallets[address].signMessage(message) - await web3wallet.respondAuthRequest( - { - id: request.id, - signature: { - s: signature, - t: 'eip191' - } - }, - iss - ) + try { + if (request) { + setIsLoadingApprove(true) + const signature = await eip155Wallets[address].signMessage(message) + await web3wallet.respondAuthRequest( + { + id: request.id, + signature: { + s: signature, + t: 'eip191' + } + }, + iss + ) + } + } catch (e) { + styledToast((e as Error).message, 'error') + } finally { setIsLoadingApprove(false) ModalStore.close() } diff --git a/advanced/wallets/react-wallet-v2/src/views/SessionAuthenticateModal.tsx b/advanced/wallets/react-wallet-v2/src/views/SessionAuthenticateModal.tsx index 3c4a18615..ceb9df4e1 100644 --- a/advanced/wallets/react-wallet-v2/src/views/SessionAuthenticateModal.tsx +++ b/advanced/wallets/react-wallet-v2/src/views/SessionAuthenticateModal.tsx @@ -92,26 +92,31 @@ export default function SessionAuthenticateModal() { // Handle approve action (logic varies based on request method) const onApprove = useCallback(async () => { - if (messages.length) { - const signedAuths = [] - for (const message of messages) { - const signature = await eip155Wallets[address].signMessage(message.message) - const signedCacao = buildAuthObject( - message.authPayload, - { - t: 'eip191', - s: signature - }, - message.iss - ) - signedAuths.push(signedCacao) - } + try { + if (messages.length) { + const signedAuths = [] + for (const message of messages) { + const signature = await eip155Wallets[address].signMessage(message.message) + const signedCacao = buildAuthObject( + message.authPayload, + { + t: 'eip191', + s: signature + }, + message.iss + ) + signedAuths.push(signedCacao) + } - await web3wallet.engine.signClient.approveSessionAuthenticate({ - id: messages[0].id, - auths: signedAuths - }) - SettingsStore.setSessions(Object.values(web3wallet.getActiveSessions())) + await web3wallet.engine.signClient.approveSessionAuthenticate({ + id: messages[0].id, + auths: signedAuths + }) + SettingsStore.setSessions(Object.values(web3wallet.getActiveSessions())) + } + } catch (e) { + styledToast((e as Error).message, 'error') + } finally { ModalStore.close() } }, [address, messages]) diff --git a/advanced/wallets/react-wallet-v2/src/views/SessionGrantPermissionsModal.tsx b/advanced/wallets/react-wallet-v2/src/views/SessionGrantPermissionsModal.tsx index 03d136ec1..658f2ec04 100644 --- a/advanced/wallets/react-wallet-v2/src/views/SessionGrantPermissionsModal.tsx +++ b/advanced/wallets/react-wallet-v2/src/views/SessionGrantPermissionsModal.tsx @@ -35,19 +35,18 @@ export default function SessionGrantPermissionsModal() { console.log({ grantPermissionsRequestParams }) // Handle approve action (logic varies based on request method) const onApprove = useCallback(async () => { - if (requestEvent) { - setIsLoadingApprove(true) - const response = await approveEIP7715Request(requestEvent) - try { + try { + if (requestEvent) { + setIsLoadingApprove(true) + const response = await approveEIP7715Request(requestEvent) await web3wallet.respondSessionRequest({ topic, response }) - } catch (e) { - setIsLoadingApprove(false) - styledToast((e as Error).message, 'error') - return } + } catch (e) { + styledToast((e as Error).message, 'error') + } finally { setIsLoadingApprove(false) ModalStore.close() } diff --git a/advanced/wallets/react-wallet-v2/src/views/SessionProposalModal.tsx b/advanced/wallets/react-wallet-v2/src/views/SessionProposalModal.tsx index 8096cad8e..27a2d898a 100644 --- a/advanced/wallets/react-wallet-v2/src/views/SessionProposalModal.tsx +++ b/advanced/wallets/react-wallet-v2/src/views/SessionProposalModal.tsx @@ -267,9 +267,9 @@ export default function SessionProposalModal() { // Hanlde approve action, construct session namespace const onApprove = useCallback(async () => { - if (proposal && namespaces) { - setIsLoadingApprove(true) - try { + try { + if (proposal && namespaces) { + setIsLoadingApprove(true) if (reorderedEip155Accounts.length > 0) { // we should append the smart accounts to the available eip155 accounts namespaces.eip155.accounts = reorderedEip155Accounts.concat(namespaces.eip155.accounts) @@ -284,14 +284,13 @@ export default function SessionProposalModal() { sessionProperties }) SettingsStore.setSessions(Object.values(web3wallet.getActiveSessions())) - } catch (e) { - setIsLoadingApprove(false) - styledToast((e as Error).message, 'error') - return } + } catch (e) { + styledToast((e as Error).message, 'error') + } finally { + setIsLoadingApprove(false) + ModalStore.close() } - setIsLoadingApprove(false) - ModalStore.close() }, [namespaces, proposal, reorderedEip155Accounts]) // Hanlde reject action diff --git a/advanced/wallets/react-wallet-v2/src/views/SessionSendCallsModal.tsx b/advanced/wallets/react-wallet-v2/src/views/SessionSendCallsModal.tsx index 8c9bf56c2..aba1d864b 100644 --- a/advanced/wallets/react-wallet-v2/src/views/SessionSendCallsModal.tsx +++ b/advanced/wallets/react-wallet-v2/src/views/SessionSendCallsModal.tsx @@ -25,20 +25,19 @@ export default function SessionSendCallsModal() { // Handle approve action const onApprove = useCallback(async () => { - if (requestEvent && topic) { - setIsLoadingApprove(true) - try { + try { + if (requestEvent && topic) { + setIsLoadingApprove(true) console.log('Calls approved.') const response = await approveEIP5792Request(requestEvent) await web3wallet.respondSessionRequest({ topic, response }) - } catch (e) { - setIsLoadingApprove(false) - styledToast((e as Error).message, 'error') - return } + } catch (e) { + styledToast((e as Error).message, 'error') + } finally { setIsLoadingApprove(false) ModalStore.close() } diff --git a/advanced/wallets/react-wallet-v2/src/views/SessionSendTransactionModal.tsx b/advanced/wallets/react-wallet-v2/src/views/SessionSendTransactionModal.tsx index 09aa54390..cdea0dcfd 100644 --- a/advanced/wallets/react-wallet-v2/src/views/SessionSendTransactionModal.tsx +++ b/advanced/wallets/react-wallet-v2/src/views/SessionSendTransactionModal.tsx @@ -26,19 +26,18 @@ export default function SessionSendTransactionModal() { // Handle approve action const onApprove = useCallback(async () => { - if (requestEvent && topic) { - setIsLoadingApprove(true) - try { + try { + if (requestEvent && topic) { + setIsLoadingApprove(true) const response = await approveEIP155Request(requestEvent) await web3wallet.respondSessionRequest({ topic, response }) - } catch (e) { - setIsLoadingApprove(false) - styledToast((e as Error).message, 'error') - return } + } catch (e) { + styledToast((e as Error).message, 'error') + } finally { setIsLoadingApprove(false) ModalStore.close() } diff --git a/advanced/wallets/react-wallet-v2/src/views/SessionSignCosmosModal.tsx b/advanced/wallets/react-wallet-v2/src/views/SessionSignCosmosModal.tsx index 4722ab0ab..61103e048 100644 --- a/advanced/wallets/react-wallet-v2/src/views/SessionSignCosmosModal.tsx +++ b/advanced/wallets/react-wallet-v2/src/views/SessionSignCosmosModal.tsx @@ -29,19 +29,18 @@ export default function SessionSignCosmosModal() { // Handle approve action (logic varies based on request method) const onApprove = useCallback(async () => { - if (requestEvent) { - setIsLoadingApprove(true) - const response = await approveCosmosRequest(requestEvent) - try { + try { + if (requestEvent) { + setIsLoadingApprove(true) + const response = await approveCosmosRequest(requestEvent) await web3wallet.respondSessionRequest({ topic, response }) - } catch (e) { - setIsLoadingApprove(false) - styledToast((e as Error).message, 'error') - return } + } catch (e) { + styledToast((e as Error).message, 'error') + } finally { setIsLoadingApprove(false) ModalStore.close() } diff --git a/advanced/wallets/react-wallet-v2/src/views/SessionSignKadenaModal.tsx b/advanced/wallets/react-wallet-v2/src/views/SessionSignKadenaModal.tsx index d50a84a76..490064eba 100644 --- a/advanced/wallets/react-wallet-v2/src/views/SessionSignKadenaModal.tsx +++ b/advanced/wallets/react-wallet-v2/src/views/SessionSignKadenaModal.tsx @@ -31,19 +31,20 @@ export default function SessionSignKadenaModal() { // Handle approve action (logic varies based on request method) const onApprove = useCallback(async () => { - if (requestEvent) { - setIsLoadingApprove(true) - const response = await approveKadenaRequest(requestEvent) - try { + try { + if (requestEvent) { + setIsLoadingApprove(true) + const response = await approveKadenaRequest(requestEvent) await web3wallet.respondSessionRequest({ topic, response }) - } catch (e) { setIsLoadingApprove(false) - styledToast((e as Error).message, 'error') - return + ModalStore.close() } + } catch (e) { + styledToast((e as Error).message, 'error') + } finally { setIsLoadingApprove(false) ModalStore.close() } diff --git a/advanced/wallets/react-wallet-v2/src/views/SessionSignModal.tsx b/advanced/wallets/react-wallet-v2/src/views/SessionSignModal.tsx index 65518486a..2cf6f4038 100644 --- a/advanced/wallets/react-wallet-v2/src/views/SessionSignModal.tsx +++ b/advanced/wallets/react-wallet-v2/src/views/SessionSignModal.tsx @@ -29,19 +29,18 @@ export default function SessionSignModal() { // Handle approve action (logic varies based on request method) const onApprove = useCallback(async () => { - if (requestEvent) { - setIsLoadingApprove(true) - const response = await approveEIP155Request(requestEvent) - try { + try { + if (requestEvent) { + setIsLoadingApprove(true) + const response = await approveEIP155Request(requestEvent) await web3wallet.respondSessionRequest({ topic, response }) - } catch (e) { - setIsLoadingApprove(false) - styledToast((e as Error).message, 'error') - return } + } catch (e) { + styledToast((e as Error).message, 'error') + } finally { setIsLoadingApprove(false) ModalStore.close() } diff --git a/advanced/wallets/react-wallet-v2/src/views/SessionSignMultiversxModal.tsx b/advanced/wallets/react-wallet-v2/src/views/SessionSignMultiversxModal.tsx index 54f8163a9..addf2797e 100644 --- a/advanced/wallets/react-wallet-v2/src/views/SessionSignMultiversxModal.tsx +++ b/advanced/wallets/react-wallet-v2/src/views/SessionSignMultiversxModal.tsx @@ -32,19 +32,18 @@ export default function SessionSignMultiversxModal() { // Handle approve action (logic varies based on request method) const onApprove = useCallback(async () => { - if (requestEvent) { - setIsLoadingApprove(true) - const response = await approveMultiversxRequest(requestEvent) - try { + try { + if (requestEvent) { + setIsLoadingApprove(true) + const response = await approveMultiversxRequest(requestEvent) await web3wallet.respondSessionRequest({ topic, response }) - } catch (e) { - setIsLoadingApprove(false) - styledToast((e as Error).message, 'error') - return } + } catch (e) { + styledToast((e as Error).message, 'error') + } finally { setIsLoadingApprove(false) ModalStore.close() } diff --git a/advanced/wallets/react-wallet-v2/src/views/SessionSignNearModal.tsx b/advanced/wallets/react-wallet-v2/src/views/SessionSignNearModal.tsx index 4ec3451ea..502ae8c7d 100644 --- a/advanced/wallets/react-wallet-v2/src/views/SessionSignNearModal.tsx +++ b/advanced/wallets/react-wallet-v2/src/views/SessionSignNearModal.tsx @@ -142,19 +142,18 @@ export default function SessionSignNearModal() { // Handle approve action (logic varies based on request method) const onApprove = useCallback(async () => { - if (requestEvent) { - setIsLoadingApprove(true) - const response = await approveNearRequest(requestEvent) - try { + try { + if (requestEvent) { + setIsLoadingApprove(true) + const response = await approveNearRequest(requestEvent) await web3wallet.respondSessionRequest({ topic, response }) - } catch (e) { - setIsLoadingApprove(false) - styledToast((e as Error).message, 'error') - return } + } catch (e) { + styledToast((e as Error).message, 'error') + } finally { setIsLoadingApprove(false) ModalStore.close() } diff --git a/advanced/wallets/react-wallet-v2/src/views/SessionSignPolkadotModal.tsx b/advanced/wallets/react-wallet-v2/src/views/SessionSignPolkadotModal.tsx index a6d1a57cf..5e6259e7b 100644 --- a/advanced/wallets/react-wallet-v2/src/views/SessionSignPolkadotModal.tsx +++ b/advanced/wallets/react-wallet-v2/src/views/SessionSignPolkadotModal.tsx @@ -29,19 +29,18 @@ export default function SessionSignPolkadotModal() { // Handle approve action (logic varies based on request method) const onApprove = useCallback(async () => { - if (requestEvent) { - setIsLoadingApprove(true) - const response = await approvePolkadotRequest(requestEvent) - try { + try { + if (requestEvent) { + setIsLoadingApprove(true) + const response = await approvePolkadotRequest(requestEvent) await web3wallet.respondSessionRequest({ topic, response }) - } catch (e) { - setIsLoadingApprove(false) - styledToast((e as Error).message, 'error') - return } + } catch (e) { + styledToast((e as Error).message, 'error') + } finally { setIsLoadingApprove(false) ModalStore.close() } diff --git a/advanced/wallets/react-wallet-v2/src/views/SessionSignSolanaModal.tsx b/advanced/wallets/react-wallet-v2/src/views/SessionSignSolanaModal.tsx index 5c1072a40..73bbe8fc9 100644 --- a/advanced/wallets/react-wallet-v2/src/views/SessionSignSolanaModal.tsx +++ b/advanced/wallets/react-wallet-v2/src/views/SessionSignSolanaModal.tsx @@ -29,19 +29,18 @@ export default function SessionSignSolanaModal() { // Handle approve action (logic varies based on request method) const onApprove = useCallback(async () => { - if (requestEvent) { - setIsLoadingApprove(true) - const response = await approveSolanaRequest(requestEvent) - try { + try { + if (requestEvent) { + setIsLoadingApprove(true) + const response = await approveSolanaRequest(requestEvent) await web3wallet.respondSessionRequest({ topic, response }) - } catch (e) { - setIsLoadingApprove(false) - styledToast((e as Error).message, 'error') - return } + } catch (e) { + styledToast((e as Error).message, 'error') + } finally { setIsLoadingApprove(false) ModalStore.close() } diff --git a/advanced/wallets/react-wallet-v2/src/views/SessionSignTezosModal.tsx b/advanced/wallets/react-wallet-v2/src/views/SessionSignTezosModal.tsx index 0f3a036d5..ee3da814d 100644 --- a/advanced/wallets/react-wallet-v2/src/views/SessionSignTezosModal.tsx +++ b/advanced/wallets/react-wallet-v2/src/views/SessionSignTezosModal.tsx @@ -29,19 +29,18 @@ export default function SessionSignTezosModal() { // Handle approve action (logic varies based on request method) const onApprove = useCallback(async () => { - if (requestEvent) { - setIsLoadingApprove(true) - const response = await approveTezosRequest(requestEvent) - try { + try { + if (requestEvent) { + setIsLoadingApprove(true) + const response = await approveTezosRequest(requestEvent) await web3wallet.respondSessionRequest({ topic, response }) - } catch (e) { - setIsLoadingApprove(false) - styledToast((e as Error).message, 'error') - return } + } catch (e) { + styledToast((e as Error).message, 'error') + } finally { setIsLoadingApprove(false) ModalStore.close() } diff --git a/advanced/wallets/react-wallet-v2/src/views/SessionSignTronModal.tsx b/advanced/wallets/react-wallet-v2/src/views/SessionSignTronModal.tsx index 0d76bc0bb..0b1993211 100644 --- a/advanced/wallets/react-wallet-v2/src/views/SessionSignTronModal.tsx +++ b/advanced/wallets/react-wallet-v2/src/views/SessionSignTronModal.tsx @@ -29,19 +29,18 @@ export default function SessionSignTronModal() { // Handle approve action (logic varies based on request method) const onApprove = useCallback(async () => { - if (requestEvent) { - setIsLoadingApprove(true) - const response = await approveTronRequest(requestEvent) - try { + try { + if (requestEvent) { + setIsLoadingApprove(true) + const response = await approveTronRequest(requestEvent) await web3wallet.respondSessionRequest({ topic, response }) - } catch (e) { - setIsLoadingApprove(false) - styledToast((e as Error).message, 'error') - return } + } catch (e) { + styledToast((e as Error).message, 'error') + } finally { setIsLoadingApprove(false) ModalStore.close() } diff --git a/advanced/wallets/react-wallet-v2/src/views/SessionSignTypedDataModal.tsx b/advanced/wallets/react-wallet-v2/src/views/SessionSignTypedDataModal.tsx index a15479c16..4e6be9d62 100644 --- a/advanced/wallets/react-wallet-v2/src/views/SessionSignTypedDataModal.tsx +++ b/advanced/wallets/react-wallet-v2/src/views/SessionSignTypedDataModal.tsx @@ -40,19 +40,18 @@ export default function SessionSignTypedDataModal() { } // Handle approve action (logic varies based on request method) const onApprove = useCallback(async () => { - if (requestEvent) { - setIsLoadingApprove(true) - const response = await approveEIP155Request(requestEvent) - try { + try { + if (requestEvent) { + setIsLoadingApprove(true) + const response = await approveEIP155Request(requestEvent) await web3wallet.respondSessionRequest({ topic, response }) - } catch (e) { - setIsLoadingApprove(false) - styledToast((e as Error).message, 'error') - return } + } catch (e) { + styledToast((e as Error).message, 'error') + } finally { setIsLoadingApprove(false) ModalStore.close() }