Skip to content

Commit

Permalink
DirectMintConnection: convert between String and Uuid
Browse files Browse the repository at this point in the history
  • Loading branch information
ok300 committed Dec 7, 2024
1 parent 90085c8 commit 6f0b2de
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 13 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

51 changes: 38 additions & 13 deletions crates/cdk-integration-tests/tests/integration_tests_pure.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ mod integration_tests_pure {
use std::assert_eq;
use std::collections::HashMap;
use std::fmt::{Debug, Formatter};
use std::str::FromStr;
use std::sync::Arc;

use async_trait::async_trait;
Expand All @@ -22,6 +23,7 @@ mod integration_tests_pure {
use cdk_integration_tests::create_backends_fake_wallet;
use rand::random;
use tokio::sync::Notify;
use uuid::Uuid;

struct DirectMintConnection {
mint: Arc<Mint>,
Expand All @@ -37,6 +39,10 @@ mod integration_tests_pure {
}
}

/// Implements the generic [MintConnector] (i.e. use the interface that expects to communicate
/// to a generic mint, where we don't know that quote ID's are [Uuid]s) for [DirectMintConnection],
/// where we know we're dealing with a mint that uses [Uuid]s for quotes.
/// Convert the requests and responses between the [String] and [Uuid] variants as necessary.
#[async_trait]
impl MintConnector for DirectMintConnection {
async fn get_mint_keys(&self) -> Result<Vec<KeySet>, Error> {
Expand All @@ -57,40 +63,59 @@ mod integration_tests_pure {
async fn post_mint_quote(
&self,
request: MintQuoteBolt11Request,
) -> Result<MintQuoteBolt11Response, Error> {
self.mint.get_mint_bolt11_quote(request).await
) -> Result<MintQuoteBolt11Response<String>, Error> {
self.mint
.get_mint_bolt11_quote(request)
.await
.map(Into::into)
}

async fn get_mint_quote_status(
&self,
quote_id: &str,
) -> Result<MintQuoteBolt11Response, Error> {
self.mint.check_mint_quote(quote_id).await
) -> Result<MintQuoteBolt11Response<String>, Error> {
let quote_id_uuid = Uuid::from_str(quote_id).unwrap();
self.mint
.check_mint_quote(&quote_id_uuid)
.await
.map(Into::into)
}

async fn post_mint(&self, request: MintBolt11Request) -> Result<MintBolt11Response, Error> {
self.mint.process_mint_request(request).await
async fn post_mint(
&self,
request: MintBolt11Request<String>,
) -> Result<MintBolt11Response, Error> {
let request_uuid = request.try_into().unwrap();
self.mint.process_mint_request(request_uuid).await
}

async fn post_melt_quote(
&self,
request: MeltQuoteBolt11Request,
) -> Result<MeltQuoteBolt11Response, Error> {
self.mint.get_melt_bolt11_quote(&request).await
) -> Result<MeltQuoteBolt11Response<String>, Error> {
self.mint
.get_melt_bolt11_quote(&request)
.await
.map(Into::into)
}

async fn get_melt_quote_status(
&self,
quote_id: &str,
) -> Result<MeltQuoteBolt11Response, Error> {
self.mint.check_melt_quote(quote_id).await
) -> Result<MeltQuoteBolt11Response<String>, Error> {
let quote_id_uuid = Uuid::from_str(quote_id).unwrap();
self.mint
.check_melt_quote(&quote_id_uuid)
.await
.map(Into::into)
}

async fn post_melt(
&self,
request: MeltBolt11Request,
) -> Result<MeltQuoteBolt11Response, Error> {
self.mint.melt_bolt11(&request).await
request: MeltBolt11Request<String>,
) -> Result<MeltQuoteBolt11Response<String>, Error> {
let request_uuid = request.try_into().unwrap();
self.mint.melt_bolt11(&request_uuid).await.map(Into::into)
}

async fn post_swap(&self, swap_request: SwapRequest) -> Result<SwapResponse, Error> {
Expand Down
22 changes: 22 additions & 0 deletions crates/cdk/src/nuts/nut04.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,17 @@ pub struct MintQuoteBolt11Response<Q> {
pub expiry: Option<u64>,
}

impl From<MintQuoteBolt11Response<Uuid>> for MintQuoteBolt11Response<String> {
fn from(value: MintQuoteBolt11Response<Uuid>) -> Self {
Self {
quote: value.quote.to_string(),
request: value.request,
state: value.state,
expiry: value.expiry,
}
}
}

#[cfg(feature = "mint")]
impl From<crate::mint::MintQuote> for MintQuoteBolt11Response<Uuid> {
fn from(mint_quote: crate::mint::MintQuote) -> MintQuoteBolt11Response<Uuid> {
Expand All @@ -121,6 +132,17 @@ pub struct MintBolt11Request<Q> {
pub outputs: Vec<BlindedMessage>,
}

impl TryFrom<MintBolt11Request<String>> for MintBolt11Request<Uuid> {
type Error = uuid::Error;

fn try_from(value: MintBolt11Request<String>) -> Result<Self, Self::Error> {
Ok(Self {
quote: Uuid::from_str(&value.quote)?,
outputs: value.outputs,
})
}
}

impl<Q> MintBolt11Request<Q> {
/// Total [`Amount`] of outputs
pub fn total_amount(&self) -> Result<Amount, Error> {
Expand Down
27 changes: 27 additions & 0 deletions crates/cdk/src/nuts/nut05.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,21 @@ pub struct MeltQuoteBolt11Response<Q> {
pub change: Option<Vec<BlindSignature>>,
}

impl From<MeltQuoteBolt11Response<Uuid>> for MeltQuoteBolt11Response<String> {
fn from(value: MeltQuoteBolt11Response<Uuid>) -> Self {
Self {
quote: value.quote.to_string(),
amount: value.amount,
fee_reserve: value.fee_reserve,
paid: value.paid,
state: value.state,
expiry: value.expiry,
payment_preimage: value.payment_preimage,
change: value.change,
}
}
}

#[cfg(feature = "mint")]
impl From<&MeltQuote> for MeltQuoteBolt11Response<Uuid> {
fn from(melt_quote: &MeltQuote) -> MeltQuoteBolt11Response<Uuid> {
Expand Down Expand Up @@ -247,6 +262,18 @@ pub struct MeltBolt11Request<Q> {
pub outputs: Option<Vec<BlindedMessage>>,
}

impl TryFrom<MeltBolt11Request<String>> for MeltBolt11Request<Uuid> {
type Error = uuid::Error;

fn try_from(value: MeltBolt11Request<String>) -> Result<Self, Self::Error> {
Ok(Self {
quote: Uuid::from_str(&value.quote)?,
inputs: value.inputs,
outputs: value.outputs,
})
}
}

impl<Q: Serialize + DeserializeOwned> MeltBolt11Request<Q> {
/// Total [`Amount`] of [`Proofs`]
pub fn proofs_amount(&self) -> Result<Amount, Error> {
Expand Down

0 comments on commit 6f0b2de

Please sign in to comment.