Skip to content

key-wallet: from_account / from_bls_account / from_eddsa_account panic on unsupported AccountType #732

Closed

Description

@QuantumExplorer

Context

Surfaced by CodeRabbit during review of #711. The panic paths are pre-existing (inherited from the original `ManagedCoreAccount::from_*_account` constructors that #711 split). Both `ManagedCoreFundsAccount` and `ManagedCoreKeysAccount` carry the same `expect(...)` fallback today.

Repo coding guideline: "Avoid `unwrap()` and `expect()` in library code; use proper error types (e.g. `thiserror`) and never panic in library code."

Problem

The constructors look like:

pub fn from_account(account: &super::super::Account) -> Self {
    let key_source = address_pool::KeySource::Public(account.account_xpub);
    let managed_type = ManagedAccountType::from_account_type(
        account.account_type,
        account.network,
        &key_source,
    )
    .unwrap_or_else(|_| {
        let no_key_source = address_pool::KeySource::NoKeySource;
        ManagedAccountType::from_account_type(
            account.account_type,
            account.network,
            &no_key_source,
        )
        .expect("Should succeed with NoKeySource")  // <-- panics
    });
    Self::new(managed_type, account.network, account.is_watch_only)
}

If `from_account_type` errors even with `NoKeySource` (e.g. an account variant we forgot to handle), the constructor aborts the caller's process instead of returning a structured error.

Suggested fix

Convert all three constructors (`from_account`, `from_bls_account`, `from_eddsa_account`) on both `ManagedCoreFundsAccount` and `ManagedCoreKeysAccount` to return `Result<Self, Error>` and propagate the underlying `crate::error::Error` from `ManagedAccountType::from_account_type`.

Touches every callsite, including the FFI surface in `key-wallet-ffi`. Worth doing in one focused PR.

Files

  • `key-wallet/src/managed_account/managed_core_funds_account.rs`
  • `key-wallet/src/managed_account/managed_core_keys_account.rs`
  • (call sites discovered via `cargo check`)

Original review thread: #711 (review comment 3187368652)

Activity

  1. added
    enhancement
    on May 5, 2026
  2. self-assigned this
    on Nov 5, 2026
  3. ZocoLini commented on Nov 6, 2026

    @ZocoLini
    Collaborator

    The NoKeySource fallback cannot fail today: with NoKeySource, AddressPool::new generates nothing and returns Ok, and a failing derivation path already falls back to master(). The only ? left on that path are the from_normal_idx(0) / from_normal_idx(1) calls in dual_pools.

    #1328 replaces those with ChildIndex constants, which makes the NoKeySource path infallible by construction. Removing the three expects still needs an infallible way to build the type without a key source, since from_account_type keeps returning Result for real key sources. That can follow once #1328 lands.

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

Metadata

Metadata

Assignees

Labels

enhancement

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions