Skip to content

refactor(platform-wallet): own the key-source choice natively #5740

Open

Description

@xdustinface

Problem

The book's rule is that the FFI and mobile SDKs hold no business logic. Several rs-platform-wallet-ffi exports still decide which key source to use. Each one probes the wallet under a short manager read guard, checks is_external_signable() and is_watch_only(), then either uses the resident keys or calls the host's mnemonic resolver to build the master key, and only then calls into platform-wallet to derive.

The files on v5.1-dev that carry this flow (resolve_master_from_resolver or the capability probe):

  • address_private_key.rs
  • derive_connect_key.rs
  • identity_discovery.rs
  • identity_key_preview.rs
  • identity_keys_from_mnemonic.rs
  • identity_loading.rs
  • masternode_update_service.rs
  • provider_key_at_index.rs
  • wallet_startup.rs

#5727 adds one more caller (platform_wallet_bip44_address_private_key) through the shared reveal_address_private_key helper, which it factored out of platform_wallet_address_private_key. It keeps the existing flow rather than moving it, so all of these move together.

Proposed direction

Add a native key-source abstraction in platform-wallet, for example a MasterKeyProvider trait that returns the master ExtendedPrivKey for a wallet id. platform-wallet then owns the whole decision: probe the wallet, pick resident keys or the provider, call the provider with no manager guard held, derive, and clean up. The FFI crate implements the trait over MnemonicResolverHandle and only marshals inputs and results.

Constraints

  • Keep releasing the manager guard before the provider is called. The iOS resolver reads the Keychain and can stall on a biometric prompt.
  • Keep today's error codes at the C boundary, including ErrorWalletOperation when a resolver is required but missing.
  • One entry point per commit makes this reviewable. address_private_key.rs is the smallest place to start.

Stale cleanup doc

The doc of resolve_master_from_resolver in identity_keys_from_mnemonic.rs says ExtendedPrivKey has no Drop or Zeroize, so the caller must zeroize master.private_key. That is untrue at the pinned rust-dashcore rev: Drop calls zeroize, which wipes the private scalar, chain code and derivation metadata. Correct it when this file moves, and say that an explicit wipe is only an optional earlier one.

Activity

  1. xdustinface commented on Nov 3, 2026

    @xdustinface
    ContributorAuthor

    Update

    #5727 moves this choice into platform-wallet for the two address key reveals (derive_core_address_private_key and bip44_address_private_key take a master_key callback), so address_private_key.rs is done and eight files remain. Its CoreAddressKeyError<E> pattern can be reused for them.

    One more thing to fix along the way: the FFI entry points call the resolver inside PLATFORM_WALLET_STORAGE.with_item, which holds the handle-storage read lock while a Keychain or biometric prompt is open. A pending handle insert or remove waits behind it. Moving the callback outside with_item means cloning the Arc<PlatformWallet> out first.

  2. xdustinface commented on Nov 3, 2026

    @xdustinface
    ContributorAuthor

    Update: scope after #5727

    #5727 now covers part of this issue:

    • platform-wallet owns the phrase to seed and phrase to master steps (seed_for_mnemonic, master_key_for_mnemonic). The shared FFI helpers resolve_seed_from_resolver* and resolve_master_from_resolver* only call the host's resolver, check its buffer and UTF-8, and hand the phrase over. That holds for every resolver caller.
    • The two address key reveals choose the key source natively (master_key callback, CoreAddressKeyError<E>).

    The earlier list of nine files counted every resolver caller. What is left in the FFI:

    • The key-source choice. These files still probe is_external_signable() / is_watch_only() themselves before resolving: identity_discovery.rs, identity_key_preview.rs, identity_loading.rs, masternode_update_service.rs and provider_key_at_index.rs. The master_key callback pattern of the address reveals fits them.
    • A second phrase to master copy. sign_with_mnemonic_resolver.rs parses the resolved phrase and builds the seed and master itself. It can call master_key_for_mnemonic.
    • The passphrase entry point. dash_sdk_derive_identity_keys_from_mnemonic parses a phrase plus passphrase in the FFI.
    • The handle-storage lock. The resolver still runs inside PLATFORM_WALLET_STORAGE.with_item, so a Keychain prompt holds the storage read lock and handle inserts and removals wait behind it.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions