|
| 1 | +use async_trait::async_trait; |
| 2 | +use bcr_ebill_core::{ |
| 3 | + PostalAddress, ServiceTraitBounds, |
| 4 | + bill::BitcreditBill, |
| 5 | + contact::{BillAnonParticipant, BillIdentParticipant, BillParticipant, ContactType}, |
| 6 | + util::BcrKeys, |
| 7 | +}; |
| 8 | +use bcr_wdc_webapi::quotes::{BillInfo, EnquireReply, EnquireRequest}; |
| 9 | +use cashu::nut01 as cdk01; |
| 10 | +use thiserror::Error; |
| 11 | + |
| 12 | +/// Generic result type |
| 13 | +pub type Result<T> = std::result::Result<T, super::Error>; |
| 14 | + |
| 15 | +/// Generic error type |
| 16 | +#[derive(Debug, Error)] |
| 17 | +pub enum Error { |
| 18 | + /// all errors originating from interacting with the web api |
| 19 | + #[error("External Mint Web API error: {0}")] |
| 20 | + Api(#[from] reqwest::Error), |
| 21 | + /// all errors originating from parsing public keys |
| 22 | + #[error("External Mint Public Key Error")] |
| 23 | + PubKey, |
| 24 | + /// all errors originating from creating signatures |
| 25 | + #[error("External Mint Signature Error")] |
| 26 | + Signature, |
| 27 | + /// all errors originating invalid dates |
| 28 | + #[error("External Mint Invalid Date Error")] |
| 29 | + InvalidDate, |
| 30 | +} |
| 31 | + |
| 32 | +#[cfg(test)] |
| 33 | +use mockall::automock; |
| 34 | + |
| 35 | +use crate::util; |
| 36 | + |
| 37 | +#[cfg_attr(test, automock)] |
| 38 | +#[cfg_attr(target_arch = "wasm32", async_trait(?Send))] |
| 39 | +#[cfg_attr(not(target_arch = "wasm32"), async_trait)] |
| 40 | +pub trait MintClientApi: ServiceTraitBounds { |
| 41 | + async fn enquire_mint_quote( |
| 42 | + &self, |
| 43 | + mint_url: &str, |
| 44 | + requester_keys: &BcrKeys, |
| 45 | + bill: &BitcreditBill, |
| 46 | + endorsees: &[BillParticipant], |
| 47 | + ) -> Result<String>; |
| 48 | +} |
| 49 | + |
| 50 | +#[derive(Debug, Clone, Default)] |
| 51 | +pub struct MintClient { |
| 52 | + cl: reqwest::Client, |
| 53 | +} |
| 54 | + |
| 55 | +impl ServiceTraitBounds for MintClient {} |
| 56 | + |
| 57 | +#[cfg(test)] |
| 58 | +impl ServiceTraitBounds for MockMintClientApi {} |
| 59 | + |
| 60 | +impl MintClient { |
| 61 | + pub fn new() -> Self { |
| 62 | + Self { |
| 63 | + cl: reqwest::Client::new(), |
| 64 | + } |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +#[cfg_attr(target_arch = "wasm32", async_trait(?Send))] |
| 69 | +#[cfg_attr(not(target_arch = "wasm32"), async_trait)] |
| 70 | +impl MintClientApi for MintClient { |
| 71 | + async fn enquire_mint_quote( |
| 72 | + &self, |
| 73 | + mint_url: &str, |
| 74 | + requester_keys: &BcrKeys, |
| 75 | + bill: &BitcreditBill, |
| 76 | + endorsees: &[BillParticipant], |
| 77 | + ) -> Result<String> { |
| 78 | + let bill_info = BillInfo { |
| 79 | + id: bill.id.clone(), |
| 80 | + drawee: map_bill_ident_participant(bill.drawee.to_owned()), |
| 81 | + drawer: map_bill_ident_participant(bill.drawer.to_owned()), |
| 82 | + payee: map_bill_participant(bill.payee.to_owned()), |
| 83 | + endorsees: endorsees |
| 84 | + .iter() |
| 85 | + .map(|e| map_bill_participant(e.to_owned())) |
| 86 | + .collect(), |
| 87 | + sum: bill.sum, |
| 88 | + maturity_date: util::date::date_string_to_rfc3339(&bill.maturity_date) |
| 89 | + .map_err(|_| Error::InvalidDate)?, |
| 90 | + }; |
| 91 | + let public_key = cdk01::PublicKey::from_hex(requester_keys.get_public_key()) |
| 92 | + .map_err(|_| Error::PubKey)?; |
| 93 | + |
| 94 | + let signature = bcr_wdc_utils::keys::schnorr_sign_borsh_msg_with_key( |
| 95 | + &bill_info, |
| 96 | + &requester_keys.get_key_pair(), |
| 97 | + ) |
| 98 | + .map_err(|_| Error::Signature)?; |
| 99 | + |
| 100 | + let payload: EnquireRequest = EnquireRequest { |
| 101 | + content: bill_info, |
| 102 | + signature, |
| 103 | + public_key, |
| 104 | + }; |
| 105 | + let url = format!("{}/v1/mint/credit/quote", mint_url); |
| 106 | + let res = self |
| 107 | + .cl |
| 108 | + .post(&url) |
| 109 | + .json(&payload) |
| 110 | + .send() |
| 111 | + .await |
| 112 | + .map_err(Error::from)?; |
| 113 | + let reply: EnquireReply = res.json().await.map_err(Error::from)?; |
| 114 | + Ok(reply.id.to_string()) |
| 115 | + } |
| 116 | +} |
| 117 | + |
| 118 | +// These are needed for now, because we use different `bcr-ebill-core` versions in wildcat and here |
| 119 | +// this should be remediated, once we're stable on both sides |
| 120 | + |
| 121 | +fn map_bill_participant(part: BillParticipant) -> bcr_wdc_webapi::bill::BillParticipant { |
| 122 | + match part { |
| 123 | + BillParticipant::Ident(data) => { |
| 124 | + bcr_wdc_webapi::bill::BillParticipant::Ident(map_bill_ident_participant(data)) |
| 125 | + } |
| 126 | + BillParticipant::Anon(data) => { |
| 127 | + bcr_wdc_webapi::bill::BillParticipant::Anon(map_bill_anon_participant(data)) |
| 128 | + } |
| 129 | + } |
| 130 | +} |
| 131 | + |
| 132 | +fn map_bill_anon_participant( |
| 133 | + ident: BillAnonParticipant, |
| 134 | +) -> bcr_wdc_webapi::bill::BillAnonParticipant { |
| 135 | + bcr_wdc_webapi::bill::BillAnonParticipant { |
| 136 | + node_id: ident.node_id, |
| 137 | + email: ident.email, |
| 138 | + nostr_relays: ident.nostr_relays, |
| 139 | + } |
| 140 | +} |
| 141 | + |
| 142 | +fn map_bill_ident_participant( |
| 143 | + ident: BillIdentParticipant, |
| 144 | +) -> bcr_wdc_webapi::bill::BillIdentParticipant { |
| 145 | + bcr_wdc_webapi::bill::BillIdentParticipant { |
| 146 | + t: map_contact_type(ident.t), |
| 147 | + node_id: ident.node_id, |
| 148 | + name: ident.name, |
| 149 | + postal_address: map_postal_address(ident.postal_address), |
| 150 | + email: ident.email, |
| 151 | + nostr_relays: ident.nostr_relays, |
| 152 | + } |
| 153 | +} |
| 154 | + |
| 155 | +fn map_contact_type(ct: ContactType) -> bcr_wdc_webapi::contact::ContactType { |
| 156 | + match ct { |
| 157 | + ContactType::Person => bcr_wdc_webapi::contact::ContactType::Person, |
| 158 | + ContactType::Company => bcr_wdc_webapi::contact::ContactType::Company, |
| 159 | + ContactType::Anon => bcr_wdc_webapi::contact::ContactType::Anon, |
| 160 | + } |
| 161 | +} |
| 162 | + |
| 163 | +fn map_postal_address(pa: PostalAddress) -> bcr_wdc_webapi::identity::PostalAddress { |
| 164 | + bcr_wdc_webapi::identity::PostalAddress { |
| 165 | + country: pa.country, |
| 166 | + city: pa.city, |
| 167 | + zip: pa.zip, |
| 168 | + address: pa.address, |
| 169 | + } |
| 170 | +} |
0 commit comments