diff --git a/integration_test/src/main.rs b/integration_test/src/main.rs index b9c50e68..7253aa7d 100644 --- a/integration_test/src/main.rs +++ b/integration_test/src/main.rs @@ -7,8 +7,7 @@ use std::time::Duration; use bitcoin::Amount; use cashu_crab::cashu_wallet::CashuWallet; use cashu_crab::client::Client; -use cashu_crab::types::{MintKeys, MintProofs, Proofs, Token}; -use lightning_invoice::Invoice; +use cashu_crab::types::{Invoice, MintKeys, MintProofs, Proofs, Token}; const MINTURL: &str = "https:dev-cashu.thesimplekid.com"; @@ -34,7 +33,7 @@ async fn main() { proofs, Some("Hello World".to_string()), ); - let new_token = test_receive(&wallet, &token.to_string()).await; + let new_token = test_receive(&wallet, &token.convert_to_string().unwrap()).await; let _proofs = Token::from_str(&new_token).unwrap().token[0].clone().proofs; let spendable = test_check_spendable(&client, &new_token).await; @@ -108,9 +107,9 @@ async fn test_receive(wallet: &CashuWallet, token: &str) -> String { memo: Some("Hello world".to_string()), }; - let s = token.to_string(); + let s = token.convert_to_string(); // println!("{s}"); - s + s.unwrap() } async fn test_check_spendable(client: &Client, token: &str) -> Proofs { @@ -138,10 +137,13 @@ async fn test_send(wallet: &CashuWallet, proofs: Proofs) -> Proofs { println!("{:?}", send); - let keep_token = wallet.proofs_to_token(send.change_proofs, Some("Keeping these".to_string())); + let keep_token = wallet + .proofs_to_token(send.change_proofs, Some("Keeping these".to_string())) + .unwrap(); - let send_token = - wallet.proofs_to_token(send.send_proofs.clone(), Some("Sending these".to_string())); + let send_token = wallet + .proofs_to_token(send.send_proofs.clone(), Some("Sending these".to_string())) + .unwrap(); println!("Keep Token: {keep_token}"); println!("Send Token: {send_token}"); diff --git a/src/cashu_wallet.rs b/src/cashu_wallet.rs index a003f5ee..471a7b47 100644 --- a/src/cashu_wallet.rs +++ b/src/cashu_wallet.rs @@ -3,6 +3,7 @@ use std::str::FromStr; use bitcoin::Amount; +pub use crate::Invoice; use crate::{ client::Client, dhke::construct_proofs, @@ -69,7 +70,7 @@ impl CashuWallet { } /// Check fee - pub async fn check_fee(&self, invoice: lightning_invoice::Invoice) -> Result { + pub async fn check_fee(&self, invoice: Invoice) -> Result { Ok(self.client.check_fees(invoice).await?.fee) } @@ -88,7 +89,7 @@ impl CashuWallet { } else { // println!("dd"); // self.mint_keys.clone() - Client::new(token.mint.as_str())?.get_keys().await.unwrap() + Client::new(token.mint.as_str())?.get_keys().await? }; // Sum amount of all proofs @@ -99,11 +100,7 @@ impl CashuWallet { let split_payload = self.create_split(Amount::ZERO, amount, token.proofs)?; - let split_response = self - .client - .split(split_payload.split_payload) - .await - .unwrap(); + let split_response = self.client.split(split_payload.split_payload).await?; // Proof to keep let keep_proofs = construct_proofs( @@ -214,11 +211,7 @@ impl CashuWallet { }) } - pub async fn melt( - &self, - invoice: lightning_invoice::Invoice, - proofs: Proofs, - ) -> Result { + pub async fn melt(&self, invoice: Invoice, proofs: Proofs) -> Result { let change = BlindedMessages::blank()?; let melt_response = self .client @@ -242,7 +235,7 @@ impl CashuWallet { }) } - pub fn proofs_to_token(&self, proofs: Proofs, memo: Option) -> String { - Token::new(self.client.mint_url.clone(), proofs, memo).to_string() + pub fn proofs_to_token(&self, proofs: Proofs, memo: Option) -> Result { + Token::new(self.client.mint_url.clone(), proofs, memo).convert_to_string() } } diff --git a/src/client.rs b/src/client.rs index 6b23e688..1bc716f6 100644 --- a/src/client.rs +++ b/src/client.rs @@ -4,10 +4,10 @@ use std::collections::HashMap; use bitcoin::Amount; use k256::PublicKey; -use lightning_invoice::Invoice; use serde_json::Value; use url::Url; +pub use crate::Invoice; use crate::{ error::Error, types::{ @@ -30,7 +30,7 @@ impl Client { if !mint_url.ends_with('/') { mint_url.push('/'); } - let mint_url = Url::parse(&mint_url).unwrap(); + let mint_url = Url::parse(&mint_url)?; Ok(Self { mint_url }) } @@ -159,7 +159,7 @@ impl Client { // specifically token already spent println!("Split Res: {:?}", res); - Ok(serde_json::from_value(res).unwrap()) + Ok(serde_json::from_value(res)?) } /// Spendable check [NUT-07] diff --git a/src/dhke.rs b/src/dhke.rs index 9fa53d8e..d12309a1 100644 --- a/src/dhke.rs +++ b/src/dhke.rs @@ -41,7 +41,7 @@ pub fn blind_message( let b = ProjectivePoint::from(y) + ProjectivePoint::from(&r.public_key()); - Ok((PublicKey::try_from(b).unwrap(), r)) + Ok((PublicKey::try_from(b)?, r)) } /// Unblind Message (Alice Step 3) @@ -59,7 +59,7 @@ pub fn unblind_message( .as_affine() .mul(Scalar::from(r.as_scalar_primitive())); - Ok(PublicKey::try_from(c).unwrap()) + Ok(PublicKey::try_from(c)?) } /// Construct Proof @@ -72,7 +72,12 @@ pub fn construct_proofs( let mut proofs = vec![]; for (i, promise) in promises.into_iter().enumerate() { let blinded_c = promise.c; - let a: PublicKey = keys.0.get(&promise.amount.to_sat()).unwrap().to_owned(); + let a: PublicKey = keys + .0 + .get(&promise.amount.to_sat()) + .ok_or(Error::CustomError("Could not get proofs".to_string()))? + .to_owned(); + let unblinded_signature = unblind_message(blinded_c, rs[i].clone(), a)?; let proof = Proof { diff --git a/src/lib.rs b/src/lib.rs index 8789e832..013bbac1 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -5,3 +5,5 @@ pub mod error; pub mod serde_utils; pub mod types; pub mod utils; + +pub use lightning_invoice::Invoice; diff --git a/src/types.rs b/src/types.rs index 494d3ea0..9dc1d63b 100644 --- a/src/types.rs +++ b/src/types.rs @@ -5,11 +5,11 @@ use std::{collections::HashMap, str::FromStr}; use base64::{engine::general_purpose, Engine as _}; use bitcoin::Amount; use k256::{PublicKey, SecretKey}; -use lightning_invoice::Invoice; use serde::{Deserialize, Deserializer, Serialize, Serializer}; use url::Url; use crate::utils::generate_secret; +pub use crate::Invoice; use crate::{ dhke::blind_message, error::Error, serde_utils, serde_utils::serde_url, utils::split_amount, }; @@ -368,11 +368,11 @@ impl FromStr for Token { } } -impl ToString for Token { - fn to_string(&self) -> String { - let json_string = serde_json::to_string(self).unwrap(); +impl Token { + pub fn convert_to_string(&self) -> Result { + let json_string = serde_json::to_string(self)?; let encoded = general_purpose::STANDARD.encode(json_string); - format!("cashuA{}", encoded) + Ok(format!("cashuA{}", encoded)) } } @@ -399,7 +399,7 @@ mod tests { ); assert_eq!(token.token[0].proofs[0].clone().id.unwrap(), "DSAl9nvvyfva"); - let encoded = &token.to_string(); + let encoded = &token.convert_to_string().unwrap(); let token_data = Token::from_str(encoded).unwrap();