Skip to content

feat: add wallet trait#1581

Open
crodas wants to merge 2 commits intocashubtc:mainfrom
crodas:feature/wallet-trait
Open

feat: add wallet trait#1581
crodas wants to merge 2 commits intocashubtc:mainfrom
crodas:feature/wallet-trait

Conversation

@crodas
Copy link
Collaborator

@crodas crodas commented Jan 28, 2026

Description

Introduce a modular trait system for wallet implementations that enables flexible composition of wallet capabilities. This allows different wallet implementations to share a common interface while supporting selective trait implementation.

The trait hierarchy consists of:

  • WalletTypes: Base trait defining associated types (Amount, Proofs, etc.)
  • WalletBalance: Balance query operations
  • WalletMintInfo: Mint information and keyset operations
  • WalletMint: Minting operations (payment -> tokens)
  • WalletMelt: Melting operations (tokens -> payment)
  • WalletReceive: Token receiving
  • WalletProofs: Proof state management
  • Wallet: Super-trait combining all capabilities with blanket impl

Implements all traits for both cdk::Wallet and cdk-ffi::Wallet, enabling generic code that works with any wallet implementation.


Notes to the reviewers


Suggested CHANGELOG Updates

CHANGED

ADDED

REMOVED

FIXED


Checklist

@crodas crodas self-assigned this Jan 28, 2026
@github-project-automation github-project-automation bot moved this to Backlog in CDK Jan 28, 2026
@crodas crodas force-pushed the feature/wallet-trait branch from 497aa6d to 85ffb8b Compare January 28, 2026 04:33
@crodas crodas marked this pull request as ready for review January 28, 2026 05:28
@crodas crodas force-pushed the feature/wallet-trait branch 2 times, most recently from 1a2fd1f to b535f81 Compare January 30, 2026 06:23
@ye0man ye0man moved this from Backlog to In Review in CDK Feb 4, 2026
@crodas crodas force-pushed the feature/wallet-trait branch from b535f81 to 84528dd Compare February 5, 2026 20:07
@crodas crodas marked this pull request as draft February 5, 2026 21:15
@crodas crodas force-pushed the feature/wallet-trait branch 4 times, most recently from 60280ad to ee94ebb Compare February 10, 2026 23:52
@thesimplekid thesimplekid added this to the 0.16.0 milestone Feb 11, 2026
@thesimplekid
Copy link
Collaborator

@crodas Now that we have released 0.15 I think we should turn our attention back to this and get it merged. Looks like it needs a rebase is there anything else that is outstanding with it?

cc @ye0man

@crodas
Copy link
Collaborator Author

crodas commented Feb 24, 2026

Rebasing, I am on it now.

@thesimplekid thesimplekid changed the title Add composable wallet trait system Add wallet trait Mar 2, 2026
@thesimplekid thesimplekid changed the title Add wallet trait feat: add wallet trait Mar 2, 2026
@crodas crodas force-pushed the feature/wallet-trait branch from ee94ebb to d30cce9 Compare March 17, 2026 23:26
@crodas crodas requested a review from thesimplekid March 18, 2026 10:06
@github-project-automation github-project-automation bot moved this from In Review to In progress in CDK Mar 18, 2026
@crodas crodas force-pushed the feature/wallet-trait branch 2 times, most recently from 8f12b14 to b160bcc Compare March 20, 2026 10:16
@crodas crodas force-pushed the feature/wallet-trait branch 4 times, most recently from 0ac5cdb to 63705bd Compare March 21, 2026 19:38
crodas added 2 commits March 21, 2026 16:39
Define the WalletTrait in cdk-common with default method implementations for
mint, melt, send, receive, and balance operations across Bolt11, Bolt12, and
custom payment methods.

Implement the trait for the concrete Wallet type in cdk, moving public API
methods from inherent impls into the trait so that consumers can program
against the trait abstraction.

Add cdk-ffi/src/wallet_trait.rs to expose WalletTrait methods through the FFI
layer, and update all CLI commands, examples, and integration tests to import
WalletTrait where needed.
…unused imports

Refactor the WalletTrait implementation so each method delegates to an inherent
method on Wallet rather than containing inline logic. This makes the trait a
thin forwarding layer for the compiler contract while the real implementations
live as regular methods on Wallet. Remove the now-unnecessary `use WalletTrait`
import from 42 caller files across CLI, FFI, examples, and integration tests,
since callers invoke inherent methods directly. Also remove section header
comments (// === ... ===) added during the refactor.
@crodas crodas force-pushed the feature/wallet-trait branch from 63705bd to 660cc55 Compare March 21, 2026 19:40
@crodas crodas marked this pull request as ready for review March 21, 2026 20:47
@crodas crodas requested a review from thesimplekid March 21, 2026 20:47
Copy link
Collaborator

@thesimplekid thesimplekid left a comment

Choose a reason for hiding this comment

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

I think this is pretty close just rename and few things to make it more clear that the Trait is more for internal use and not the primary API for the wallet and the Wallet struct should still be used directly, we should also add doc comments to this effect.

};
pub use builder::WalletBuilder;
pub use cdk_common::wallet as types;
pub use cdk_common::wallet::{ReceiveOptions, SendMemo, SendOptions, Wallet as WalletTrait};
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
pub use cdk_common::wallet::{ReceiveOptions, SendMemo, SendOptions, Wallet as WalletTrait};
pub use cdk_common::wallet::{ReceiveOptions, SendMemo, SendOptions};

I don't think we should re-export and promote the trait to be used publicity. The trait should be used internally by use to keep the ffi inline but public use should not be encouraged.

Comment on lines +357 to +395
/// Data for a prepared send operation (owned, no lifetime)
#[derive(Debug, Clone)]
pub struct PreparedSendData {
/// Operation ID
pub operation_id: Uuid,
/// Amount to send
pub amount: Amount,
/// Send options
pub options: SendOptions,
/// Proofs that need swapping
pub proofs_to_swap: Proofs,
/// Proofs to send directly
pub proofs_to_send: Proofs,
/// Fee for swap
pub swap_fee: Amount,
/// Fee for send
pub send_fee: Amount,
}

/// Data for a prepared melt operation (owned, no lifetime)
#[derive(Debug, Clone)]
pub struct PreparedMeltData {
/// Operation ID
pub operation_id: Uuid,
/// Melt quote
pub quote: MeltQuote,
/// Proofs to melt
pub proofs: Proofs,
/// Proofs that need swapping
pub proofs_to_swap: Proofs,
/// Swap fee
pub swap_fee: Amount,
/// Input fee
pub input_fee: Amount,
/// Input fee without swap
pub input_fee_without_swap: Amount,
/// Metadata
pub metadata: HashMap<String, String>,
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

Having these owned types is required for the trait and ffi but it has the downside of adding extra clones since we cannot borrow. This is one reason I suggest we dont encourage the public use of the trait and the Wallet should be used directly.

Comment on lines +846 to +853
async fn prepare_melt(
&self,
quote_id: &str,
metadata: HashMap<String, String>,
) -> Result<Self::PreparedMelt, Self::Error>;

/// Prepare a melt operation with specific proofs
async fn prepare_melt_proofs(
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
async fn prepare_melt(
&self,
quote_id: &str,
metadata: HashMap<String, String>,
) -> Result<Self::PreparedMelt, Self::Error>;
/// Prepare a melt operation with specific proofs
async fn prepare_melt_proofs(
async fn prepare_melt_data(
&self,
quote_id: &str,
metadata: HashMap<String, String>,
) -> Result<Self::PreparedMelt, Self::Error>;
/// Prepare a melt operation with specific proofs
async fn prepare_melt_proofs_data(

We should name this fns so its clear they use the ones with the owned type and not the borrowed like the rust api can.

) -> Result<Self::Amount, Self::Error>;

/// Prepare a send transaction
async fn prepare_send(
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
async fn prepare_send(
async fn prepare_send_data(

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

Projects

Status: In progress

Development

Successfully merging this pull request may close these issues.

3 participants