Skip to content

Commit 7cc4585

Browse files
authored
feat: Optimize Sign/verify Message (#3293)
* feat: Optimize Sign/verify Message * fix: check * fix * fix: comment
1 parent a6be675 commit 7cc4585

File tree

2 files changed

+39
-7
lines changed

2 files changed

+39
-7
lines changed

packages/neuron-ui/src/components/SignAndVerify/index.tsx

Lines changed: 37 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,18 @@
1-
import React, { useState, useEffect, useCallback } from 'react'
1+
import React, { useState, useEffect, useCallback, useMemo } from 'react'
22
import { TFunction } from 'i18next'
33
import { useTranslation } from 'react-i18next'
44
import { showErrorMessage, signMessage, verifyMessage } from 'services/remote'
55
import { ControllerResponse } from 'services/remote/remoteApiWrapper'
6-
import { ErrorCode, isSuccessResponse, shannonToCKBFormatter, useExitOnWalletChange, useGoBack } from 'utils'
6+
import {
7+
ErrorCode,
8+
isSuccessResponse,
9+
shannonToCKBFormatter,
10+
useExitOnWalletChange,
11+
useGoBack,
12+
validateAddress,
13+
isMainnet as isMainnetUtil,
14+
} from 'utils'
15+
import { isErrorWithI18n } from 'exceptions'
716
import { useState as useGlobalState } from 'states'
817
import Button from 'widgets/Button'
918
import Balance from 'widgets/Balance'
@@ -130,8 +139,13 @@ const SignAndVerify = () => {
130139
const [message, setMessage] = useState('')
131140
const [signature, setSignature] = useState('')
132141
const [address, setAddress] = useState('')
133-
const { wallet } = useGlobalState()
142+
const {
143+
chain: { networkID },
144+
settings: { networks },
145+
wallet,
146+
} = useGlobalState()
134147
const [isDropdownOpen, setIsDropdownOpen] = useState(false)
148+
const isMainnet = isMainnetUtil(networks, networkID)
135149
useExitOnWalletChange()
136150

137151
const handlePasswordDialogOpen = useCallback(() => {
@@ -226,12 +240,29 @@ const SignAndVerify = () => {
226240

227241
const onBack = useGoBack()
228242

243+
const addressError = useMemo(() => {
244+
if (!address) {
245+
return undefined
246+
}
247+
try {
248+
validateAddress(address, isMainnet)
249+
} catch (err) {
250+
if (isErrorWithI18n(err)) {
251+
return t(err.message, err.i18n)
252+
}
253+
}
254+
if (wallet?.addresses && !wallet.addresses.find(item => item.address === address)) {
255+
return t('sign-and-verify.address-not-found')
256+
}
257+
return undefined
258+
}, [t, address, isMainnet, wallet.addresses])
259+
229260
return (
230261
<div>
231262
<Dialog
232263
show={showDialog}
233264
title={t('sign-and-verify.sign-or-verify-message')}
234-
disabled={!message || !signature || !address}
265+
disabled={!message || !signature || !address || !!addressError}
235266
onCancel={onBack}
236267
confirmText={t('sign-and-verify.verify')}
237268
onConfirm={handleVerifyMessage}
@@ -270,6 +301,7 @@ const SignAndVerify = () => {
270301
</div>
271302
}
272303
width="100%"
304+
error={addressError}
273305
/>
274306
</div>
275307
{isDropdownOpen && wallet?.addresses ? (
@@ -311,7 +343,7 @@ const SignAndVerify = () => {
311343

312344
{wallet?.isWatchOnly || (
313345
<div className={styles.signWrap}>
314-
<Button type="text" disabled={!message || !address} onClick={handlePasswordDialogOpen}>
346+
<Button type="text" disabled={!message || !address || !!addressError} onClick={handlePasswordDialogOpen}>
315347
<Sign />
316348
{t('sign-and-verify.sign')}
317349
</Button>

packages/neuron-ui/src/utils/hooks/useGetCountDownAndFeeRateStats.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ const useGetCountDownAndFeeRateStats = ({ seconds = 30, interval = 1000 }: Count
1515
suggestFeeRate: number
1616
}>({ suggestFeeRate: MEDIUM_FEE_RATE })
1717

18-
const handleGetFeeRateStatis = useCallback(() => {
18+
const handleGetFeeRateStatistics = useCallback(() => {
1919
getFeeRateStatistics()
2020
.then(res => {
2121
const { median } = res ?? {}
@@ -50,7 +50,7 @@ const useGetCountDownAndFeeRateStats = ({ seconds = 30, interval = 1000 }: Count
5050

5151
useEffect(() => {
5252
if (countDown === seconds) {
53-
handleGetFeeRateStatis()
53+
handleGetFeeRateStatistics()
5454
}
5555
}, [countDown, seconds])
5656

0 commit comments

Comments
 (0)