Skip to content
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
14 changes: 14 additions & 0 deletions packages/extension/src/libs/utils/number-formatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -517,6 +517,19 @@ const toBNSafe = (number: number) => {
return toBN(new BigNumber(number).toFixed(0));
};

/**
* Validates if a string represents a positive numeric value less than 2^256.
* Used for validating transaction amounts across different blockchain implementations.
* @param {string} value - The string value to validate
* @returns {boolean} - True if the value is a valid positive number within bounds
*/
const isNumericPositive = (value: string) => {
if (!value?.trim()) return false;
const num = BigNumber(value);
return !num.isNaN() && num.isPositive() && num.lt(new BigNumber(2).pow(256));
};


export {
formatIntegerToString,
formatIntegerValue,
Expand All @@ -526,4 +539,5 @@ export {
formatPercentageValue,
formatGasValue,
toBNSafe,
isNumericPositive
};
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,10 @@ import { BTCToken } from '../../types/btc-token';
import BigNumber from 'bignumber.js';
import { defaultGasCostVals } from '@/providers/common/libs/default-vals';
import { fromBase, toBase, isValidDecimals } from '@enkryptcom/utils';
import { formatFloatingPointValue } from '@/libs/utils/number-formatter';
import {
formatFloatingPointValue,
isNumericPositive,
} from '@/libs/utils/number-formatter';
import { routes as RouterNames } from '@/ui/action/router';
import getUiPath from '@/libs/utils/get-ui-path';
import Browser from 'webextension-polyfill';
Expand Down Expand Up @@ -218,6 +221,10 @@ onMounted(async () => {
});

const nativeBalanceAfterTransaction = computed(() => {
if (!isNumericPositive(sendAmount.value)) {
return toBN(0);
}

if (
selectedAsset.value &&
isValidDecimals(sendAmount.value, selectedAsset.value.decimals!)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,10 @@ import erc20 from '../../libs/abi/erc20';
import erc721 from '../../libs/abi/erc721';
import erc1155 from '../../libs/abi/erc1155';
import { SendTransactionDataType, VerifyTransactionParams } from '../types';
import { formatFloatingPointValue } from '@/libs/utils/number-formatter';
import {
formatFloatingPointValue,
isNumericPositive,
} from '@/libs/utils/number-formatter';
import { routes as RouterNames } from '@/ui/action/router';
import getUiPath from '@/libs/utils/get-ui-path';
import Browser from 'webextension-polyfill';
Expand Down Expand Up @@ -219,6 +222,11 @@ const hasEnoughBalance = computed(() => {
return false;
}

// check if valid sendAmount.value
if (!isNumericPositive(sendAmount.value)) {
return false;
}

return toBN(selectedAsset.value.balance ?? '0').gte(
toBN(toBase(sendAmount.value ?? '0', selectedAsset.value.decimals!)),
);
Expand Down Expand Up @@ -345,8 +353,9 @@ const nativeBalanceAfterTransaction = computed(() => {
let endingAmount = toBN(nativeBalance.value);

if (selectedAsset.value.contract === NATIVE_TOKEN_ADDRESS) {
const locAmount = isNumericPositive(amount.value) ? amount.value : '0';
const rawAmount = toBN(
toBase(amount.value, selectedAsset.value.decimals!),
toBase(locAmount ?? '0', selectedAsset.value.decimals!),
);
endingAmount = endingAmount.sub(rawAmount);
}
Expand Down Expand Up @@ -488,6 +497,7 @@ const isInputsValid = computed<boolean>(() => {
}
if (new BigNumber(sendAmount.value).gt(assetMaxValue.value)) return false;
if (gasCostValues.value.REGULAR.nativeValue === '0') return false;
if (!isNumericPositive(sendAmount.value)) return false;
return true;
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,10 @@ import BaseButton from '@action/components/base-button/index.vue';
import { AccountsHeaderData } from '@action/types/account';
import { GasFeeInfo } from '@/providers/ethereum/ui/types';
import { toBN } from 'web3-utils';
import { formatFloatingPointValue } from '@/libs/utils/number-formatter';
import {
formatFloatingPointValue,
isNumericPositive,
} from '@/libs/utils/number-formatter';
import { fromBase, toBase, isValidDecimals } from '@enkryptcom/utils';
import BigNumber from 'bignumber.js';
import { VerifyTransactionParams } from '../types';
Expand Down Expand Up @@ -235,6 +238,12 @@ const validateFields = async () => {
return;
}

if (!isNumericPositive(amount.value)) {
fieldsValidation.value.amount = false;
errorMsg.value = 'Invalid amount. Amount has to be greater than 0';
return;
}

rawAmount = toBN(
toBase(amount.value.toString(), selectedAsset.value.decimals!),
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,10 @@ import { AccountsHeaderData } from '@action/types/account';
import { GasFeeInfo } from '@/providers/ethereum/ui/types';
import { SubstrateNetwork } from '../../types/substrate-network';
import { toBN } from 'web3-utils';
import { formatFloatingPointValue } from '@/libs/utils/number-formatter';
import {
formatFloatingPointValue,
isNumericPositive,
} from '@/libs/utils/number-formatter';
import { fromBase, toBase, isValidDecimals } from '@enkryptcom/utils';
import BigNumber from 'bignumber.js';
import { AlertType, VerifyTransactionParams } from '../types';
Expand Down Expand Up @@ -195,7 +198,12 @@ const edWarn = computed(() => {
}

const rawAmount = toBN(
toBase(amount.value.toString(), selectedAsset.value.decimals ?? 0),
toBase(
isNumericPositive(amount.value.toString())
? amount.value.toString()
: '0',
selectedAsset.value.decimals ?? 0,
),
);
const ed = selectedAsset.value.existentialDeposit ?? toBN(0);
const userBalance = toBN(selectedAsset.value.balance ?? 0);
Expand Down Expand Up @@ -246,7 +254,9 @@ const validateFields = async () => {

let rawAmount = toBN(
toBase(
amount.value ? amount.value.toString() : '0',
amount.value && isNumericPositive(amount.value.toString())
? amount.value.toString()
: '0',
selectedAsset.value.decimals!,
),
);
Expand Down Expand Up @@ -448,9 +458,10 @@ const destinationBalanceCheck = computed(() => {
if (selectedAsset.value.symbol !== accountAssets.value[0].symbol)
return destinationHasEnough.value;
else {
const checkedValue = amount.value?.toString() ?? '0';
const rawAmount = toBN(
toBase(
amount.value?.toString() ?? '0',
isNumericPositive(checkedValue) ? checkedValue : '0',
selectedAsset.value.decimals ?? 0,
),
);
Expand All @@ -475,6 +486,7 @@ const isDisabled = computed(() => {
if (
amount.value !== undefined &&
amount.value !== '' &&
isNumericPositive(amount.value) &&
hasEnough.value &&
addressIsValid &&
!edWarn.value &&
Expand Down