Skip to content

Commit

Permalink
feat: whats another crate
Browse files Browse the repository at this point in the history
  • Loading branch information
thesimplekid committed Feb 14, 2025
1 parent 6c2aec9 commit 44c331f
Show file tree
Hide file tree
Showing 18 changed files with 184 additions and 91 deletions.
5 changes: 3 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ jobs:
-p cdk-strike,
-p cdk-lnbits,
-p cdk-fake-wallet,
-p cdk-payment-processor,
--bin cdk-cli,
--bin cdk-mintd,
--bin cdk-mintd --no-default-features --features swagger,
Expand Down Expand Up @@ -216,6 +217,7 @@ jobs:
-p cdk-fake-wallet,
-p cdk-cln,
-p cdk-mint-rpc,
-p cdk-payment-processor,
]
steps:
- name: checkout
Expand Down Expand Up @@ -294,8 +296,7 @@ jobs:
-p cdk,
-p cdk --no-default-features,
-p cdk --no-default-features --features wallet,
]
steps:
payment-processor
- name: checkout
uses: actions/checkout@v4
- name: Install Nix
Expand Down
9 changes: 0 additions & 9 deletions crates/cdk-common/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,19 +38,10 @@ futures = "0.3.31"
anyhow = "1.0"
serde_json = "1"
serde_with = "3"
tonic = { version = "0.9", features = [
"channel",
"tls",
"tls-webpki-roots",
] }
prost = "0.11.0"

[target.'cfg(target_arch = "wasm32")'.dependencies]
instant = { version = "0.1", features = ["wasm-bindgen", "inaccurate"] }

[dev-dependencies]
rand = "0.8.5"
bip39 = "2.0"

[build-dependencies]
tonic-build = "0.9"
5 changes: 0 additions & 5 deletions crates/cdk-common/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -27,7 +26,3 @@ 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;
6 changes: 3 additions & 3 deletions crates/cdk-common/src/lightning.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,9 @@ pub enum Error {
/// NUT05 Error
#[error(transparent)]
NUT05(#[from] crate::nuts::nut05::Error),
/// Tonic
#[error(transparent)]
Tonic(#[from] tonic::Status),
/// Custom
#[error("`{0}`")]
Custom(String),
}

/// MintLighting Trait
Expand Down
1 change: 1 addition & 0 deletions crates/cdk-mintd/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
Expand Down
7 changes: 4 additions & 3 deletions crates/cdk-mintd/src/setup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -208,8 +209,8 @@ impl LnBackendSetup for config::FakeWallet {
_router: &mut Vec<Router>,
_settings: &Settings,
_unit: CurrencyUnit,
) -> anyhow::Result<PaymentProcessor> {
let fake_wallet = PaymentProcessor::new("127.0.0.1", 8089, None).await?;
) -> anyhow::Result<PaymentProcessorClient> {
let fake_wallet = PaymentProcessorClient::new("127.0.0.1", 8089, None).await?;

Ok(fake_wallet)
}
Expand Down
59 changes: 59 additions & 0 deletions crates/cdk-payment-processor/Cargo.toml
Original file line number Diff line number Diff line change
@@ -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"
File renamed without changes.
20 changes: 20 additions & 0 deletions crates/cdk-payment-processor/src/error.rs
Original file line number Diff line number Diff line change
@@ -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),
}
8 changes: 8 additions & 0 deletions crates/cdk-payment-processor/src/lib.rs
Original file line number Diff line number Diff line change
@@ -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;
Original file line number Diff line number Diff line change
Expand Up @@ -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<Mutex<CdkPaymentProcessorClient<Channel>>>,
}

impl PaymentProcessor {
impl PaymentProcessorClient {
/// Payment Processor
pub async fn new(addr: &str, port: u16, tls_dir: Option<PathBuf>) -> anyhow::Result<Self> {
let addr = format!("{}:{}", addr, port);
Expand Down Expand Up @@ -58,12 +58,18 @@ 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<Settings, Self::Err> {
let mut inner = self.inner.lock().await;
let response = inner.get_settings(Request::new(SettingsRequest {})).await?;
let response = inner
.get_settings(Request::new(SettingsRequest {}))
.await
.map_err(|err| {
tracing::error!("Could not get settings: {}", err);
cdk_common::lightning::Error::Custom(err.to_string())
})?;

let settings = response.into_inner();

Expand All @@ -90,12 +96,16 @@ impl MintLightning for PaymentProcessor {
description,
unix_expiry: Some(unix_expiry),
}))
.await?;
.await
.map_err(|err| {
tracing::error!("Could not create invoice: {}", err);
cdk_common::lightning::Error::Custom(err.to_string())
})?;

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"))
})?)
}

Expand All @@ -106,7 +116,11 @@ impl MintLightning for PaymentProcessor {
let mut inner = self.inner.lock().await;
let response = inner
.get_payment_quote(Request::new(melt_quote_request.into()))
.await?;
.await
.map_err(|err| {
tracing::error!("Could not get payment quote: {}", err);
cdk_common::lightning::Error::Custom(err.to_string())
})?;

let response = response.into_inner();

Expand All @@ -126,12 +140,16 @@ impl MintLightning for PaymentProcessor {
partial_amount: partial_amount.map(|a| a.into()),
max_fee_amount: max_fee_amount.map(|a| a.into()),
}))
.await?;
.await
.map_err(|err| {
tracing::error!("Could not pay invoice: {}", err);
cdk_common::lightning::Error::Custom(err.to_string())
})?;

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"))
})?)
}

Expand Down Expand Up @@ -161,7 +179,11 @@ impl MintLightning for PaymentProcessor {
.check_incoming_payment(Request::new(CheckIncomingPaymentRequest {
request_lookup_id: request_lookup_id.to_string(),
}))
.await?;
.await
.map_err(|err| {
tracing::error!("Could not check incoming payment: {}", err);
cdk_common::lightning::Error::Custom(err.to_string())
})?;

let check_incoming = response.into_inner();

Expand All @@ -179,12 +201,16 @@ impl MintLightning for PaymentProcessor {
.check_outgoing_payment(Request::new(CheckOutgoingPaymentRequest {
request_lookup_id: request_lookup_id.to_string(),
}))
.await?;
.await
.map_err(|err| {
tracing::error!("Could not check outgoing payment: {}", err);
cdk_common::lightning::Error::Custom(err.to_string())
})?;

let check_outgoing = response.into_inner();

Ok(check_outgoing
.try_into()
.map_err(|_| crate::cdk_lightning::Error::UnknownPaymentState)?)
.map_err(|_| cdk_common::lightning::Error::UnknownPaymentState)?)
}
}
Loading

0 comments on commit 44c331f

Please sign in to comment.