|
@@ -1,24 +1,27 @@
|
|
|
//! Client to connet to mint
|
|
|
use std::fmt;
|
|
|
|
|
|
-use gloo::net::http::Request;
|
|
|
use serde::{Deserialize, Serialize};
|
|
|
use serde_json::Value;
|
|
|
use url::Url;
|
|
|
|
|
|
-use crate::nuts::nut00::{wallet::BlindedMessages, BlindedMessage, Proof};
|
|
|
-use crate::nuts::nut01::Keys;
|
|
|
-use crate::nuts::nut03::RequestMintResponse;
|
|
|
-use crate::nuts::nut04::{MintRequest, PostMintResponse};
|
|
|
-use crate::nuts::nut05::{CheckFeesRequest, CheckFeesResponse};
|
|
|
-use crate::nuts::nut06::{SplitRequest, SplitResponse};
|
|
|
-use crate::nuts::nut07::{CheckSpendableRequest, CheckSpendableResponse};
|
|
|
-use crate::nuts::nut08::{MeltRequest, MeltResponse};
|
|
|
-use crate::nuts::nut09::MintInfo;
|
|
|
-use crate::nuts::*;
|
|
|
-use crate::utils;
|
|
|
-use crate::Amount;
|
|
|
-pub use crate::Bolt11Invoice;
|
|
|
+use cashu::nuts::nut00::{wallet::BlindedMessages, BlindedMessage, Proof};
|
|
|
+use cashu::nuts::nut01::Keys;
|
|
|
+use cashu::nuts::nut03::RequestMintResponse;
|
|
|
+use cashu::nuts::nut04::{MintRequest, PostMintResponse};
|
|
|
+use cashu::nuts::nut05::{CheckFeesRequest, CheckFeesResponse};
|
|
|
+use cashu::nuts::nut06::{SplitRequest, SplitResponse};
|
|
|
+use cashu::nuts::nut07::{CheckSpendableRequest, CheckSpendableResponse};
|
|
|
+use cashu::nuts::nut08::{MeltRequest, MeltResponse};
|
|
|
+use cashu::nuts::nut09::MintInfo;
|
|
|
+use cashu::nuts::*;
|
|
|
+use cashu::utils;
|
|
|
+use cashu::Amount;
|
|
|
+
|
|
|
+#[cfg(target_arch = "wasm32")]
|
|
|
+use gloo::net::http::Request;
|
|
|
+
|
|
|
+pub use cashu::Bolt11Invoice;
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
pub enum Error {
|
|
@@ -28,7 +31,10 @@ pub enum Error {
|
|
|
UrlParseError(url::ParseError),
|
|
|
/// Serde Json error
|
|
|
SerdeJsonError(serde_json::Error),
|
|
|
- /// Gloo error
|
|
|
+ /// Min req error
|
|
|
+ #[cfg(not(target_arch = "wasm32"))]
|
|
|
+ MinReqError(minreq::Error),
|
|
|
+ #[cfg(target_arch = "wasm32")]
|
|
|
GlooError(String),
|
|
|
/// Custom Error
|
|
|
Custom(String),
|
|
@@ -46,6 +52,13 @@ impl From<serde_json::Error> for Error {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+#[cfg(not(target_arch = "wasm32"))]
|
|
|
+impl From<minreq::Error> for Error {
|
|
|
+ fn from(err: minreq::Error) -> Error {
|
|
|
+ Error::MinReqError(err)
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
impl std::error::Error for Error {}
|
|
|
|
|
|
impl fmt::Display for Error {
|
|
@@ -61,6 +74,9 @@ impl fmt::Display for Error {
|
|
|
}
|
|
|
Error::UrlParseError(err) => write!(f, "{}", err),
|
|
|
Error::SerdeJsonError(err) => write!(f, "{}", err),
|
|
|
+ #[cfg(not(target_arch = "wasm32"))]
|
|
|
+ Error::MinReqError(err) => write!(f, "{}", err),
|
|
|
+ #[cfg(target_arch = "wasm32")]
|
|
|
Error::GlooError(err) => write!(f, "{}", err),
|
|
|
Error::Custom(message) => write!(f, "{}", message),
|
|
|
}
|
|
@@ -106,6 +122,38 @@ impl Client {
|
|
|
}
|
|
|
|
|
|
/// Get Mint Keys [NUT-01]
|
|
|
+ #[cfg(not(target_arch = "wasm32"))]
|
|
|
+ pub async fn get_keys(&self) -> Result<Keys, Error> {
|
|
|
+ let url = self.mint_url.join("keys")?;
|
|
|
+ let keys = minreq::get(url).send()?.json::<Value>()?;
|
|
|
+
|
|
|
+ let keys: Keys = serde_json::from_str(&keys.to_string())?;
|
|
|
+ /*
|
|
|
+ let keys: BTreeMap<u64, String> = match serde_json::from_value(keys.clone()) {
|
|
|
+ Ok(keys) => keys,
|
|
|
+ Err(_err) => {
|
|
|
+ return Err(Error::CustomError(format!(
|
|
|
+ "url: {}, {}",
|
|
|
+ url,
|
|
|
+ serde_json::to_string(&keys)?
|
|
|
+ )))
|
|
|
+ }
|
|
|
+ };
|
|
|
+
|
|
|
+ let mint_keys: BTreeMap<u64, PublicKey> = keys
|
|
|
+ .into_iter()
|
|
|
+ .filter_map(|(k, v)| {
|
|
|
+ let key = hex::decode(v).ok()?;
|
|
|
+ let public_key = PublicKey::from_sec1_bytes(&key).ok()?;
|
|
|
+ Some((k, public_key))
|
|
|
+ })
|
|
|
+ .collect();
|
|
|
+ */
|
|
|
+ Ok(keys)
|
|
|
+ }
|
|
|
+
|
|
|
+ /// Get Mint Keys [NUT-01]
|
|
|
+ #[cfg(target_arch = "wasm32")]
|
|
|
pub async fn get_keys(&self) -> Result<Keys, Error> {
|
|
|
let url = self.mint_url.join("keys")?;
|
|
|
let keys = Request::get(url.as_str())
|
|
@@ -142,6 +190,22 @@ impl Client {
|
|
|
}
|
|
|
|
|
|
/// Get Keysets [NUT-02]
|
|
|
+ #[cfg(not(target_arch = "wasm32"))]
|
|
|
+ pub async fn get_keysets(&self) -> Result<nut02::Response, Error> {
|
|
|
+ let url = self.mint_url.join("keysets")?;
|
|
|
+ let res = minreq::get(url).send()?.json::<Value>()?;
|
|
|
+
|
|
|
+ let response: Result<nut02::Response, serde_json::Error> =
|
|
|
+ serde_json::from_value(res.clone());
|
|
|
+
|
|
|
+ match response {
|
|
|
+ Ok(res) => Ok(res),
|
|
|
+ Err(_) => Err(Error::from_json(&res.to_string())?),
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /// Get Keysets [NUT-02]
|
|
|
+ #[cfg(target_arch = "wasm32")]
|
|
|
pub async fn get_keysets(&self) -> Result<nut02::Response, Error> {
|
|
|
let url = self.mint_url.join("keysets")?;
|
|
|
let res = Request::get(url.as_str())
|
|
@@ -162,6 +226,25 @@ impl Client {
|
|
|
}
|
|
|
|
|
|
/// Request Mint [NUT-03]
|
|
|
+ #[cfg(not(target_arch = "wasm32"))]
|
|
|
+ pub async fn request_mint(&self, amount: Amount) -> Result<RequestMintResponse, Error> {
|
|
|
+ let mut url = self.mint_url.join("mint")?;
|
|
|
+ url.query_pairs_mut()
|
|
|
+ .append_pair("amount", &amount.to_sat().to_string());
|
|
|
+
|
|
|
+ let res = minreq::get(url).send()?.json::<Value>()?;
|
|
|
+
|
|
|
+ let response: Result<RequestMintResponse, serde_json::Error> =
|
|
|
+ serde_json::from_value(res.clone());
|
|
|
+
|
|
|
+ match response {
|
|
|
+ Ok(res) => Ok(res),
|
|
|
+ Err(_) => Err(Error::from_json(&res.to_string())?),
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /// Request Mint [NUT-03]
|
|
|
+ #[cfg(target_arch = "wasm32")]
|
|
|
pub async fn request_mint(&self, amount: Amount) -> Result<RequestMintResponse, Error> {
|
|
|
let mut url = self.mint_url.join("mint")?;
|
|
|
url.query_pairs_mut()
|
|
@@ -185,6 +268,35 @@ impl Client {
|
|
|
}
|
|
|
|
|
|
/// Mint Tokens [NUT-04]
|
|
|
+ #[cfg(not(target_arch = "wasm32"))]
|
|
|
+ pub async fn mint(
|
|
|
+ &self,
|
|
|
+ blinded_messages: BlindedMessages,
|
|
|
+ hash: &str,
|
|
|
+ ) -> Result<PostMintResponse, Error> {
|
|
|
+ let mut url = self.mint_url.join("mint")?;
|
|
|
+ url.query_pairs_mut().append_pair("hash", hash);
|
|
|
+
|
|
|
+ let request = MintRequest {
|
|
|
+ outputs: blinded_messages.blinded_messages,
|
|
|
+ };
|
|
|
+
|
|
|
+ let res = minreq::post(url)
|
|
|
+ .with_json(&request)?
|
|
|
+ .send()?
|
|
|
+ .json::<Value>()?;
|
|
|
+
|
|
|
+ let response: Result<PostMintResponse, serde_json::Error> =
|
|
|
+ serde_json::from_value(res.clone());
|
|
|
+
|
|
|
+ match response {
|
|
|
+ Ok(res) => Ok(res),
|
|
|
+ Err(_) => Err(Error::from_json(&res.to_string())?),
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /// Mint Tokens [NUT-04]
|
|
|
+ #[cfg(target_arch = "wasm32")]
|
|
|
pub async fn mint(
|
|
|
&self,
|
|
|
blinded_messages: BlindedMessages,
|
|
@@ -217,6 +329,28 @@ impl Client {
|
|
|
}
|
|
|
|
|
|
/// Check Max expected fee [NUT-05]
|
|
|
+ #[cfg(not(target_arch = "wasm32"))]
|
|
|
+ pub async fn check_fees(&self, invoice: Bolt11Invoice) -> Result<CheckFeesResponse, Error> {
|
|
|
+ let url = self.mint_url.join("checkfees")?;
|
|
|
+
|
|
|
+ let request = CheckFeesRequest { pr: invoice };
|
|
|
+
|
|
|
+ let res = minreq::post(url)
|
|
|
+ .with_json(&request)?
|
|
|
+ .send()?
|
|
|
+ .json::<Value>()?;
|
|
|
+
|
|
|
+ let response: Result<CheckFeesResponse, serde_json::Error> =
|
|
|
+ serde_json::from_value(res.clone());
|
|
|
+
|
|
|
+ match response {
|
|
|
+ Ok(res) => Ok(res),
|
|
|
+ Err(_) => Err(Error::from_json(&res.to_string())?),
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /// Check Max expected fee [NUT-05]
|
|
|
+ #[cfg(target_arch = "wasm32")]
|
|
|
pub async fn check_fees(&self, invoice: Bolt11Invoice) -> Result<CheckFeesResponse, Error> {
|
|
|
let url = self.mint_url.join("checkfees")?;
|
|
|
|
|
@@ -243,6 +377,38 @@ impl Client {
|
|
|
|
|
|
/// Melt [NUT-05]
|
|
|
/// [Nut-08] Lightning fee return if outputs defined
|
|
|
+ #[cfg(not(target_arch = "wasm32"))]
|
|
|
+ pub async fn melt(
|
|
|
+ &self,
|
|
|
+ proofs: Vec<Proof>,
|
|
|
+ invoice: Bolt11Invoice,
|
|
|
+ outputs: Option<Vec<BlindedMessage>>,
|
|
|
+ ) -> Result<MeltResponse, Error> {
|
|
|
+ let url = self.mint_url.join("melt")?;
|
|
|
+
|
|
|
+ let request = MeltRequest {
|
|
|
+ proofs,
|
|
|
+ pr: invoice,
|
|
|
+ outputs,
|
|
|
+ };
|
|
|
+
|
|
|
+ let value = minreq::post(url)
|
|
|
+ .with_json(&request)?
|
|
|
+ .send()?
|
|
|
+ .json::<Value>()?;
|
|
|
+
|
|
|
+ let response: Result<MeltResponse, serde_json::Error> =
|
|
|
+ serde_json::from_value(value.clone());
|
|
|
+
|
|
|
+ match response {
|
|
|
+ Ok(res) => Ok(res),
|
|
|
+ Err(_) => Err(Error::from_json(&value.to_string())?),
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /// Melt [NUT-05]
|
|
|
+ /// [Nut-08] Lightning fee return if outputs defined
|
|
|
+ #[cfg(target_arch = "wasm32")]
|
|
|
pub async fn melt(
|
|
|
&self,
|
|
|
proofs: Vec<Proof>,
|
|
@@ -277,6 +443,26 @@ impl Client {
|
|
|
}
|
|
|
|
|
|
/// Split Token [NUT-06]
|
|
|
+ #[cfg(not(target_arch = "wasm32"))]
|
|
|
+ pub async fn split(&self, split_request: SplitRequest) -> Result<SplitResponse, Error> {
|
|
|
+ let url = self.mint_url.join("split")?;
|
|
|
+
|
|
|
+ let res = minreq::post(url)
|
|
|
+ .with_json(&split_request)?
|
|
|
+ .send()?
|
|
|
+ .json::<Value>()?;
|
|
|
+
|
|
|
+ let response: Result<SplitResponse, serde_json::Error> =
|
|
|
+ serde_json::from_value(res.clone());
|
|
|
+
|
|
|
+ match response {
|
|
|
+ Ok(res) => Ok(res),
|
|
|
+ Err(_) => Err(Error::from_json(&res.to_string())?),
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /// Split Token [NUT-06]
|
|
|
+ #[cfg(target_arch = "wasm32")]
|
|
|
pub async fn split(&self, split_request: SplitRequest) -> Result<SplitResponse, Error> {
|
|
|
let url = self.mint_url.join("split")?;
|
|
|
|
|
@@ -300,6 +486,32 @@ impl Client {
|
|
|
}
|
|
|
|
|
|
/// Spendable check [NUT-07]
|
|
|
+ #[cfg(not(target_arch = "wasm32"))]
|
|
|
+ pub async fn check_spendable(
|
|
|
+ &self,
|
|
|
+ proofs: &Vec<nut00::mint::Proof>,
|
|
|
+ ) -> Result<CheckSpendableResponse, Error> {
|
|
|
+ let url = self.mint_url.join("check")?;
|
|
|
+ let request = CheckSpendableRequest {
|
|
|
+ proofs: proofs.to_owned(),
|
|
|
+ };
|
|
|
+
|
|
|
+ let res = minreq::post(url)
|
|
|
+ .with_json(&request)?
|
|
|
+ .send()?
|
|
|
+ .json::<Value>()?;
|
|
|
+
|
|
|
+ let response: Result<CheckSpendableResponse, serde_json::Error> =
|
|
|
+ serde_json::from_value(res.clone());
|
|
|
+
|
|
|
+ match response {
|
|
|
+ Ok(res) => Ok(res),
|
|
|
+ Err(_) => Err(Error::from_json(&res.to_string())?),
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /// Spendable check [NUT-07]
|
|
|
+ #[cfg(target_arch = "wasm32")]
|
|
|
pub async fn check_spendable(
|
|
|
&self,
|
|
|
proofs: &Vec<nut00::mint::Proof>,
|
|
@@ -329,6 +541,21 @@ impl Client {
|
|
|
}
|
|
|
|
|
|
/// Get Mint Info [NUT-09]
|
|
|
+ #[cfg(not(target_arch = "wasm32"))]
|
|
|
+ pub async fn get_info(&self) -> Result<MintInfo, Error> {
|
|
|
+ let url = self.mint_url.join("info")?;
|
|
|
+ let res = minreq::get(url).send()?.json::<Value>()?;
|
|
|
+
|
|
|
+ let response: Result<MintInfo, serde_json::Error> = serde_json::from_value(res.clone());
|
|
|
+
|
|
|
+ match response {
|
|
|
+ Ok(res) => Ok(res),
|
|
|
+ Err(_) => Err(Error::from_json(&res.to_string())?),
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /// Get Mint Info [NUT-09]
|
|
|
+ #[cfg(target_arch = "wasm32")]
|
|
|
pub async fn get_info(&self) -> Result<MintInfo, Error> {
|
|
|
let url = self.mint_url.join("info")?;
|
|
|
let res = Request::get(url.as_str())
|