Skip to content

Commit

Permalink
feat: the signer no longer initiate in 'addressOwnership' and 'round'…
Browse files Browse the repository at this point in the history
… instances
  • Loading branch information
sifnoc committed Nov 1, 2023
1 parent ffd8259 commit f10af6e
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 83 deletions.
49 changes: 12 additions & 37 deletions backend/examples/summa_solvency_flow.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
#![feature(generic_const_exprs)]
use std::{error::Error, fs::File, io::BufReader, io::Write};
use std::{error::Error, fs::File, io::BufReader, io::Write, sync::Arc};

use ethers::{
abi::{encode, Token},
providers::Provider,
types::{Bytes, U256},
utils::keccak256,
};
Expand All @@ -13,6 +14,7 @@ use summa_backend::{
address_ownership::AddressOwnership,
round::{MstInclusionProof, Round},
},
contracts::signer::SummaSigner,
tests::initialize_test_env,
};
use summa_solvency::merkle_sum_tree::utils::generate_leaf_hash;
Expand All @@ -29,14 +31,17 @@ async fn main() -> Result<(), Box<dyn Error>> {
//
// Each CEX prepares its own `signature` CSV file.
let signature_csv_path = "src/apis/csv/signatures.csv";
let mut address_ownership_client = AddressOwnership::new(
// The signer would be using `provider` that shared with `address_ownership` and `round` instances.
let provider = Arc::new(Provider::try_from(anvil.endpoint().as_str())?);
let signer = SummaSigner::new(
"0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80",
anvil.chain_id(),
anvil.endpoint().as_str(),
provider,
summa_contract.address(),
signature_csv_path,
)
.unwrap();
);

let mut address_ownership_client =
AddressOwnership::new(signer.clone(), signature_csv_path).unwrap();

// Retrieve hashed addresses using the `keccak256` method.
let address_hashes = address_ownership_client
Expand All @@ -52,19 +57,6 @@ async fn main() -> Result<(), Box<dyn Error>> {
.await
.unwrap();

// If the `addressHash` isn't found in the `addressOwnershipProofs` mapping of the Summa contract,
// it will return 0; otherwise, it will return a non-zero value.
//
// You can find unregistered address with null bytes as follows:
//
// let unregistered = summa_contract
// .ownership_proof_by_address([0u8; 32])
// .call()
// .await
// .unwrap();
//
// assert_eq!(unregistered, 0);

// Verify whether the addresses have been registered within the Summa contract.
for address_hash in address_hashes.iter() {
let registered = summa_contract
Expand All @@ -85,28 +77,11 @@ async fn main() -> Result<(), Box<dyn Error>> {
let params_path = "ptau/hermez-raw-11";

// Using the `round` instance, the solvency proof is dispatched to the Summa contract with the `dispatch_solvency_proof` method.
let mut round = Round::<4, 2, 14>::new(
"0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80", // anvil account [0]
anvil.chain_id(),
anvil.endpoint().as_str(),
summa_contract.address(),
entry_csv,
asset_csv,
params_path,
1,
)
.unwrap();
let mut round = Round::<4, 2, 14>::new(signer, entry_csv, asset_csv, params_path, 1).unwrap();

// Sends the solvency proof, which should ideally complete without errors.
assert_eq!(round.dispatch_solvency_proof().await.unwrap(), ());

// You can also use the `solvency_proof_submitted_filter` method to check if the solvency proof is submitted.
// println!("{:?}", summa_contract
// .solvency_proof_submitted_filter()
// .query()
// .await
// .unwrap(););

println!("2. Solvency proof is submitted successfully!");

// 3. Generate Inclusion Proof
Expand Down
8 changes: 2 additions & 6 deletions backend/src/apis/address_ownership.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use crate::contracts::{generated::summa_contract::AddressOwnershipProof, signer::SummaSigner};
use ethers::types::Address;
use std::{error::Error, result::Result};

use super::csv_parser::parse_signature_csv;
Expand All @@ -11,17 +10,14 @@ pub struct AddressOwnership {

impl AddressOwnership {
pub fn new(
signer_key: &str,
chain_id: u64,
rpc_url: &str,
summa_sc_address: Address,
signer: SummaSigner,
signature_csv_path: &str,
) -> Result<AddressOwnership, Box<dyn Error>> {
let address_ownership_proofs = parse_signature_csv(signature_csv_path)?;

Ok(AddressOwnership {
address_ownership_proofs,
signer: SummaSigner::new(signer_key, chain_id, rpc_url, summa_sc_address),
signer,
})
}

Expand Down
12 changes: 3 additions & 9 deletions backend/src/apis/round.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
use ethers::{
abi::Address,
types::{Bytes, U256},
};
use ethers::types::{Bytes, U256};
use halo2_proofs::{
halo2curves::bn256::{Bn256, Fr as Fp, G1Affine},
plonk::{ProvingKey, VerifyingKey},
Expand Down Expand Up @@ -79,10 +76,7 @@ where
[usize; 2 * (1 + N_ASSETS)]: Sized,
{
pub fn new(
signer_key: &str,
chain_id: u64,
rpc_url: &str,
summa_sc_address: Address,
signer: SummaSigner,
entry_csv_path: &str,
asset_csv_path: &str,
params_path: &str,
Expand All @@ -96,7 +90,7 @@ where
params_path,
)
.unwrap(),
signer: SummaSigner::new(signer_key, chain_id, rpc_url, summa_sc_address),
signer,
})
}

Expand Down
2 changes: 1 addition & 1 deletion backend/src/contracts/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
pub mod generated;
pub mod mock;
pub(crate) mod signer;
pub mod signer;
20 changes: 10 additions & 10 deletions backend/src/contracts/signer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,30 +6,30 @@ use ethers::{
types::Address,
};
use serde_json::Value;
use std::{
error::Error, fs::File, io::BufReader, path::Path, str::FromStr, sync::Arc, time::Duration,
};
use std::{error::Error, fs::File, io::BufReader, path::Path, str::FromStr, sync::Arc};

use super::generated::summa_contract::{AddressOwnershipProof, Asset};

#[derive(Debug)]
#[derive(Debug, Clone)]
pub struct SummaSigner {
summa_contract: Summa<SignerMiddleware<Provider<Http>, LocalWallet>>,
summa_contract: Summa<SignerMiddleware<Arc<Provider<Http>>, LocalWallet>>,
}

impl SummaSigner {
/// Creates a new SummaSigner instance
/// # Arguments
/// * `signer_key` - The private key of wallet that will interact with the chain on behalf of the exchange
/// * `chain_id` - The chain id of the network
/// * `rpc_url` - The RPC URL of the network
/// * `provider` - Provider
/// * `address` - The address of the Summa contract
pub fn new(signer_key: &str, chain_id: u64, rpc_url: &str, address: Address) -> Self {
pub fn new(
signer_key: &str,
chain_id: u64,
provider: Arc<Provider<Http>>,
address: Address,
) -> Self {
let wallet: LocalWallet = LocalWallet::from_str(signer_key).unwrap();

let provider = Provider::<Http>::try_from(rpc_url)
.unwrap()
.interval(Duration::from_millis(10u64));
let client = Arc::new(SignerMiddleware::new(
provider,
wallet.with_chain_id(chain_id),
Expand Down
39 changes: 19 additions & 20 deletions backend/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,29 +104,36 @@ pub async fn initialize_test_env() -> (
mod test {
use ethers::{
abi::AbiEncode,
providers::Provider,
types::{Bytes, U256},
utils::to_checksum,
};
use std::{convert::TryFrom, error::Error, sync::Arc};

use crate::apis::{address_ownership::AddressOwnership, round::Round};
use crate::contracts::generated::summa_contract::{
AddressOwnershipProof, AddressOwnershipProofSubmittedFilter, Asset,
SolvencyProofSubmittedFilter,
use crate::contracts::{
generated::summa_contract::{
AddressOwnershipProof, AddressOwnershipProofSubmittedFilter, Asset,
SolvencyProofSubmittedFilter,
},
signer::SummaSigner,
};
use crate::tests::initialize_test_env;

#[tokio::test]
async fn test_round_features() {
async fn test_round_features() -> Result<(), Box<dyn Error>> {
let (anvil, cex_addr_1, cex_addr_2, _, summa_contract) = initialize_test_env().await;

let mut address_ownership_client = AddressOwnership::new(
let provider = Arc::new(Provider::try_from(anvil.endpoint().as_str())?);
let signer = SummaSigner::new(
"0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80",
anvil.chain_id(),
anvil.endpoint().as_str(),
provider,
summa_contract.address(),
"src/apis/csv/signatures.csv",
)
.unwrap();
);

let mut address_ownership_client =
AddressOwnership::new(signer.clone(), "src/apis/csv/signatures.csv").unwrap();

let ownership_submitted_result = address_ownership_client
.dispatch_proof_of_address_ownership()
Expand Down Expand Up @@ -166,17 +173,8 @@ mod test {
let entry_csv = "../zk_prover/src/merkle_sum_tree/csv/entry_16.csv";
let params_path = "ptau/hermez-raw-11";

let mut round = Round::<4, 2, 14>::new(
"0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80", // anvil account [0]
anvil.chain_id(),
anvil.endpoint().as_str(),
summa_contract.address(),
entry_csv,
asset_csv,
params_path,
1,
)
.unwrap();
let mut round =
Round::<4, 2, 14>::new(signer, entry_csv, asset_csv, params_path, 1).unwrap();

// Verify solvency proof
let mut logs = summa_contract
Expand Down Expand Up @@ -247,5 +245,6 @@ mod test {
assert_eq!(verified, true);

drop(anvil);
Ok(())
}
}

0 comments on commit f10af6e

Please sign in to comment.