Jelajahi Sumber

feat: payment method on wallet tx (#1432)

tsk 3 minggu lalu
induk
melakukan
1db80a5ee4

+ 3 - 0
crates/cdk-common/src/wallet.rs

@@ -208,6 +208,9 @@ pub struct Transaction {
     pub payment_request: Option<String>,
     /// Payment proof (e.g., preimage for Lightning melt transactions)
     pub payment_proof: Option<String>,
+    /// Payment method (e.g., Bolt11, Bolt12) for mint/melt transactions
+    #[serde(default)]
+    pub payment_method: Option<PaymentMethod>,
 }
 
 impl Transaction {

+ 5 - 0
crates/cdk-ffi/src/types/transaction.rs

@@ -8,6 +8,7 @@ use super::amount::{Amount, CurrencyUnit};
 use super::keys::PublicKey;
 use super::mint::MintUrl;
 use super::proof::Proofs;
+use super::quote::PaymentMethod;
 use crate::error::FfiError;
 
 /// FFI-compatible Transaction
@@ -39,6 +40,8 @@ pub struct Transaction {
     pub payment_request: Option<String>,
     /// Payment proof (e.g., preimage for Lightning melt transactions)
     pub payment_proof: Option<String>,
+    /// Payment method (e.g., Bolt11, Bolt12) for mint/melt transactions
+    pub payment_method: Option<PaymentMethod>,
 }
 
 impl From<cdk::wallet::types::Transaction> for Transaction {
@@ -57,6 +60,7 @@ impl From<cdk::wallet::types::Transaction> for Transaction {
             quote_id: tx.quote_id,
             payment_request: tx.payment_request,
             payment_proof: tx.payment_proof,
+            payment_method: tx.payment_method.map(Into::into),
         }
     }
 }
@@ -83,6 +87,7 @@ impl TryFrom<Transaction> for cdk::wallet::types::Transaction {
             quote_id: tx.quote_id,
             payment_request: tx.payment_request,
             payment_proof: tx.payment_proof,
+            payment_method: tx.payment_method.map(Into::into),
         })
     }
 }

+ 2 - 0
crates/cdk-sql-common/src/wallet/migrations/postgres/20251216000000_add_payment_method_to_transactions.sql

@@ -0,0 +1,2 @@
+-- Add payment_method to transactions table
+ALTER TABLE transactions ADD COLUMN payment_method TEXT;

+ 2 - 0
crates/cdk-sql-common/src/wallet/migrations/sqlite/20251216000000_add_payment_method_to_transactions.sql

@@ -0,0 +1,2 @@
+-- Add payment_method to transactions table
+ALTER TABLE transactions ADD COLUMN payment_method TEXT;

+ 15 - 6
crates/cdk-sql-common/src/wallet/mod.rs

@@ -360,9 +360,9 @@ where
         query(
                r#"
    INSERT INTO transactions
-   (id, mint_url, direction, unit, amount, fee, ys, timestamp, memo, metadata, quote_id, payment_request, payment_proof)
+   (id, mint_url, direction, unit, amount, fee, ys, timestamp, memo, metadata, quote_id, payment_request, payment_proof, payment_method)
    VALUES
-   (:id, :mint_url, :direction, :unit, :amount, :fee, :ys, :timestamp, :memo, :metadata, :quote_id, :payment_request, :payment_proof)
+   (:id, :mint_url, :direction, :unit, :amount, :fee, :ys, :timestamp, :memo, :metadata, :quote_id, :payment_request, :payment_proof, :payment_method)
    ON CONFLICT(id) DO UPDATE SET
        mint_url = excluded.mint_url,
        direction = excluded.direction,
@@ -374,7 +374,8 @@ where
        metadata = excluded.metadata,
        quote_id = excluded.quote_id,
        payment_request = excluded.payment_request,
-       payment_proof = excluded.payment_proof
+       payment_proof = excluded.payment_proof,
+       payment_method = excluded.payment_method
    ;
            "#,
            )?
@@ -394,6 +395,7 @@ where
            .bind("quote_id", transaction.quote_id)
            .bind("payment_request", transaction.payment_request)
            .bind("payment_proof", transaction.payment_proof)
+           .bind("payment_method", transaction.payment_method.map(|pm| pm.to_string()))
            .execute(&self.inner)
            .await?;
 
@@ -1251,7 +1253,8 @@ where
                 metadata,
                 quote_id,
                 payment_request,
-                payment_proof
+                payment_proof,
+                payment_method
             FROM
                 transactions
             WHERE
@@ -1288,7 +1291,8 @@ where
                 metadata,
                 quote_id,
                 payment_request,
-                payment_proof
+                payment_proof,
+                payment_method
             FROM
                 transactions
             "#,
@@ -1516,7 +1520,8 @@ fn sql_row_to_transaction(row: Vec<Column>) -> Result<Transaction, Error> {
             metadata,
             quote_id,
             payment_request,
-            payment_proof
+            payment_proof,
+            payment_method
         ) = row
     );
 
@@ -1542,6 +1547,10 @@ fn sql_row_to_transaction(row: Vec<Column>) -> Result<Transaction, Error> {
         quote_id: column_as_nullable_string!(quote_id),
         payment_request: column_as_nullable_string!(payment_request),
         payment_proof: column_as_nullable_string!(payment_proof),
+        payment_method: column_as_nullable_string!(payment_method)
+            .map(|v| PaymentMethod::from_str(&v))
+            .transpose()
+            .map_err(Error::from)?,
     })
 }
 

+ 1 - 0
crates/cdk/src/wallet/issue/issue_bolt11.rs

@@ -367,6 +367,7 @@ impl Wallet {
             quote_id: Some(quote_id.to_string()),
             payment_request: Some(quote_info.request),
             payment_proof: None,
+            payment_method: Some(quote_info.payment_method),
         })
         .await?;
 

+ 1 - 0
crates/cdk/src/wallet/issue/issue_bolt12.rs

@@ -246,6 +246,7 @@ impl Wallet {
             quote_id: Some(quote_id.to_string()),
             payment_request: Some(quote_info.request),
             payment_proof: None,
+            payment_method: Some(quote_info.payment_method),
         })
         .await?;
 

+ 2 - 0
crates/cdk/src/wallet/melt/melt_bolt11.rs

@@ -294,6 +294,7 @@ impl Wallet {
         quote_info.state = cdk_common::MeltQuoteState::Paid;
 
         let payment_request = quote_info.request.clone();
+        let payment_method = quote_info.payment_method.clone();
         tx.add_melt_quote(quote_info).await?;
 
         let deleted_ys = proofs.ys()?;
@@ -314,6 +315,7 @@ impl Wallet {
             quote_id: Some(quote_id.to_string()),
             payment_request: Some(payment_request),
             payment_proof: payment_preimage,
+            payment_method: Some(payment_method),
         })
         .await?;
 

+ 1 - 0
crates/cdk/src/wallet/melt/mod.rs

@@ -83,6 +83,7 @@ impl Wallet {
                     quote_id: Some(quote.id.clone()),
                     payment_request: Some(quote.request.clone()),
                     payment_proof: response.payment_preimage.clone(),
+                    payment_method: Some(quote.payment_method.clone()),
                 })
                 .await?;
             }

+ 1 - 0
crates/cdk/src/wallet/receive.rs

@@ -191,6 +191,7 @@ impl Wallet {
             quote_id: None,
             payment_request: None,
             payment_proof: None,
+            payment_method: None,
         })
         .await?;
 

+ 1 - 0
crates/cdk/src/wallet/send.rs

@@ -388,6 +388,7 @@ impl PreparedSend {
             quote_id: None,
             payment_request: None,
             payment_proof: None,
+            payment_method: None,
         })
         .await?;