Skip to content

Commit

Permalink
refactor: return empty string when decoding CompactBytesArray
Browse files Browse the repository at this point in the history
  • Loading branch information
CJ42 committed Jan 24, 2023
1 parent 7a8a65c commit 5064b0e
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 14 deletions.
51 changes: 46 additions & 5 deletions src/lib/encoder.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,11 +132,6 @@ describe('encoder', () => {
decodedValue: [`0x${'cafe'.repeat(256)}`, `0x${'beef'.repeat(250)}`],
encodedValue: `0x0200${'cafe'.repeat(256)}01f4${'beef'.repeat(250)}`,
},
{
valueType: 'bytes[CompactBytesArray]',
decodedValue: ['0xaabb', '0x', '0x', '0xbeefbeefbeefbeefbeef'],
encodedValue: '0x0002aabb00000000000abeefbeefbeefbeefbeef',
},
];

testCases.forEach((testCase) => {
Expand All @@ -154,6 +149,52 @@ describe('encoder', () => {
});
});

describe('when encoding bytes[CompactBytesArray]', () => {
it('should encode `0x` elements as `0x0000`', async () => {
const testCase = {
valueType: 'bytes[CompactBytesArray]',
decodedValue: ['0xaabb', '0x', '0x', '0xbeefbeefbeefbeefbeef'],
encodedValue: '0x0002aabb00000000000abeefbeefbeefbeefbeef',
};

const encodedValue = encodeValueType(
testCase.valueType,
testCase.decodedValue,
);
assert.deepStrictEqual(encodedValue, testCase.encodedValue);
});

it("should encode '' (empty strings) elements as `0x0000`", async () => {
const testCase = {
valueType: 'bytes[CompactBytesArray]',
decodedValue: ['0xaabb', '', '', '0xbeefbeefbeefbeefbeef'],
encodedValue: '0x0002aabb00000000000abeefbeefbeefbeefbeef',
};

const encodedValue = encodeValueType(
testCase.valueType,
testCase.decodedValue,
);
assert.deepStrictEqual(encodedValue, testCase.encodedValue);
});
});

describe('when decoding a bytes[CompactBytesArray] that contains `0000` entries', () => {
it("should decode as '' (empty string) in the decoded array", async () => {
const testCase = {
valueType: 'bytes[CompactBytesArray]',
decodedValue: ['0xaabb', '', '', '0xbeefbeefbeefbeefbeef'],
encodedValue: '0x0002aabb00000000000abeefbeefbeefbeefbeef',
};

const decodedValue = decodeValueType(
testCase.valueType,
testCase.encodedValue,
);
assert.deepStrictEqual(decodedValue, testCase.decodedValue);
});
});

it('should throw when valueType is unknown', () => {
assert.throws(
() => {
Expand Down
18 changes: 9 additions & 9 deletions src/lib/encoder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,12 +84,6 @@ const decodeDataSourceWithHash = (value: string): URLDataWithHash => {
const encodeCompactBytesArray = (values: string[]): string => {
const compactBytesArray = values
.filter((value, index) => {
if (value === '') {
throw new Error(
`Couldn't encode, value at index ${index} is '', expected '0x'`,
);
}

if (!isHex(value)) {
throw new Error(`Couldn't encode, value at index ${index} is not hex`);
}
Expand Down Expand Up @@ -121,9 +115,15 @@ const decodeCompactBytesArray = (compactBytesArray: string): string[] => {
const length = hexToNumber(
'0x' + compactBytesArray.slice(pointer, pointer + 4),
);
encodedValues.push(
'0x' + compactBytesArray.slice(pointer + 4, pointer + 2 * (length + 2)),
);

if (length === 0) {
// empty entries (`0x0000`) in a CompactBytesArray are returned as empty entries in the array
encodedValues.push('');
} else {
encodedValues.push(
'0x' + compactBytesArray.slice(pointer + 4, pointer + 2 * (length + 2)),
);
}

pointer += 2 * (length + 2);
}
Expand Down

0 comments on commit 5064b0e

Please sign in to comment.