Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Optimize Sign/verify Message #3293

Open
wants to merge 4 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 35 additions & 5 deletions packages/neuron-ui/src/components/SignAndVerify/index.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,18 @@
import React, { useState, useEffect, useCallback } from 'react'
import React, { useState, useEffect, useCallback, useMemo } from 'react'
import { TFunction } from 'i18next'
import { useTranslation } from 'react-i18next'
import { showErrorMessage, signMessage, verifyMessage } from 'services/remote'
import { ControllerResponse } from 'services/remote/remoteApiWrapper'
import { ErrorCode, isSuccessResponse, shannonToCKBFormatter, useExitOnWalletChange, useGoBack } from 'utils'
import {
ErrorCode,
isSuccessResponse,
shannonToCKBFormatter,
useExitOnWalletChange,
useGoBack,
validateAddress,
isMainnet as isMainnetUtil,
} from 'utils'
import { isErrorWithI18n } from 'exceptions'
import { useState as useGlobalState } from 'states'
import Button from 'widgets/Button'
import Balance from 'widgets/Balance'
Expand Down Expand Up @@ -130,8 +139,14 @@ const SignAndVerify = () => {
const [message, setMessage] = useState('')
const [signature, setSignature] = useState('')
const [address, setAddress] = useState('')
const { wallet } = useGlobalState()
// const [addressError, setAddressError] = useState('')
const {
devchenyan marked this conversation as resolved.
Show resolved Hide resolved
chain: { networkID },
settings: { networks },
wallet,
} = useGlobalState()
const [isDropdownOpen, setIsDropdownOpen] = useState(false)
const isMainnet = isMainnetUtil(networks, networkID)
useExitOnWalletChange()

const handlePasswordDialogOpen = useCallback(() => {
Expand Down Expand Up @@ -226,12 +241,26 @@ const SignAndVerify = () => {

const onBack = useGoBack()

const addressError = useMemo(() => {
if (!address) {
return undefined
}
try {
validateAddress(address, isMainnet)
} catch (err) {
if (isErrorWithI18n(err)) {
return t(err.message, err.i18n)
}
}
return undefined
}, [t, address, isMainnet])

return (
<div>
<Dialog
show={showDialog}
title={t('sign-and-verify.sign-or-verify-message')}
disabled={!message || !signature || !address}
disabled={!message || !signature || !address || !!addressError}
onCancel={onBack}
confirmText={t('sign-and-verify.verify')}
onConfirm={handleVerifyMessage}
Expand Down Expand Up @@ -270,6 +299,7 @@ const SignAndVerify = () => {
</div>
}
width="100%"
error={addressError}
/>
</div>
{isDropdownOpen && wallet?.addresses ? (
Expand Down Expand Up @@ -311,7 +341,7 @@ const SignAndVerify = () => {

{wallet?.isWatchOnly || (
<div className={styles.signWrap}>
<Button type="text" disabled={!message || !address} onClick={handlePasswordDialogOpen}>
<Button type="text" disabled={!message || !address || !!addressError} onClick={handlePasswordDialogOpen}>
<Sign />
{t('sign-and-verify.sign')}
</Button>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const useGetCountDownAndFeeRateStats = ({ seconds = 30, interval = 1000 }: Count
suggestFeeRate: number
}>({ suggestFeeRate: MEDIUM_FEE_RATE })

const handleGetFeeRateStatis = useCallback(() => {
const handleGetFeeRateStatistics = useCallback(() => {
getFeeRateStatistics()
.then(res => {
const { median } = res ?? {}
Expand Down Expand Up @@ -50,7 +50,7 @@ const useGetCountDownAndFeeRateStats = ({ seconds = 30, interval = 1000 }: Count

useEffect(() => {
if (countDown === seconds) {
handleGetFeeRateStatis()
handleGetFeeRateStatistics()
}
}, [countDown, seconds])

Expand Down
Loading