Skip to content

Commit

Permalink
chore: remove amount from /split api
Browse files Browse the repository at this point in the history
  • Loading branch information
ngutech21 committed Oct 25, 2023
1 parent 21ed3bd commit b9ae875
Show file tree
Hide file tree
Showing 12 changed files with 68 additions and 232 deletions.
7 changes: 1 addition & 6 deletions flutter/native/src/wasm_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,10 @@ impl Client for WasmClient {
async fn post_split_tokens(
&self,
mint_url: &Url,
amount: u64,
proofs: Proofs,
outputs: Vec<BlindedMessage>,
) -> Result<PostSplitResponse, MokshaWalletError> {
let body = &PostSplitRequest {
amount: Some(amount),
proofs,
outputs,
};
let body = &PostSplitRequest { proofs, outputs };

let resp = Request::post(mint_url.join("split")?.as_str())
.header("content-type", "application/json")
Expand Down
28 changes: 10 additions & 18 deletions moksha-core/src/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,13 @@ impl TokenV3 {
}
}

pub fn empty() -> Self {
Self {
tokens: vec![],
memo: None,
}
}

pub fn total_amount(&self) -> u64 {
self.tokens
.iter()
Expand Down Expand Up @@ -357,31 +364,16 @@ pub struct PostMeltResponse {
pub struct PostSplitRequest {
pub proofs: Proofs,
pub outputs: Vec<BlindedMessage>,
pub amount: Option<u64>,
}

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

impl PostSplitResponse {
pub fn with_promises(promises: Vec<BlindedSignature>) -> Self {
Self {
promises: Some(promises),
..Default::default()
}
}

pub fn with_fst_and_snd(fst: Vec<BlindedSignature>, snd: Vec<BlindedSignature>) -> Self {
Self {
fst: Some(fst),
snd: Some(snd),
..Default::default()
}
Self { promises }
}
}

Expand Down Expand Up @@ -460,7 +452,7 @@ mod tests {
fn test_serialize_empty_split_response() -> anyhow::Result<()> {
let response = PostSplitResponse::default();
let serialized = serde_json::to_string(&response)?;
assert_eq!(serialized, "{}");
assert_eq!(serialized, "{\"promises\":[]}");
Ok(())
}

Expand Down
3 changes: 1 addition & 2 deletions moksha-mint/src/fixtures/post_split_request_64_20.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,5 @@
"amount": 16,
"B_": "02364fe16667a049eb6dbdf4a8db23c250822fb8bc9806f4b82cc100ab00872959"
}
],
"amount": 20
]
}
32 changes: 0 additions & 32 deletions moksha-mint/src/fixtures/post_split_request_64_20_no_amount.json

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,5 @@
"amount": 16,
"B_": "03e997c205b170ed2f88c26f61559733144a55da6e66334b0f4a030b708a49e5ab"
}
],
"amount": 20
]
}
120 changes: 25 additions & 95 deletions moksha-mint/src/mint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,7 @@ use std::{collections::HashSet, sync::Arc};
use moksha_core::{
crypto,
dhke::Dhke,
model::{
split_amount, BlindedMessage, BlindedSignature, MintKeyset, PostSplitResponse, Proofs,
TotalAmount,
},
model::{BlindedMessage, BlindedSignature, MintKeyset, PostSplitResponse, Proofs, TotalAmount},
};

use crate::{
Expand Down Expand Up @@ -138,7 +135,6 @@ impl Mint {

pub async fn split(
&self,
amount: Option<u64>,
proofs: &Proofs,
blinded_messages: &[BlindedMessage],
) -> Result<PostSplitResponse, MokshaMintError> {
Expand All @@ -150,42 +146,16 @@ impl Mint {

let sum_proofs = proofs.total_amount();

match amount {
Some(amount) => {
if amount > sum_proofs {
return Err(MokshaMintError::SplitAmountTooHigh);
}
let sum_first = split_amount(sum_proofs - amount).len();
let first_slice = blinded_messages[0..sum_first].to_vec();
let first_sigs = self.create_blinded_signatures(&first_slice)?;
let second_slice = blinded_messages[sum_first..blinded_messages.len()].to_vec();
let second_sigs = self.create_blinded_signatures(&second_slice)?;

let amount_first = first_sigs.total_amount();
let amount_second = second_sigs.total_amount();

if sum_proofs != (amount_first + amount_second) {
return Err(MokshaMintError::SplitAmountMismatch(format!(
"Split amount mismatch: {sum_proofs} != {amount_first} + {amount_second}"
)));
}

self.db.add_used_proofs(proofs)?;
Ok(PostSplitResponse::with_fst_and_snd(first_sigs, second_sigs))
}
None => {
let promises = self.create_blinded_signatures(blinded_messages)?;
let amount_promises = promises.total_amount();
if sum_proofs != amount_promises {
return Err(MokshaMintError::SplitAmountMismatch(format!(
"Split amount mismatch: {sum_proofs} != {amount_promises}"
)));
}

self.db.add_used_proofs(proofs)?;
Ok(PostSplitResponse::with_promises(promises))
}
let promises = self.create_blinded_signatures(blinded_messages)?;
let amount_promises = promises.total_amount();
if sum_proofs != amount_promises {
return Err(MokshaMintError::SplitAmountMismatch(format!(
"Split amount mismatch: {sum_proofs} != {amount_promises}"
)));
}

self.db.add_used_proofs(proofs)?;
Ok(PostSplitResponse::with_promises(promises))
}

pub async fn melt(
Expand Down Expand Up @@ -405,11 +375,9 @@ mod tests {
let mint = create_mint_from_mocks(Some(create_mock_db_get_used_proofs()), None);

let proofs = Proofs::empty();
let result = mint.split(Some(0), &proofs, &blinded_messages).await?;
let result = mint.split(&proofs, &blinded_messages).await?;

assert!(result.promises.is_none());
assert_eq!(result.fst.unwrap().len(), 0);
assert_eq!(result.snd.unwrap().len(), 0);
assert!(result.promises.is_empty());
Ok(())
}

Expand All @@ -418,56 +386,20 @@ 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(Some(20), &request.proofs, &request.outputs)
.await?;

assert!(result.promises.is_none());
assert_eq!(result.fst.unwrap().total_amount(), 44);
assert_eq!(result.snd.unwrap().total_amount(), 20);
Ok(())
}

#[tokio::test]
async fn test_split_64_in_20_no_amount() -> anyhow::Result<()> {
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_no_amount.json".to_string())?;
let result = mint.split(&request.proofs, &request.outputs).await?;
assert_eq!(result.promises.total_amount(), 64);

let result = mint.split(None, &request.proofs, &request.outputs).await?;
let prv_lst = result
.promises
.get((result.promises.len() - 2) as usize)
.unwrap();
let lst = result
.promises
.get((result.promises.len() - 1) as usize)
.unwrap();

assert_eq!(result.promises.unwrap().total_amount(), 64);
assert!(result.fst.is_none());
assert!(result.snd.is_none());
Ok(())
}

#[tokio::test]
async fn test_split_64_in_64() -> anyhow::Result<()> {
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(Some(64), &request.proofs, &request.outputs)
.await?;

assert!(result.promises.is_none());
assert_eq!(result.fst.unwrap().total_amount(), 0);
assert_eq!(result.snd.unwrap().total_amount(), 64);
Ok(())
}

#[tokio::test]
async fn test_split_amount_is_too_high() -> anyhow::Result<()> {
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(Some(65), &request.proofs, &request.outputs)
.await;
assert!(result.is_err());
let _err = result.unwrap_err();
assert!(matches!(MokshaMintError::SplitAmountTooHigh, _err));
assert_eq!(prv_lst.amount, 4);
assert_eq!(lst.amount, 16);
Ok(())
}

Expand All @@ -477,9 +409,7 @@ mod tests {
let request =
create_request_from_fixture("post_split_request_duplicate_key.json".to_string())?;

let result = mint
.split(Some(20), &request.proofs, &request.outputs)
.await;
let result = mint.split(&request.proofs, &request.outputs).await;
assert!(result.is_err());
Ok(())
}
Expand Down
6 changes: 1 addition & 5 deletions moksha-mint/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,11 +104,7 @@ async fn post_split(
Json(split_request): Json<PostSplitRequest>,
) -> Result<Json<PostSplitResponse>, MokshaMintError> {
let response = mint
.split(
split_request.amount,
&split_request.proofs,
&split_request.outputs,
)
.split(&split_request.proofs, &split_request.outputs)
.await?;

Ok(Json(response))
Expand Down
1 change: 0 additions & 1 deletion moksha-wallet/src/client/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ pub trait Client {
async fn post_split_tokens(
&self,
mint_url: &Url,
amount: u64,
proofs: Proofs,
output: Vec<BlindedMessage>,
) -> Result<PostSplitResponse, MokshaWalletError>;
Expand Down
7 changes: 1 addition & 6 deletions moksha-wallet/src/client/reqwest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,10 @@ impl Client for HttpClient {
async fn post_split_tokens(
&self,
mint_url: &Url,
amount: u64,
proofs: Proofs,
outputs: Vec<BlindedMessage>,
) -> Result<PostSplitResponse, MokshaWalletError> {
let body = serde_json::to_string(&PostSplitRequest {
amount: Some(amount),
proofs,
outputs,
})?;
let body = serde_json::to_string(&PostSplitRequest { proofs, outputs })?;

let resp = self
.request_client
Expand Down
6 changes: 2 additions & 4 deletions moksha-wallet/src/fixtures/post_split_response_24_40.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"fst": [
"promises": [
{
"amount": 4,
"C_": "03b2e736e1280f1e64eedf3fc53eeb5fc74e6f1d1664e3b2b7b8e2934afd908673",
Expand All @@ -14,9 +14,7 @@
"amount": 4,
"C_": "036614b4844efe234e0ec2293938a84a42b6e803126b365074943dd338f813421f",
"id": "mR9PJ3MzjL1y"
}
],
"snd": [
},
{
"amount": 8,
"C_": "03859164602a27319bcd5c377bec90eda6f9d5d9e9c7987fce5bff4b69151a122f",
Expand Down
Loading

0 comments on commit b9ae875

Please sign in to comment.