Skip to content

Commit c25e860

Browse files
authored
fix(coins): Validate amount as integer type in sendCoins
2 parents 972f9cc + c571b13 commit c25e860

File tree

2 files changed

+52
-6
lines changed

2 files changed

+52
-6
lines changed

modules/bitgo/test/v2/unit/wallet.ts

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4893,4 +4893,46 @@ describe('V2 Wallet:', function () {
48934893
await wallet.approveErc20Token(walletPassphrase, tokenName).should.be.rejectedWith(sendError);
48944894
});
48954895
});
4896+
4897+
describe('Amount Validation for sendCoins', function () {
4898+
let wallet;
4899+
let params;
4900+
4901+
before(function () {
4902+
const basecoin = bitgo.coin('tbtc');
4903+
wallet = new Wallet(bitgo, basecoin, {
4904+
id: '5b34252f1bf349930e34020a',
4905+
coin: 'tbtc',
4906+
keys: ['5b3424f91bf349930e340175'],
4907+
});
4908+
params = {
4909+
address: '2N4Xz4itCdKKUREiytEJ1KGPoEnKHmJUIwq',
4910+
walletPassphrase: 'test123',
4911+
};
4912+
});
4913+
4914+
it('should reject decimal amounts in send coins', async function () {
4915+
params.amount = 150000000.55;
4916+
4917+
await wallet
4918+
.send(params)
4919+
.should.be.rejectedWith('invalid argument for amount - Integer greater than zero or numeric string expected');
4920+
});
4921+
4922+
it('should reject negative amount in send coins', async function () {
4923+
params.amount = '-12';
4924+
4925+
await wallet
4926+
.send(params)
4927+
.should.be.rejectedWith('invalid argument for amount - Integer greater than zero or numeric string expected');
4928+
});
4929+
4930+
it('should reject zero in send coins', async function () {
4931+
params.amount = '0';
4932+
4933+
await wallet
4934+
.send(params)
4935+
.should.be.rejectedWith('invalid argument for amount - Integer greater than zero or numeric string expected');
4936+
});
4937+
});
48964938
});

modules/sdk-core/src/bitgo/wallet/wallet.ts

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2408,13 +2408,17 @@ export class Wallet implements IWallet {
24082408
const coin = this.baseCoin;
24092409

24102410
const amount = new BigNumber(params.amount);
2411-
if (amount.isNegative()) {
2412-
throw new Error('invalid argument for amount - positive number greater than zero or numeric string expected');
2413-
}
24142411

2415-
if (!coin.valuelessTransferAllowed() && amount.isZero()) {
2416-
throw new Error('invalid argument for amount - positive number greater than zero or numeric string expected');
2417-
}
2412+
const isAmountNegative = amount.isNegative();
2413+
const isAmountZero = amount.isZero();
2414+
const isAmountDecimal = !amount.isInteger();
2415+
2416+
_.some([isAmountNegative, !coin.valuelessTransferAllowed() && isAmountZero, isAmountDecimal], (condition) => {
2417+
if (condition) {
2418+
throw new Error('invalid argument for amount - Integer greater than zero or numeric string expected');
2419+
}
2420+
});
2421+
24182422
const recipients: SendManyOptions['recipients'] = [
24192423
{
24202424
address: params.address,

0 commit comments

Comments
 (0)