Skip to content

Commit

Permalink
feat: create submit proof of ownership example
Browse files Browse the repository at this point in the history
  • Loading branch information
sifnoc committed Sep 15, 2023
1 parent a36f076 commit c60dd43
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 5 deletions.
81 changes: 81 additions & 0 deletions backend/examples/submit_ownership.rs
Original file line number Diff line number Diff line change
@@ -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::<Vec<[u8; 32]>>();

// 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!")
}
4 changes: 4 additions & 0 deletions backend/src/apis/address_ownership.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ impl AddressOwnership {
})
}

pub fn get_ownership_proofs(&self) -> &Vec<AddressOwnershipProof> {
&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.
Expand Down
7 changes: 2 additions & 5 deletions backend/src/contracts/signer.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand Down

0 comments on commit c60dd43

Please sign in to comment.