client.rs 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. //! Client to connet to mint
  2. use bitcoin::Amount;
  3. use serde_json::Value;
  4. use url::Url;
  5. pub use crate::Invoice;
  6. use crate::{
  7. error::Error,
  8. keyset::{Keys, MintKeySets},
  9. types::{
  10. BlindedMessage, BlindedMessages, CheckFeesRequest, CheckFeesResponse,
  11. CheckSpendableRequest, CheckSpendableResponse, MeltRequest, MeltResponse, MintError,
  12. MintInfo, MintRequest, PostMintResponse, Proof, RequestMintResponse, SplitRequest,
  13. SplitResponse,
  14. },
  15. };
  16. #[derive(Debug, Clone)]
  17. pub struct Client {
  18. pub mint_url: Url,
  19. }
  20. impl Client {
  21. pub fn new(mint_url: &str) -> Result<Self, Error> {
  22. // HACK
  23. let mut mint_url = String::from(mint_url);
  24. if !mint_url.ends_with('/') {
  25. mint_url.push('/');
  26. }
  27. let mint_url = Url::parse(&mint_url)?;
  28. Ok(Self { mint_url })
  29. }
  30. /// Get Mint Keys [NUT-01]
  31. pub async fn get_keys(&self) -> Result<Keys, Error> {
  32. let url = self.mint_url.join("keys")?;
  33. let keys = minreq::get(url).send()?.json::<Value>()?;
  34. let keys: Keys = serde_json::from_str(&keys.to_string())?;
  35. /*
  36. let keys: BTreeMap<u64, String> = match serde_json::from_value(keys.clone()) {
  37. Ok(keys) => keys,
  38. Err(_err) => {
  39. return Err(Error::CustomError(format!(
  40. "url: {}, {}",
  41. url,
  42. serde_json::to_string(&keys)?
  43. )))
  44. }
  45. };
  46. let mint_keys: BTreeMap<u64, PublicKey> = keys
  47. .into_iter()
  48. .filter_map(|(k, v)| {
  49. let key = hex::decode(v).ok()?;
  50. let public_key = PublicKey::from_sec1_bytes(&key).ok()?;
  51. Some((k, public_key))
  52. })
  53. .collect();
  54. */
  55. Ok(keys)
  56. }
  57. /// Get Keysets [NUT-02]
  58. pub async fn get_keysets(&self) -> Result<MintKeySets, Error> {
  59. let url = self.mint_url.join("keysets")?;
  60. let res = minreq::get(url).send()?.json::<Value>()?;
  61. let response: Result<MintKeySets, serde_json::Error> = serde_json::from_value(res.clone());
  62. match response {
  63. Ok(res) => Ok(res),
  64. Err(_) => Err(Error::CustomError(res.to_string())),
  65. }
  66. }
  67. /// Request Mint [NUT-03]
  68. pub async fn request_mint(&self, amount: Amount) -> Result<RequestMintResponse, Error> {
  69. let mut url = self.mint_url.join("mint")?;
  70. url.query_pairs_mut()
  71. .append_pair("amount", &amount.to_sat().to_string());
  72. let res = minreq::get(url).send()?.json::<Value>()?;
  73. let response: Result<RequestMintResponse, serde_json::Error> =
  74. serde_json::from_value(res.clone());
  75. match response {
  76. Ok(res) => Ok(res),
  77. Err(_) => Err(Error::CustomError(res.to_string())),
  78. }
  79. }
  80. /// Mint Tokens [NUT-04]
  81. pub async fn mint(
  82. &self,
  83. blinded_messages: BlindedMessages,
  84. hash: &str,
  85. ) -> Result<PostMintResponse, Error> {
  86. let mut url = self.mint_url.join("mint")?;
  87. url.query_pairs_mut().append_pair("hash", hash);
  88. let request = MintRequest {
  89. outputs: blinded_messages.blinded_messages,
  90. };
  91. let res = minreq::post(url)
  92. .with_json(&request)?
  93. .send()?
  94. .json::<Value>()?;
  95. let response: Result<PostMintResponse, serde_json::Error> =
  96. serde_json::from_value(res.clone());
  97. match response {
  98. Ok(res) => Ok(res),
  99. Err(_) => Err(MintError::from_json(&res.to_string())?.into()),
  100. }
  101. }
  102. /// Check Max expected fee [NUT-05]
  103. pub async fn check_fees(&self, invoice: Invoice) -> Result<CheckFeesResponse, Error> {
  104. let url = self.mint_url.join("checkfees")?;
  105. let request = CheckFeesRequest { pr: invoice };
  106. let res = minreq::post(url)
  107. .with_json(&request)?
  108. .send()?
  109. .json::<Value>()?;
  110. let response: Result<CheckFeesResponse, serde_json::Error> =
  111. serde_json::from_value(res.clone());
  112. match response {
  113. Ok(res) => Ok(res),
  114. Err(_) => Err(MintError::from_json(&res.to_string())?.into()),
  115. }
  116. }
  117. /// Melt [NUT-05]
  118. /// [Nut-08] Lightning fee return if outputs defined
  119. pub async fn melt(
  120. &self,
  121. proofs: Vec<Proof>,
  122. invoice: Invoice,
  123. outputs: Option<Vec<BlindedMessage>>,
  124. ) -> Result<MeltResponse, Error> {
  125. let url = self.mint_url.join("melt")?;
  126. let request = MeltRequest {
  127. proofs,
  128. pr: invoice,
  129. outputs,
  130. };
  131. let value = minreq::post(url)
  132. .with_json(&request)?
  133. .send()?
  134. .json::<Value>()?;
  135. let response: Result<MeltResponse, serde_json::Error> =
  136. serde_json::from_value(value.clone());
  137. match response {
  138. Ok(res) => Ok(res),
  139. Err(_) => Err(MintError::from_json(&value.to_string())?.into()),
  140. }
  141. }
  142. /// Split Token [NUT-06]
  143. pub async fn split(&self, split_request: SplitRequest) -> Result<SplitResponse, Error> {
  144. let url = self.mint_url.join("split")?;
  145. let res = minreq::post(url)
  146. .with_json(&split_request)?
  147. .send()?
  148. .json::<Value>()?;
  149. let response: Result<SplitResponse, serde_json::Error> =
  150. serde_json::from_value(res.clone());
  151. match response {
  152. Ok(res) => Ok(res),
  153. Err(_) => Err(MintError::from_json(&res.to_string())?.into()),
  154. }
  155. }
  156. /// Spendable check [NUT-07]
  157. pub async fn check_spendable(
  158. &self,
  159. proofs: &Vec<Proof>,
  160. ) -> Result<CheckSpendableResponse, Error> {
  161. let url = self.mint_url.join("check")?;
  162. let request = CheckSpendableRequest {
  163. proofs: proofs.to_owned(),
  164. };
  165. let res = minreq::post(url)
  166. .with_json(&request)?
  167. .send()?
  168. .json::<Value>()?;
  169. let response: Result<CheckSpendableResponse, serde_json::Error> =
  170. serde_json::from_value(res.clone());
  171. match response {
  172. Ok(res) => Ok(res),
  173. Err(_) => Err(MintError::from_json(&res.to_string())?.into()),
  174. }
  175. }
  176. /// Get Mint Info [NUT-09]
  177. pub async fn get_info(&self) -> Result<MintInfo, Error> {
  178. let url = self.mint_url.join("info")?;
  179. let res = minreq::get(url).send()?.json::<Value>()?;
  180. let response: Result<MintInfo, serde_json::Error> = serde_json::from_value(res.clone());
  181. match response {
  182. Ok(res) => Ok(res),
  183. Err(_) => Err(MintError::from_json(&res.to_string())?.into()),
  184. }
  185. }
  186. }