/
SessionSignModal.tsx
91 lines (84 loc) · 2.85 KB
/
SessionSignModal.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
/* eslint-disable react-hooks/rules-of-hooks */
import { Col, Divider, Row, Text } from '@nextui-org/react'
import { useCallback, useState } from 'react'
import RequesDetailsCard from '@/components/RequestDetalilsCard'
import ModalStore from '@/store/ModalStore'
import { approveEIP155Request, rejectEIP155Request } from '@/utils/EIP155RequestHandlerUtil'
import { getSignParamsMessage, styledToast } from '@/utils/HelperUtil'
import { web3wallet } from '@/utils/WalletConnectUtil'
import RequestModal from './RequestModal'
export default function SessionSignModal() {
// Get request and wallet data from store
const requestEvent = ModalStore.state.data?.requestEvent
const requestSession = ModalStore.state.data?.requestSession
const [isLoadingApprove, setIsLoadingApprove] = useState(false)
const [isLoadingReject, setIsLoadingReject] = useState(false)
// Ensure request and wallet are defined
if (!requestEvent || !requestSession) {
return <Text>Missing request data</Text>
}
// Get required request data
const { topic, params } = requestEvent
const { request, chainId } = params
// Get message, convert it to UTF8 string if it is valid hex
const message = getSignParamsMessage(request.params)
// Handle approve action (logic varies based on request method)
const onApprove = useCallback(async () => {
if (requestEvent) {
setIsLoadingApprove(true)
const response = await approveEIP155Request(requestEvent)
try {
await web3wallet.respondSessionRequest({
topic,
response
})
} catch (e) {
setIsLoadingApprove(false)
styledToast((e as Error).message, 'error')
return
}
setIsLoadingApprove(false)
ModalStore.close()
}
}, [requestEvent, topic])
// Handle reject action
const onReject = useCallback(async () => {
if (requestEvent) {
setIsLoadingReject(true)
const response = rejectEIP155Request(requestEvent)
try {
await web3wallet.respondSessionRequest({
topic,
response
})
} catch (e) {
setIsLoadingReject(false)
styledToast((e as Error).message, 'error')
return
}
setIsLoadingReject(false)
ModalStore.close()
}
}, [requestEvent, topic])
return (
<RequestModal
intention="request a signature"
metadata={requestSession.peer.metadata}
onApprove={onApprove}
onReject={onReject}
approveLoader={{ active: isLoadingApprove }}
rejectLoader={{ active: isLoadingReject }}
>
<RequesDetailsCard chains={[chainId ?? '']} protocol={requestSession.relay.protocol} />
<Divider y={1} />
<Row>
<Col>
<Text h5>Message</Text>
<Text color="$gray400" data-testid="request-message-text">
{message}
</Text>
</Col>
</Row>
</RequestModal>
)
}