Skip to content

Commit 1235708

Browse files
committed
feat: Optimize Sign/verify Message
1 parent 0d64329 commit 1235708

File tree

1 file changed

+35
-5
lines changed
  • packages/neuron-ui/src/components/SignAndVerify

1 file changed

+35
-5
lines changed

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

Lines changed: 35 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,14 @@ const SignAndVerify = () => {
130139
const [message, setMessage] = useState('')
131140
const [signature, setSignature] = useState('')
132141
const [address, setAddress] = useState('')
133-
const { wallet } = useGlobalState()
142+
// const [addressError, setAddressError] = useState('')
143+
const {
144+
chain: { networkID },
145+
settings: { networks },
146+
wallet,
147+
} = useGlobalState()
134148
const [isDropdownOpen, setIsDropdownOpen] = useState(false)
149+
const isMainnet = isMainnetUtil(networks, networkID)
135150
useExitOnWalletChange()
136151

137152
const handlePasswordDialogOpen = useCallback(() => {
@@ -226,12 +241,26 @@ const SignAndVerify = () => {
226241

227242
const onBack = useGoBack()
228243

244+
const addressError = useMemo(() => {
245+
if (!address) {
246+
return undefined
247+
}
248+
try {
249+
validateAddress(address, isMainnet)
250+
} catch (err) {
251+
if (isErrorWithI18n(err)) {
252+
return t(err.message, err.i18n)
253+
}
254+
}
255+
return undefined
256+
}, [t, address, isMainnet])
257+
229258
return (
230259
<div>
231260
<Dialog
232261
show={showDialog}
233262
title={t('sign-and-verify.sign-or-verify-message')}
234-
disabled={!message || !signature || !address}
263+
disabled={!message || !signature || !address || !!addressError}
235264
onCancel={onBack}
236265
confirmText={t('sign-and-verify.verify')}
237266
onConfirm={handleVerifyMessage}
@@ -270,6 +299,7 @@ const SignAndVerify = () => {
270299
</div>
271300
}
272301
width="100%"
302+
error={addressError}
273303
/>
274304
</div>
275305
{isDropdownOpen && wallet?.addresses ? (
@@ -311,7 +341,7 @@ const SignAndVerify = () => {
311341

312342
{wallet?.isWatchOnly || (
313343
<div className={styles.signWrap}>
314-
<Button type="text" disabled={!message || !address} onClick={handlePasswordDialogOpen}>
344+
<Button type="text" disabled={!message || !address || !!addressError} onClick={handlePasswordDialogOpen}>
315345
<Sign />
316346
{t('sign-and-verify.sign')}
317347
</Button>

0 commit comments

Comments
 (0)