Skip to content

refactor(key): share derivation between generate and restore - #383

Merged
tvpeter merged 1 commit into
bitcoindevkit:masterfrom
chukwudiikeh:refactor/key-shared-derivation
Nov 30, 2026
Merged

tvpeter merged 1 commit into
bitcoindevkit:masterfrom
chukwudiikeh:refactor/key-shared-derivation

Conversation

@chukwudiikeh

Copy link
Copy Markdown
Contributor

Fixes #382

key generate and key restore repeated the same steps to derive the master key from a mnemonic and passphrase, differing only in where the mnemonic comes from. That duplication is how issue #372 happened: one copy dropped the passphrase.

This PR moves the shared steps into key_from_mnemonic, which both commands call, and replaces the manual fold in key generate with the Display impl of Mnemonic, as key restore already did. Display joins the words with single spaces, the same output as the fold; I compared generate and restore output for 40 mnemonics (12 and 24 words) and all matched.No behaviour change. The existing key tests cover both paths and pass unchanged.

@chukwudiikeh

Copy link
Copy Markdown
Contributor Author

@vadim-anfv @tvpeter this is the follow-up from issue #373, ready for review whenever you have time. No behaviour change the existing key tests cover both paths.

@emma31-dev emma31-dev left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This overlaps with #384 . If #384 gets merged first this change is then redundant. It is not an exact duplicate but It's worth deciding which lands first and rebasing the other on top.

@chukwudiikeh
chukwudiikeh force-pushed the refactor/key-shared-derivation branch from 86cf70b to a302315 Compare November 12, 2026 11:09
@codecov

codecov Bot commented Nov 12, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 95.00000% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 59.86%. Comparing base (d8176e8) to head (767592c).
⚠️ Report is 21 commits behind head on master.

Files with missing lines Patch % Lines
src/handlers/key.rs 95.00% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master     #383      +/-   ##
==========================================
+ Coverage   59.63%   59.86%   +0.22%     
==========================================
  Files          25       25              
  Lines        4544     4550       +6     
==========================================
+ Hits         2710     2724      +14     
+ Misses       1834     1826       -8     
Flag Coverage Δ
rust 59.86% <95.00%> (+0.22%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@chukwudiikeh

Copy link
Copy Markdown
Contributor Author

Thanks @emma31-dev . I stacked it on purpose: #384 is built on top of this PR #382 first, then the #381 rename, so this one should land first. I've rebased #384 onto the current commit here so it only adds the rename on top.

@tvpeter tvpeter added this to the CLI 4.1.0 milestone Nov 18, 2026
@tvpeter tvpeter moved this from Todo to Ready to Review in BDK-CLI Nov 18, 2026

@Musab1258 Musab1258 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

tACK

I fetched the PR and ran just pre-push. All tests pass across the three feature configurations (including test_cli_key_restore_with_password), and clippy -D warnings (no-default-features and all-features) and fmt --check are clean.

I reviewed the diff for both commands. The only change that could affect output is key generate building the mnemonic via to_string() instead of fold + trim.

To check that, I wrote a throwaway test that generated 1000 English mnemonics (500 × 12-word, 500 × 24-word) via Mnemonic::generate and asserted the old fold expression equals to_string(). All matched. This backs the 40-mnemonic comparison in the PR description with a larger sample.

I also replaced password with None in key_from_mnemonic, which made test_cli_key_restore_with_password fail at the “restore without password must differ” assertion (tests/integration/init.rs:170). So the existing test guards the shared function against a regression of #372.

@vadim-anfv vadim-anfv left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

ACK a302315; successfully ran local tests

@vadim-anfv
vadim-anfv requested a review from tvpeter November 26, 2026 09:57

@emma31-dev emma31-dev left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

ACK a302315

@tvpeter tvpeter left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Thank you for working on this @chukwudiikeh .

I left a few comments.

Comment thread src/handlers/key.rs Resolved
Comment thread src/handlers/key.rs Resolved
Comment thread src/handlers/key.rs Resolved
Comment thread src/handlers/key.rs Resolved
@chukwudiikeh
chukwudiikeh force-pushed the refactor/key-shared-derivation branch from a302315 to fc2a483 Compare November 30, 2026 20:15
@chukwudiikeh

Copy link
Copy Markdown
Contributor Author

Thanks @tvpeter, I have applied all suggested changes: shortened the doc comment, merged the Network import, simplified the error message, and renamed the helper to derive_master_key_from_mnemonic.

GenerateKeyCommand and RestoreKeyCommand repeated the same steps to
derive the master key from a mnemonic, and the duplication is how
bitcoindevkit#372 happened. Move them into derive_master_key_from_mnemonic, called
by both, and use the Display impl of Mnemonic instead of the manual
fold, which gives the same output.

Fixes bitcoindevkit#382
@chukwudiikeh
chukwudiikeh force-pushed the refactor/key-shared-derivation branch from fc2a483 to 767592c Compare November 30, 2026 20:29
@chukwudiikeh
chukwudiikeh requested a review from tvpeter November 30, 2026 20:32

@tvpeter tvpeter left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

ACK 767592c

@tvpeter
tvpeter merged commit e933e7e into bitcoindevkit:master Nov 30, 2026
10 checks passed
@github-project-automation github-project-automation Bot moved this from Ready to Review to Done in BDK-CLI Nov 30, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

key command: extract the shared key derivation into one function

5 participants