Description
Ethers Version
6.13.5
Search Terms
Bytes32String
Describe the Problem
Bytes32String Unnecessary Null Termination
bytes32 string must be less than 32 bytes
From the documentation:
Since we need only a single byte for the null termination, we can store strings up to 31 bytes long in a word.
Why limit strings to 31 bytes?
There is no need to force a null termination on a fixed-length data type if the string fits nicely into it. It can be safely assumed that the non-existent byte 33 is zero. The string already terminates at 32 bytes by definition — there’s no need for a redundant termination byte.
The output of formatBytes32String
(v5) and encodeBytes32String
(v6) always contains a null character at the last byte. This self-imposed limitation is wasteful and makes no practical sense.
Bytes32String should be able to handle 32 bytes long strings, otherwise it would be more truthful to call it Bytes31String
.
If the string is exactly 32 bytes long, you can just use toUtf8Bytes
, but what if the string length is variable up to 32 bytes?
Notably, the equivalent function in viem does not have this limitation and accepts 32 bytes long strings as input without issues.
Proposal: Raise the limit from 31 to 32 bytes
Additionally, decodeBytes32String
behaves oddly — it searches backwards for the null terminator (assuming zero padding). This is not standard behavior in other languages, where it’s expected that the string would be cut at the first null character, not the first byte of the padding.
This would resolve:
Code Snippet
Contract ABI
Errors
Environment
No response
Environment (Other)
No response