From 74fd983fb37ee26fc20a075649650767787531cd Mon Sep 17 00:00:00 2001 From: neeboo Date: Sun, 4 Sep 2022 14:03:10 +0800 Subject: [PATCH 1/4] :bug: fix fromHex when hex string length potentially not to be even, to prevent BigInt(n) failure --- packages/agent/src/utils/buffer.ts | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/packages/agent/src/utils/buffer.ts b/packages/agent/src/utils/buffer.ts index 4db40bdd7..883001a3b 100644 --- a/packages/agent/src/utils/buffer.ts +++ b/packages/agent/src/utils/buffer.ts @@ -27,6 +27,12 @@ const hexRe = new RegExp(/^([0-9A-F]{2})*$/i); * @param hex The hexadecimal string to use. */ export function fromHex(hex: string): ArrayBuffer { + //FIXME: always padding hex string length to be even + // then run the reg test + if (hex.length % 2 !== 0) { + hex = '0' + hex; + } + if (!hexRe.test(hex)) { throw new Error('Invalid hexadecimal string.'); } From 76df16ec74da978a24e095b56be0a71b4be3ce58 Mon Sep 17 00:00:00 2001 From: neeboo Date: Sun, 4 Sep 2022 14:12:24 +0800 Subject: [PATCH 2/4] :white_check_mark: adding test to cbor --- packages/agent/src/cbor.test.ts | 76 +++++++++++++++++++++++++++++++++ 1 file changed, 76 insertions(+) diff --git a/packages/agent/src/cbor.test.ts b/packages/agent/src/cbor.test.ts index b36c8bfa6..b49dd1821 100644 --- a/packages/agent/src/cbor.test.ts +++ b/packages/agent/src/cbor.test.ts @@ -54,6 +54,82 @@ test('empty canister ID', () => { expect(Principal.fromUint8Array(outputA as any).toText()).toBe('aaaaa-aa'); }); +describe('cbor encode/decode bigint', () => { + test('encode decode 0 to 1_000_000', () => { + for (let i = 0; i < 1_000_000; i += 1) { + let buffer; + + try { + buffer = encode(BigInt(i)); + } catch (error) { + console.log(`error encoding on ${i.toString()}`); + expect((error as Error).message).toBeTruthy(); + } + + try { + const decoded = decode(buffer); + expect(decoded).toBe(BigInt(i)); + } catch (error) { + console.log(`error decoding on ${i.toString()}`); + expect((error as Error).message).toBeTruthy(); + } + // if (i % 100000 === 0) { + // console.log(`${new Date(Date.now()).toLocaleTimeString()} with ${i.toString()}`); + // } + } + }); + test('encode decode 1_000_000n to 1_000_000_000_000n', () => { + for (let i = 1_000_000; i < 1_000_000_000_000; i += 1_000_000) { + let buffer; + + const random_num = Math.floor(Math.random() * Math.pow(10, i.toString().length - 1)); + + const actual = BigInt(i + random_num); + try { + buffer = encode(actual); + } catch (error) { + console.log(`error encoding on ${i.toString()}`); + expect((error as Error).message).toBeTruthy(); + } + + try { + const decoded = decode(buffer); + expect(decoded).toBe(actual); + } catch (error) { + console.log(`error decoding on ${i.toString()}`); + expect((error as Error).message).toBeTruthy(); + } + // if (i % 1_000_000_000_00 === 0) { + // console.log(`${new Date(Date.now()).toLocaleTimeString()} with ${actual.toString()}`); + // } + } + }); + test('encode decode 1_000_000_000_000n to 1_000_000_000_000_000_000n', () => { + for (let i = 1_000_000_000_000n; i < 1_000_000_000_000_000_000n; i += 1_000_000_000_000n) { + let buffer; + const random_num = BigInt(Math.floor(Math.random() * Math.pow(10, i.toString().length - 1))); + const actual = i + random_num; + try { + buffer = encode(actual); + } catch (error) { + console.log(`error encoding on ${i.toString()}`); + expect((error as Error).message).toBeTruthy(); + } + + try { + const decoded = decode(buffer); + expect(decoded).toBe(actual); + } catch (error) { + console.log(`error decoding on ${i.toString()}`); + expect((error as Error).message).toBeTruthy(); + } + // if (i % 1_000_000_000_000_000_00n === 0n) { + // console.log(`${new Date(Date.now()).toLocaleTimeString()} with ${actual.toString()}`); + // } + } + }); +}); + function buf2hex(buffer: Uint8Array) { // Construct an array such that each number is translated to the // hexadecimal equivalent, ensure it is a string and padded then From 9bc956832379613e86578d6d36cb6b7bcd730cea Mon Sep 17 00:00:00 2001 From: neeboo Date: Sun, 4 Sep 2022 14:17:29 +0800 Subject: [PATCH 3/4] fix: adding test to cbor --- packages/agent/src/cbor.test.ts | 3 +++ 1 file changed, 3 insertions(+) diff --git a/packages/agent/src/cbor.test.ts b/packages/agent/src/cbor.test.ts index b49dd1821..6a1ea691c 100644 --- a/packages/agent/src/cbor.test.ts +++ b/packages/agent/src/cbor.test.ts @@ -73,6 +73,7 @@ describe('cbor encode/decode bigint', () => { console.log(`error decoding on ${i.toString()}`); expect((error as Error).message).toBeTruthy(); } + // // if we log the time // if (i % 100000 === 0) { // console.log(`${new Date(Date.now()).toLocaleTimeString()} with ${i.toString()}`); // } @@ -99,6 +100,7 @@ describe('cbor encode/decode bigint', () => { console.log(`error decoding on ${i.toString()}`); expect((error as Error).message).toBeTruthy(); } + // // if we log the time // if (i % 1_000_000_000_00 === 0) { // console.log(`${new Date(Date.now()).toLocaleTimeString()} with ${actual.toString()}`); // } @@ -123,6 +125,7 @@ describe('cbor encode/decode bigint', () => { console.log(`error decoding on ${i.toString()}`); expect((error as Error).message).toBeTruthy(); } + // // if we log the time // if (i % 1_000_000_000_000_000_00n === 0n) { // console.log(`${new Date(Date.now()).toLocaleTimeString()} with ${actual.toString()}`); // } From bd214d6ffc474ca3b27dcbb92a652501dff49fa9 Mon Sep 17 00:00:00 2001 From: neeboo Date: Wed, 21 Sep 2022 11:57:27 +0800 Subject: [PATCH 4/4] Update packages/agent/src/cbor.test.ts Co-authored-by: Kyle Peacock --- packages/agent/src/cbor.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/agent/src/cbor.test.ts b/packages/agent/src/cbor.test.ts index 6a1ea691c..c5bfc267e 100644 --- a/packages/agent/src/cbor.test.ts +++ b/packages/agent/src/cbor.test.ts @@ -107,7 +107,7 @@ describe('cbor encode/decode bigint', () => { } }); test('encode decode 1_000_000_000_000n to 1_000_000_000_000_000_000n', () => { - for (let i = 1_000_000_000_000n; i < 1_000_000_000_000_000_000n; i += 1_000_000_000_000n) { + for (let i = BigInt(1_000_000_000_000); i < BigInt(1_000_000_000_000_000_000); i += BigInt(1_000_000_000_000)) { let buffer; const random_num = BigInt(Math.floor(Math.random() * Math.pow(10, i.toString().length - 1))); const actual = i + random_num;