fix(Base58): preserve leading zeros for zero-valued and empty inputs#276
Open
spokodev wants to merge 1 commit into
Open
fix(Base58): preserve leading zeros for zero-valued and empty inputs#276spokodev wants to merge 1 commit into
spokodev wants to merge 1 commit into
Conversation
Base58 dropped one leading "1" per all-zero input and threw on empty
input, so round-trips were broken for zero-valued payloads:
Base58.fromHex('0x00') // '' (should be '1')
Base58.fromHex('0x0000') // '1' (should be '11')
Base58.fromHex('0x') // throws (should be '')
Base58.toHex('1') // '0x000' (should be '0x00')
Base58.toHex('') // '0x0' (should be '0x')
The encoder's leading-zero loop ran while `bytes.length > 1`, leaving
the final zero byte uncounted, and `BigInt('0x')` threw on empty input.
The decoder appended a stray "0" nibble whenever the decoded value was
zero. Each issue only surfaced when the value portion was entirely zero;
inputs with a non-zero tail were already correct, which is why the
existing leading-zero vectors (0x0000287fb4cd) passed.
Per the Base58 spec, each leading 0x00 byte maps to exactly one "1".
Output verified against bs58 and ethers across all-zero, leading-zero,
empty and random inputs.
|
@spokodev is attempting to deploy a commit to the Wevm Team on Vercel. A member of the Team first needs to authorize it. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
Base58drops a leading1for all-zero inputs and throws on empty input, so round-trips are broken for zero-valued payloads:Inputs with a non-zero tail were already correct (e.g.
0x0000287fb4cd→1111233QC4), which is why the existing leading-zero vectors passed and this went unnoticed. It only surfaces when the value portion is entirely zero.Impact: any all-zero or empty Base58 payload round-trips to the wrong byte length and is rejected/misread by other Base58 tools (
bs58,ethers). Empty input throws an uncaughtCannot convert 0x to a BigIntinstead of returning''.Cause
src/core/internal/base58.ts(encode): the leading-zero loop runs whilebytes.length > 1, so the final zero byte is never counted (n zero bytes produce n−11s), andBigInt('0x')throws on empty input.src/core/Base58.ts(decode):integer.toString(16)appends a stray0nibble when the decoded value is0n.Fix
> 0instead of> 1).0ninstead of throwing.0(the zero bytes come entirely from the leading-1count).Non-zero inputs are untouched: both loops already stop at the first non-zero byte, and the decode body is unchanged for non-zero values.
Spec
The Base58 spec referenced in the
Base58docs (and Base58Check) maps each leading0x00byte to exactly one1. Output verified to matchbs58andethersacross all-zero, leading-zero, empty and random inputs.Tests
Added regression cases for the all-zero and empty paths on
fromHex/toHex/toBytes. They fail onmainand pass with the fix; the existing suite is unchanged (12/12 green).