Skip to content

fix(parser): BytesToHexWithPrefix drops leading zero bytes from addresses - #216

Open
boleklebovski wants to merge 1 commit into
initia-labs:mainfrom
boleklebovski:fix/bytestohex-strips-leading-zeros
Open

fix(parser): BytesToHexWithPrefix drops leading zero bytes from addresses#216
boleklebovski wants to merge 1 commit into
initia-labs:mainfrom
boleklebovski:fix/bytestohex-strips-leading-zeros

Conversation

@boleklebovski

@boleklebovski boleklebovski commented Aug 11, 2026

Copy link
Copy Markdown

Problem

pkg/parser/accounts.go:60:

func BytesToHexWithPrefix(b []byte) string {
	return "0x" + strings.TrimLeft(hex.EncodeToString(b), "0")
}

strings.TrimLeft takes a cutset, not a prefix — it removes every leading '0' character from the encoded string. Any address with leading zero bytes comes back truncated, and the result is not merely shorter, it is a different value:

input current expected
20-byte 0x00…01 0x1 0x0000000000000000000000000000000000000001
32-byte 0x00…01 0x1 0x0000…0001 (64 hex chars)
20-byte 0x000a00… 0xa000000000000000000000000000000000000 0x000a000000000000000000000000000000000000
0xdeadbeef 0xdeadbeef 0xdeadbeef

Two things stand out. A 20-byte and a 32-byte address that both end in 01 collapse onto the same string, so they collide as lookup keys. And in the third row the remaining characters shift left, producing a well-formed but entirely different address.

That string is used as a query key throughout the API layer — api/handlers/module.go (6 call sites) and api/handlers/nft.go (4 call sites) pass it straight into GetModuleById, GetCollectionsByAccountAddress, GetCollectionActivities and friends. Any account, module or collection whose address starts with a zero byte is looked up under the wrong key, and the lookup simply returns nothing.

Fix

return "0x" + hex.EncodeToString(b)

This is exactly what the sister repo already does — initia-labs/rollytics util/util.go:49:

func BytesToHexWithPrefix(b []byte) string {
	return "0x" + hex.EncodeToString(b)
}

Same function name, same purpose, no TrimLeft. That looked like the strongest evidence of intent, so I matched it exactly.

Scope

One line. strings is still used elsewhere in the file, so the import stays; gofmt is clean.

I deliberately did not touch AccAddressFromString above it, which also calls TrimLeft. There the "0x" prefix is removed first with TrimPrefix and the zero-strip is followed by re-padding to 40 or 64 characters, so it is deliberate normalisation rather than the same mistake. There is arguably an edge case when a 32-byte address strips down to ≤40 characters and gets padded back to 20 bytes, but that depends on address-width conventions I am not in a position to judge — happy to open a separate issue if it is worth a look.

Verification

I could not run the package's tests here — pkg/mq and the movevm dependency need cgo libraries I do not have in this environment — so I verified the semantics with a standalone program on Go 1.25.8 that runs both the old and new expression over the cases in the table above. The outputs are as shown.

Summary by CodeRabbit

  • Bug Fixes
    • Fixed hexadecimal encoding to preserve leading zeroes after the 0x prefix.

TrimLeft takes a cutset, so every leading zero nibble was removed and
distinct addresses collapsed onto the same key. rollytics already has the
correct form of this function.

Signed-off-by: boleklebovski <160799963+boleklebovski@users.noreply.github.com>
@coderabbitai

coderabbitai Bot commented Aug 11, 2026

Copy link
Copy Markdown

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro Plus

Run ID: a5500b2f-e966-4fa5-97b2-a214e88a9977

📥 Commits

Reviewing files that changed from the base of the PR and between cf0313d and 69f9cdf.

📒 Files selected for processing (1)
  • pkg/parser/accounts.go

Walkthrough

BytesToHexWithPrefix now preserves leading zeroes in hexadecimal output after adding the 0x prefix.

Changes

Hexadecimal prefix encoding

Layer / File(s) Summary
Preserve leading zeroes
pkg/parser/accounts.go
BytesToHexWithPrefix no longer trims leading zeroes from the encoded bytes.

Estimated code review effort: 1 (Trivial) | ~2 minutes

Poem

I’m a rabbit with bytes in my den,
Zeroes stay visible again.
With 0x in the light,
Each digit sits right,
And hex hops true to the end.

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and concisely describes the fix to preserve leading zero bytes in encoded addresses.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant