Skip to content

feat(platform-wallet): withdraw masternode credits with a wallet master-key provider or a host signer - #6028

Draft
Claudius-Maginificent wants to merge 22 commits into
feat/mn-withdrawal-heightfrom
feat/mn-withdrawal-key-source
Draft

Claudius-Maginificent wants to merge 22 commits into
feat/mn-withdrawal-heightfrom
feat/mn-withdrawal-key-source

Conversation

@Claudius-Maginificent

Copy link
Copy Markdown
Collaborator

TL;DR: Masternode credit withdrawals can now be signed either with a key the wallet derives on demand from its master key or with a key held by the host app, and every key is checked against the node's current owner identity before anything is signed.

User story

As a wallet app developer, I want to withdraw a masternode's credits with a key my app holds or one the wallet derives from a master key it asks me for, to achieve withdrawals from wallets that do not keep their seed resident, without signing with a wrong or disabled key.

Scenario

Base flow

The user withdraws credits from a masternode whose owner or payout key lives in the wallet (behind a keychain prompt) or in the host app.

Actual behavior

  • The withdrawal needs the key resident in the wallet.
  • A wrong tracked key is refused only by a check against a cached snapshot, and a key the owner identity has disabled can be broadcast and end as an unconfirmed withdrawal.
  • A failed key prompt can use up an identity nonce.

Expected behavior

  • The host passes a key source: the wallet (with an async master-key provider) or a host signer.
  • The key must be an enabled key of the live owner identity; otherwise the withdrawal is refused with a typed error before anything is signed or broadcast.
  • A nonce is taken only right before signing.

Detailed discussion

Issue being fixed or feature implemented

Stacked atop #6027 (feat/mn-withdrawal-height), last PR of the stack over #5889.

What was done?

  • New PlatformWalletManager::masternode_withdraw_with taking a MasternodeKeySource (wallet/masternode_withdrawal.rs):
    • Wallet: the key at the path MasternodeWithdrawalKeys resolved, from the wallet's resident keys or from the master key an async MasterKeyProvider supplies (ahead of refactor(platform-wallet): own the key-source choice natively #5740). The provider runs with no wallet-manager guard held. The derived key must hash to the key the request names, so another wallet's master key or a wrong path is refused before signing. The derived key is dropped right after signing, before broadcast and wait; intermediate secrets are wiped.
    • Host (MasternodeKeySource::host(signer)): a host-held identity signer, used for the owner identity key it reports it can sign with; with none, nothing is broadcast.
  • Both arms select only enabled OWNER / TRANSFER keys (the rule the key reference lookup uses) and share one sign, broadcast and settle path. prepare_withdrawal fetches the owner identity and selects the key; UnsignedWithdrawal::sign takes the nonce right before signing, so a failed key source never consumes a nonce.
  • masternode_withdrawal_keys takes a payout_path_hint, used when the funding pools miss the payout address and checked against the payout key hash at derivation. The FFI passes no hint.
  • PlatformWallet::masternode_withdraw and TrackedMasternodes::withdraw are thin wrappers over the same path; the tracked path checks the host key against the live identity instead of the cached snapshot.
  • An explicit destination for another network than the SDK's is refused before anything is fetched, on every path.
  • Typed errors: MasternodeWithdrawalError::{Wallet, KeySource}, PlatformWalletError::MasternodeWithdrawalKeyMismatch and MasternodeWithdrawalKeyAbsent (the latter says which key to use instead). Messages say "owner key" / "payout key". MasternodeKeySource implements Debug without printing keys; RawSecretCoreSigner wipes its key on drop.

FFI / mobile behaviour changes

  • masternode_withdraw (wallet path): a key the owner identity does not carry now returns ErrorInvalidParameter (key mismatch / key absent) instead of ErrorUnknown.
  • tracked_masternode_withdraw: a wrong secret used to be refused locally (ErrorInvalidParameter); it is now refused after fetching the identity (a network round trip), still as ErrorInvalidParameter. A missing identity gives ErrorIdentityNotFound; offline gives a network error. The "refresh the node first" refusals are removed.
  • A matching but disabled key used to end as ErrorMasternodeWithdrawalUnconfirmed after a broadcast; it is now refused before broadcast (ErrorInvalidParameter).
  • The destination network check is in the library; it is visible only if the SDK network differs from the wallet network (the FFI already checked).
  • Messages say "owner key" / "payout key", with a separate message when the node has no payout key.
  • Minor ordering: the wallet path checks signer capability before a zero amount; a tracked owner withdrawal with an unparsable destination gets the parse error text.

How Has This Been Tested?

  • Unit tests in masternode_withdrawal.rs and core_address_key.rs: key-type selection, wallet removal while the key provider runs, Send withdrawals, key mismatch and key absent.
  • Integration tests against a mocked Platform: tests/masternode_withdraw_host_source.rs (a host payout-key withdrawal without a destination pays the enabled TRANSFER key's P2PKH address, never the disabled key it replaced; a disabled host key is refused before signing; an owner-key withdrawal signs no payout script) and tests/masternode_withdraw_nonce.rs (a withdrawal stopped by its key source takes no identity nonce).
  • Full stack review: platform-wallet and FFI test suites green on the reviewed tree. Not executed: a live withdrawal, Swift/Kotlin builds.

Breaking Changes

Public Rust API (not consensus): masternode_withdrawal_keys gains a payout_path_hint parameter. New: MasterKeyProvider, MasternodeKeySource, MasternodeWithdrawalError, PlatformWalletManager::masternode_withdraw_with, PlatformWalletError::{MasternodeWithdrawalKeyMismatch, MasternodeWithdrawalKeyAbsent} (FFI ErrorInvalidParameter). FFI behaviour changes are listed above.

Deferred

  • Swift doc comments still describe the old contract (PlatformWalletManagerTrackedMasternodes.swift, PlatformWalletManagerMasternodes.swift); Swift is out of scope here.
  • Identity credit withdrawals: the shared credit_signing_key selector can still pick disabled keys, unlike the masternode path.
  • Two master-key contracts sit side by side (the new async MasterKeyProvider and the closure-based reveal path); to be unified with refactor(platform-wallet): own the key-source choice natively #5740.
  • SignerRef exists in several copies; the root fix is a blanket &S signer impl in rs-dpp.

Checklist:

  • I have performed a self-review of my own code
  • My Rust, FFI, WASM, Swift or Kotlin changes follow the coding conventions
  • I have commented my code, particularly in hard-to-understand areas
  • I have added or updated relevant unit/integration/functional/e2e tests
  • I have added "!" to the title and described breaking changes in the corresponding section if my code contains any
  • I have made corresponding changes to the documentation if needed
  • If I added or changed GroveDB structure, I described it in the area's structure.rs, regenerated grovedb-structure.json, and checked the structure viewer link posted on this pull request
  • If I added or changed a system contract's document schemas, I checked the contract viewer link posted on this pull request

For repository code-owners and collaborators only

  • I have assigned this pull request to a milestone

🤖 Generated with Claude Code

https://claude.ai/code/session_01WunYYk4eUkHuGstPtj83j4

🤖 Co-authored by Claudius the Magnificent AI Agent

lklimek and others added 20 commits November 21, 2026 00:42
… secret

Add verify_masternode_key_id: compares a key's 20-byte id (hash160 for the
secp256k1 roles, the Tenderdash node id for PlatformNode) against a
MasternodeKeyReference. Operator has no 20-byte id and stays Unverifiable.
verify_masternode_key now maps each role to its reference id and compares
through the same rule, so the secret and the hash paths cannot disagree.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01WunYYhpfoE88XJoyRt4hcR
…ternodeListUnavailable

SpvRuntime::current_masternode_list returns
Err(PlatformWalletError::MasternodeListUnavailable) instead of None while
the list does not follow the chain or SPV restarted during the read, the
error #5889's callers already map that case to. The crate-private
current_masternode_list_summaries keeps returning None in that case.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01WunYYhpfoE88XJoyRt4hcR
…ts of masternode key checks

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…entity disables it

Reproduction tests: they fail until learn_owner_identity and
learn_operator_identity stop keeping a previously learned key when the
fetched identity has no enabled key of that purpose.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
… a known operator key

KeyVerification::Unverifiable also covers a role that cannot be checked by
its 20-byte id: verify_masternode_key_id returns it for an operator key,
whose reference is the whole BLS key.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01WunYYhpfoE88XJoyRt4hcR
…e and describe its dash-spv follow-up in words

CurrentMasternodeList is exported from `masternode` only, like
MasternodeListSummary and MasternodeKeyReference. The TODO on
current_masternode_list names the dependency in words, as no upstream
issue exists yet.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01WunYYhpfoE88XJoyRt4hcR
…m the list and its Platform identities

Add PlatformWalletManager::fetch_masternode_key_reference: the current
masternode list's fields joined with the owner and payout key hashes of
the node's owner identity and the operator payout key hash of its
operator identity, for any node, tracked or not. Nothing is persisted.
It fails with MasternodeListUnavailable while the list does not follow
the chain; a node absent from the list but with an owner identity
(retired, credits still claimable) gets its list fields None; Ok(None)
when neither the list nor Platform knows the node.

Fix the tracked-node refresh with the same rule: it took any key of the
purpose, so after a payout address change it could keep a disabled
TRANSFER key depending on key-id order. One helper now picks the enabled
ECDSA_HASH160 key. The refresh also reads the list only while it follows
the chain, so a stale list no longer overwrites the cached entry.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01WunYYhpfoE88XJoyRt4hcR
…ched identity disables it

The tracked-node refresh kept the last known owner and operator payout
key hashes when the freshly fetched identity had no enabled key for them.
Drive disables every owner TRANSFER key when the payout is no sole P2PKH
output, and the operator TRANSFER key when the operator payout is
removed, so the kept key was stale. A fetched identity now sets the key
hashes to its enabled keys; a failed or empty fetch still leaves the
snapshot as it was.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01WunYYhpfoE88XJoyRt4hcR
…the key provider, and Send withdrawals

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…eep no stray copy of the derived key

- A withdrawal key the owner identity does not hold, enabled, for the
  withdrawal's purpose is refused with
  PlatformWalletError::MasternodeWithdrawalKeyMismatch, for the tracked
  path and the Host arm alike. The FFI maps it to ErrorInvalidParameter,
  the code mobile hosts got for a wrong tracked key before.
- MasternodeKeySource::host builds a Host source without naming the
  provider type.
- The withdrawal key derivation returns only the zeroized scalar; the WIF
  is rendered only for a key reveal, and the intermediate secret keys are
  wiped.
- Drop an orphaned doc line in tracked.rs.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01WunYYhpfoE88XJoyRt4hcR
…and default payout destination

Against a mocked Platform: a host payout-key withdrawal without a
destination pays the enabled TRANSFER key's P2PKH address, never the
disabled key it replaced; a host key the identity has disabled is refused
as a key mismatch before anything is signed; an owner-key withdrawal
signs no payout output script.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…ut of tracked reads

Once the owner identity has been read, its keys are the current ones:
TrackedMasternode::key_reference and record no longer fall back to the
registered payout script (or owner key) when the identity holds no
enabled key for it, so a retired payout key no longer comes back. Before
that read, the registration is still used.

Add CurrentMasternodeList::entry and use it for the per-node lookups in
key_reference and the tracked refresh. Fold the learn tests that
re-asserted the enabled-key ordering into the learn regression tests.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01WunYYhpfoE88XJoyRt4hcR
…al's remaining balance

Add WithdrawalOutcome { remaining_credits, proved_at: BlockTime }, built
from the result proof's response metadata, so a host can tell a
withdrawal's proved balance from an older balance read.

PlatformWallet::masternode_withdraw, TrackedMasternodes::withdraw and
IdentityWallet::withdraw_credits_with_signer return it instead of the bare
balance; the masternode path waits with
wait_for_affected_state_with_metadata, the identity path uses
withdraw_with_metadata. When an outcome is unconfirmed is unchanged.

These public Rust signatures change (not consensus). The FFI keeps its
balance out-parameter and reads remaining_credits.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01WunYYhpfoE88XJoyRt4hcR
…er-key provider or a host signer

Add PlatformWalletManager::masternode_withdraw_with, which takes the
signing key from a MasternodeKeySource:
- Wallet: the wallet's key at the path MasternodeWithdrawalKeys resolved,
  from its resident keys or from the master key an async
  MasterKeyProvider (#5740) supplies. The provider runs with no
  wallet-manager guard held, and the derived key must hash to the key the
  request names, so another wallet's master key or a wrong path is
  refused before anything is signed.
- Host: a host-held identity signer, used for the owner identity's key
  it reports it can sign with; with none, nothing is broadcast.
Both arms select only enabled OWNER / TRANSFER keys (the rule the key
reference lookup uses) and share the sign, broadcast and settle path.
Errors are typed: MasternodeWithdrawalError::{Wallet, KeySource}.

masternode_withdrawal_keys takes a payout_path_hint, used when the
funding pools miss the payout address and checked against the payout key
hash when the key is derived.

PlatformWallet::masternode_withdraw and TrackedMasternodes::withdraw are
thin wrappers over the same path. The tracked path checks the host key
against the live identity instead of the cached snapshot. The FFI passes
no payout path hint. RawSecretCoreSigner wipes its key on drop.

Public Rust API changes (not consensus): masternode_withdrawal_keys gains
a parameter; MasterKeyProvider, MasternodeKeySource and
MasternodeWithdrawalError are new.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01WunYYhpfoE88XJoyRt4hcR
…rnode withdrawal key

- The Wallet key source fetches the owner identity, selects its key and
  takes the nonce before asking the MasterKeyProvider, and drops the
  derived key once it has signed, before the broadcast and the wait.
- An explicit destination for another network than the SDK's is refused
  before anything is fetched, on every withdrawal path.
- Key refusals name the owner key or payout key. An identity with no
  current key of the requested kind gets MasternodeWithdrawalKeyAbsent,
  which says which key to use instead; the FFI maps it, like
  MasternodeWithdrawalKeyMismatch, to ErrorInvalidParameter.
- select_withdrawal_key uses the shared AvailableSigningKey selector.
- One p2pkh_address helper; MasternodeKeySource implements Debug without
  printing keys; transfer_key and payout_path_hint docs cover the
  unverified hint.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01WunYYhpfoE88XJoyRt4hcR
…rce takes no identity nonce

Fails on purpose: the wallet key source now takes the owner identity's
nonce before asking the master-key provider, so a declined keychain
prompt consumes one. The SDK nonce cache never gives a bumped nonce
back, and Platform refuses a nonce more than 24 past the identity's.
Passes on a425fdf, where the provider ran before any network call.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…signing

The SDK caches identity nonces, so one taken before the key source runs
was used up whenever the key source then failed (provider error, wrong
key, removed wallet, cancelled future). Enough such failures left every
withdrawal of the node failing until restart. prepare_withdrawal now only
fetches the identity and selects its key; UnsignedWithdrawal::sign takes
the nonce right before signing.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01WunYYhpfoE88XJoyRt4hcR
@coderabbitai

coderabbitai Bot commented Nov 21, 2026

Copy link
Copy Markdown
Contributor

Important

Draft PR not reviewed

Draft PRs are not automatically reviewed by default.

  • Trigger a manual review

To automatically review draft PRs, update your CodeRabbit configuration:

reviews:
  auto_review:
    drafts: true
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

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

@thepastaclaw

thepastaclaw commented Nov 21, 2026 •

Copy link
Copy Markdown
Collaborator

🕓 Review not started yet because this PR is a draft.

  • Request normal review — click when the PR is ready for review.
  • Request priority review — click to move this review to the front of the queue.

Commit 573207d. Normal review starts when eligible; priority review starts as soon as a slot is available.

lklimek and others added 2 commits November 24, 2026 09:52
Check both ECDSA encodings with independent public-key ids and distinct
role fields. Cover operator exclusion with populated references, direct
curve mismatches, and zero scalars. Specify public id encodings in rustdoc.

Co-Authored-By: Codex <[email protected]>

<sub>🤖 Co-authored by [Claudius the Magnificent](https://github.com/lklimek/claudius) AI Agent</sub>
…its height

Add SpvRuntime::current_masternode_list, returning a CurrentMasternodeList
(the list's known height and its summaries from one snapshot) only while
the list follows the chain, through the existing while_following_the_chain
gate. CurrentMasternodeList::key_reference gives the list-known keys of a
node, or None when the node is not on the list. The crate-private
current_masternode_list_summaries is now a projection of it; its callers
see the same results.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01WunYYhpfoE88XJoyRt4hcR
@lklimek
lklimek added this pull request to stack #6211 November 24, 2026 09:56
@lklimek
lklimek force-pushed the feat/mn-withdrawal-key-source branch from 6e833fb to 573207d Compare November 24, 2026 11:15

This branch has not been deployed

No deployments
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.

3 participants