Skip to content

fix(sdk-coin-trx): handling string type for scanning factor #6108

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 9, 2025
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
7 changes: 6 additions & 1 deletion modules/sdk-coin-trx/src/trx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -586,10 +586,15 @@ export class Trx extends BaseCoin {
} else if (!isInteger(startIdx) || startIdx < 0) {
throw new Error('Invalid starting index to scan for addresses');
}

let numIteration = params.scan;
if (isUndefined(numIteration)) {
numIteration = 20;
} else if (!isInteger(numIteration) || numIteration <= 0) {
} else if (typeof numIteration === 'string') {
numIteration = parseInt(numIteration, 10);
}

if (!isInteger(numIteration) || numIteration <= 0) {
throw new Error('Invalid scanning factor');
}

Expand Down
72 changes: 72 additions & 0 deletions modules/sdk-coin-trx/test/unit/trx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,78 @@ describe('TRON:', function () {
assert.equal(Utils.getBase58AddressFromHex(value.to_address), TestRecoverData.recoveryDestination);
});

it('should recover trx from base address to recovery address with scan passed as valid integer string', async function () {
mock.method(Trx.prototype as any, 'getAccountBalancesFromNode', (...args) => {
if (args.length > 0 && args[0] === TestRecoverData.baseAddress) {
return Promise.resolve(baseAddressBalance(3000000));
}

return undefined;
});

const baseAddrHex = Utils.getHexAddressFromBase58Address(TestRecoverData.baseAddress);
const destinationHex = Utils.getHexAddressFromBase58Address(TestRecoverData.recoveryDestination);

mock.method(Trx.prototype as any, 'getBuildTransaction', (...args) => {
if (args.length > 0 && args[0] === destinationHex && args[1] === baseAddrHex && args[2] === 900000) {
return Promise.resolve(creationTransaction(baseAddrHex, destinationHex, 900000));
}

return undefined;
});

const res = await basecoin.recover({
userKey: TestRecoverData.userKey,
backupKey: TestRecoverData.backupKey,
bitgoKey: TestRecoverData.bitgoKey,
recoveryDestination: TestRecoverData.recoveryDestination,
scan: '10',
});
assert.notEqual(res.length, 0);
assert.ok(Object.prototype.hasOwnProperty.call(res, 'txHex'));
assert.ok(Object.prototype.hasOwnProperty.call(res, 'feeInfo'));
const rawData = JSON.parse(res.txHex).raw_data;
assert.ok(Object.prototype.hasOwnProperty.call(rawData, 'contract'));
const value = rawData.contract[0].parameter.value;
assert.equal(value.amount, 900000);
assert.equal(Utils.getBase58AddressFromHex(value.owner_address), TestRecoverData.baseAddress);
assert.equal(Utils.getBase58AddressFromHex(value.to_address), TestRecoverData.recoveryDestination);
});

it('should fail recover trx from base address to recovery address with scan passed as valid integer string', async function () {
mock.method(Trx.prototype as any, 'getAccountBalancesFromNode', (...args) => {
if (args.length > 0 && args[0] === TestRecoverData.baseAddress) {
return Promise.resolve(baseAddressBalance(3000000));
}

return undefined;
});

const baseAddrHex = Utils.getHexAddressFromBase58Address(TestRecoverData.baseAddress);
const destinationHex = Utils.getHexAddressFromBase58Address(TestRecoverData.recoveryDestination);

mock.method(Trx.prototype as any, 'getBuildTransaction', (...args) => {
if (args.length > 0 && args[0] === destinationHex && args[1] === baseAddrHex && args[2] === 900000) {
return Promise.resolve(creationTransaction(baseAddrHex, destinationHex, 900000));
}

return undefined;
});

await assert.rejects(
basecoin.recover({
userKey: TestRecoverData.userKey,
backupKey: TestRecoverData.backupKey,
bitgoKey: TestRecoverData.bitgoKey,
recoveryDestination: TestRecoverData.recoveryDestination,
scan: 'abc',
}),
{
message: 'Invalid scanning factor',
}
);
});

it('should recover trx from receive address to base address', async function () {
mock.method(Trx.prototype as any, 'getAccountBalancesFromNode', (...args) => {
if (args.length > 0 && args[0] === TestRecoverData.baseAddress) {
Expand Down