Skip to content

feat(settings): normalize hex keys to bech32 in admin key setup - #101

Merged
arkanoider merged 1 commit into
MostroP2P:mainfrom
Arowolokehinde:feat/accept-hex-keys
Jul 24, 2026
Merged

feat(settings): normalize hex keys to bech32 in admin key setup#101
arkanoider merged 1 commit into
MostroP2P:mainfrom
Arowolokehinde:feat/accept-hex-keys

Conversation

@Arowolokehinde

@Arowolokehinde Arowolokehinde commented Jul 24, 2026

Copy link
Copy Markdown
Contributor

Summary

Closes #57

  • Add normalize_to_nsec() to validation.rs — validates and converts a raw 64-char hex private key to nsec1... bech32 format in a single pass, reusing the existing hex_seckey_to_nsec helper
  • Wire normalize_to_nsec into the SetupAdminKey flow so both the confirmation popup and settings.toml always receive canonical nsec1... format regardless of whether the user typed hex or bech32
  • Add 4 unit tests: bech32 passthrough, hex → bech32 conversion, invalid input rejection, and round-trip correctness

What was already implemented

The validation functions (validate_npub, validate_nsec), UI placeholders (nsec... / hex...), and wire-format normalization for Add Solver (normalize_solver_pubkey) were already in place. This PR completes the missing normalization step for the admin key input path.

Test plan

  • Enter a 64-char hex private key in Settings → Change Admin Key
  • Confirm the popup shows nsec1... (not raw hex)
  • Confirm ~/.mostrix/settings.toml saves admin_privkey as nsec1...
  • Enter an nsec1... key — verify it is accepted and saved unchanged
  • Enter an invalid string — verify the error message appears
  • [s] Run cargo test normalize_to_nsec — all 4 tests pass

Summary by CodeRabbit

  • New Features

    • Admin setup now accepts secret keys in either hexadecimal or nsec1... format.
    • Inputs are automatically trimmed and converted to a standardized format when needed.
  • Bug Fixes

    • Improved validation rejects empty or malformed keys with clearer error feedback.
    • Existing valid nsec1... keys continue to work unchanged.
  • Tests

    • Added coverage for key conversion, validation, and format consistency.

@coderabbitai

coderabbitai Bot commented Jul 24, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

Warning

Review limit reached

@Arowolokehinde, you've reached your PR review limit, so we couldn't start this review.

Next review available in: 11 minutes

Enable usage-based reviews in Billing to review now. Otherwise, wait until the next included review is available.
You're only billed for reviews past your plan's rate limits ($0.25/file).

How can I continue?

After more reviews become available, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

To avoid repeated limits, reduce automatic review volume by pausing incremental auto-reviews earlier, using label-based review opt-in, excluding WIP or generated PR titles, or requesting reviews manually when the PR is ready. If your team needs uninterrupted high-volume reviews, an organization admin can enable usage-based reviews.

How do review limits work?

CodeRabbit enforces per-developer PR review limits for each organization. Most developers receive the normal plan review availability.

For paid Pro and Pro+ PR reviews, CodeRabbit uses adaptive limits for sustained high-volume activity. When a developer's recent PR review activity reaches the 95th percentile or higher among CodeRabbit users, additional reviews become available more gradually as earlier reviews age out of the rolling window.

Please refer docs for additional details.

Review details
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro Plus

Run ID: c077f0ca-3aa8-4e3e-846e-20354c9b50e0

📥 Commits

Reviewing files that changed from the base of the PR and between 2165cca and c800aa3.

📒 Files selected for processing (2)
  • src/ui/key_handler/admin_handlers.rs
  • src/ui/key_handler/validation.rs

Walkthrough

Admin key setup now trims and normalizes hexadecimal or valid nsec1... input before confirmation, with errors preserved for invalid values. Tests cover acceptance, conversion, rejection, and round-trip decoding.

Changes

Admin key normalization

Layer / File(s) Summary
Secret-key normalization helper
src/ui/key_handler/validation.rs
Adds normalize_to_nsec for trimmed bech32 or hexadecimal secret keys, preserves the existing validator, and tests successful conversion, errors, and round trips.
Admin setup integration
src/ui/key_handler/admin_handlers.rs
Uses normalized secret-key input in the admin setup confirmation flow while retaining error display handling.

Estimated code review effort: 2 (Simple) | ~10 minutes

Suggested reviewers: arkanoider

Sequence Diagram(s)

sequenceDiagram
  participant handle_enter_admin_mode
  participant normalize_to_nsec
  participant handle_input_to_confirmation
  handle_enter_admin_mode->>normalize_to_nsec: key input
  normalize_to_nsec-->>handle_enter_admin_mode: normalized nsec or error
  handle_enter_admin_mode->>handle_input_to_confirmation: normalized key
Loading

Poem

A bunny trims the secret string,
Turns hex to nsec with a hop and spring.
Bad keys tumble, errors appear,
Good keys reach confirmation clear.
Wiggle ears—admin setup is here!

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Linked Issues check ⚠️ Warning The PR covers admin key normalization, but issue #57 also requires hex support for Add Solver and updated UX guidance. Extend hex/bech32 acceptance to Add Solver too, and update placeholders/error messages to mention both supported formats.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately summarizes the main change: normalizing admin key input from hex to bech32.
Out of Scope Changes check ✅ Passed The changes are focused on key normalization logic and tests, with no clear unrelated scope added.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.
✨ 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.

@ermeme ermeme Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Approved. I reviewed the full PR conversation and the exact current head c800aa3fc90dc2044823b839d56620694bc4c3ad.

The admin-key flow now converts valid hex secret keys before confirmation and persistence, while the existing Add Solver normalization and UX complete the linked issue scope. Local verification with Rust 1.90.0 passed:

  • cargo test
  • cargo clippy --all-targets --all-features -- -D warnings
  • cargo fmt --all -- --check

Non-blocking follow-up: normalize_to_nsec returns valid bech32 input verbatim, so an uppercase NSEC1... remains uppercase despite the canonical lowercase nsec1... wording. Parsing and re-encoding valid bech32 input would make that guarantee exact.

@arkanoider

Copy link
Copy Markdown
Collaborator

LGTM!

@arkanoider
arkanoider merged commit 8321cdc into MostroP2P:main Jul 24, 2026
7 checks passed
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.

Accept hexadecimal key format (hex) in addition to npub/nsec

2 participants