Skip to content

Commit

Permalink
Only tightly pack bytesN values
Browse files Browse the repository at this point in the history
  • Loading branch information
Mrtenz committed Oct 10, 2022
1 parent 03f8125 commit bba5eb6
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
16 changes: 16 additions & 0 deletions src/parsers/array.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,22 @@ describe('array', () => {
);
});

it('tightly encodes a packed number array', () => {
expect(
bytesToHex(
array.encode({
type: 'uint256[]',
value: [BigInt(12), BigInt(34), BigInt(56), BigInt(78)],
buffer: new Uint8Array(),
packed: true,
tight: true,
}),
),
).toBe(
'0x000000000000000000000000000000000000000000000000000000000000000c00000000000000000000000000000000000000000000000000000000000000220000000000000000000000000000000000000000000000000000000000000038000000000000000000000000000000000000000000000000000000000000004e',
);
});

it('encodes a packed bytes1 array', () => {
expect(
bytesToHex(
Expand Down
5 changes: 3 additions & 2 deletions src/parsers/array.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { padStart } from '../utils';
import { ParserError } from '../errors';
import { Parser } from './parser';
import { tuple } from './tuple';
import { fixedBytes } from './fixed-bytes';

const ARRAY_REGEX = /^(?<type>.*)\[(?<length>\d*?)\]$/u;

Expand Down Expand Up @@ -144,7 +145,7 @@ export const array: Parser<unknown[]> = {
type: getTupleType(arrayType, fixedLength),
buffer,
value,
packed: tight,
packed: fixedBytes.isType(arrayType) && tight,
tight,
});
}
Expand All @@ -156,7 +157,7 @@ export const array: Parser<unknown[]> = {
types: new Array(value.length).fill(arrayType),
values: value,
byteArray: buffer,
packed: tight,
packed: fixedBytes.isType(arrayType) && tight,
arrayPacked: true,
});
}
Expand Down

0 comments on commit bba5eb6

Please sign in to comment.