From 95cea3c43c7abde36fb246ec9f6816e5b8b43183 Mon Sep 17 00:00:00 2001 From: thesimplekid Date: Fri, 14 Feb 2025 10:04:22 +0000 Subject: [PATCH] feat: whats another crate --- crates/cdk-common/src/lib.rs | 3 - crates/cdk-mintd/Cargo.toml | 1 + crates/cdk-mintd/src/setup.rs | 7 +- crates/cdk-payment-processor/Cargo.toml | 59 +++++++++++++++++ .../build.rs | 0 crates/cdk-payment-processor/src/error.rs | 20 ++++++ crates/cdk-payment-processor/src/lib.rs | 8 +++ .../src/proto}/client.rs | 28 ++++---- .../src/proto/mod.rs | 66 ++++++++++--------- .../src/proto/payment_processor.proto | 0 .../src/proto}/server.rs | 6 +- crates/cdk/src/lib.rs | 4 +- crates/cdk/src/mint/mod.rs | 3 - crates/cdk/src/mint/payment_processor/mod.rs | 4 -- 14 files changed, 146 insertions(+), 63 deletions(-) create mode 100644 crates/cdk-payment-processor/Cargo.toml rename crates/{cdk-common => cdk-payment-processor}/build.rs (100%) create mode 100644 crates/cdk-payment-processor/src/error.rs create mode 100644 crates/cdk-payment-processor/src/lib.rs rename crates/{cdk/src/mint/payment_processor => cdk-payment-processor/src/proto}/client.rs (90%) rename crates/{cdk-common => cdk-payment-processor}/src/proto/mod.rs (69%) rename crates/{cdk-common => cdk-payment-processor}/src/proto/payment_processor.proto (100%) rename crates/{cdk/src/mint/payment_processor => cdk-payment-processor/src/proto}/server.rs (96%) delete mode 100644 crates/cdk/src/mint/payment_processor/mod.rs diff --git a/crates/cdk-common/src/lib.rs b/crates/cdk-common/src/lib.rs index df2560ec2..ed95f2d0f 100644 --- a/crates/cdk-common/src/lib.rs +++ b/crates/cdk-common/src/lib.rs @@ -11,7 +11,6 @@ pub mod database; pub mod error; #[cfg(feature = "mint")] pub mod lightning; -pub mod proto; pub mod pub_sub; #[cfg(feature = "mint")] pub mod subscription; @@ -27,7 +26,5 @@ pub use cashu::nuts::{self, *}; #[cfg(feature = "wallet")] pub use cashu::wallet; pub use cashu::{dhke, mint_url, secret, util, SECP256K1}; -pub use proto::cdk_payment_processor_client::CdkPaymentProcessorClient; -pub use proto::cdk_payment_processor_server::CdkPaymentProcessorServer; #[doc(hidden)] pub use tonic; diff --git a/crates/cdk-mintd/Cargo.toml b/crates/cdk-mintd/Cargo.toml index cdef1276b..52871cf35 100644 --- a/crates/cdk-mintd/Cargo.toml +++ b/crates/cdk-mintd/Cargo.toml @@ -34,6 +34,7 @@ cdk-fake-wallet = { path = "../cdk-fake-wallet", version = "0.7.1", default-feat cdk-strike = { path = "../cdk-strike", version = "0.7.1" } cdk-axum = { path = "../cdk-axum", version = "0.7.1", default-features = false } cdk-mint-rpc = { path = "../cdk-mint-rpc", version = "0.7.1", default-features = false, optional = true } +cdk-payment-processor = { path = "../cdk-payment-processor", version = "0.7.1", default-features = false } config = { version = "0.13.3", features = ["toml"] } clap = { version = "~4.0.32", features = ["derive"] } bitcoin = { version = "0.32.2", features = [ diff --git a/crates/cdk-mintd/src/setup.rs b/crates/cdk-mintd/src/setup.rs index 32c4694f3..eff12cff8 100644 --- a/crates/cdk-mintd/src/setup.rs +++ b/crates/cdk-mintd/src/setup.rs @@ -3,9 +3,10 @@ use std::sync::Arc; use anyhow::{anyhow, bail}; use axum::{async_trait, Router}; use cdk::cdk_lightning::MintLightning; -use cdk::mint::{FeeReserve, PaymentProcessor}; +use cdk::mint::FeeReserve; use cdk::mint_url::MintUrl; use cdk::nuts::CurrencyUnit; +use cdk_payment_processor::PaymentProcessorClient; use tokio::sync::Mutex; use url::Url; @@ -208,8 +209,8 @@ impl LnBackendSetup for config::FakeWallet { _router: &mut Vec, _settings: &Settings, _unit: CurrencyUnit, - ) -> anyhow::Result { - let fake_wallet = PaymentProcessor::new("127.0.0.1", 8089, None).await?; + ) -> anyhow::Result { + let fake_wallet = PaymentProcessorClient::new("127.0.0.1", 8089, None).await?; Ok(fake_wallet) } diff --git a/crates/cdk-payment-processor/Cargo.toml b/crates/cdk-payment-processor/Cargo.toml new file mode 100644 index 000000000..2ef13ee13 --- /dev/null +++ b/crates/cdk-payment-processor/Cargo.toml @@ -0,0 +1,59 @@ +[package] +name = "cdk-payment-processor" +version = "0.7.1" +edition = "2021" +authors = ["CDK Developers"] +description = "CDK payment processor" +homepage = "https://github.com/cashubtc/cdk" +repository = "https://github.com/cashubtc/cdk.git" +rust-version = "1.63.0" # MSRV +license = "MIT" + +[features] +bench = [] + +[dependencies] +anyhow = "1.0" +async-trait = "0.1" +bitcoin = { version = "0.32.2", features = [ + "base64", + "serde", + "rand", + "rand-std", +] } +cdk-common = { path = "../cdk-common", default-features = false, version = "0.7.1", features = ["mint"] } +serde = { version = "1", features = ["derive"] } +thiserror = "2" +tracing = "0.1" +lightning-invoice = { version = "0.32.0", features = ["serde", "std"] } +uuid = { version = "=1.12.1", features = ["v4", "serde"], optional = true } +utoipa = { version = "4", optional = true } +futures = "0.3.31" +serde_json = "1" +serde_with = "3" +tonic = { version = "0.9", features = [ + "channel", + "tls", + "tls-webpki-roots", +] } +prost = "0.11.0" + + +[target.'cfg(not(target_arch = "wasm32"))'.dependencies] +tokio = { version = "1.21", features = [ + "rt-multi-thread", + "time", + "macros", + "sync", +] } + + +[target.'cfg(target_arch = "wasm32")'.dependencies] +tokio = { version = "1.21", features = ["rt", "macros", "sync", "time"] } + +[dev-dependencies] +rand = "0.8.5" +bip39 = "2.0" + +[build-dependencies] +tonic-build = "0.9" diff --git a/crates/cdk-common/build.rs b/crates/cdk-payment-processor/build.rs similarity index 100% rename from crates/cdk-common/build.rs rename to crates/cdk-payment-processor/build.rs diff --git a/crates/cdk-payment-processor/src/error.rs b/crates/cdk-payment-processor/src/error.rs new file mode 100644 index 000000000..a4c27251b --- /dev/null +++ b/crates/cdk-payment-processor/src/error.rs @@ -0,0 +1,20 @@ +//! Errors + +use thiserror::Error; + +/// CDK Payment processor error +#[derive(Debug, Error)] +pub enum Error { + /// Invalid ID + #[error("Invalid id")] + InvalidId, + /// NUT00 Error + #[error(transparent)] + NUT00(#[from] cdk_common::nuts::nut00::Error), + /// NUT05 error + #[error(transparent)] + NUT05(#[from] cdk_common::nuts::nut05::Error), + /// Parse invoice error + #[error(transparent)] + Invoice(#[from] lightning_invoice::ParseOrSemanticError), +} diff --git a/crates/cdk-payment-processor/src/lib.rs b/crates/cdk-payment-processor/src/lib.rs new file mode 100644 index 000000000..a39b9bd04 --- /dev/null +++ b/crates/cdk-payment-processor/src/lib.rs @@ -0,0 +1,8 @@ +pub mod error; +pub mod proto; + +pub use proto::cdk_payment_processor_client::CdkPaymentProcessorClient; +pub use proto::cdk_payment_processor_server::CdkPaymentProcessorServer; +pub use proto::{PaymentProcessorClient, PaymentProcessorServer}; +#[doc(hidden)] +pub use tonic; diff --git a/crates/cdk/src/mint/payment_processor/client.rs b/crates/cdk-payment-processor/src/proto/client.rs similarity index 90% rename from crates/cdk/src/mint/payment_processor/client.rs rename to crates/cdk-payment-processor/src/proto/client.rs index bec9828b8..8e948b26f 100644 --- a/crates/cdk/src/mint/payment_processor/client.rs +++ b/crates/cdk-payment-processor/src/proto/client.rs @@ -7,25 +7,25 @@ use anyhow::anyhow; use cdk_common::lightning::{ CreateInvoiceResponse, MintLightning, PayInvoiceResponse, PaymentQuoteResponse, Settings, }; -use cdk_common::proto::{ - CheckIncomingPaymentRequest, CheckOutgoingPaymentRequest, CreatePaymentRequest, - MakePaymentRequest, SettingsRequest, -}; -use cdk_common::{ - mint, Amount, CdkPaymentProcessorClient, CurrencyUnit, MeltQuoteBolt11Request, MintQuoteState, -}; +use cdk_common::{mint, Amount, CurrencyUnit, MeltQuoteBolt11Request, MintQuoteState}; use futures::Stream; use tokio::sync::Mutex; use tonic::transport::{Certificate, Channel, ClientTlsConfig, Identity}; use tonic::{async_trait, Request}; +use super::cdk_payment_processor_client::CdkPaymentProcessorClient; +use super::{ + CheckIncomingPaymentRequest, CheckOutgoingPaymentRequest, CreatePaymentRequest, + MakePaymentRequest, SettingsRequest, +}; + /// Payment Processor #[derive(Clone)] -pub struct PaymentProcessor { +pub struct PaymentProcessorClient { inner: Arc>>, } -impl PaymentProcessor { +impl PaymentProcessorClient { /// Payment Processor pub async fn new(addr: &str, port: u16, tls_dir: Option) -> anyhow::Result { let addr = format!("{}:{}", addr, port); @@ -58,8 +58,8 @@ impl PaymentProcessor { } #[async_trait] -impl MintLightning for PaymentProcessor { - type Err = crate::cdk_lightning::Error; +impl MintLightning for PaymentProcessorClient { + type Err = cdk_common::lightning::Error; async fn get_settings(&self) -> Result { let mut inner = self.inner.lock().await; @@ -95,7 +95,7 @@ impl MintLightning for PaymentProcessor { let response = response.into_inner(); Ok(response.try_into().map_err(|_| { - crate::cdk_lightning::Error::Anyhow(anyhow!("Could not create invoice")) + cdk_common::lightning::Error::Anyhow(anyhow!("Could not create invoice")) })?) } @@ -131,7 +131,7 @@ impl MintLightning for PaymentProcessor { let response = response.into_inner(); Ok(response.try_into().map_err(|_err| { - crate::cdk_lightning::Error::Anyhow(anyhow!("could not make payment")) + cdk_common::lightning::Error::Anyhow(anyhow!("could not make payment")) })?) } @@ -185,6 +185,6 @@ impl MintLightning for PaymentProcessor { Ok(check_outgoing .try_into() - .map_err(|_| crate::cdk_lightning::Error::UnknownPaymentState)?) + .map_err(|_| cdk_common::lightning::Error::UnknownPaymentState)?) } } diff --git a/crates/cdk-common/src/proto/mod.rs b/crates/cdk-payment-processor/src/proto/mod.rs similarity index 69% rename from crates/cdk-common/src/proto/mod.rs rename to crates/cdk-payment-processor/src/proto/mod.rs index 76b8f36ed..3902a9fb1 100644 --- a/crates/cdk-common/src/proto/mod.rs +++ b/crates/cdk-payment-processor/src/proto/mod.rs @@ -1,9 +1,13 @@ use std::str::FromStr; -use cashu::{Bolt11Invoice, CurrencyUnit, MeltQuoteBolt11Request}; +use cdk_common::lightning::{CreateInvoiceResponse, PayInvoiceResponse, Settings}; +use cdk_common::{Bolt11Invoice, CurrencyUnit, MeltQuoteBolt11Request}; use melt_options::Options; +mod client; +mod server; -use crate::lightning::{CreateInvoiceResponse, PayInvoiceResponse, Settings}; +pub use client::PaymentProcessorClient; +pub use server::PaymentProcessorServer; tonic::include_proto!("cdk_payment_processor"); @@ -74,8 +78,8 @@ impl From<&MeltQuoteBolt11Request> for PaymentQuoteRequest { } } -impl From for PaymentQuoteResponse { - fn from(value: crate::lightning::PaymentQuoteResponse) -> Self { +impl From for PaymentQuoteResponse { + fn from(value: cdk_common::lightning::PaymentQuoteResponse) -> Self { Self { request_lookup_id: value.request_lookup_id, amount: value.amount.into(), @@ -85,34 +89,34 @@ impl From for PaymentQuoteResponse { } } -impl From for MeltOptions { - fn from(value: cashu::nut05::MeltOptions) -> Self { +impl From for MeltOptions { + fn from(value: cdk_common::nut05::MeltOptions) -> Self { Self { options: Some(value.into()), } } } -impl From for Options { - fn from(value: cashu::nut05::MeltOptions) -> Self { +impl From for Options { + fn from(value: cdk_common::nut05::MeltOptions) -> Self { match value { - cashu::MeltOptions::Mpp { mpp } => Self::Mpp(Mpp { + cdk_common::MeltOptions::Mpp { mpp } => Self::Mpp(Mpp { amount: mpp.amount.into(), }), } } } -impl From for cashu::nut05::MeltOptions { +impl From for cdk_common::nut05::MeltOptions { fn from(value: MeltOptions) -> Self { let options = value.options.expect("option defined"); match options { - Options::Mpp(mpp) => cashu::MeltOptions::new_mpp(mpp.amount), + Options::Mpp(mpp) => cdk_common::MeltOptions::new_mpp(mpp.amount), } } } -impl From for crate::lightning::PaymentQuoteResponse { +impl From for cdk_common::lightning::PaymentQuoteResponse { fn from(value: PaymentQuoteResponse) -> Self { Self { request_lookup_id: value.request_lookup_id.clone(), @@ -123,7 +127,7 @@ impl From for crate::lightning::PaymentQuoteResponse { } } -impl From for cashu::nut05::QuoteState { +impl From for cdk_common::nut05::QuoteState { fn from(value: QuoteState) -> Self { match value { QuoteState::Unpaid => Self::Unpaid, @@ -136,31 +140,31 @@ impl From for cashu::nut05::QuoteState { } } -impl From for QuoteState { - fn from(value: cashu::nut05::QuoteState) -> Self { +impl From for QuoteState { + fn from(value: cdk_common::nut05::QuoteState) -> Self { match value { - cashu::MeltQuoteState::Unpaid => Self::Unpaid, - cashu::MeltQuoteState::Paid => Self::Paid, - cashu::MeltQuoteState::Pending => Self::Pending, - cashu::MeltQuoteState::Unknown => Self::Unknown, - cashu::MeltQuoteState::Failed => Self::Failed, + cdk_common::MeltQuoteState::Unpaid => Self::Unpaid, + cdk_common::MeltQuoteState::Paid => Self::Paid, + cdk_common::MeltQuoteState::Pending => Self::Pending, + cdk_common::MeltQuoteState::Unknown => Self::Unknown, + cdk_common::MeltQuoteState::Failed => Self::Failed, } } } -impl From for QuoteState { - fn from(value: cashu::nut04::QuoteState) -> Self { +impl From for QuoteState { + fn from(value: cdk_common::nut04::QuoteState) -> Self { match value { - cashu::MintQuoteState::Unpaid => Self::Unpaid, - cashu::MintQuoteState::Paid => Self::Paid, - cashu::MintQuoteState::Pending => Self::Pending, - cashu::MintQuoteState::Issued => Self::Issued, + cdk_common::MintQuoteState::Unpaid => Self::Unpaid, + cdk_common::MintQuoteState::Paid => Self::Paid, + cdk_common::MintQuoteState::Pending => Self::Pending, + cdk_common::MintQuoteState::Issued => Self::Issued, } } } -impl From for MeltQuote { - fn from(value: cashu::mint::MeltQuote) -> Self { +impl From for MeltQuote { + fn from(value: cdk_common::mint::MeltQuote) -> Self { Self { id: value.id.to_string(), unit: value.unit.to_string(), @@ -176,7 +180,7 @@ impl From for MeltQuote { } } -impl TryFrom for cashu::mint::MeltQuote { +impl TryFrom for cdk_common::mint::MeltQuote { type Error = crate::error::Error; fn try_from(value: MeltQuote) -> Result { @@ -184,12 +188,12 @@ impl TryFrom for cashu::mint::MeltQuote { id: value .id .parse() - .map_err(|_| crate::error::Error::Internal)?, + .map_err(|_| crate::error::Error::InvalidId)?, unit: value.unit.parse()?, amount: value.amount.into(), request: value.request.clone(), fee_reserve: value.fee_reserve.into(), - state: cashu::nut05::QuoteState::from(value.state()), + state: cdk_common::nut05::QuoteState::from(value.state()), expiry: value.expiry, payment_preimage: value.payment_preimage, request_lookup_id: value.request_lookup_id, diff --git a/crates/cdk-common/src/proto/payment_processor.proto b/crates/cdk-payment-processor/src/proto/payment_processor.proto similarity index 100% rename from crates/cdk-common/src/proto/payment_processor.proto rename to crates/cdk-payment-processor/src/proto/payment_processor.proto diff --git a/crates/cdk/src/mint/payment_processor/server.rs b/crates/cdk-payment-processor/src/proto/server.rs similarity index 96% rename from crates/cdk/src/mint/payment_processor/server.rs rename to crates/cdk-payment-processor/src/proto/server.rs index dbee4f218..d6ec7ab27 100644 --- a/crates/cdk/src/mint/payment_processor/server.rs +++ b/crates/cdk-payment-processor/src/proto/server.rs @@ -5,19 +5,19 @@ use std::sync::Arc; use std::time::Duration; use cdk_common::lightning::MintLightning; -use cdk_common::proto::cdk_payment_processor_server::CdkPaymentProcessor; -use cdk_common::{CdkPaymentProcessorServer, CurrencyUnit, MeltQuoteBolt11Request}; +use cdk_common::{CurrencyUnit, MeltQuoteBolt11Request}; use tokio::sync::Notify; use tokio::task::JoinHandle; use tonic::transport::{Certificate, Identity, Server, ServerTlsConfig}; use tonic::{async_trait, Request, Response, Status}; +use super::cdk_payment_processor_server::{CdkPaymentProcessor, CdkPaymentProcessorServer}; use crate::proto::*; /// Payment Processor #[derive(Clone)] pub struct PaymentProcessorServer { - inner: Arc + Send + Sync>, + inner: Arc + Send + Sync>, socket_addr: SocketAddr, shutdown: Arc, handle: Option>>>, diff --git a/crates/cdk/src/lib.rs b/crates/cdk/src/lib.rs index 2dbb8b5cf..82f1d158c 100644 --- a/crates/cdk/src/lib.rs +++ b/crates/cdk/src/lib.rs @@ -18,8 +18,8 @@ pub mod pub_sub; pub use cdk_common::{ amount, common as types, dhke, error::{self, Error}, - lightning as cdk_lightning, lightning_invoice, mint_url, nuts, proto, secret, subscription, - tonic, util, ws, Amount, Bolt11Invoice, + lightning as cdk_lightning, lightning_invoice, mint_url, nuts, secret, subscription, tonic, + util, ws, Amount, Bolt11Invoice, }; pub mod fees; diff --git a/crates/cdk/src/mint/mod.rs b/crates/cdk/src/mint/mod.rs index 94d249084..6ee908e3b 100644 --- a/crates/cdk/src/mint/mod.rs +++ b/crates/cdk/src/mint/mod.rs @@ -30,7 +30,6 @@ mod keysets; mod ln; mod melt; mod mint_nut04; -mod payment_processor; mod start_up_check; pub mod subscription; mod swap; @@ -38,8 +37,6 @@ mod verification; pub use builder::{MintBuilder, MintMeltLimits}; pub use cdk_common::mint::{MeltQuote, MintQuote}; -pub use payment_processor::client::PaymentProcessor; -pub use payment_processor::PaymentProcessorServer; /// Cashu Mint #[derive(Clone)] diff --git a/crates/cdk/src/mint/payment_processor/mod.rs b/crates/cdk/src/mint/payment_processor/mod.rs deleted file mode 100644 index 55fce55ae..000000000 --- a/crates/cdk/src/mint/payment_processor/mod.rs +++ /dev/null @@ -1,4 +0,0 @@ -pub mod client; -pub mod server; - -pub use server::PaymentProcessorServer;