-
-
Notifications
You must be signed in to change notification settings - Fork 251
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(suite-native): send address checksum
- Loading branch information
Showing
8 changed files
with
195 additions
and
62 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
suite-native/module-send/src/components/AddressChecksumMessage.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import { Link } from '@suite-native/link'; | ||
import { HStack, Text } from '@suite-native/atoms'; | ||
import { Icon } from '@suite-native/icons'; | ||
import { Translation } from '@suite-native/intl'; | ||
|
||
const LINK_URL = 'https://trezor.io/learn/a/evm-address-checksum-in-trezor-suite'; | ||
|
||
export const AddressChecksumMessage = () => ( | ||
<HStack> | ||
<Icon name="info" size="medium" color="iconSubdued" /> | ||
<Text variant="label" color="textSubdued"> | ||
<Translation | ||
id="moduleSend.outputs.recipients.checksum.label" | ||
values={{ | ||
link: linkChunk => ( | ||
<Link | ||
href={LINK_URL} | ||
label={linkChunk} | ||
textVariant="label" | ||
isUnderlined | ||
textColor="textSubdued" | ||
/> | ||
), | ||
}} | ||
/> | ||
</Text> | ||
</HStack> | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
138 changes: 138 additions & 0 deletions
138
suite-native/module-send/src/hooks/useAddressValidationAlerts.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
import { useCallback, useEffect, useState } from 'react'; | ||
import { useSelector } from 'react-redux'; | ||
|
||
import { useRoute, RouteProp } from '@react-navigation/native'; | ||
import { checkAddressCheckSum, toChecksumAddress } from 'web3-utils'; | ||
import { G } from '@mobily/ts-belt'; | ||
|
||
import { SendStackParamList, SendStackRoutes } from '@suite-native/navigation'; | ||
import { Translation } from '@suite-native/intl'; | ||
import { selectAccountTokenSymbol, TokensRootState } from '@suite-native/tokens'; | ||
import { useAlert } from '@suite-native/alerts'; | ||
import { useFormContext } from '@suite-native/forms'; | ||
import { isAddressValid } from '@suite-common/wallet-utils'; | ||
import { AccountsRootState, selectAccountNetworkSymbol } from '@suite-common/wallet-core'; | ||
import TrezorConnect from '@trezor/connect'; | ||
import { Link } from '@suite-native/link'; | ||
|
||
import { getOutputFieldName } from '../utils'; | ||
import { TokenOfNetworkAlertBody } from '../components/TokenOfNetworkAlertContent'; | ||
|
||
type UseAddressValidationAlertsArgs = { | ||
inputIndex: number; | ||
}; | ||
|
||
const CHECKSUM_LINK_URL = 'https://trezor.io/learn/a/evm-address-checksum-in-trezor-suite'; | ||
|
||
export const useAddressValidationAlerts = ({ inputIndex }: UseAddressValidationAlertsArgs) => { | ||
const { | ||
params: { tokenContract, accountKey }, | ||
} = useRoute<RouteProp<SendStackParamList, SendStackRoutes.SendOutputs>>(); | ||
const [wasAddressChecksummed, setWasAddressChecksummed] = useState(false); | ||
const [wasTokenAlertDisplayed, setWasTokenAlertDisplayed] = useState( | ||
G.isNullable(tokenContract), | ||
); | ||
const { showAlert } = useAlert(); | ||
|
||
const tokenSymbol = useSelector((state: TokensRootState) => | ||
selectAccountTokenSymbol(state, accountKey, tokenContract), | ||
); | ||
const networkSymbol = useSelector((state: AccountsRootState) => | ||
selectAccountNetworkSymbol(state, accountKey), | ||
); | ||
|
||
const { watch, setValue } = useFormContext(); | ||
|
||
const addressFieldName = getOutputFieldName(inputIndex, 'address'); | ||
const addressValue = watch(addressFieldName); | ||
|
||
const isFilledValidAddress = | ||
addressValue && networkSymbol && isAddressValid(addressValue, networkSymbol); | ||
|
||
const convertAddressToChecksum = useCallback(() => { | ||
setValue(addressFieldName, toChecksumAddress(addressValue), { | ||
shouldValidate: true, | ||
}); | ||
setWasAddressChecksummed(true); | ||
}, [addressFieldName, addressValue, setValue]); | ||
|
||
const handleAddressChecksum = useCallback(async () => { | ||
if (isFilledValidAddress && !checkAddressCheckSum(addressValue)) { | ||
const params = { | ||
descriptor: addressValue, | ||
coin: networkSymbol, | ||
}; | ||
|
||
const addressInfo = await TrezorConnect.getAccountInfo(params); | ||
|
||
if (addressInfo.success) { | ||
// Already used addresses are checksumed without displaying the alert. | ||
const isUsedAddress = addressInfo.payload.history.total !== 0; | ||
if (isUsedAddress) { | ||
convertAddressToChecksum(); | ||
|
||
return; | ||
} | ||
} | ||
|
||
showAlert({ | ||
title: <Translation id="moduleSend.outputs.recipients.checksum.alert.title" />, | ||
description: ( | ||
<Translation | ||
id="moduleSend.outputs.recipients.checksum.alert.body" | ||
values={{ | ||
link: linkChunk => ( | ||
<Link | ||
href={CHECKSUM_LINK_URL} | ||
label={linkChunk} | ||
isUnderlined | ||
textColor="textSubdued" | ||
/> | ||
), | ||
}} | ||
/> | ||
), | ||
primaryButtonTitle: ( | ||
<Translation id="moduleSend.outputs.recipients.checksum.alert.primaryButton" /> | ||
), | ||
onPressPrimaryButton: convertAddressToChecksum, | ||
}); | ||
} | ||
}, [addressValue, isFilledValidAddress, networkSymbol, showAlert, convertAddressToChecksum]); | ||
|
||
useEffect(() => { | ||
const shouldShowTokenAlert = | ||
tokenContract && isFilledValidAddress && !wasTokenAlertDisplayed; | ||
const shouldChecksumAddress = | ||
!wasAddressChecksummed && isFilledValidAddress && wasTokenAlertDisplayed; | ||
|
||
if (shouldShowTokenAlert) { | ||
showAlert({ | ||
appendix: ( | ||
<TokenOfNetworkAlertBody | ||
accountKey={accountKey} | ||
tokenContract={tokenContract} | ||
/> | ||
), | ||
primaryButtonTitle: <Translation id="generic.buttons.gotIt" />, | ||
onPressPrimaryButton: () => setWasTokenAlertDisplayed(true), | ||
}); | ||
} else if (shouldChecksumAddress) handleAddressChecksum(); | ||
// TODO: add path for contract address alert: https://github.com/trezor/trezor-suite/issues/14936. | ||
else if (!isFilledValidAddress) { | ||
setWasTokenAlertDisplayed(false); | ||
setWasAddressChecksummed(false); | ||
} | ||
}, [ | ||
isFilledValidAddress, | ||
showAlert, | ||
tokenContract, | ||
tokenSymbol, | ||
accountKey, | ||
wasAddressChecksummed, | ||
handleAddressChecksum, | ||
wasTokenAlertDisplayed, | ||
]); | ||
|
||
return { wasAddressChecksummed }; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters