Skip to content

Commit

Permalink
feat(suite-native): advanced recipient address validation
Browse files Browse the repository at this point in the history
  • Loading branch information
PeKne committed Nov 27, 2024
1 parent 477dece commit 4c39df6
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 5 deletions.
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

0 comments on commit 4c39df6

Please sign in to comment.