From f9a4c4c714bc23de77d400d85f163852df3d54bd Mon Sep 17 00:00:00 2001 From: Assaf Morami Date: Wed, 8 Nov 2023 14:41:05 +0200 Subject: [PATCH 1/3] Fix `bytesToNumber()` This fixes `bytesToNumber()` to always return the result as an unsigned integer. Example: `byteArray` holds the value `2^32 - 3`, which is `11111111111111111111111111111101` in binary. The old implementation uses the `number` type, which is a signed integer, to calculate intermediate values. And because the most significant bit (MSB) is turned on, it interprets this as a negative number and returns `-3`. --- src/utils.ts | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/src/utils.ts b/src/utils.ts index a24bda6..37de02f 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -21,14 +21,13 @@ export const numberToBytes = (uint32Value: number) => { return byteArray; }; -export const bytesToNumber = (byteArray: Uint8Array): number => { - let uint32Value = 0; +export const bytesToNumber = function (byteArray: Uint8Array): number { + const length = byteArray.length; - for (let i = 0; i < byteArray.length; i++) { - uint32Value |= byteArray[i] << (8 * (byteArray.length - 1 - i)); - } + const buffer = Buffer.from(byteArray); + const result = buffer.readUIntBE(0, length); - return uint32Value; + return result; }; export const isAddress = function (address: string) { From 39c023776dc140864b19fa73f00e905b4b4c63e5 Mon Sep 17 00:00:00 2001 From: Assaf Morami Date: Thu, 9 Nov 2023 14:34:50 +0200 Subject: [PATCH 2/3] Fix `bytesToNumber()` in case of 0/false --- src/utils.ts | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/utils.ts b/src/utils.ts index 37de02f..ce6de12 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -22,6 +22,10 @@ export const numberToBytes = (uint32Value: number) => { }; export const bytesToNumber = function (byteArray: Uint8Array): number { + if (!byteArray || byteArray?.length === 0) { + return 0; + } + const length = byteArray.length; const buffer = Buffer.from(byteArray); From 35dc65dccb3df81c14da4a7b7b06ec0c4df2f4a6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20=27birdy=27=20Danjou?= Date: Fri, 10 Nov 2023 16:27:45 +0100 Subject: [PATCH 3/3] test() add test for utils --- src/utils.test.ts | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 src/utils.test.ts diff --git a/src/utils.test.ts b/src/utils.test.ts new file mode 100644 index 0000000..107571e --- /dev/null +++ b/src/utils.test.ts @@ -0,0 +1,23 @@ +import { numberToBytes, bytesToNumber } from './utils'; + +describe('decrypt', () => { + it('converts a number to bytes', async () => { + const value = 28482; + const bytes = numberToBytes(value); + expect(bytes).toEqual(new Uint8Array([111, 66])); + + const value2 = 255; + const bytes2 = numberToBytes(value2); + expect(bytes2).toEqual(new Uint8Array([255])); + }); + + it('converts bytes to number', async () => { + const value = new Uint8Array([23, 200, 15]); + const bytes = bytesToNumber(value); + expect(bytes).toBe(1558543); + + const value2 = new Uint8Array(); + const bytes2 = bytesToNumber(value2); + expect(bytes2).toBe(0); + }); +});