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

fix: CP-9311 x and p send set address #83

Merged
merged 4 commits into from
Nov 4, 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
2 changes: 1 addition & 1 deletion src/pages/Send/components/SendAVM.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ export const SendAVM = ({
token={nativeToken}
tokenList={tokenList}
onContactChanged={(contact) => {
if (contact?.addressXP) setAddress(contact.addressXP);
setAddress(contact?.addressXP ?? '');
}}
onAmountChanged={(newAmount) => setAmount(newAmount)}
onTokenChanged={() => {}} // noop, AVAX has only one token
Expand Down
2 changes: 1 addition & 1 deletion src/pages/Send/components/SendPVM.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ export const SendPVM = ({
token={nativeToken}
tokenList={tokenList}
onContactChanged={(contact) => {
if (contact?.addressXP) setAddress(contact.addressXP);
setAddress(contact?.addressXP ?? '');
}}
onAmountChanged={(newAmount) => setAmount(newAmount)}
onTokenChanged={() => {}} // noop, AVAX has only one token
Expand Down
24 changes: 17 additions & 7 deletions src/pages/Send/hooks/useIdentifyAddress.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { useCallback } from 'react';
import { isBitcoin } from '@src/utils/isBitcoin';
import { isPchainNetwork } from '@src/background/services/network/utils/isAvalanchePchainNetwork';
import { isXchainNetwork } from '@src/background/services/network/utils/isAvalancheXchainNetwork';
import { correctAddressByPrefix } from '../utils/correctAddressByPrefix';

const UNSAVED_CONTACT_BASE = {
id: '',
Expand All @@ -24,14 +25,21 @@ export const useIdentifyAddress = () => {
const identifyAddress = useCallback(
(address: string): Contact => {
if (!address)
return { ...UNSAVED_CONTACT_BASE, address: '', addressBTC: '' };
return {
...UNSAVED_CONTACT_BASE,
address: '',
addressBTC: '',
addressXP: '',
};
const addressLowerCase = address.toLowerCase();
for (const contact of contacts) {
if (
contact.address.toLowerCase() === addressLowerCase ||
contact.addressBTC?.toLowerCase() === addressLowerCase ||
`p-${contact.addressXP?.toLowerCase()}` === addressLowerCase ||
`x-${contact.addressXP?.toLowerCase()}` === addressLowerCase
`p-${contact.addressXP?.toLowerCase()}` ===
correctAddressByPrefix(addressLowerCase, 'p-') ||
`x-${contact.addressXP?.toLowerCase()}` ===
correctAddressByPrefix(addressLowerCase, 'x-')
) {
const addressToUse = isBitcoin(network)
? { addressBTC: address, address: '', addressPVM: '' }
Expand All @@ -50,20 +58,22 @@ export const useIdentifyAddress = () => {
if (
account.addressC.toLowerCase() === addressLowerCase ||
account.addressBTC?.toLocaleLowerCase() === addressLowerCase ||
account.addressPVM?.toLocaleLowerCase() === addressLowerCase ||
account.addressAVM?.toLowerCase() === addressLowerCase
account.addressPVM?.toLocaleLowerCase() ===
correctAddressByPrefix(addressLowerCase, 'p-') ||
account.addressAVM?.toLowerCase() ===
correctAddressByPrefix(addressLowerCase, 'x-')
) {
const addressToUse = isBitcoin(network)
? { addressBTC: account.addressBTC, address: '' }
: isPchainNetwork(network)
? {
addressXP: account.addressPVM ? account.addressPVM : '',
addressXP: address,
address: '',
addressBTC: '',
}
: isXchainNetwork(network)
? {
addressXP: account.addressAVM ? account.addressAVM : '',
addressXP: address,
address: '',
addressBTC: '',
}
Expand Down
3 changes: 2 additions & 1 deletion src/pages/Send/hooks/useSend/useAVMSend.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import type { AvalancheSendTransactionHandler } from '@src/background/services/w
import { getMaxUtxoSet } from '../../utils/getMaxUtxos';
import { SendAdapterAVM } from './models';
import { AVMSendOptions } from '../../models';
import { correctAddressByPrefix } from '../../utils/correctAddressByPrefix';

const XCHAIN_ALIAS = 'X' as const;

Expand Down Expand Up @@ -135,7 +136,7 @@ export const useAvmSend: SendAdapterAVM = ({
const unsignedTx = wallet.baseTX({
utxoSet: utxos.utxos,
chain: XCHAIN_ALIAS,
toAddress: address,
toAddress: correctAddressByPrefix(address, 'X-'),
amountsPerAsset: {
[avax]: amountBigInt,
},
Expand Down
3 changes: 2 additions & 1 deletion src/pages/Send/hooks/useSend/usePVMSend.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import type { AvalancheSendTransactionHandler } from '@src/background/services/w
import { getMaxUtxoSet } from '../../utils/getMaxUtxos';
import { PVMSendOptions } from '../../models';
import { SendAdapterPVM } from './models';
import { correctAddressByPrefix } from '../../utils/correctAddressByPrefix';

const PCHAIN_ALIAS = 'P' as const;

Expand Down Expand Up @@ -134,7 +135,7 @@ export const usePvmSend: SendAdapterPVM = ({
const unsignedTx = wallet.baseTX({
utxoSet: utxos,
chain: PCHAIN_ALIAS,
toAddress: address,
toAddress: correctAddressByPrefix(address, 'P-'),
amountsPerAsset: {
[avax]: amountBigInt,
},
Expand Down
14 changes: 14 additions & 0 deletions src/pages/Send/utils/correctAddressByPrefix.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { correctAddressByPrefix } from './correctAddressByPrefix';

describe('src/pages/Send/utils/correctAddressByPrefix', () => {
it('should add the prefix when the address does not start with that', () => {
const address = 'address';
expect(correctAddressByPrefix(address, 'PREFIX-')).toBe(
`PREFIX-${address}`
);
});
it('sgould not duplicate the prefix when it is the start of the address', () => {
const address = 'PREFIX-address';
expect(correctAddressByPrefix(address, 'PREFIX-')).toBe(address);
});
});
3 changes: 3 additions & 0 deletions src/pages/Send/utils/correctAddressByPrefix.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export const correctAddressByPrefix = (address: string, prefix: string) => {
return !address.startsWith(prefix) ? `${prefix}${address}` : address;
};
4 changes: 0 additions & 4 deletions src/utils/isAddressValid.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,6 @@ describe('src/utils/isAddressValid.ts', () => {
});

describe('isValidPvmAddress', () => {
it('should return false if address does not start with P-', async () => {
const result = isValidPvmAddress(address);
expect(result).toEqual(false);
});
it('should return false if not valid', async () => {
const result = isValidPvmAddress(`P-testAddress}`);
expect(result).toEqual(false);
Expand Down
17 changes: 7 additions & 10 deletions src/utils/isAddressValid.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { isBech32Address } from '@avalabs/core-bridge-sdk';
import { isAddress } from 'ethers';
import { isNil, isString } from 'lodash';
import { stripAddressPrefix } from './stripAddressPrefix';
import { utils } from '@avalabs/avalanchejs';

Expand All @@ -20,15 +19,13 @@ export const isValidAvmAddress = (address: string) => {
return isValidXPAddressWithPrefix(address, 'X-');
};

function isValidXPAddressWithPrefix(value: unknown, forcedPrefix?: string) {
if (
isNil(value) ||
!isString(value) ||
(forcedPrefix && !value.startsWith(forcedPrefix))
) {
return false;
}
const addressBody = stripAddressPrefix(value);
function isValidXPAddressWithPrefix(value: string, forcedPrefix?: string) {
const address =
forcedPrefix && !value.startsWith(forcedPrefix)
? `${forcedPrefix}${value}`
: value;

const addressBody = stripAddressPrefix(address);
return isValidXPAddress(addressBody);
}

Expand Down
Loading