Skip to content

Commit 9f8a074

Browse files
authored
Merge pull request #6165 from BitGo/WIN-5606
fix(sdk-coin-icp): enhance transaction ID generation with 32-bit range checks
2 parents 40ff3b5 + e25c65c commit 9f8a074

File tree

2 files changed

+25
-3
lines changed

2 files changed

+25
-3
lines changed

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -706,8 +706,10 @@ export class Utils implements BaseUtils {
706706
}
707707

708708
if (typeof value === 'number') {
709-
const isUnsafe = value > Number.MAX_SAFE_INTEGER || value < Number.MIN_SAFE_INTEGER;
710-
return isUnsafe ? BigInt(value) : value;
709+
const MAX_32BIT = 4294967295; // 2^32 - 1
710+
const MIN_32BIT = -4294967296; // -(2^32)
711+
const isOutside32BitRange = value > MAX_32BIT || value < MIN_32BIT;
712+
return isOutside32BitRange ? BigInt(value) : value;
711713
}
712714

713715
throw new Error(`Invalid type: expected a number or bigint, but received ${typeof value}`);
@@ -720,7 +722,7 @@ export class Utils implements BaseUtils {
720722
const transferFields = new Map<any, any>([
721723
[0, senderAccount],
722724
[1, receiverAccount],
723-
[2, new Map([[0, this.safeBigInt(Number(sendArgs.payment.receiverGets.e8s))]])],
725+
[2, new Map([[0, this.safeBigInt(sendArgs.payment.receiverGets.e8s)]])],
724726
[3, new Map([[0, sendArgs.maxFee.e8s]])],
725727
]);
726728

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,4 +207,24 @@ describe('utils', () => {
207207
should.throws(() => utils.validateRawTransaction(data), 'amount cannot be less than or equal to zero');
208208
});
209209
});
210+
211+
describe('getTransactionId()', () => {
212+
const sender = '47867f2cfb85094275c847435fa10cad54a813eba7e6a9bc3538aa2f537f1d73';
213+
const receiver = 'a1c60efca988c411cd7bc5e481364b9c94caebb24c00e01db269e3a0541ee498';
214+
it('should return the correct transaction hash for amount less than 2^32 (amount = 1000)', () => {
215+
const unsignedTransaction =
216+
'b90002677570646174657381826b5452414e53414354494f4eb900056b63616e69737465725f69644a000000000000000201016b6d6574686f645f6e616d656773656e645f70626361726758400a02080012050a0308e8071a0308904e2a220a20a1c60efca988c411cd7bc5e481364b9c94caebb24c00e01db269e3a0541ee4983a0a088084a8a988e4f4a0186673656e646572581da905b86bba9bfed194ac8c12377b5d48847128dbfff10f01adb80f7f026e696e67726573735f6578706972791b000000000000000070696e67726573735f6578706972696573811b1841d35866476200';
217+
const transactionHash = utils.getTransactionId(unsignedTransaction, sender, receiver);
218+
const expectedTransactionHash = 'bb502d0566f726da02a1925415f68cb6f175cdd6cba1e09823143078715b2ba4';
219+
should.equal(transactionHash, expectedTransactionHash);
220+
});
221+
222+
it('should return the correct transaction hash for amount greater than 2^32 (amount = 6948150200)', () => {
223+
const unsignedTransaction =
224+
'b90002677570646174657381826b5452414e53414354494f4eb900056b63616e69737465725f69644a000000000000000201016b6d6574686f645f6e616d656773656e645f70626361726758430a02080012080a0608b8b791f1191a0308904e2a220a20a1c60efca988c411cd7bc5e481364b9c94caebb24c00e01db269e3a0541ee4983a0a0880b8a789bfebf4a0186673656e646572581da905b86bba9bfed194ac8c12377b5d48847128dbfff10f01adb80f7f026e696e67726573735f6578706972791b000000000000000070696e67726573735f6578706972696573811b1841d393d2473c00';
225+
const transactionHash = utils.getTransactionId(unsignedTransaction, sender, receiver);
226+
const expectedTransactionHash = '016411110006d4645cbcb553a84efdec6c402847f856caa909111d81bb513565';
227+
should.equal(transactionHash, expectedTransactionHash);
228+
});
229+
});
210230
});

0 commit comments

Comments
 (0)