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(suite-native): advanced recipient address validation #15521

Merged
merged 1 commit into from
Nov 27, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
3 changes: 2 additions & 1 deletion suite-native/forms/src/hooks/useField.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export const useField = ({

const {
field: { onBlur, onChange, value },
fieldState: { error, isDirty },
fieldState: { error, isDirty, isTouched },
} = useController({
name,
control,
Expand All @@ -47,6 +47,7 @@ export const useField = ({
errorMessage,
hasError,
isDirty,
isTouched,
value: transformedValue,
onBlur,
onChange,
Expand Down
5 changes: 4 additions & 1 deletion suite-native/module-send/src/components/AddressInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,10 @@ export const AddressInput = ({ index, accountKey }: AddressInputProps) => {
// Debug helper to fill opened account address.
const fillSelfAddress = () => {
if (freshAccountAddress)
setValue(addressFieldName, freshAccountAddress.address, { shouldValidate: true });
setValue(addressFieldName, freshAccountAddress.address, {
shouldValidate: true,
shouldTouch: true,
});
};

return (
Expand Down
24 changes: 11 additions & 13 deletions suite-native/module-send/src/components/SendMaxButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,8 @@ import { useCryptoFiatConverters } from '@suite-native/formatters';
import { AccountsRootState, selectAccountNetworkSymbol } from '@suite-common/wallet-core';
import { Button } from '@suite-native/atoms';
import { AccountKey, TokenAddress } from '@suite-common/wallet-types';
import { useFormContext } from '@suite-native/forms';
import { useField, useFormContext } from '@suite-native/forms';
import { useDebounce } from '@trezor/react-utils';
import { isAddressValid } from '@suite-common/wallet-utils';
import { Translation } from '@suite-native/intl';
import { selectAccountTokenBalance, TokensRootState } from '@suite-native/tokens';

Expand All @@ -28,6 +27,7 @@ type SendMaxButtonProps = {
export const SendMaxButton = ({ outputIndex, accountKey, tokenContract }: SendMaxButtonProps) => {
const dispatch = useDispatch();
const debounce = useDebounce();

const networkSymbol = useSelector((state: AccountsRootState) =>
selectAccountNetworkSymbol(state, accountKey),
);
Expand All @@ -36,19 +36,21 @@ export const SendMaxButton = ({ outputIndex, accountKey, tokenContract }: SendMa
selectAccountTokenBalance(state, accountKey, tokenContract),
);

const { hasError: hasAddressError, isTouched: isAddressTouched } = useField({
name: getOutputFieldName(outputIndex, 'address'),
});

const [maxAmountValue, setMaxAmountValue] = useState<string | null>();

const converters = useCryptoFiatConverters({ networkSymbol: networkSymbol!, tokenContract });
const { setValue, watch } = useFormContext<SendOutputsFormValues>();

const formValues = watch();
const addressValue = formValues.outputs[outputIndex]?.address;

const hasOutputValidAddress =
addressValue && networkSymbol && isAddressValid(addressValue, networkSymbol);
const isAddressValid = isAddressTouched && !hasAddressError;

const isMainnetSendMaxAvailable =
!tokenContract && formValues.outputs.length === 1 && hasOutputValidAddress;
!tokenContract && formValues.outputs.length === 1 && isAddressValid;
const isSendMaxAvailable = tokenContract || isMainnetSendMaxAvailable;

const calculateFeeLevelsMaxAmount = useCallback(async () => {
Expand Down Expand Up @@ -90,14 +92,10 @@ export const SendMaxButton = ({ outputIndex, accountKey, tokenContract }: SendMa
};

return (
isSendMaxAvailable && (
isSendMaxAvailable &&
maxAmountValue && (
<Animated.View entering={FadeIn}>
<Button
size="small"
colorScheme="tertiaryElevation0"
onPress={setOutputSendMax}
isLoading={!maxAmountValue}
>
<Button size="small" colorScheme="tertiaryElevation0" onPress={setOutputSendMax}>
<Translation id="moduleSend.outputs.recipients.maxButton" />
</Button>
</Animated.View>
Expand Down
4 changes: 4 additions & 0 deletions suite-native/module-send/src/screens/SendOutputsScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
SendRootState,
composeSendFormTransactionFeeLevelsThunk,
selectAccountByKey,
selectDeviceUnavailableCapabilities,
selectNetworkFeeInfo,
selectSendFormDraftByKey,
sendFormActions,
Expand Down Expand Up @@ -96,6 +97,8 @@ export const SendOutputsScreen = ({
selectSendFormDraftByKey(state, accountKey, tokenContract),
);

const deviceUnavailableCapabilities = useSelector(selectDeviceUnavailableCapabilities);

const network = account ? getNetwork(account.symbol) : null;

const form = useForm<SendOutputsFormValues>({
Expand All @@ -111,6 +114,7 @@ export const SendOutputsScreen = ({
isValueInSats: isAmountInSats,
feeLevelsMaxAmount,
decimals: tokenInfo?.decimals ?? network?.decimals,
isTaprootAvailable: !deviceUnavailableCapabilities?.taproot,
},
defaultValues: getDefaultValues({ tokenContract }),
});
Expand Down
27 changes: 22 additions & 5 deletions suite-native/module-send/src/sendOutputsFormSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,14 @@ import { G } from '@mobily/ts-belt';

import { BigNumber } from '@trezor/utils';
import { getNetworkType, NetworkSymbol } from '@suite-common/wallet-config';
import { formatNetworkAmount, isAddressValid, isDecimalsValid } from '@suite-common/wallet-utils';
import {
formatNetworkAmount,
isAddressDeprecated,
isAddressValid,
isBech32AddressUppercase,
isDecimalsValid,
isTaprootAddress,
} from '@suite-common/wallet-utils';
import { FeeInfo } from '@suite-common/wallet-types';
import { yup } from '@suite-common/validators';
import { U_INT_32 } from '@suite-common/wallet-constants';
Expand All @@ -18,6 +25,7 @@ export type SendFormFormContext = {
feeLevelsMaxAmount?: FeeLevelsMaxAmount;
decimals?: number;
accountDescriptor?: string;
isTaprootAvailable?: boolean;
};

const isAmountDust = (amount: string, context?: SendFormFormContext) => {
Expand Down Expand Up @@ -89,12 +97,21 @@ export const sendOutputsFormValidationSchema = yup.object({
'is-invalid-address',
'The address format is incorrect.',
(value, { options: { context } }: yup.TestContext<SendFormFormContext>) => {
const networkSymbol = context?.networkSymbol;
if (!value || !context) {
return false;
}
const { networkSymbol, isTaprootAvailable } = context;

if (!networkSymbol) return false;

const isTaprootValid =
isTaprootAvailable || !isTaprootAddress(value, networkSymbol);

return (
G.isNotNullable(value) &&
G.isNotNullable(networkSymbol) &&
isAddressValid(value, networkSymbol)
isAddressValid(value, networkSymbol) &&
!isAddressDeprecated(value, networkSymbol) &&
!isBech32AddressUppercase(value) && // bech32 addresses are valid as uppercase but are not accepted by Trezor
isTaprootValid // bech32m/Taproot addresses are valid but may not be supported by older FW
);
},
)
Expand Down
Loading