Repository navigation
Non-reverting encode/decode, AddressCoder dispatcher, EvmCoder - #1
Merged
Merged
Conversation
- Every coder's encode now returns (bool ok, string addr) and never reverts: invalid bytes give (false, ""). decode still reverts on invalid input. Fixed-size coders keep an infallible encode(bytes32) / encode(address). - AddressCoder: opt-in dispatcher by coinType (encode, decode, isSupported). - EvmCoder: ETH (60) and ENSIP-11 EVM chains, EIP-55 checksummed. - Blake2b: detect a missing 0x09 precompile (the call "succeeds" with no return data, which previously produced a wrong checksum silently); add tryHash512. - Bech32: encodedLength / isValidWitnessProgram helpers for pre-checks. - NatSpec References on every coder and util. - Tests run through AddressCoder; fuzz that encode never reverts for any coinType; ETH/OP vectors from the reference. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
- decode returns (bool ok, bytes data) in every coder, AddressCoder and util.
Internal library calls can't be try/caught, so reverting left callers no
way to handle bad input. All custom errors are removed.
- Bech32.encode/encodeSegwit and CashAddr.encode return (bool, string);
Bech32.encodedLength is gone. Blake2b.hash512 returns ok (tryHash512 removed).
- testFuzz_decodeNeverReverts{,_mutated} cover every coinType.
- EvmCoder: COIN_TYPE -> COIN_TYPE_ETH, EVM_BIT -> COIN_TYPE_DEFAULT,
ENSIP-11 -> ENSIP-19, and empty bytes encode as the zero address.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
- encode(bytes, bool checksum) / encode(address, bool checksum): EIP-55 or lowercase. encode(bytes) keeps checksum = true. - decode(string, bool checksum): with checksum, mixed-case input must be a valid EIP-55 checksum; without it, any case is accepted. decode(string) keeps checksum = true (matches the reference). - Empty bytes no longer encode as the zero address. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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.
Summary
Nothing reverts:
encodeanddecodeboth returnok.Invalid input gives
(false, ""). A caller can't wrap aninternallibrary call intry/catch, so with a reverting decoder a contract had no way to handle a bad address. Callers who want a revert can add their own (if (!ok) revert ...). All custom errors are gone. Coders with fixed-size input (Solana, Chia, Stellar, EVM) keep an infallibleencode(bytes32)/encode(address)that returns a plainstring.The utils follow the same rule:
Base58,Base32,Bech32,CashAddr,SS58andBitcoinLikedecoders returnok.Bech32.encode/encodeSegwitreturnfalseover the length limit or for an invalid witness program, which replacesencodedLength.CashAddr.encodereturnsfalsefor an unsupported hash size.Blake2b.hash512returnsfalsewithout the precompile.AddressCoder: an opt-in dispatcher by coinType.It compiles to about 8–9 KB each for encode and decode. The per-chain coders don't import it, so tree-shaking is unchanged. For example,
BitcoinCoderis about 3.3 KB for each direction.EvmCodercovers ETH (COIN_TYPE_ETH = 60) and ENSIP-19 EVM chains (COIN_TYPE_DEFAULT | chainId, whereCOIN_TYPE_DEFAULT = 0x80000000).encode(data, checksum)/encode(address, checksum)emit EIP-55 or lowercase. Withchecksum,decode(addr, checksum)requires mixed-case input to be a valid EIP-55 checksum; all-lowercase and all-uppercase carry no checksum, so they're accepted. Without it, any case is accepted. The one-argumentencode(bytes)/decode(string)usechecksum = true, which matches the reference.Blake2b fix. On a chain without the
0x09precompile, thestaticcall"succeeds" with empty return data, so Polkadot and Kusama silently produced a wrong checksum. It now checksreturndatasize() == 64, and DOT/KSM returnfalseon those chains.References. Every coder and util now has a NatSpec
References:list: the specs, where the chain's constants come from, and the matching address-encoder source file.Tests
AddressCoder. TheinvalidAddressandinvalidBytescases must returnok == falsewith empty output, and must not revert.testFuzz_encodeNeverReverts{,_shaped}run every supported coinType and some random ones. Wheneverokis true, the output must decode back to the input.testFuzz_decodeNeverReverts{,_mutated}feed in arbitrary strings, and valid addresses with one character changed or the end cut off. Wheneverokis true, the decoded bytes must encode again.test_missingPrecompilemocks0x09to return nothing; both DOT encode and DOT decode returnfalse.forge fmt --check,forge lintandforge testpass locally: 49 tests, 2000 fuzz runs each. I also ran the never-revert fuzzers once at 50k runs.Breaking changes
XCoder.encode(bytes)returns(bool, string)andXCoder.decode(string)returns(bool, bytes).Blake2b.tryHash512andBech32.encodedLength.🤖 Generated with Claude Code