|
1 | | -import React, { useState, useEffect, useCallback } from 'react' |
| 1 | +import React, { useState, useEffect, useCallback, useMemo } from 'react' |
2 | 2 | import { TFunction } from 'i18next' |
3 | 3 | import { useTranslation } from 'react-i18next' |
4 | 4 | import { showErrorMessage, signMessage, verifyMessage } from 'services/remote' |
5 | 5 | 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' |
7 | 16 | import { useState as useGlobalState } from 'states' |
8 | 17 | import Button from 'widgets/Button' |
9 | 18 | import Balance from 'widgets/Balance' |
@@ -130,8 +139,14 @@ const SignAndVerify = () => { |
130 | 139 | const [message, setMessage] = useState('') |
131 | 140 | const [signature, setSignature] = useState('') |
132 | 141 | 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() |
134 | 148 | const [isDropdownOpen, setIsDropdownOpen] = useState(false) |
| 149 | + const isMainnet = isMainnetUtil(networks, networkID) |
135 | 150 | useExitOnWalletChange() |
136 | 151 |
|
137 | 152 | const handlePasswordDialogOpen = useCallback(() => { |
@@ -226,12 +241,26 @@ const SignAndVerify = () => { |
226 | 241 |
|
227 | 242 | const onBack = useGoBack() |
228 | 243 |
|
| 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 | + |
229 | 258 | return ( |
230 | 259 | <div> |
231 | 260 | <Dialog |
232 | 261 | show={showDialog} |
233 | 262 | title={t('sign-and-verify.sign-or-verify-message')} |
234 | | - disabled={!message || !signature || !address} |
| 263 | + disabled={!message || !signature || !address || !!addressError} |
235 | 264 | onCancel={onBack} |
236 | 265 | confirmText={t('sign-and-verify.verify')} |
237 | 266 | onConfirm={handleVerifyMessage} |
@@ -270,6 +299,7 @@ const SignAndVerify = () => { |
270 | 299 | </div> |
271 | 300 | } |
272 | 301 | width="100%" |
| 302 | + error={addressError} |
273 | 303 | /> |
274 | 304 | </div> |
275 | 305 | {isDropdownOpen && wallet?.addresses ? ( |
@@ -311,7 +341,7 @@ const SignAndVerify = () => { |
311 | 341 |
|
312 | 342 | {wallet?.isWatchOnly || ( |
313 | 343 | <div className={styles.signWrap}> |
314 | | - <Button type="text" disabled={!message || !address} onClick={handlePasswordDialogOpen}> |
| 344 | + <Button type="text" disabled={!message || !address || !!addressError} onClick={handlePasswordDialogOpen}> |
315 | 345 | <Sign /> |
316 | 346 | {t('sign-and-verify.sign')} |
317 | 347 | </Button> |
|
0 commit comments