Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions src/parsers/bool.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,5 +107,14 @@ describe('boolean', () => {
bool.decode({ type: 'bool', value: falseValue, skip: jest.fn() }),
).toBe(false);
});

it('rejects a boolean that is not 0 or 1', () => {
const twoValue = hexToBytes(
'0000000000000000000000000000000000000000000000000000000000000002',
);
expect(() =>
bool.decode({ type: 'bool', value: twoValue, skip: jest.fn() }),
).toThrow('Invalid boolean value. Expected 0 or 1, but received "2".');
});
});
});
13 changes: 11 additions & 2 deletions src/parsers/bool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,16 @@ export const bool: Parser<BooleanLike, boolean> = {
*/
decode(args): boolean {
// Booleans are encoded as 32-byte integers, so we use the number parser
// to decode the boolean value.
return number.decode({ ...args, type: 'uint256' }) === BigInt(1);
// to decode the boolean value. Only 0 and 1 are valid.
const decoded = number.decode({ ...args, type: 'uint256' });
if (decoded === BigInt(0)) {
return false;
}
if (decoded === BigInt(1)) {
return true;
}
throw new ParserError(
`Invalid boolean value. Expected 0 or 1, but received "${decoded.toString()}".`,
);
},
};