Skip to content

Commit 7de803e

Browse files
Merge pull request #6108 from BitGo/WIN-5440
fix(sdk-coin-trx): handling string type for scanning factor
2 parents 398815d + e465870 commit 7de803e

File tree

2 files changed

+78
-1
lines changed

2 files changed

+78
-1
lines changed

modules/sdk-coin-trx/src/trx.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -586,10 +586,15 @@ export class Trx extends BaseCoin {
586586
} else if (!isInteger(startIdx) || startIdx < 0) {
587587
throw new Error('Invalid starting index to scan for addresses');
588588
}
589+
589590
let numIteration = params.scan;
590591
if (isUndefined(numIteration)) {
591592
numIteration = 20;
592-
} else if (!isInteger(numIteration) || numIteration <= 0) {
593+
} else if (typeof numIteration === 'string') {
594+
numIteration = parseInt(numIteration, 10);
595+
}
596+
597+
if (!isInteger(numIteration) || numIteration <= 0) {
593598
throw new Error('Invalid scanning factor');
594599
}
595600

modules/sdk-coin-trx/test/unit/trx.ts

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,78 @@ describe('TRON:', function () {
232232
assert.equal(Utils.getBase58AddressFromHex(value.to_address), TestRecoverData.recoveryDestination);
233233
});
234234

235+
it('should recover trx from base address to recovery address with scan passed as valid integer string', async function () {
236+
mock.method(Trx.prototype as any, 'getAccountBalancesFromNode', (...args) => {
237+
if (args.length > 0 && args[0] === TestRecoverData.baseAddress) {
238+
return Promise.resolve(baseAddressBalance(3000000));
239+
}
240+
241+
return undefined;
242+
});
243+
244+
const baseAddrHex = Utils.getHexAddressFromBase58Address(TestRecoverData.baseAddress);
245+
const destinationHex = Utils.getHexAddressFromBase58Address(TestRecoverData.recoveryDestination);
246+
247+
mock.method(Trx.prototype as any, 'getBuildTransaction', (...args) => {
248+
if (args.length > 0 && args[0] === destinationHex && args[1] === baseAddrHex && args[2] === 900000) {
249+
return Promise.resolve(creationTransaction(baseAddrHex, destinationHex, 900000));
250+
}
251+
252+
return undefined;
253+
});
254+
255+
const res = await basecoin.recover({
256+
userKey: TestRecoverData.userKey,
257+
backupKey: TestRecoverData.backupKey,
258+
bitgoKey: TestRecoverData.bitgoKey,
259+
recoveryDestination: TestRecoverData.recoveryDestination,
260+
scan: '10',
261+
});
262+
assert.notEqual(res.length, 0);
263+
assert.ok(Object.prototype.hasOwnProperty.call(res, 'txHex'));
264+
assert.ok(Object.prototype.hasOwnProperty.call(res, 'feeInfo'));
265+
const rawData = JSON.parse(res.txHex).raw_data;
266+
assert.ok(Object.prototype.hasOwnProperty.call(rawData, 'contract'));
267+
const value = rawData.contract[0].parameter.value;
268+
assert.equal(value.amount, 900000);
269+
assert.equal(Utils.getBase58AddressFromHex(value.owner_address), TestRecoverData.baseAddress);
270+
assert.equal(Utils.getBase58AddressFromHex(value.to_address), TestRecoverData.recoveryDestination);
271+
});
272+
273+
it('should fail recover trx from base address to recovery address with scan passed as valid integer string', async function () {
274+
mock.method(Trx.prototype as any, 'getAccountBalancesFromNode', (...args) => {
275+
if (args.length > 0 && args[0] === TestRecoverData.baseAddress) {
276+
return Promise.resolve(baseAddressBalance(3000000));
277+
}
278+
279+
return undefined;
280+
});
281+
282+
const baseAddrHex = Utils.getHexAddressFromBase58Address(TestRecoverData.baseAddress);
283+
const destinationHex = Utils.getHexAddressFromBase58Address(TestRecoverData.recoveryDestination);
284+
285+
mock.method(Trx.prototype as any, 'getBuildTransaction', (...args) => {
286+
if (args.length > 0 && args[0] === destinationHex && args[1] === baseAddrHex && args[2] === 900000) {
287+
return Promise.resolve(creationTransaction(baseAddrHex, destinationHex, 900000));
288+
}
289+
290+
return undefined;
291+
});
292+
293+
await assert.rejects(
294+
basecoin.recover({
295+
userKey: TestRecoverData.userKey,
296+
backupKey: TestRecoverData.backupKey,
297+
bitgoKey: TestRecoverData.bitgoKey,
298+
recoveryDestination: TestRecoverData.recoveryDestination,
299+
scan: 'abc',
300+
}),
301+
{
302+
message: 'Invalid scanning factor',
303+
}
304+
);
305+
});
306+
235307
it('should recover trx from receive address to base address', async function () {
236308
mock.method(Trx.prototype as any, 'getAccountBalancesFromNode', (...args) => {
237309
if (args.length > 0 && args[0] === TestRecoverData.baseAddress) {

0 commit comments

Comments
 (0)