Skip to content

Commit fef971a

Browse files
committed
fix(sdk-coin-rune): validate gas amount
TICKET: COIN-2038
1 parent 75019d3 commit fef971a

File tree

3 files changed

+64
-6
lines changed

3 files changed

+64
-6
lines changed

modules/sdk-coin-rune/src/lib/utils.ts

Lines changed: 38 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -115,16 +115,49 @@ export class RuneUtils extends CosmosUtils {
115115
/** @inheritdoc */
116116
validateAmount(amount: Coin): void {
117117
const amountBig = BigNumber(amount.amount);
118-
if (amountBig.isLessThanOrEqualTo(0)) {
118+
if (amountBig.isNaN() || amountBig.isLessThanOrEqualTo(0)) {
119119
throw new InvalidTransactionError('transactionBuilder: validateAmount: Invalid amount: ' + amount.amount);
120120
}
121+
this.validateDenomination(amount.denom);
122+
}
123+
124+
/**
125+
* Validates the gas limit and gas amount for a transaction.
126+
* @param {FeeData} gasBudget - The gas budget to validate.
127+
* @throws {InvalidTransactionError} Throws an error if the gas budget is invalid.
128+
*/
129+
validateGasBudget(gasBudget: FeeData): void {
130+
if (gasBudget.gasLimit <= 0) {
131+
throw new InvalidTransactionError('Invalid gas limit ' + gasBudget.gasLimit);
132+
}
133+
this.validateGasAmountData(gasBudget.amount);
134+
}
135+
136+
/**
137+
* Validates an array of coin amounts.
138+
* @param {Coin[]} amountArray - The array of coin amounts to validate.
139+
*/
140+
validateGasAmountData(amountArray: Coin[]): void {
141+
amountArray.forEach((coinAmount) => {
142+
this.validateGasAmount(coinAmount);
143+
});
144+
}
145+
146+
validateGasAmount(amount: Coin): void {
147+
const amountBig = BigNumber(amount.amount);
148+
if (amountBig.isNaN() || amountBig.isLessThan(0)) {
149+
throw new InvalidTransactionError('transactionBuilder: validateAmount: Invalid amount: ' + amount.amount);
150+
}
151+
this.validateDenomination(amount.denom);
152+
}
153+
154+
validateDenomination(amountDenom: string): void {
121155
if (
122156
(this.networkType === NetworkType.TESTNET &&
123-
!constants.testnetValidDenoms.find((denom) => denom === amount.denom)) ||
124-
(this.networkType === NetworkType.MAINNET &&
125-
!constants.mainnetValidDenoms.find((denom) => denom === amount.denom))
157+
!constants.testnetValidDenoms.find((denom) => denom === amountDenom)) ||
158+
(this.networkType === NetworkType.MAINNET && !constants.mainnetValidDenoms.find((denom) => denom === amountDenom))
126159
) {
127-
throw new InvalidTransactionError('transactionBuilder: validateAmount: Invalid denom: ' + amount.denom);
160+
throw new InvalidTransactionError('transactionBuilder: validateAmount: Invalid denom: ' + amountDenom);
128161
}
129162
}
130163

modules/sdk-coin-rune/test/resources/rune.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,3 +47,10 @@ export const mainnetCoinAmounts = {
4747
amount4: { amount: '-1', denom: 'rune' },
4848
amount5: { amount: '1000000000', denom: 'arune' },
4949
};
50+
51+
export const mainnetGasAmounts = {
52+
positiveGasAmount: { amount: '100', denom: 'rune' },
53+
zeroGasAmount: { amount: '0', denom: 'rune' },
54+
emptyGasAmount: { amount: '', denom: 'rune' },
55+
alphabeticGasAmount: { amount: 'xyz', denom: 'rune' },
56+
};

modules/sdk-coin-rune/test/unit/utils.ts

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { NetworkType } from '@bitgo/statics';
22
import should from 'should';
33
import { RuneUtils } from '../../src/lib/utils';
44
import { MAINNET_ADDRESS_PREFIX, TESTNET_ADDRESS_PREFIX } from '../../src/lib/constants';
5-
import { blockHash, mainnetCoinAmounts, txIds, mainnetAddress } from '../resources/rune';
5+
import { blockHash, mainnetCoinAmounts, txIds, mainnetAddress, mainnetGasAmounts } from '../resources/rune';
66
import { testnetCoinAmounts, testnetAddress } from '../resources/trune';
77
const bech32 = require('bech32-buffer');
88

@@ -63,6 +63,24 @@ describe('utils', () => {
6363
);
6464
});
6565

66+
it('validate gas amount', function () {
67+
const gasBudget = { amount: [mainnetGasAmounts.positiveGasAmount], gasLimit: 1 };
68+
should.doesNotThrow(() => mainnetUtils.validateGasBudget(gasBudget));
69+
70+
gasBudget.amount[0] = mainnetGasAmounts.zeroGasAmount;
71+
should.doesNotThrow(() => mainnetUtils.validateGasBudget(gasBudget));
72+
73+
gasBudget.amount[0] = mainnetGasAmounts.emptyGasAmount;
74+
should(() => mainnetUtils.validateGasBudget(gasBudget)).throwError(
75+
'transactionBuilder: validateAmount: Invalid amount: '
76+
);
77+
78+
gasBudget.amount[0] = mainnetGasAmounts.alphabeticGasAmount;
79+
should(() => mainnetUtils.validateGasBudget(gasBudget)).throwError(
80+
'transactionBuilder: validateAmount: Invalid amount: xyz'
81+
);
82+
});
83+
6684
it('should validate mainnet address', () => {
6785
should.equal(mainnetUtils.isValidAddress(mainnetAddress.address1), true);
6886
should.equal(mainnetUtils.isValidAddress(mainnetAddress.validMemoIdAddress), true);

0 commit comments

Comments
 (0)