Skip to content

fix(key-wallet): take the seed by reference in Wallet::from_seed_bytes - #842

Closed
shumkov wants to merge 1 commit into
devfrom
fix/from-seed-bytes-take-ref
Closed

shumkov wants to merge 1 commit into
devfrom
fix/from-seed-bytes-take-ref

Conversation

@shumkov

@shumkov shumkov commented Jul 5, 2026 •

Copy link
Copy Markdown
Collaborator

What

Wallet::from_seed_bytes took the 64-byte seed by value ([u8; 64]). Because
[u8; 64] is Copy, a caller that holds the master secret in a zeroizing buffer
(Zeroizing<[u8; 64]>) has to dereference it to pass by value — materializing an
extra, un-scrubbed stack copy of the master secret at the call boundary that the
caller's zeroizing wrapper does not cover.

This changes the signature to take &[u8; 64], so such a caller can pass &*buf
with no extra copy. The body derefs into the (already Zeroize-deriving) Seed.

Why

Minor secret-hygiene improvement: fewer transient, un-zeroized copies of the HD
master seed on the stack. Surfaced from dashpay/platform's
create_wallet_from_seed_bytes, which wraps the seed in Zeroizing but is forced
to make the by-value copy to call this function.

Breaking change

from_seed_bytes now takes &[u8; 64]. There are no callers within
rust-dashcore
(the FFI wallet_create_from_seed uses Wallet::from_seed, not
from_seed_bytes). The only known consumer is dashpay/platform
(rs-platform-wallet), which will update from_seed_bytes(*seed) →
from_seed_bytes(&*seed) when it bumps the key-wallet rev.

Test

cargo check -p key-wallet clean; no internal call sites to update.

🤖 Generated with Claude Code

Summary by CodeRabbit

  • Bug Fixes
    • Improved wallet seed handling by accepting seed bytes by reference, reducing unnecessary copying during wallet creation.
    • Updated wallet initialization guidance to reflect safer use of zeroized seed buffers.

`[u8; 64]` is `Copy`, so a caller holding the master secret in a `Zeroizing`
buffer had to deref it to pass by value, materializing an extra un-scrubbed
stack copy of the 64-byte master seed at the call boundary. Take `&[u8; 64]` so
such a caller passes `&*buf` with no extra copy. No callers within rust-dashcore;
the only known consumer is dashpay/platform.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
@coderabbitai

coderabbitai Bot commented Jul 5, 2026 •

Copy link
Copy Markdown
Contributor

Review Change Stack

📝 Walkthrough

Walkthrough

The public Wallet::from_seed_bytes constructor signature was changed to accept seed_bytes as a reference (&[u8; 64]) instead of by value. Documentation was updated to describe avoiding an extra by-value copy, and the implementation was adjusted to construct a Seed from the dereferenced array.

Changes

Seed Bytes API Update

Layer / File(s) Summary
from_seed_bytes reference parameter
key-wallet/src/wallet/initialization.rs
Changed from_seed_bytes to accept seed_bytes: &[u8; 64] instead of [u8; 64], updated doc comments, and adjusted the implementation to build a Seed from the dereferenced array before forwarding to Wallet::from_seed.

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

🚥 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 is concise and accurately describes the main API change: passing the seed by reference in Wallet::from_seed_bytes.
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 docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/from-seed-bytes-take-ref

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.

@coderabbitai coderabbitai Bot 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.

Actionable comments posted: 2

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@key-wallet/src/wallet/initialization.rs`:
- Around line 329-342: The `from_seed_bytes` docs currently overstate the
zero-copy behavior: `Seed::new(*seed_bytes)` still creates a transient stack
copy even though the caller-side copy is avoided. Update
`Wallet::from_seed_bytes` (and the related `Seed::new` call path) so the
implementation either accepts a `&[u8; 64]`-based zeroizing path end-to-end or,
if that is not changing, tighten the doc comment to say only the call-boundary
copy is avoided.
- Around line 337-338: The public Wallet::from_seed_bytes signature change is a
breaking API update, so update the PR title to use a breaking-change semantic
marker such as fix!: or refactor!:. Make sure the title clearly signals
downstream consumers that the change affects the Wallet::from_seed_bytes
interface.
🪄 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: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: 27202d6c-8a43-439f-99db-167c9bd066b9

📥 Commits

Reviewing files that changed from the base of the PR and between b0a909c and b955d92.

📒 Files selected for processing (1)
  • key-wallet/src/wallet/initialization.rs

Comment on lines +329 to +342
/// Takes the seed by reference so a caller holding it in a zeroizing buffer
/// can pass `&*buf` without materializing an extra un-scrubbed by-value copy
/// of the 64-byte master secret at the call boundary (`[u8; 64]` is `Copy`).
///
/// # Arguments
/// * `seed_bytes` - The seed bytes array
/// * `network` - Network to create accounts for
/// * `account_creation_options` - Specifies which accounts to create during initialization
pub fn from_seed_bytes(
seed_bytes: [u8; 64],
seed_bytes: &[u8; 64],
network: Network,
account_creation_options: WalletAccountCreationOptions,
) -> Result<Self> {
Self::from_seed(Seed::new(seed_bytes), network, account_creation_options)
Self::from_seed(Seed::new(*seed_bytes), network, account_creation_options)

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.

🔒 Security & Privacy | 🟡 Minor | ⚡ Quick win

🧩 Analysis chain

🏁 Script executed:

#!/bin/bash
set -euo pipefail

# Locate the relevant symbols and inspect their definitions.
rg -n "pub fn from_seed_bytes|struct Seed|impl Seed|Zeroize|from_seed\(" key-wallet/src -S

echo
echo "---- file outline ----"
ast-grep outline key-wallet/src/wallet/initialization.rs --view expanded || true

echo
echo "---- seed-related files ----"
fd -a "seed.rs|Seed.rs" key-wallet/src || true

Repository: dashpay/rust-dashcore

Length of output: 4963


🏁 Script executed:

#!/bin/bash
set -euo pipefail

# Read the relevant slices if the symbols are found.
sed -n '300,380p' key-wallet/src/wallet/initialization.rs

Repository: dashpay/rust-dashcore

Length of output: 2493


🏁 Script executed:

#!/bin/bash
set -euo pipefail

# Inspect the Seed type definition and constructors if present.
rg -n "pub struct Seed|impl Seed|fn new\(" key-wallet/src -S

Repository: dashpay/rust-dashcore

Length of output: 2917


Avoid implying the intermediate copy is gone

from_seed_bytes avoids the caller-side copy, but Seed::new(*seed_bytes) still creates a transient [u8; 64] on the stack. If the goal is to avoid any un-scrubbed intermediate, this needs a &[u8; 64]-based zeroizing path; otherwise tighten the doc to say only the call-boundary copy is avoided.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@key-wallet/src/wallet/initialization.rs` around lines 329 - 342, The
`from_seed_bytes` docs currently overstate the zero-copy behavior:
`Seed::new(*seed_bytes)` still creates a transient stack copy even though the
caller-side copy is avoided. Update `Wallet::from_seed_bytes` (and the related
`Seed::new` call path) so the implementation either accepts a `&[u8; 64]`-based
zeroizing path end-to-end or, if that is not changing, tighten the doc comment
to say only the call-boundary copy is avoided.

Comment on lines 337 to +338
pub fn from_seed_bytes(
seed_bytes: [u8; 64],
seed_bytes: &[u8; 64],

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.

📐 Maintainability & Code Quality | 🟡 Minor | ⚡ Quick win

🧩 Analysis chain

🏁 Script executed:

#!/bin/bash
# Inspect PR title against allowed semantic prefixes and breaking-change convention.
gh pr view --json title,number -q '.title'
fd pr-title.yml .github/workflows --exec cat {}

Repository: dashpay/rust-dashcore

Length of output: 821


🏁 Script executed:

#!/bin/bash
set -euo pipefail

# Inspect the relevant function and nearby context.
FILE="key-wallet/src/wallet/initialization.rs"

wc -l "$FILE"
sed -n '300,380p' "$FILE"

# Find declarations/usages of from_seed_bytes.
rg -n "from_seed_bytes" "$FILE" key-wallet/src -g '!target'

Repository: dashpay/rust-dashcore

Length of output: 2681


🏁 Script executed:

#!/bin/bash
set -euo pipefail

# Show the change around the public initializer and any related call sites.
git diff --unified=20 -- key-wallet/src/wallet/initialization.rs

printf '\n---\n'

rg -n "from_seed_bytes\(" key-wallet/src

Repository: dashpay/rust-dashcore

Length of output: 236


🏁 Script executed:

#!/bin/bash
set -euo pipefail

# Compare the current commit with its parent around the public API.
git rev-parse --verify HEAD
git show --stat --oneline --format=fuller HEAD -- key-wallet/src/wallet/initialization.rs
printf '\n--- parent diff ---\n'
git diff --unified=20 HEAD^ HEAD -- key-wallet/src/wallet/initialization.rs

Repository: dashpay/rust-dashcore

Length of output: 3504


Use a breaking-change PR title marker The public Wallet::from_seed_bytes signature changed, so the PR title should use a breaking semantic form like fix!: or refactor!: to warn downstream consumers.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@key-wallet/src/wallet/initialization.rs` around lines 337 - 338, The public
Wallet::from_seed_bytes signature change is a breaking API update, so update the
PR title to use a breaking-change semantic marker such as fix!: or refactor!:.
Make sure the title clearly signals downstream consumers that the change affects
the Wallet::from_seed_bytes interface.

Source: Path instructions

@codecov

codecov Bot commented Jul 5, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 0% with 2 lines in your changes missing coverage. Please review.
✅ Project coverage is 73.36%. Comparing base (b0a909c) to head (b955d92).

Files with missing lines Patch % Lines
key-wallet/src/wallet/initialization.rs 0.00% 2 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##              dev     #842      +/-   ##
==========================================
- Coverage   73.44%   73.36%   -0.09%     
==========================================
  Files         324      324              
  Lines       72577    72577              
==========================================
- Hits        53306    53245      -61     
- Misses      19271    19332      +61     
Flag Coverage Δ
core 76.94% <ø> (ø)
ffi 46.84% <ø> (-1.14%) ⬇️
rpc 20.00% <ø> (ø)
spv 90.54% <ø> (+0.27%) ⬆️
wallet 72.61% <0.00%> (ø)
Files with missing lines Coverage Δ
key-wallet/src/wallet/initialization.rs 93.02% <0.00%> (ø)

... and 20 files with indirect coverage changes

@shumkov

shumkov commented Jul 5, 2026

Copy link
Copy Markdown
Collaborator Author

Closing in favor of #843. This change only removed one call-boundary copy; the underlying issue is that Seed is Copy and can't be ZeroizeOnDrop, so the master seed is never scrubbed. #843 tracks the proper (breaking) fix — drop Copy, add ZeroizeOnDrop, audit the ~47 Seed sites + downstream — which subsumes this.

@shumkov shumkov closed this Jul 5, 2026
@shumkov
shumkov deleted the fix/from-seed-bytes-take-ref branch July 5, 2026 14:30
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