From c60dd433bc7266a990f0fd9b9d3ed67b7b22f740 Mon Sep 17 00:00:00 2001 From: sifnoc Date: Fri, 15 Sep 2023 16:08:37 +0000 Subject: [PATCH] feat: create submit proof of ownership example --- backend/examples/submit_ownership.rs | 81 +++++++++++++++++++++++++++ backend/src/apis/address_ownership.rs | 4 ++ backend/src/contracts/signer.rs | 7 +-- 3 files changed, 87 insertions(+), 5 deletions(-) create mode 100644 backend/examples/submit_ownership.rs diff --git a/backend/examples/submit_ownership.rs b/backend/examples/submit_ownership.rs new file mode 100644 index 00000000..73707318 --- /dev/null +++ b/backend/examples/submit_ownership.rs @@ -0,0 +1,81 @@ +use std::sync::Arc; + +use ethers::{ + abi::{encode, Token}, + types::{Address, U256}, + utils::keccak256, +}; + +use summa_backend::{ + apis::address_ownership::AddressOwnership, contracts::generated::summa_contract::Summa, + tests::initialize_anvil, +}; + +// In this example, we will demonstrate how to submit ownership of address to the Summa contract. +#[tokio::main] +async fn main() { + // We have already demonstrated how to generate a CSV file containing the asset ownership proofs, `AddressOwnershipProof`. + // For more details on this, kindly refer to the "generate_signature" example. + // + // For the current demonstration, we'll use the same CSV file produced in `generate_signature` example. + let signature_csv_path = "src/apis/csv/signatures.csv"; + + // Initialize test environment + let (anvil, _, _, client, _) = initialize_anvil().await; + + // Deploy contracts with null addresses for verifier contracts for this example + let summa_contract = Summa::deploy(Arc::clone(&client), (Address::zero(), Address::zero())) + .unwrap() + .send() + .await + .unwrap(); + + // Initialize `AddressOwnership` client + let mut address_ownership_client = AddressOwnership::new( + "0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80", + anvil.chain_id(), + anvil.endpoint().as_str(), + summa_contract.address(), + signature_csv_path, + ) + .unwrap(); + + // Get hashed addresses using the `keccak256` method. + let address_hashes = address_ownership_client + .get_ownership_proofs() + .iter() + .map(|x| keccak256(encode(&[Token::String(x.cex_address.clone())]))) + .collect::>(); + + // Dispatches the proof of address ownership. + // In the client, the `dispatch_proof_of_address_ownership` function sends a transaction to the Summa contract + address_ownership_client + .dispatch_proof_of_address_ownership() + .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); + + // Check if the addresses are registered on the Summa contract. + for address_hash in address_hashes.iter() { + let registered = summa_contract + .ownership_proof_by_address(*address_hash) + .call() + .await + .unwrap(); + + assert_ne!(registered, U256::from(0)); + } + println!("Ownership proofs are submitted successfully!") +} diff --git a/backend/src/apis/address_ownership.rs b/backend/src/apis/address_ownership.rs index 1eb6b34b..7b56348b 100644 --- a/backend/src/apis/address_ownership.rs +++ b/backend/src/apis/address_ownership.rs @@ -25,6 +25,10 @@ impl AddressOwnership { }) } + pub fn get_ownership_proofs(&self) -> &Vec { + &self.address_ownership_proofs + } + // This function dispatches the proof of address ownership. Before calling this function, // ensure externally that the provided `addresses` in `address_ownership_proof` are not already registered // on the Summa contract. diff --git a/backend/src/contracts/signer.rs b/backend/src/contracts/signer.rs index 7303c841..5b4f3ccc 100644 --- a/backend/src/contracts/signer.rs +++ b/backend/src/contracts/signer.rs @@ -1,13 +1,10 @@ use crate::contracts::generated::summa_contract::Summa; use ethers::{ - abi::{encode, Token}, prelude::SignerMiddleware, providers::{Http, Provider}, - signers::{LocalWallet, Signer, WalletError}, - types::{Address, Signature}, - utils::keccak256, + signers::{LocalWallet, Signer}, + types::Address, }; -use futures::future::join_all; use serde_json::Value; use std::{ error::Error, fs::File, io::BufReader, path::Path, str::FromStr, sync::Arc, time::Duration,