diff --git a/moksha-core/src/primitives.rs b/moksha-core/src/primitives.rs index eebfecd4..6a546a61 100644 --- a/moksha-core/src/primitives.rs +++ b/moksha-core/src/primitives.rs @@ -54,17 +54,17 @@ pub struct PostMeltResponse { } #[derive(Clone, Debug, Serialize, Deserialize)] -pub struct PostSplitRequest { +pub struct PostSwapRequest { pub proofs: Proofs, pub outputs: Vec, } #[derive(Clone, Debug, Serialize, Deserialize, Default)] -pub struct PostSplitResponse { +pub struct PostSwapResponse { pub promises: Vec, } -impl PostSplitResponse { +impl PostSwapResponse { pub fn with_promises(promises: Vec) -> Self { Self { promises } } @@ -181,12 +181,12 @@ pub struct PostMeltBolt11Response { mod tests { use crate::{ dhke::public_key_from_hex, - primitives::{KeyResponse, MintInfoResponse, Parameter, PostSplitResponse}, + primitives::{KeyResponse, MintInfoResponse, Parameter, PostSwapResponse}, }; #[test] fn test_serialize_empty_split_response() -> anyhow::Result<()> { - let response = PostSplitResponse::default(); + let response = PostSwapResponse::default(); let serialized = serde_json::to_string(&response)?; assert_eq!(serialized, "{\"promises\":[]}"); Ok(()) diff --git a/moksha-mint/src/error.rs b/moksha-mint/src/error.rs index f56fd8d3..4abd48d8 100644 --- a/moksha-mint/src/error.rs +++ b/moksha-mint/src/error.rs @@ -47,13 +47,10 @@ pub enum MokshaMintError { ProofAlreadyUsed(String), #[error("{0}")] - SplitAmountMismatch(String), - - #[error("split amount is higher than the total sum.")] - SplitAmountTooHigh, + SwapAmountMismatch(String), #[error("duplicate promises.")] - SplitHasDuplicatePromises, + SwapHasDuplicatePromises, #[error("Invalid amount")] InvalidAmount, diff --git a/moksha-mint/src/mint.rs b/moksha-mint/src/mint.rs index aedcefc7..585b6e00 100644 --- a/moksha-mint/src/mint.rs +++ b/moksha-mint/src/mint.rs @@ -4,7 +4,7 @@ use moksha_core::{ blind::{BlindedMessage, BlindedSignature, TotalAmount}, dhke::Dhke, keyset::MintKeyset, - primitives::PostSplitResponse, + primitives::PostSwapResponse, proof::Proofs, }; @@ -138,15 +138,15 @@ impl Mint { !outputs.iter().all(move |x| uniq.insert(x.b_)) } - pub async fn split( + pub async fn swap( &self, proofs: &Proofs, blinded_messages: &[BlindedMessage], - ) -> Result { + ) -> Result { self.check_used_proofs(proofs)?; if Self::has_duplicate_pubkeys(blinded_messages) { - return Err(MokshaMintError::SplitHasDuplicatePromises); + return Err(MokshaMintError::SwapHasDuplicatePromises); } let sum_proofs = proofs.total_amount(); @@ -154,13 +154,13 @@ impl Mint { let promises = self.create_blinded_signatures(blinded_messages)?; let amount_promises = promises.total_amount(); if sum_proofs != amount_promises { - return Err(MokshaMintError::SplitAmountMismatch(format!( + return Err(MokshaMintError::SwapAmountMismatch(format!( "Split amount mismatch: {sum_proofs} != {amount_promises}" ))); } self.db.add_used_proofs(proofs)?; - Ok(PostSplitResponse::with_promises(promises)) + Ok(PostSwapResponse::with_promises(promises)) } pub async fn melt( @@ -314,7 +314,7 @@ mod tests { use crate::{database::MockDatabase, error::MokshaMintError}; use moksha_core::blind::{BlindedMessage, TotalAmount}; use moksha_core::dhke; - use moksha_core::primitives::PostSplitRequest; + use moksha_core::primitives::PostSwapRequest; use moksha_core::proof::Proofs; use moksha_core::token::TokenV3; use std::str::FromStr; @@ -382,7 +382,7 @@ mod tests { let mint = create_mint_from_mocks(Some(create_mock_db_get_used_proofs()), None); let proofs = Proofs::empty(); - let result = mint.split(&proofs, &blinded_messages).await?; + let result = mint.swap(&proofs, &blinded_messages).await?; assert!(result.promises.is_empty()); Ok(()) @@ -393,7 +393,7 @@ mod tests { let mint = create_mint_from_mocks(Some(create_mock_db_get_used_proofs()), None); let request = create_request_from_fixture("post_split_request_64_20.json".to_string())?; - let result = mint.split(&request.proofs, &request.outputs).await?; + let result = mint.swap(&request.proofs, &request.outputs).await?; assert_eq!(result.promises.total_amount(), 64); let prv_lst = result.promises.get(result.promises.len() - 2).unwrap(); @@ -410,7 +410,7 @@ mod tests { let request = create_request_from_fixture("post_split_request_duplicate_key.json".to_string())?; - let result = mint.split(&request.proofs, &request.outputs).await; + let result = mint.swap(&request.proofs, &request.outputs).await; assert!(result.is_err()); Ok(()) } @@ -463,10 +463,10 @@ mod tests { Ok(raw_token.trim().to_string().try_into()?) } - fn create_request_from_fixture(fixture: String) -> Result { + fn create_request_from_fixture(fixture: String) -> Result { let base_dir = std::env::var("CARGO_MANIFEST_DIR")?; let raw_token = std::fs::read_to_string(format!("{base_dir}/src/fixtures/{fixture}"))?; - Ok(serde_json::from_str::(&raw_token)?) + Ok(serde_json::from_str::(&raw_token)?) } fn create_blinded_msgs_from_fixture( diff --git a/moksha-mint/src/server.rs b/moksha-mint/src/server.rs index e3bd82b7..cea8f090 100644 --- a/moksha-mint/src/server.rs +++ b/moksha-mint/src/server.rs @@ -20,7 +20,7 @@ use moksha_core::primitives::{ PaymentRequest, PostMeltBolt11Request, PostMeltBolt11Response, PostMeltQuoteBolt11Request, PostMeltQuoteBolt11Response, PostMeltRequest, PostMeltResponse, PostMintBolt11Request, PostMintBolt11Response, PostMintQuoteBolt11Request, PostMintQuoteBolt11Response, - PostMintRequest, PostMintResponse, PostSplitRequest, PostSplitResponse, + PostMintRequest, PostMintResponse, PostSwapRequest, PostSwapResponse, }; use secp256k1::PublicKey; @@ -79,7 +79,7 @@ fn app(mint: Mint, serve_wallet_path: Option, prefix: Option) - .route("/mint", get(get_legacy_mint).post(post_legacy_mint)) .route("/checkfees", post(post_legacy_check_fees)) .route("/melt", post(post_legacy_melt)) - .route("/split", post(post_split)) + .route("/split", post(post_swap)) .route("/info", get(get_legacy_info)); let routes = Router::new() @@ -91,7 +91,7 @@ fn app(mint: Mint, serve_wallet_path: Option, prefix: Option) - .route("/v1/melt/quote/bolt11", post(post_melt_quote_bolt11)) .route("/v1/melt/quote/bolt11/:quote", get(get_melt_quote_bolt11)) .route("/v1/melt/bolt11", post(post_melt_bolt11)) - .route("/v1/swap", post(post_split)) + .route("/v1/swap", post(post_swap)) .route("/v1/info", get(get_legacy_info)); let prefix = prefix.unwrap_or_else(|| "".to_owned()); @@ -145,12 +145,12 @@ async fn add_response_headers( Ok(res) } -async fn post_split( +async fn post_swap( State(mint): State, - Json(split_request): Json, -) -> Result, MokshaMintError> { + Json(swap_request): Json, +) -> Result, MokshaMintError> { let response = mint - .split(&split_request.proofs, &split_request.outputs) + .swap(&swap_request.proofs, &swap_request.outputs) .await?; Ok(Json(response)) diff --git a/moksha-wallet/src/client/mod.rs b/moksha-wallet/src/client/mod.rs index be114e4b..0b1ffd18 100644 --- a/moksha-wallet/src/client/mod.rs +++ b/moksha-wallet/src/client/mod.rs @@ -5,7 +5,7 @@ use moksha_core::{ blind::BlindedMessage, keyset::Keysets, primitives::{ - CheckFeesResponse, PaymentRequest, PostMeltResponse, PostMintResponse, PostSplitResponse, + CheckFeesResponse, PaymentRequest, PostMeltResponse, PostMintResponse, PostSwapResponse, }, proof::Proofs, }; @@ -24,7 +24,7 @@ pub trait Client { mint_url: &Url, proofs: Proofs, output: Vec, - ) -> Result; + ) -> Result; async fn post_mint_payment_request( &self, diff --git a/moksha-wallet/src/client/reqwest.rs b/moksha-wallet/src/client/reqwest.rs index dc1b6077..7ad357cb 100644 --- a/moksha-wallet/src/client/reqwest.rs +++ b/moksha-wallet/src/client/reqwest.rs @@ -6,7 +6,7 @@ use moksha_core::{ keyset::Keysets, primitives::{ CashuErrorResponse, CheckFeesRequest, CheckFeesResponse, PaymentRequest, PostMeltRequest, - PostMeltResponse, PostMintRequest, PostMintResponse, PostSplitRequest, PostSplitResponse, + PostMeltResponse, PostMintRequest, PostMintResponse, PostSwapRequest, PostSwapResponse, }, proof::Proofs, }; @@ -44,8 +44,8 @@ impl Client for HttpClient { mint_url: &Url, proofs: Proofs, outputs: Vec, - ) -> Result { - let body = serde_json::to_string(&PostSplitRequest { proofs, outputs })?; + ) -> Result { + let body = serde_json::to_string(&PostSwapRequest { proofs, outputs })?; let resp = self .request_client @@ -55,7 +55,7 @@ impl Client for HttpClient { .send() .await?; - extract_response_data::(resp).await + extract_response_data::(resp).await } async fn post_melt_tokens( diff --git a/moksha-wallet/src/wallet.rs b/moksha-wallet/src/wallet.rs index 355d010c..b162a9f7 100644 --- a/moksha-wallet/src/wallet.rs +++ b/moksha-wallet/src/wallet.rs @@ -451,7 +451,7 @@ mod tests { use moksha_core::fixture::{read_fixture, read_fixture_as}; use moksha_core::keyset::{Keysets, MintKeyset}; use moksha_core::primitives::{ - CheckFeesResponse, PaymentRequest, PostMeltResponse, PostMintResponse, PostSplitResponse, + CheckFeesResponse, PaymentRequest, PostMeltResponse, PostMintResponse, PostSwapResponse, }; use moksha_core::proof::Proofs; use moksha_core::token::{Token, TokenV3}; @@ -526,14 +526,14 @@ mod tests { #[derive(Clone, Default)] struct MockClient { - split_response: PostSplitResponse, + split_response: PostSwapResponse, post_mint_response: PostMintResponse, post_melt_response: PostMeltResponse, keyset: MockKeys, } impl MockClient { - fn with_split_response(split_response: PostSplitResponse) -> Self { + fn with_split_response(split_response: PostSwapResponse) -> Self { Self { split_response, ..Default::default() @@ -550,7 +550,7 @@ mod tests { fn with_melt_response(post_melt_response: PostMeltResponse) -> Self { Self { post_melt_response, - split_response: PostSplitResponse::with_promises(vec![]), + split_response: PostSwapResponse::with_promises(vec![]), ..Default::default() } } @@ -563,7 +563,7 @@ mod tests { _mint_url: &Url, _proofs: Proofs, _output: Vec, - ) -> Result { + ) -> Result { Ok(self.split_response.clone()) } @@ -642,8 +642,7 @@ mod tests { #[tokio::test] async fn test_split() -> anyhow::Result<()> { - let split_response = - read_fixture_as::("post_split_response_24_40.json")?; + let split_response = read_fixture_as::("post_split_response_24_40.json")?; let client = MockClient::with_split_response(split_response); let localstore = MockLocalStore::default(); diff --git a/moksha-wallet/tests/tests.rs b/moksha-wallet/tests/tests.rs index 7a585c0f..0e1123d8 100644 --- a/moksha-wallet/tests/tests.rs +++ b/moksha-wallet/tests/tests.rs @@ -5,7 +5,7 @@ use moksha_core::blind::BlindedMessage; use moksha_core::fixture::{read_fixture, read_fixture_as}; use moksha_core::keyset::{Keysets, MintKeyset}; use moksha_core::primitives::{ - CheckFeesResponse, PaymentRequest, PostMeltResponse, PostMintResponse, PostSplitResponse, + CheckFeesResponse, PaymentRequest, PostMeltResponse, PostMintResponse, PostSwapResponse, }; use moksha_core::proof::Proofs; use moksha_core::token::TokenV3; @@ -18,7 +18,7 @@ use secp256k1::PublicKey; #[derive(Clone, Default)] struct MockClient { - split_response: PostSplitResponse, + split_response: PostSwapResponse, post_mint_response: PostMintResponse, post_melt_response: PostMeltResponse, mint_keys: HashMap, @@ -28,7 +28,7 @@ struct MockClient { impl MockClient { fn with( post_melt_response: PostMeltResponse, - post_split_response: PostSplitResponse, + post_split_response: PostSwapResponse, mint_keys: HashMap, keysets: Keysets, ) -> Self { @@ -49,7 +49,7 @@ impl Client for MockClient { _mint_url: &Url, _proofs: Proofs, _output: Vec, - ) -> Result { + ) -> Result { Ok(self.split_response.clone()) } @@ -121,7 +121,7 @@ async fn test_pay_invoice_can_not_melt() -> anyhow::Result<()> { assert_eq!(64, localstore.get_proofs().await?.total_amount()); let melt_response = read_fixture_as::("post_melt_response_not_paid.json")?; - let split_response = read_fixture_as::("post_split_response_24_40.json")?; + let split_response = read_fixture_as::("post_split_response_24_40.json")?; let mint_keyset = MintKeyset::legacy_new("mysecret".to_string(), "".to_string()); let keysets = Keysets::new(vec![mint_keyset.keyset_id]);