diff --git a/Cargo.lock b/Cargo.lock index c7bd915b..8fe068c3 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1988,6 +1988,7 @@ dependencies = [ "rand", "retry", "serde_json", + "signup-sequencer", "tokio", "tracing", "tracing-futures", diff --git a/e2e_tests/scenarios/Cargo.toml b/e2e_tests/scenarios/Cargo.toml index ab3198b8..ec9a6a00 100644 --- a/e2e_tests/scenarios/Cargo.toml +++ b/e2e_tests/scenarios/Cargo.toml @@ -2,10 +2,18 @@ name = "e2e-tests" version = "0.1.0" edition = "2021" +description = "A tool that processes WorldID signups on-chain." +keywords = ["worldcoin", "protocol", "signup"] +categories = ["cryptography::cryptocurrencies"] +repository = "https://github.com/worldcoin/signup-sequencer" +readme = "./../../Readme.md" +license-file = "./../../LICENSE.md" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] +signup-sequencer = { path = "./../.." } + anyhow = "1.0" ethers = { version = "2.0.10" } hex = "0.4.3" diff --git a/e2e_tests/scenarios/tests/common/api.rs b/e2e_tests/scenarios/tests/common/api.rs index 14510873..eee76f2d 100644 --- a/e2e_tests/scenarios/tests/common/api.rs +++ b/e2e_tests/scenarios/tests/common/api.rs @@ -4,6 +4,10 @@ use anyhow::Error; use hyper::client::HttpConnector; use hyper::{Body, Client, Request}; use serde_json::{json, Value}; +use signup_sequencer::identity_tree::Hash; +use signup_sequencer::server::data::{ + DeletionRequest, InclusionProofRequest, InclusionProofResponse, InsertCommitmentRequest, +}; use tracing::debug; use crate::common::prelude::StatusCode; @@ -16,15 +20,12 @@ pub struct RawResponse { pub async fn insert_identity( client: &Client, uri: &String, - commitment: &String, + commitment: &Hash, ) -> anyhow::Result<()> { debug!("Calling /insertIdentity"); - let body = Body::from( - json!({ - "identityCommitment": format!("0x{}", commitment), - }) - .to_string(), - ); + let body = Body::from(serde_json::to_string(&InsertCommitmentRequest { + identity_commitment: *commitment, + })?); let req = Request::builder() .method("POST") @@ -55,15 +56,12 @@ pub async fn insert_identity( pub async fn delete_identity( client: &Client, uri: &String, - commitment: &String, + commitment: &Hash, ) -> anyhow::Result<()> { debug!("Calling /deleteIdentity"); - let body = Body::from( - json!({ - "identityCommitment": format!("0x{}", commitment), - }) - .to_string(), - ); + let body = Body::from(serde_json::to_string(&DeletionRequest { + identity_commitment: *commitment, + })?); let req = Request::builder() .method("POST") @@ -94,15 +92,12 @@ pub async fn delete_identity( pub async fn inclusion_proof_raw( client: &Client, uri: &String, - commitment: &String, + commitment: &Hash, ) -> anyhow::Result { debug!("Calling /inclusionProof"); - let body = Body::from( - json!({ - "identityCommitment": format!("0x{}", commitment), - }) - .to_string(), - ); + let body = Body::from(serde_json::to_string(&InclusionProofRequest { + identity_commitment: *commitment, + })?); let req = Request::builder() .method("POST") @@ -130,12 +125,12 @@ pub async fn inclusion_proof_raw( pub async fn inclusion_proof( client: &Client, uri: &String, - commitment: &String, -) -> anyhow::Result { + commitment: &Hash, +) -> anyhow::Result { let result = inclusion_proof_raw(client, uri, commitment).await?; - let result_json = - serde_json::from_str::(&result.body).expect("Failed to parse response as json"); + let result_json = serde_json::from_str::(&result.body) + .expect("Failed to parse response as json"); Ok(result_json) } diff --git a/e2e_tests/scenarios/tests/common/docker_compose.rs b/e2e_tests/scenarios/tests/common/docker_compose.rs index b11ae9d3..81af9baf 100644 --- a/e2e_tests/scenarios/tests/common/docker_compose.rs +++ b/e2e_tests/scenarios/tests/common/docker_compose.rs @@ -168,13 +168,11 @@ pub async fn setup(cwd: &str) -> anyhow::Result { } fn generate_project_name() -> String { - let charset: &[u8] = b"abcdefghijklmnopqrstuvwxyz"; - let mut rng = rand::thread_rng(); - (0..8) - .map(|_| { - let idx = rng.gen_range(0..charset.len()); - charset[idx] as char - }) + thread_rng() + .sample_iter(Alphanumeric) + .filter(|c| c.is_ascii_lowercase()) + .take(8) + .map(char::from) .collect() } diff --git a/e2e_tests/scenarios/tests/common/mod.rs b/e2e_tests/scenarios/tests/common/mod.rs index 7f034fdd..44e7851a 100644 --- a/e2e_tests/scenarios/tests/common/mod.rs +++ b/e2e_tests/scenarios/tests/common/mod.rs @@ -2,11 +2,24 @@ // test crates - so some code may not be used in some cases #![allow(dead_code, clippy::too_many_arguments, unused_imports)] +use std::time::Duration; + +use anyhow::anyhow; use ethers::types::U256; +use hyper::client::HttpConnector; +use hyper::Client; +use serde_json::Value; +use signup_sequencer::identity_tree::ProcessedStatus::Mined; +use signup_sequencer::identity_tree::{Hash, Status}; +use signup_sequencer::server::data::InclusionProofResponse; +use tokio::time::sleep; use tracing::error; use tracing_subscriber::fmt::format::FmtSpan; use tracing_subscriber::fmt::time::Uptime; +use crate::common::api::{delete_identity, inclusion_proof, inclusion_proof_raw, insert_identity}; +use crate::common::prelude::StatusCode; + mod api; pub mod docker_compose; @@ -25,7 +38,11 @@ pub mod prelude { pub use tracing::{error, info, instrument}; pub use tracing_subscriber::fmt::format; - pub use super::{generate_test_commitments, init_tracing_subscriber}; + pub use super::{ + bad_request_inclusion_proof_with_retries, delete_identity_with_retries, + generate_test_commitments, init_tracing_subscriber, insert_identity_with_retries, + mined_inclusion_proof_with_retries, + }; pub use crate::common::api::{ delete_identity, inclusion_proof, inclusion_proof_raw, insert_identity, }; @@ -67,17 +84,106 @@ pub fn init_tracing_subscriber() { /// would be used in reality. This is both to make them easier to generate and /// to ensure that we do not run afoul of the element numeric limit for the /// snark scalar field. -pub fn generate_test_commitments(count: usize) -> Vec { +pub fn generate_test_commitments(count: usize) -> Vec { let mut commitments = vec![]; for _ in 0..count { // Generate the identities using the just the last 64 bits (of 256) has so we're // guaranteed to be less than SNARK_SCALAR_FIELD. - let bytes: [u8; 32] = U256::from(rand::random::()).into(); - let identity_string: String = hex::encode(bytes); + let commitment = Hash::from(rand::random::()); - commitments.push(identity_string); + commitments.push(commitment); } commitments } + +pub async fn delete_identity_with_retries( + client: &Client, + uri: &String, + commitment: &Hash, + retries_count: usize, + retries_interval: f32, +) -> anyhow::Result<()> { + let mut last_err = None; + + for _ in 0..retries_count { + match delete_identity(client, uri, commitment).await { + Ok(_) => return Ok(()), + Err(err) => last_err = Some(err), + } + sleep(Duration::from_secs_f32(retries_interval)).await; + } + + Err(last_err.unwrap_or_else(|| anyhow!("All retries failed without error"))) +} + +pub async fn insert_identity_with_retries( + client: &Client, + uri: &String, + commitment: &Hash, + retries_count: usize, + retries_interval: f32, +) -> anyhow::Result<()> { + let mut last_err = None; + for _ in 0..retries_count { + match insert_identity(&client, &uri, &commitment).await { + Ok(_) => return Ok(()), + Err(err) => last_err = Some(err), + } + _ = sleep(Duration::from_secs_f32(retries_interval)).await; + } + + Err(last_err.unwrap_or_else(|| anyhow!("All retries failed without error"))) +} + +pub async fn mined_inclusion_proof_with_retries( + client: &Client, + uri: &String, + commitment: &Hash, + retries_count: usize, + retries_interval: f32, +) -> anyhow::Result { + let mut last_res = Err(anyhow!("No calls at all")); + for _i in 0..retries_count { + last_res = inclusion_proof(&client, &uri, &commitment).await; + + if let Ok(ref inclusion_proof_json) = last_res { + if inclusion_proof_json.0.status == Status::Processed(Mined) { + break; + } + }; + + _ = sleep(Duration::from_secs_f32(retries_interval)).await; + } + + let inclusion_proof_json = last_res?; + + assert_eq!(inclusion_proof_json.0.status, Status::Processed(Mined)); + + Ok(inclusion_proof_json) +} + +pub async fn bad_request_inclusion_proof_with_retries( + client: &Client, + uri: &String, + commitment: &Hash, + retries_count: usize, + retries_interval: f32, +) -> anyhow::Result<()> { + let mut last_err = None; + + for _ in 0..retries_count { + match inclusion_proof_raw(client, uri, commitment).await { + Ok(response) if response.status_code == StatusCode::BAD_REQUEST => return Ok(()), + Err(err) => { + error!("error: {}", err); + last_err = Some(err); + } + _ => {} + } + sleep(Duration::from_secs_f32(retries_interval)).await; + } + + Err(last_err.unwrap_or_else(|| anyhow!("All retries failed to return BAD_REQUEST"))) +} diff --git a/e2e_tests/scenarios/tests/insert_100.rs b/e2e_tests/scenarios/tests/insert_100.rs index de657fc3..bd6861e6 100644 --- a/e2e_tests/scenarios/tests/insert_100.rs +++ b/e2e_tests/scenarios/tests/insert_100.rs @@ -1,8 +1,6 @@ mod common; use common::prelude::*; -use serde_json::Value; -use tokio::time::sleep; use crate::common::docker_compose; @@ -17,7 +15,7 @@ async fn insert_100() -> anyhow::Result<()> { let uri = format!("http://{}", docker_compose.get_local_addr()); let client = Client::new(); - let identities = generate_test_commitments(100); + let identities = generate_test_commitments(10); for commitment in identities.iter() { _ = insert_identity_with_retries(&client, &uri, commitment, 10, 3.0).await?; @@ -29,55 +27,3 @@ async fn insert_100() -> anyhow::Result<()> { Ok(()) } - -async fn insert_identity_with_retries( - client: &Client, - uri: &String, - commitment: &String, - retries_count: usize, - retries_interval: f32, -) -> anyhow::Result<()> { - let mut last_res = Err(Error::msg("No calls at all")); - for _i in 0..retries_count { - last_res = insert_identity(&client, &uri, &commitment).await; - - if last_res.is_ok() { - break; - } - - _ = sleep(Duration::from_secs_f32(retries_interval)).await; - } - - if let Err(err) = last_res { - return Err(err); - }; - - last_res -} - -async fn mined_inclusion_proof_with_retries( - client: &Client, - uri: &String, - commitment: &String, - retries_count: usize, - retries_interval: f32, -) -> anyhow::Result { - let mut last_res = Err(Error::msg("No calls at all")); - for _i in 0..retries_count { - last_res = inclusion_proof(&client, &uri, &commitment).await; - - if let Ok(ref inclusion_proof_json) = last_res { - if inclusion_proof_json["status"] == "mined" { - break; - } - }; - - _ = sleep(Duration::from_secs_f32(retries_interval)).await; - } - - let inclusion_proof_json = last_res?; - - assert_eq!(inclusion_proof_json["status"], "mined"); - - Ok(inclusion_proof_json) -} diff --git a/e2e_tests/scenarios/tests/insert_delete_insert.rs b/e2e_tests/scenarios/tests/insert_delete_insert.rs index c56dae40..8708bf76 100644 --- a/e2e_tests/scenarios/tests/insert_delete_insert.rs +++ b/e2e_tests/scenarios/tests/insert_delete_insert.rs @@ -1,10 +1,6 @@ mod common; use common::prelude::*; -use serde_json::Value; -use tokio::time::sleep; -use tracing::debug; -use tracing::field::debug; use crate::common::docker_compose; @@ -47,109 +43,3 @@ async fn insert_delete_insert() -> anyhow::Result<()> { Ok(()) } - -async fn insert_identity_with_retries( - client: &Client, - uri: &String, - commitment: &String, - retries_count: usize, - retries_interval: f32, -) -> anyhow::Result<()> { - let mut last_res = Err(Error::msg("No calls at all")); - for _i in 0..retries_count { - last_res = insert_identity(&client, &uri, &commitment).await; - - if last_res.is_ok() { - break; - } - - _ = sleep(Duration::from_secs_f32(retries_interval)).await; - } - - if let Err(err) = last_res { - return Err(err); - }; - - last_res -} - -async fn mined_inclusion_proof_with_retries( - client: &Client, - uri: &String, - commitment: &String, - retries_count: usize, - retries_interval: f32, -) -> anyhow::Result { - let mut last_res = Err(Error::msg("No calls at all")); - for _i in 0..retries_count { - last_res = inclusion_proof(&client, &uri, &commitment).await; - - if let Ok(ref inclusion_proof_json) = last_res { - if inclusion_proof_json["status"] == "mined" { - break; - } - }; - - _ = sleep(Duration::from_secs_f32(retries_interval)).await; - } - - let inclusion_proof_json = last_res?; - - assert_eq!(inclusion_proof_json["status"], "mined"); - - Ok(inclusion_proof_json) -} - -async fn bad_request_inclusion_proof_with_retries( - client: &Client, - uri: &String, - commitment: &String, - retries_count: usize, - retries_interval: f32, -) -> anyhow::Result<()> { - let mut last_res = Err(Error::msg("No calls at all")); - for _i in 0..retries_count { - last_res = inclusion_proof_raw(&client, &uri, &commitment).await; - - if let Ok(ref response) = last_res { - if response.status_code == StatusCode::BAD_REQUEST { - break; - } - } else { - error!("error: {}", last_res.as_ref().err().unwrap()); - } - - _ = sleep(Duration::from_secs_f32(retries_interval)).await; - } - - let response = last_res?; - - assert_eq!(response.status_code, StatusCode::BAD_REQUEST); - - Ok(()) -} - -async fn delete_identity_with_retries( - client: &Client, - uri: &String, - commitment: &String, - retries_count: usize, - retries_interval: f32, -) -> anyhow::Result<()> { - let mut last_res = Err(Error::msg("No calls at all")); - for _i in 0..retries_count { - last_res = delete_identity(&client, &uri, &commitment).await; - - if last_res.is_ok() { - break; - } - - _ = sleep(Duration::from_secs_f32(retries_interval)).await; - } - - if let Err(err) = last_res { - return Err(err); - }; - - last_res -} diff --git a/e2e_tests/scenarios/tests/insert_restart_insert.rs b/e2e_tests/scenarios/tests/insert_restart_insert.rs index b6988d3a..49c6f426 100644 --- a/e2e_tests/scenarios/tests/insert_restart_insert.rs +++ b/e2e_tests/scenarios/tests/insert_restart_insert.rs @@ -1,10 +1,6 @@ mod common; use common::prelude::*; -use serde_json::Value; -use tokio::time::sleep; -use tracing::debug; -use tracing::field::debug; use crate::common::docker_compose; @@ -43,109 +39,3 @@ async fn insert_restart_insert() -> anyhow::Result<()> { Ok(()) } - -async fn insert_identity_with_retries( - client: &Client, - uri: &String, - commitment: &String, - retries_count: usize, - retries_interval: f32, -) -> anyhow::Result<()> { - let mut last_res = Err(Error::msg("No calls at all")); - for _i in 0..retries_count { - last_res = insert_identity(&client, &uri, &commitment).await; - - if last_res.is_ok() { - break; - } - - _ = sleep(Duration::from_secs_f32(retries_interval)).await; - } - - if let Err(err) = last_res { - return Err(err); - }; - - last_res -} - -async fn mined_inclusion_proof_with_retries( - client: &Client, - uri: &String, - commitment: &String, - retries_count: usize, - retries_interval: f32, -) -> anyhow::Result { - let mut last_res = Err(Error::msg("No calls at all")); - for _i in 0..retries_count { - last_res = inclusion_proof(&client, &uri, &commitment).await; - - if let Ok(ref inclusion_proof_json) = last_res { - if inclusion_proof_json["status"] == "mined" { - break; - } - }; - - _ = sleep(Duration::from_secs_f32(retries_interval)).await; - } - - let inclusion_proof_json = last_res?; - - assert_eq!(inclusion_proof_json["status"], "mined"); - - Ok(inclusion_proof_json) -} - -async fn bad_request_inclusion_proof_with_retries( - client: &Client, - uri: &String, - commitment: &String, - retries_count: usize, - retries_interval: f32, -) -> anyhow::Result<()> { - let mut last_res = Err(Error::msg("No calls at all")); - for _i in 0..retries_count { - last_res = inclusion_proof_raw(&client, &uri, &commitment).await; - - if let Ok(ref response) = last_res { - if response.status_code == StatusCode::BAD_REQUEST { - break; - } - } else { - error!("error: {}", last_res.as_ref().err().unwrap()); - } - - _ = sleep(Duration::from_secs_f32(retries_interval)).await; - } - - let response = last_res?; - - assert_eq!(response.status_code, StatusCode::BAD_REQUEST); - - Ok(()) -} - -async fn delete_identity_with_retries( - client: &Client, - uri: &String, - commitment: &String, - retries_count: usize, - retries_interval: f32, -) -> anyhow::Result<()> { - let mut last_res = Err(Error::msg("No calls at all")); - for _i in 0..retries_count { - last_res = delete_identity(&client, &uri, &commitment).await; - - if last_res.is_ok() { - break; - } - - _ = sleep(Duration::from_secs_f32(retries_interval)).await; - } - - if let Err(err) = last_res { - return Err(err); - }; - - last_res -} diff --git a/src/identity_tree/mod.rs b/src/identity_tree/mod.rs index 2b32dcef..303a1557 100644 --- a/src/identity_tree/mod.rs +++ b/src/identity_tree/mod.rs @@ -51,7 +51,7 @@ pub struct RootItem { pub mined_valid_as_of: Option>, } -#[derive(Debug, Serialize, Deserialize)] +#[derive(Debug, Serialize, Deserialize, PartialEq)] #[serde(rename_all = "camelCase")] pub struct InclusionProof { pub status: Status, diff --git a/src/server/data.rs b/src/server/data.rs index 7e4ef702..6697e619 100644 --- a/src/server/data.rs +++ b/src/server/data.rs @@ -8,7 +8,7 @@ use crate::identity_tree::{ }; use crate::prover::{ProverConfig, ProverType}; -#[derive(Debug, Serialize, Deserialize)] +#[derive(Debug, Serialize, Deserialize, PartialEq)] #[serde(transparent)] pub struct InclusionProofResponse(pub InclusionProof); diff --git a/tests/common/mod.rs b/tests/common/mod.rs index 43df3e2f..f84bb257 100644 --- a/tests/common/mod.rs +++ b/tests/common/mod.rs @@ -59,7 +59,7 @@ pub mod prelude { DEFAULT_TREE_DENSE_PREFIX_DEPTH, DEFAULT_TREE_DEPTH, }; pub use super::{ - abi as ContractAbi, generate_reference_proof_json, generate_test_identities, + abi as ContractAbi, generate_reference_proof, generate_test_identities, init_tracing_subscriber, spawn_app, spawn_deps, spawn_mock_deletion_prover, spawn_mock_insertion_prover, test_inclusion_proof, test_insert_identity, test_verify_proof, test_verify_proof_on_chain, @@ -76,7 +76,13 @@ use std::sync::Arc; use futures::stream::FuturesUnordered; use futures::StreamExt; use hyper::StatusCode; -use signup_sequencer::identity_tree::{Status, TreeState, TreeVersionReadOps}; +use semaphore::poseidon_tree::Proof; +use signup_sequencer::identity_tree::ProcessedStatus::{Mined, Pending}; +use signup_sequencer::identity_tree::{InclusionProof, Status, TreeState, TreeVersionReadOps}; +use signup_sequencer::server::data::{ + AddBatchSizeRequest, DeletionRequest, InclusionProofRequest, InclusionProofResponse, + InsertCommitmentRequest, RecoveryRequest, RemoveBatchSizeRequest, VerifySemaphoreProofRequest, +}; use signup_sequencer::task_monitor::TaskMonitor; use testcontainers::clients::Cli; use tracing::trace; @@ -269,28 +275,28 @@ pub async fn test_inclusion_proof( .expect("Failed to convert response body to bytes"); let result = String::from_utf8(bytes.into_iter().collect()) .expect("Could not parse response bytes to utf-8"); - let result_json = serde_json::from_str::(&result) + let result = serde_json::from_str::(&result) .expect("Failed to parse response as json"); - let status = result_json["status"] - .as_str() - .expect("Failed to get status"); - - if status == "pending" { - assert_eq!( - result_json, - generate_reference_proof_json(ref_tree, leaf_index, "pending") - ); - assert_eq!(response.status(), StatusCode::ACCEPTED); - info!("Got pending, waiting 5 seconds, iteration {}", i); - tokio::time::sleep(Duration::from_secs(5)).await; - } else if status == "mined" { - // We don't differentiate between these 2 states in tests - let proof_json = generate_reference_proof_json(ref_tree, leaf_index, status); - assert_eq!(result_json, proof_json); - return; - } else { - panic!("Unexpected status: {}", status); + match result.0.status { + Status::Processed(Pending) => { + assert_eq!( + result, + generate_reference_proof(ref_tree, leaf_index, Status::Processed(Pending)) + ); + assert_eq!(response.status(), StatusCode::ACCEPTED); + info!("Got pending, waiting 5 seconds, iteration {}", i); + tokio::time::sleep(Duration::from_secs(5)).await; + } + Status::Processed(Mined) => { + // We don't differentiate between these 2 states in tests + let proof_json = + generate_reference_proof(ref_tree, leaf_index, Status::Processed(Mined)); + assert_eq!(result, proof_json); + + return; + } + _ => panic!("Unexpected status: {:?}", result.0.status), } } @@ -331,18 +337,12 @@ pub async fn test_inclusion_status( result: {:?}", result ); - let result_json = serde_json::from_str::(&result) + let result = serde_json::from_str::(&result) .expect("Failed to parse response as json"); - let status = result_json["status"] - .as_str() - .expect("Failed to get status"); let expected_status = expected_status.into(); - assert_eq!( - expected_status, - Status::from_str(status).expect("Could not convert str to Status") - ); + assert_eq!(expected_status, result.0.status,); } #[instrument(skip_all)] @@ -441,17 +441,12 @@ pub async fn test_add_batch_size( prover_type: ProverType, client: &Client, ) -> anyhow::Result<()> { - let prover_url_string: String = prover_url.into(); - - let body = Body::from( - json!({ - "url": prover_url_string, - "batchSize": batch_size, - "timeoutSeconds": 3, - "proverType": prover_type - }) - .to_string(), - ); + let body = Body::from(serde_json::to_string(&AddBatchSizeRequest { + url: prover_url.into(), + batch_size: batch_size as usize, + timeout_seconds: 3, + prover_type, + })?); let request = Request::builder() .method("POST") .uri(uri.into() + "/addBatchSize") @@ -475,8 +470,10 @@ pub async fn test_remove_batch_size( prover_type: ProverType, expect_failure: bool, ) -> anyhow::Result<()> { - let body = - Body::from(json!({ "batchSize": batch_size, "proverType": prover_type }).to_string()); + let body = Body::from(serde_json::to_string(&RemoveBatchSizeRequest { + batch_size: batch_size as usize, + prover_type, + })?); let request = Request::builder() .method("POST") .uri(uri.into() + "/removeBatchSize") @@ -537,41 +534,41 @@ pub async fn test_insert_identity( fn construct_inclusion_proof_body(identity_commitment: &Hash) -> Body { Body::from( - json!({ - "identityCommitment": identity_commitment, + serde_json::to_string(&InclusionProofRequest { + identity_commitment: *identity_commitment, }) - .to_string(), + .expect("Cannot serialize InclusionProofRequest"), ) } fn construct_delete_identity_body(identity_commitment: &Hash) -> Body { Body::from( - json!({ - "identityCommitment": identity_commitment, + serde_json::to_string(&DeletionRequest { + identity_commitment: *identity_commitment, }) - .to_string(), + .expect("Cannot serialize DeletionRequest"), ) } pub fn construct_recover_identity_body( - prev_identity_commitment: &Hash, + previous_identity_commitment: &Hash, new_identity_commitment: &Hash, ) -> Body { Body::from( - json!({ - "previousIdentityCommitment":prev_identity_commitment , - "newIdentityCommitment": new_identity_commitment, + serde_json::to_string(&RecoveryRequest { + previous_identity_commitment: *previous_identity_commitment, + new_identity_commitment: *new_identity_commitment, }) - .to_string(), + .expect("Cannot serialize RecoveryRequest"), ) } pub fn construct_insert_identity_body(identity_commitment: &Field) -> Body { Body::from( - json!({ - "identityCommitment": identity_commitment, + serde_json::to_string(&InsertCommitmentRequest { + identity_commitment: *identity_commitment, }) - .to_string(), + .expect("Cannot serialize InsertCommitmentRequest"), ) } @@ -583,14 +580,14 @@ fn construct_verify_proof_body( proof: protocol::Proof, ) -> Body { Body::from( - json!({ - "root": root, - "signalHash": signal_hash, - "nullifierHash": nullifer_hash, - "externalNullifierHash": external_nullifier_hash, - "proof": proof, + serde_json::to_string(&VerifySemaphoreProofRequest { + root, + signal_hash, + nullifier_hash: nullifer_hash, + external_nullifier_hash, + proof, }) - .to_string(), + .expect("Cannot serialize VerifySemaphoreProofRequest"), ) } @@ -800,27 +797,16 @@ pub fn init_tracing_subscriber() { } } -pub fn generate_reference_proof_json( +pub fn generate_reference_proof( ref_tree: &PoseidonTree, leaf_idx: usize, - status: &str, -) -> serde_json::Value { - let proof = ref_tree - .proof(leaf_idx) - .unwrap() - .0 - .iter() - .map(|branch| match branch { - Branch::Left(hash) => json!({ "Left": hash }), - Branch::Right(hash) => json!({ "Right": hash }), - }) - .collect::>(); - let root = ref_tree.root(); - json!({ - "status": status, - "root": root, - "proof": proof, - "message": serde_json::Value::Null + status: Status, +) -> InclusionProofResponse { + InclusionProofResponse(InclusionProof { + status, + root: Some(ref_tree.root()), + proof: ref_tree.proof(leaf_idx), + message: None, }) }