1
+ use std:: str:: FromStr ;
2
+
1
3
use async_trait:: async_trait;
2
4
use bcr_ebill_core:: {
3
5
PostalAddress , ServiceTraitBounds ,
4
6
bill:: BitcreditBill ,
5
7
contact:: { BillAnonParticipant , BillIdentParticipant , BillParticipant , ContactType } ,
6
8
util:: { BcrKeys , date:: DateTimeUtc } ,
7
9
} ;
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 } ;
9
12
use cashu:: { nut01 as cdk01, nut02 as cdk02} ;
10
13
use thiserror:: Error ;
11
14
@@ -24,9 +27,18 @@ pub enum Error {
24
27
/// all errors originating from creating signatures
25
28
#[ error( "External Mint Signature Error" ) ]
26
29
Signature ,
27
- /// all errors originating invalid dates
30
+ /// all errors originating from invalid dates
28
31
#[ error( "External Mint Invalid Date Error" ) ]
29
32
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 ,
30
42
}
31
43
32
44
#[ cfg( test) ]
@@ -52,12 +64,16 @@ pub trait MintClientApi: ServiceTraitBounds {
52
64
mint_url : & str ,
53
65
quote_id : & str ,
54
66
) -> Result < QuoteStatusReply > ;
67
+ async fn resolve_quote_for_mint (
68
+ & self ,
69
+ mint_url : & str ,
70
+ quote_id : & str ,
71
+ resolve : ResolveMintOffer ,
72
+ ) -> Result < ( ) > ;
55
73
}
56
74
57
75
#[ derive( Debug , Clone , Default ) ]
58
- pub struct MintClient {
59
- cl : reqwest:: Client ,
60
- }
76
+ pub struct MintClient { }
61
77
62
78
impl ServiceTraitBounds for MintClient { }
63
79
@@ -66,9 +82,14 @@ impl ServiceTraitBounds for MockMintClientApi {}
66
82
67
83
impl MintClient {
68
84
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)
72
93
}
73
94
}
74
95
@@ -98,39 +119,67 @@ impl MintClientApi for MintClient {
98
119
let public_key = cdk01:: PublicKey :: from_hex ( requester_keys. get_public_key ( ) )
99
120
. map_err ( |_| Error :: PubKey ) ?;
100
121
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 ( ) )
118
125
. 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 ( ) )
122
131
}
123
132
124
133
async fn lookup_quote_for_mint (
125
134
& self ,
126
135
mint_url : & str ,
127
136
quote_id : & str ,
128
137
) -> 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
+ } ) ?;
132
146
Ok ( reply. into ( ) )
133
147
}
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
+ }
134
183
}
135
184
136
185
#[ derive( Debug , Clone ) ]
0 commit comments