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 95cea3c
Show file tree
Hide file tree
Showing 14 changed files with 146 additions and 63 deletions.
3 changes: 0 additions & 3 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,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;
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,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<Settings, Self::Err> {
let mut inner = self.inner.lock().await;
Expand Down Expand Up @@ -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"))
})?)
}

Expand Down Expand Up @@ -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"))
})?)
}

Expand Down Expand Up @@ -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)?)
}
}
Original file line number Diff line number Diff line change
@@ -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");

Expand Down Expand Up @@ -74,8 +78,8 @@ impl From<&MeltQuoteBolt11Request> for PaymentQuoteRequest {
}
}

impl From<crate::lightning::PaymentQuoteResponse> for PaymentQuoteResponse {
fn from(value: crate::lightning::PaymentQuoteResponse) -> Self {
impl From<cdk_common::lightning::PaymentQuoteResponse> for PaymentQuoteResponse {
fn from(value: cdk_common::lightning::PaymentQuoteResponse) -> Self {
Self {
request_lookup_id: value.request_lookup_id,
amount: value.amount.into(),
Expand All @@ -85,34 +89,34 @@ impl From<crate::lightning::PaymentQuoteResponse> for PaymentQuoteResponse {
}
}

impl From<cashu::nut05::MeltOptions> for MeltOptions {
fn from(value: cashu::nut05::MeltOptions) -> Self {
impl From<cdk_common::nut05::MeltOptions> for MeltOptions {
fn from(value: cdk_common::nut05::MeltOptions) -> Self {
Self {
options: Some(value.into()),
}
}
}

impl From<cashu::nut05::MeltOptions> for Options {
fn from(value: cashu::nut05::MeltOptions) -> Self {
impl From<cdk_common::nut05::MeltOptions> 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<MeltOptions> for cashu::nut05::MeltOptions {
impl From<MeltOptions> 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<PaymentQuoteResponse> for crate::lightning::PaymentQuoteResponse {
impl From<PaymentQuoteResponse> for cdk_common::lightning::PaymentQuoteResponse {
fn from(value: PaymentQuoteResponse) -> Self {
Self {
request_lookup_id: value.request_lookup_id.clone(),
Expand All @@ -123,7 +127,7 @@ impl From<PaymentQuoteResponse> for crate::lightning::PaymentQuoteResponse {
}
}

impl From<QuoteState> for cashu::nut05::QuoteState {
impl From<QuoteState> for cdk_common::nut05::QuoteState {
fn from(value: QuoteState) -> Self {
match value {
QuoteState::Unpaid => Self::Unpaid,
Expand All @@ -136,31 +140,31 @@ impl From<QuoteState> for cashu::nut05::QuoteState {
}
}

impl From<cashu::nut05::QuoteState> for QuoteState {
fn from(value: cashu::nut05::QuoteState) -> Self {
impl From<cdk_common::nut05::QuoteState> 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<cashu::nut04::QuoteState> for QuoteState {
fn from(value: cashu::nut04::QuoteState) -> Self {
impl From<cdk_common::nut04::QuoteState> 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<cashu::mint::MeltQuote> for MeltQuote {
fn from(value: cashu::mint::MeltQuote) -> Self {
impl From<cdk_common::mint::MeltQuote> for MeltQuote {
fn from(value: cdk_common::mint::MeltQuote) -> Self {
Self {
id: value.id.to_string(),
unit: value.unit.to_string(),
Expand All @@ -176,20 +180,20 @@ impl From<cashu::mint::MeltQuote> for MeltQuote {
}
}

impl TryFrom<MeltQuote> for cashu::mint::MeltQuote {
impl TryFrom<MeltQuote> for cdk_common::mint::MeltQuote {
type Error = crate::error::Error;

fn try_from(value: MeltQuote) -> Result<Self, Self::Error> {
Ok(Self {
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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<dyn MintLightning<Err = crate::cdk_lightning::Error> + Send + Sync>,
inner: Arc<dyn MintLightning<Err = cdk_common::lightning::Error> + Send + Sync>,
socket_addr: SocketAddr,
shutdown: Arc<Notify>,
handle: Option<Arc<JoinHandle<anyhow::Result<()>>>>,
Expand Down
4 changes: 2 additions & 2 deletions crates/cdk/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Loading

0 comments on commit 95cea3c

Please sign in to comment.