Skip to content

Commit

Permalink
fix: let use the p and x addresses without prefix
Browse files Browse the repository at this point in the history
  • Loading branch information
vvava committed Oct 29, 2024
1 parent e19795e commit a089dbb
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 12 deletions.
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;
};
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

0 comments on commit a089dbb

Please sign in to comment.