Skip to content

Commit

Permalink
fix: Repair limitation
Browse files Browse the repository at this point in the history
  • Loading branch information
richtera committed Jul 9, 2024
1 parent 2874516 commit b0ffa5b
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 5 deletions.
22 changes: 17 additions & 5 deletions src/lib/encodeKeyName.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 <bytesM> is called with wrong number of bytes', () => {
// assert.throws(() =>
// encodeDynamicKeyPart('<bytes8>', '0xd1b2917d26eeeaad1234', 20),
// );
// });
it('throws if <bytesM> is called with wrong number of bytes', () => {
assert.throws(() =>
encodeDynamicKeyPart('<bytes8>', '0xd1b2917d26eeeaad1234', 20),
);
});

it('throws if <uintN> is called with too large of a number', () => {
assert.throws(() => encodeDynamicKeyPart('<uint8>', '0x100', 20));
});

it('throws if <intN> is is used because intN is not supported (positive number)', () => {
assert.throws(() => encodeDynamicKeyPart('<int8>', '0x100', 20));
});

it('throws if <intN> is is used because intN is not supported (negative number)', () => {
assert.throws(() => encodeDynamicKeyPart('<int8>', '0xFFFFFFFF100', 20));
});
});

describe('generateDynamicKeyName', () => {
Expand Down
8 changes: 8 additions & 0 deletions src/lib/encodeKeyName.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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:
Expand Down

0 comments on commit b0ffa5b

Please sign in to comment.