mirror of
https://github.com/aljazceru/cdk.git
synced 2025-12-24 08:05:02 +01:00
remove unwraps
This commit is contained in:
@@ -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}");
|
||||
|
||||
@@ -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<Amount, Error> {
|
||||
pub async fn check_fee(&self, invoice: Invoice) -> Result<Amount, Error> {
|
||||
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<Melted, Error> {
|
||||
pub async fn melt(&self, invoice: Invoice, proofs: Proofs) -> Result<Melted, Error> {
|
||||
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>) -> String {
|
||||
Token::new(self.client.mint_url.clone(), proofs, memo).to_string()
|
||||
pub fn proofs_to_token(&self, proofs: Proofs, memo: Option<String>) -> Result<String, Error> {
|
||||
Token::new(self.client.mint_url.clone(), proofs, memo).convert_to_string()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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]
|
||||
|
||||
11
src/dhke.rs
11
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 {
|
||||
|
||||
@@ -5,3 +5,5 @@ pub mod error;
|
||||
pub mod serde_utils;
|
||||
pub mod types;
|
||||
pub mod utils;
|
||||
|
||||
pub use lightning_invoice::Invoice;
|
||||
|
||||
12
src/types.rs
12
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<String, Error> {
|
||||
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();
|
||||
|
||||
|
||||
Reference in New Issue
Block a user