Skip to content

Commit 508bf4f

Browse files
authored
use quote client (#526)
1 parent 2d726d5 commit 508bf4f

File tree

2 files changed

+82
-33
lines changed

2 files changed

+82
-33
lines changed

crates/bcr-ebill-api/Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ bcr-ebill-transport = { path = "../bcr-ebill-transport" }
3131
tokio.workspace = true
3232
tokio_with_wasm.workspace = true
3333
secp256k1.workspace = true
34-
bcr-wdc-webapi = { git = "https://github.com/BitcreditProtocol/wildcat", rev = "db63f77e092e529912baebd201f998479b8fefcb" }
35-
bcr-wdc-utils = { git = "https://github.com/BitcreditProtocol/wildcat", rev = "db63f77e092e529912baebd201f998479b8fefcb" }
34+
bcr-wdc-webapi = { git = "https://github.com/BitcreditProtocol/wildcat", rev = "30634137db2675ce60dc4cc7c197a8fea9a1b78d" }
35+
bcr-wdc-quote-client = { git = "https://github.com/BitcreditProtocol/wildcat", rev = "30634137db2675ce60dc4cc7c197a8fea9a1b78d" }
3636
cashu = { version = "0.9", default-features = false }
3737

3838
[target.'cfg(target_arch = "wasm32")'.dependencies]

crates/bcr-ebill-api/src/external/mint.rs

Lines changed: 80 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
1+
use std::str::FromStr;
2+
13
use async_trait::async_trait;
24
use bcr_ebill_core::{
35
PostalAddress, ServiceTraitBounds,
46
bill::BitcreditBill,
57
contact::{BillAnonParticipant, BillIdentParticipant, BillParticipant, ContactType},
68
util::{BcrKeys, date::DateTimeUtc},
79
};
8-
use bcr_wdc_webapi::quotes::{BillInfo, EnquireReply, EnquireRequest, StatusReply};
10+
use bcr_wdc_quote_client::QuoteClient;
11+
use bcr_wdc_webapi::quotes::{BillInfo, ResolveOffer, StatusReply};
912
use cashu::{nut01 as cdk01, nut02 as cdk02};
1013
use thiserror::Error;
1114

@@ -24,9 +27,18 @@ pub enum Error {
2427
/// all errors originating from creating signatures
2528
#[error("External Mint Signature Error")]
2629
Signature,
27-
/// all errors originating invalid dates
30+
/// all errors originating from invalid dates
2831
#[error("External Mint Invalid Date Error")]
2932
InvalidDate,
33+
/// all errors originating from invalid mint urls
34+
#[error("External Mint Invalid Mint Url Error")]
35+
InvalidMintUrl,
36+
/// all errors originating from invalid mint request ids
37+
#[error("External Mint Invalid Mint Request Id Error")]
38+
InvalidMintRequestId,
39+
/// all errors originating from the quote client
40+
#[error("External Mint Quote Client Error")]
41+
QuoteClient,
3042
}
3143

3244
#[cfg(test)]
@@ -52,12 +64,16 @@ pub trait MintClientApi: ServiceTraitBounds {
5264
mint_url: &str,
5365
quote_id: &str,
5466
) -> Result<QuoteStatusReply>;
67+
async fn resolve_quote_for_mint(
68+
&self,
69+
mint_url: &str,
70+
quote_id: &str,
71+
resolve: ResolveMintOffer,
72+
) -> Result<()>;
5573
}
5674

5775
#[derive(Debug, Clone, Default)]
58-
pub struct MintClient {
59-
cl: reqwest::Client,
60-
}
76+
pub struct MintClient {}
6177

6278
impl ServiceTraitBounds for MintClient {}
6379

@@ -66,9 +82,14 @@ impl ServiceTraitBounds for MockMintClientApi {}
6682

6783
impl MintClient {
6884
pub fn new() -> Self {
69-
Self {
70-
cl: reqwest::Client::new(),
71-
}
85+
Self {}
86+
}
87+
88+
pub fn quote_client(&self, mint_url: &str) -> Result<QuoteClient> {
89+
let quote_client = bcr_wdc_quote_client::QuoteClient::new(
90+
reqwest::Url::parse(mint_url).map_err(|_| Error::InvalidMintUrl)?,
91+
);
92+
Ok(quote_client)
7293
}
7394
}
7495

@@ -98,39 +119,67 @@ impl MintClientApi for MintClient {
98119
let public_key = cdk01::PublicKey::from_hex(requester_keys.get_public_key())
99120
.map_err(|_| Error::PubKey)?;
100121

101-
let signature = bcr_wdc_utils::keys::schnorr_sign_borsh_msg_with_key(
102-
&bill_info,
103-
&requester_keys.get_key_pair(),
104-
)
105-
.map_err(|_| Error::Signature)?;
106-
107-
let payload: EnquireRequest = EnquireRequest {
108-
content: bill_info,
109-
signature,
110-
public_key,
111-
};
112-
let url = format!("{}/v1/mint/credit/quote", mint_url);
113-
let res = self
114-
.cl
115-
.post(&url)
116-
.json(&payload)
117-
.send()
122+
let mint_request_id = self
123+
.quote_client(mint_url)?
124+
.enquire(bill_info, public_key, &requester_keys.get_key_pair())
118125
.await
119-
.map_err(Error::from)?;
120-
let reply: EnquireReply = res.json().await.map_err(Error::from)?;
121-
Ok(reply.id.to_string())
126+
.map_err(|e| {
127+
log::error!("Error enquiring to mint {mint_url}: {e}");
128+
Error::QuoteClient
129+
})?;
130+
Ok(mint_request_id.to_string())
122131
}
123132

124133
async fn lookup_quote_for_mint(
125134
&self,
126135
mint_url: &str,
127136
quote_id: &str,
128137
) -> Result<QuoteStatusReply> {
129-
let url = format!("{}/v1/mint/credit/quote/{quote_id}", mint_url);
130-
let res = self.cl.get(&url).send().await.map_err(Error::from)?;
131-
let reply: StatusReply = res.json().await.map_err(Error::from)?;
138+
let reply = self
139+
.quote_client(mint_url)?
140+
.lookup(uuid::Uuid::from_str(quote_id).map_err(|_| Error::InvalidMintRequestId)?)
141+
.await
142+
.map_err(|e| {
143+
log::error!("Error looking up request on mint {mint_url}: {e}");
144+
Error::QuoteClient
145+
})?;
132146
Ok(reply.into())
133147
}
148+
149+
#[allow(dead_code)]
150+
async fn resolve_quote_for_mint(
151+
&self,
152+
mint_url: &str,
153+
quote_id: &str,
154+
resolve: ResolveMintOffer,
155+
) -> Result<()> {
156+
self.quote_client(mint_url)?
157+
.resolve(
158+
uuid::Uuid::from_str(quote_id).map_err(|_| Error::InvalidMintRequestId)?,
159+
resolve.into(),
160+
)
161+
.await
162+
.map_err(|e| {
163+
log::error!("Error resolving request on mint {mint_url}: {e}");
164+
Error::QuoteClient
165+
})?;
166+
Ok(())
167+
}
168+
}
169+
170+
#[derive(Debug, Clone)]
171+
pub enum ResolveMintOffer {
172+
Accept,
173+
Reject,
174+
}
175+
176+
impl From<ResolveMintOffer> for ResolveOffer {
177+
fn from(value: ResolveMintOffer) -> Self {
178+
match value {
179+
ResolveMintOffer::Accept => ResolveOffer::Accept,
180+
ResolveMintOffer::Reject => ResolveOffer::Reject,
181+
}
182+
}
134183
}
135184

136185
#[derive(Debug, Clone)]

0 commit comments

Comments
 (0)