Skip to content

Commit

Permalink
wrap onApprove code with try/catch
Browse files Browse the repository at this point in the history
  • Loading branch information
KannuSingh committed Sep 10, 2024
1 parent 8321a4b commit cad0190
Show file tree
Hide file tree
Showing 16 changed files with 141 additions and 142 deletions.
32 changes: 19 additions & 13 deletions advanced/wallets/react-wallet-v2/src/views/AuthRequestModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
Expand Down
15 changes: 7 additions & 8 deletions advanced/wallets/react-wallet-v2/src/views/SessionSignModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
Expand Down
Loading

0 comments on commit cad0190

Please sign in to comment.