From b0ffa5b1051015268c552b53b4334e0795295e1d Mon Sep 17 00:00:00 2001 From: Andreas Richter <708186+richtera@users.noreply.github.com> Date: Tue, 9 Jul 2024 09:42:30 -0400 Subject: [PATCH] fix: Repair limitation --- src/lib/encodeKeyName.test.ts | 22 +++++++++++++++++----- src/lib/encodeKeyName.ts | 8 ++++++++ 2 files changed, 25 insertions(+), 5 deletions(-) diff --git a/src/lib/encodeKeyName.test.ts b/src/lib/encodeKeyName.test.ts index 30cf2e27..a0adb20d 100644 --- a/src/lib/encodeKeyName.test.ts +++ b/src/lib/encodeKeyName.test.ts @@ -399,11 +399,23 @@ describe('encodeDynamicKeyPart', () => { // Since we're allowing things to be truncated and right and left padded // This doesn't seem an ideal test case. - // it('throws if is called with wrong number of bytes', () => { - // assert.throws(() => - // encodeDynamicKeyPart('', '0xd1b2917d26eeeaad1234', 20), - // ); - // }); + it('throws if is called with wrong number of bytes', () => { + assert.throws(() => + encodeDynamicKeyPart('', '0xd1b2917d26eeeaad1234', 20), + ); + }); + + it('throws if is called with too large of a number', () => { + assert.throws(() => encodeDynamicKeyPart('', '0x100', 20)); + }); + + it('throws if is is used because intN is not supported (positive number)', () => { + assert.throws(() => encodeDynamicKeyPart('', '0x100', 20)); + }); + + it('throws if is is used because intN is not supported (negative number)', () => { + assert.throws(() => encodeDynamicKeyPart('', '0xFFFFFFFF100', 20)); + }); }); describe('generateDynamicKeyName', () => { diff --git a/src/lib/encodeKeyName.ts b/src/lib/encodeKeyName.ts index e7caf1f5..5ab705ef 100644 --- a/src/lib/encodeKeyName.ts +++ b/src/lib/encodeKeyName.ts @@ -98,6 +98,9 @@ export const encodeDynamicKeyPart = ( // e.g.: uint8 max value is 255, uint16 is 65535... let hex = numberToHex(value).slice(2); + if (hex.length > size / 4) { + throw new Error(`Value: ${value} is too big for uint${size}.`); + } if (hex.length > bytes * 2) { hex = `0x${hex.slice(-bytes * 2)}`; } else { @@ -117,6 +120,11 @@ export const encodeDynamicKeyPart = ( `Wrong value: ${value} for dynamic key with type: $type. Value is not in hex.`, ); } + if (value.length > 2 + size * 2) { + throw new Error( + `Wrong value: ${value} for dynamic key with type: $type. Value is too big.`, + ); + } return padRight(value.slice(0, 2 + bytes * 2), bytes * 2).slice(2); } default: