Skip to content

Commit 061641d

Browse files
committed
fix(express): validate amount as integer type in sendCoins
Improve error message for integer validation in sendCoins functionality Ticket: WIN-4726
1 parent edd5b27 commit 061641d

File tree

5 files changed

+82
-9
lines changed

5 files changed

+82
-9
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/express/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@
5959
"@bitgo/public-types": "5.1.0",
6060
"@bitgo/sdk-lib-mpc": "^10.2.0",
6161
"@bitgo/sdk-test": "^8.0.84",
62+
"@bitgo/sdk-api": "^1.62.3",
6263
"@types/argparse": "^1.0.36",
6364
"@types/body-parser": "^1.17.0",
6465
"@types/express": "4.17.13",
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import 'should';
2+
import { BitGoAPI } from '@bitgo/sdk-api';
3+
4+
describe('Wallet Send Coins Amount Validation', () => {
5+
let bitgo;
6+
let wallet;
7+
const walletId = 'TEST_WALLET_ID';
8+
9+
before(async function () {
10+
bitgo = new BitGoAPI({ env: 'test' });
11+
12+
// Create a wallet using newWalletObject
13+
wallet = bitgo.newWalletObject({ id: walletId });
14+
});
15+
16+
it('should throw an error when sending decimal amount', async function () {
17+
try {
18+
await wallet.sendCoins({
19+
address: 'TEST_WALLET1_ADDRESS',
20+
amount: 0.5, // Decimal amount
21+
walletPassphrase: 'dummy',
22+
});
23+
} catch (error) {
24+
error.message.should.equal('invalid argument for amount - Integer expected');
25+
}
26+
});
27+
});

modules/sdk-api/src/v1/wallet.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1096,9 +1096,8 @@ Wallet.prototype.confirmInviteAndShareWallet = function (params, callback) {
10961096
Wallet.prototype.sendCoins = function (params, callback) {
10971097
params = params || {};
10981098
common.validateParams(params, ['address'], ['message'], callback);
1099-
1100-
if (!_.isNumber(params.amount)) {
1101-
throw new Error('invalid argument for amount - number expected');
1099+
if (!_.isInteger(params.amount)) {
1100+
throw new Error('invalid argument for amount - Integer expected');
11021101
}
11031102

11041103
params.recipients = {};

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)