Skip to content

Commit

Permalink
fix: filter accounts by chain (#2655)
Browse files Browse the repository at this point in the history
  • Loading branch information
Asmadek authored Nov 14, 2024
1 parent e54e49f commit 993f059
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 24 deletions.
53 changes: 37 additions & 16 deletions src/renderer/shared/lib/utils/address.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,22 +56,7 @@ export const toShortAddress = (address: Address, chunk = 6): string => {
* @returns {Boolean}
*/
export const validateAddress = (address?: Address | AccountId): boolean => {
if (!address) return false;

if (isU8a(address) || isHex(address)) {
return [ETHEREUM_PUBLIC_KEY_LENGTH_BYTES, PUBLIC_KEY_LENGTH_BYTES].includes(u8aToU8a(address).length);
}

try {
const decoded = base58Decode(address);
if (!ADDRESS_ALLOWED_ENCODED_LENGTHS.includes(decoded.length)) return false;

const [isValid, endPos, ss58Length] = checkAddressChecksum(decoded);

return isValid && Boolean(decoded.slice(ss58Length, endPos));
} catch {
return false;
}
return validateEthereumAddress(address) || validateSubstrateAddress(address);
};

/**
Expand Down Expand Up @@ -113,3 +98,39 @@ export const isEthereumAccountId = (accountId?: AccountId): boolean => {
return false;
}
};

export const validateEthereumAddress = (address?: Address | AccountId): boolean => {
if (!address) return false;

if (isU8a(address) || isHex(address)) {
return ETHEREUM_PUBLIC_KEY_LENGTH_BYTES === u8aToU8a(address).length;
}

return false;
};

/**
* Check is account's address valid
*
* @param address Account's address
*
* @returns {Boolean}
*/
export const validateSubstrateAddress = (address?: Address | AccountId): boolean => {
if (!address) return false;

if (isU8a(address) || isHex(address)) {
return PUBLIC_KEY_LENGTH_BYTES === u8aToU8a(address).length;
}

try {
const decoded = base58Decode(address);
if (!ADDRESS_ALLOWED_ENCODED_LENGTHS.includes(decoded.length)) return false;

const [isValid, endPos, ss58Length] = checkAddressChecksum(decoded);

return isValid && Boolean(decoded.slice(ss58Length, endPos));
} catch {
return false;
}
};
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,21 @@ import { useForm } from 'effector-forms';
import { useUnit } from 'effector-react';
import { useEffect, useMemo, useState } from 'react';

import { type ChainAccount, type WalletFamily } from '@/shared/core';
import { type WalletFamily } from '@/shared/core';
import { useI18n } from '@/shared/i18n';
import { performSearch, toAccountId, toAddress, validateAddress } from '@/shared/lib/utils';
import {
performSearch,
toAccountId,
toAddress,
validateEthereumAddress,
validateSubstrateAddress,
} from '@/shared/lib/utils';
import { CaptionText, Combobox, IconButton, Identicon, Input } from '@/shared/ui';
import { type ComboboxOption } from '@/shared/ui/types';
import { Address } from '@/shared/ui-entities';
import { Box } from '@/shared/ui-kit';
import { contactModel } from '@/entities/contact';
import { networkUtils } from '@/entities/network';
import { WalletIcon, accountUtils, walletModel, walletUtils } from '@/entities/wallet';
import { filterModel } from '@/features/contacts';
import { walletSelectUtils } from '@/features/wallets/WalletSelect/lib/wallet-select-utils';
Expand Down Expand Up @@ -95,11 +102,7 @@ export const Signatory = ({

return acc.concat(
wallet.accounts
.filter(
(account) =>
(account as ChainAccount).chainId === undefined ||
(account as ChainAccount).chainId === chain.value.chainId,
)
.filter((account) => accountUtils.isChainAndCryptoMatch(account, chain.value))
.map((account) => {
const address = toAddress(account.accountId, { prefix: chain.value.addressPrefix });

Expand Down Expand Up @@ -151,6 +154,7 @@ export const Signatory = ({
// list of contacts in case of not own account
useEffect(() => {
if (isOwnAccount || contacts.length === 0) return;

setOptions(
contactsFiltered.map(({ name, address }) => {
const displayAddress = toAddress(address, { prefix: chain.value.addressPrefix });
Expand Down Expand Up @@ -180,7 +184,10 @@ export const Signatory = ({
}, [displayName]);

const onAddressChange = (data: ComboboxOption) => {
const validatedAddress = validateAddress(data.value) ? data.value : '';
const isEthereumChain = networkUtils.isEthereumBased(chain.value.options);
const validateFn = isEthereumChain ? validateEthereumAddress : validateSubstrateAddress;

const validatedAddress = validateFn(data.value) ? data.value : '';
const fixedAddress = toAddress(validatedAddress, { prefix: chain.value.addressPrefix });

signatoryModel.events.changeSignatory({
Expand Down

0 comments on commit 993f059

Please sign in to comment.