Skip to content

Commit

Permalink
chore: rename split to swap
Browse files Browse the repository at this point in the history
  • Loading branch information
ngutech21 committed Dec 6, 2023
1 parent 12ff24d commit e3f40a2
Show file tree
Hide file tree
Showing 8 changed files with 43 additions and 47 deletions.
10 changes: 5 additions & 5 deletions moksha-core/src/primitives.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,17 +54,17 @@ pub struct PostMeltResponse {
}

#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct PostSplitRequest {
pub struct PostSwapRequest {
pub proofs: Proofs,
pub outputs: Vec<BlindedMessage>,
}

#[derive(Clone, Debug, Serialize, Deserialize, Default)]
pub struct PostSplitResponse {
pub struct PostSwapResponse {
pub promises: Vec<BlindedSignature>,
}

impl PostSplitResponse {
impl PostSwapResponse {
pub fn with_promises(promises: Vec<BlindedSignature>) -> Self {
Self { promises }
}
Expand Down Expand Up @@ -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(())
Expand Down
7 changes: 2 additions & 5 deletions moksha-mint/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
24 changes: 12 additions & 12 deletions moksha-mint/src/mint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use moksha_core::{
blind::{BlindedMessage, BlindedSignature, TotalAmount},
dhke::Dhke,
keyset::MintKeyset,
primitives::PostSplitResponse,
primitives::PostSwapResponse,
proof::Proofs,
};

Expand Down Expand Up @@ -138,29 +138,29 @@ 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<PostSplitResponse, MokshaMintError> {
) -> Result<PostSwapResponse, MokshaMintError> {
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();

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(
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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(())
Expand All @@ -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();
Expand All @@ -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(())
}
Expand Down Expand Up @@ -463,10 +463,10 @@ mod tests {
Ok(raw_token.trim().to_string().try_into()?)
}

fn create_request_from_fixture(fixture: String) -> Result<PostSplitRequest, anyhow::Error> {
fn create_request_from_fixture(fixture: String) -> Result<PostSwapRequest, anyhow::Error> {
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::<PostSplitRequest>(&raw_token)?)
Ok(serde_json::from_str::<PostSwapRequest>(&raw_token)?)
}

fn create_blinded_msgs_from_fixture(
Expand Down
14 changes: 7 additions & 7 deletions moksha-mint/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -79,7 +79,7 @@ fn app(mint: Mint, serve_wallet_path: Option<PathBuf>, prefix: Option<String>) -
.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()
Expand All @@ -91,7 +91,7 @@ fn app(mint: Mint, serve_wallet_path: Option<PathBuf>, prefix: Option<String>) -
.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());
Expand Down Expand Up @@ -145,12 +145,12 @@ async fn add_response_headers(
Ok(res)
}

async fn post_split(
async fn post_swap(
State(mint): State<Mint>,
Json(split_request): Json<PostSplitRequest>,
) -> Result<Json<PostSplitResponse>, MokshaMintError> {
Json(swap_request): Json<PostSwapRequest>,
) -> Result<Json<PostSwapResponse>, MokshaMintError> {
let response = mint
.split(&split_request.proofs, &split_request.outputs)
.swap(&swap_request.proofs, &swap_request.outputs)
.await?;

Ok(Json(response))
Expand Down
4 changes: 2 additions & 2 deletions moksha-wallet/src/client/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use moksha_core::{
blind::BlindedMessage,
keyset::Keysets,
primitives::{
CheckFeesResponse, PaymentRequest, PostMeltResponse, PostMintResponse, PostSplitResponse,
CheckFeesResponse, PaymentRequest, PostMeltResponse, PostMintResponse, PostSwapResponse,
},
proof::Proofs,
};
Expand All @@ -24,7 +24,7 @@ pub trait Client {
mint_url: &Url,
proofs: Proofs,
output: Vec<BlindedMessage>,
) -> Result<PostSplitResponse, MokshaWalletError>;
) -> Result<PostSwapResponse, MokshaWalletError>;

async fn post_mint_payment_request(
&self,
Expand Down
8 changes: 4 additions & 4 deletions moksha-wallet/src/client/reqwest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};
Expand Down Expand Up @@ -44,8 +44,8 @@ impl Client for HttpClient {
mint_url: &Url,
proofs: Proofs,
outputs: Vec<BlindedMessage>,
) -> Result<PostSplitResponse, MokshaWalletError> {
let body = serde_json::to_string(&PostSplitRequest { proofs, outputs })?;
) -> Result<PostSwapResponse, MokshaWalletError> {
let body = serde_json::to_string(&PostSwapRequest { proofs, outputs })?;

let resp = self
.request_client
Expand All @@ -55,7 +55,7 @@ impl Client for HttpClient {
.send()
.await?;

extract_response_data::<PostSplitResponse>(resp).await
extract_response_data::<PostSwapResponse>(resp).await
}

async fn post_melt_tokens(
Expand Down
13 changes: 6 additions & 7 deletions moksha-wallet/src/wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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()
Expand All @@ -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()
}
}
Expand All @@ -563,7 +563,7 @@ mod tests {
_mint_url: &Url,
_proofs: Proofs,
_output: Vec<BlindedMessage>,
) -> Result<PostSplitResponse, MokshaWalletError> {
) -> Result<PostSwapResponse, MokshaWalletError> {
Ok(self.split_response.clone())
}

Expand Down Expand Up @@ -642,8 +642,7 @@ mod tests {

#[tokio::test]
async fn test_split() -> anyhow::Result<()> {
let split_response =
read_fixture_as::<PostSplitResponse>("post_split_response_24_40.json")?;
let split_response = read_fixture_as::<PostSwapResponse>("post_split_response_24_40.json")?;
let client = MockClient::with_split_response(split_response);
let localstore = MockLocalStore::default();

Expand Down
10 changes: 5 additions & 5 deletions moksha-wallet/tests/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<u64, PublicKey>,
Expand All @@ -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<u64, PublicKey>,
keysets: Keysets,
) -> Self {
Expand All @@ -49,7 +49,7 @@ impl Client for MockClient {
_mint_url: &Url,
_proofs: Proofs,
_output: Vec<BlindedMessage>,
) -> Result<PostSplitResponse, MokshaWalletError> {
) -> Result<PostSwapResponse, MokshaWalletError> {
Ok(self.split_response.clone())
}

Expand Down Expand Up @@ -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::<PostMeltResponse>("post_melt_response_not_paid.json")?;
let split_response = read_fixture_as::<PostSplitResponse>("post_split_response_24_40.json")?;
let split_response = read_fixture_as::<PostSwapResponse>("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]);

Expand Down

0 comments on commit e3f40a2

Please sign in to comment.