Skip to content

fix: guard CoreContract register_username against empty string input#457

Open
cypriannwokolo2-creator wants to merge 1 commit into
Alien-Protocol:devfrom
cypriannwokolo2-creator:fix/core-empty-username-guard
Open

fix: guard CoreContract register_username against empty string input#457
cypriannwokolo2-creator wants to merge 1 commit into
Alien-Protocol:devfrom
cypriannwokolo2-creator:fix/core-empty-username-guard

Conversation

@cypriannwokolo2-creator
Copy link
Copy Markdown

@cypriannwokolo2-creator cypriannwokolo2-creator commented Apr 27, 2026

Closes #437


fix: guard CoreContract register_username against empty string input

Summary by CodeRabbit

  • New Features

    • Added username registration functionality to the contract with input validation.
    • Invalid usernames (empty or whitespace-only) are now rejected with appropriate error handling.
  • Tests

    • Expanded test coverage for username validation scenarios.

@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented Apr 27, 2026

📝 Walkthrough

Walkthrough

Introduces username registration validation to the contract by adding a register_username method that accepts byte-based usernames, converts them to UTF-8, trims whitespace, and rejects empty inputs with a new InvalidUsername error code. Supporting unit tests validate the rejection behavior.

Changes

Cohort / File(s) Summary
Username Registration
onchain/contracts/core_contract/src/lib.rs, onchain/contracts/core_contract/src/registration.rs
Added public register_username method that validates and processes byte-based usernames, rejecting empty or whitespace-only values by panicking with CoreError::InvalidUsername.
Error Definition
onchain/shared/src/errors.rs
Added InvalidUsername enum variant with error code 4012 to represent invalid or empty username rejections.
Validation Tests
onchain/contracts/core_contract/src/test.rs
Added two unit tests exercising register_username with empty and whitespace-only inputs, verifying panic with error code 4012.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Possibly related issues

Poem

A rabbit hops through contracts new, 🐰
"No empty names will ever do!"
Trim the spaces, check the bytes,
Validate usernames just right.
Username safety takes its flight! ✨

🚥 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 specifically describes the main change: adding input validation to guard against empty strings in the register_username method.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.
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.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ 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 and usage tips.

@drips-wave
Copy link
Copy Markdown

drips-wave Bot commented Apr 27, 2026

@cypriannwokolo2-creator Great news! 🎉 Based on an automated assessment of this PR, the linked Wave issue(s) no longer count against your application limits.

You can now already apply to more issues while waiting for a review of this PR. Keep up the great work! 🚀

Learn more about application limits

Copy link
Copy Markdown

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2

🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@onchain/contracts/core_contract/src/registration.rs`:
- Around line 126-131: The rustfmt failure is caused by inconsistent formatting
around the UTF-8 conversion and trimming; adjust the block that defines
username_str and trimmed to follow standard Rust formatting (consistent
indentation, no extra blank lines, and proper chaining). Locate the code using
core::str::from_utf8(username.as_ref()) assigned to username_str and the
subsequent let trimmed = username_str.trim(); then reformat that small block to
a single idiomatic snippet (no stray blank lines, aligned let bindings) so
rustfmt passes.
- Around line 124-135: register_username currently only validates input; update
it to persist the trimmed username into contract storage and emit the
username_registered_event so get_username can read it later. Specifically, after
UTF-8 conversion and trimming (retain the existing CoreError::InvalidUsername
check), store the trimmed username under the same storage key/namespace used by
get_username and then call username_registered_event with the Env and the stored
username; ensure you handle any necessary ownership/Bytes conversions and keep
the validation logic intact.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: df89f3be-d71e-4521-b9d4-0da2e5f7bd51

📥 Commits

Reviewing files that changed from the base of the PR and between b7c5e37 and f623e21.

📒 Files selected for processing (4)
  • onchain/contracts/core_contract/src/lib.rs
  • onchain/contracts/core_contract/src/registration.rs
  • onchain/contracts/core_contract/src/test.rs
  • onchain/shared/src/errors.rs

Comment thread onchain/contracts/core_contract/src/registration.rs
Comment thread onchain/contracts/core_contract/src/registration.rs
@ryzen-xp ryzen-xp self-requested a review April 28, 2026 02:30
@ryzen-xp ryzen-xp added the CI Failed Please check why you CI is faileing fix your code label Apr 28, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CI Failed Please check why you CI is faileing fix your code

Projects

None yet

Development

Successfully merging this pull request may close these issues.

fix: guard CoreContract register_username against empty string input

2 participants