Skip to content

Commit

Permalink
fix: related address ownership
Browse files Browse the repository at this point in the history
  • Loading branch information
sifnoc committed Sep 11, 2023
1 parent c50eda9 commit ce908f0
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 67 deletions.
38 changes: 38 additions & 0 deletions backend/src/apis/address_ownership.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
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;

pub struct AddressOwnership {
address_ownership_proofs: Vec<AddressOwnershipProof>,
signer: SummaSigner,
}

impl AddressOwnership {
pub fn new(
signer_key: &str,
chain_id: u64,
rpc_url: &str,
summa_sc_address: Address,
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),
})
}

// 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.
pub async fn dispatch_proof_of_address_ownership(&mut self) -> Result<(), Box<dyn Error>> {
self.signer
.submit_proof_of_address_ownership(self.address_ownership_proofs.clone())
.await?;

Ok(())
}
}
7 changes: 3 additions & 4 deletions backend/src/apis/csv/signatures.csv
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
address;signature
0x70997970C51812dc3A010C7d01b50e0d17dc79C8;0xb17a9e25265d3b88de7bfad81e7accad6e3d5612308ff83cc0fef76a34152b0444309e8fc3dea5139e49b6fc83a8553071a7af3d0cfd3fb8c1aea2a4c171729c1c
0x3C44CdDdB6a900fa2b585dd299e03d12FA4293BC;0x089b32327d332c295dc3b8873c205b72153211de6dc1c51235782b091cefb9d06d6df2661b86a7d441cd322f125b84901486b150e684221a7b7636eb8182af551b
0x90F79bf6EB2c4f870365E785982E1f101E93b906;0xeb648c7409f45ba9064707d22bdae23dff15517aaf0942b8507b60b9a924bbeb4c8f2ceafc26ede9fd9eb3232cc138500ded3e3c7b8555fa43b995bd15c234ff1c
chain;address;signature;message
ETH;0x70997970C51812dc3A010C7d01b50e0d17dc79C8;0x089b32327d332c295dc3b8873c205b72153211de6dc1c51235782b091cefb9d06d6df2661b86a7d441cd322f125b84901486b150e684221a7b7636eb8182af551b;Summa proof of solvency for CryptoExchange
ETH;0x3C44CdDdB6a900fa2b585dd299e03d12FA4293BC;0xb17a9e25265d3b88de7bfad81e7accad6e3d5612308ff83cc0fef76a34152b0444309e8fc3dea5139e49b6fc83a8553071a7af3d0cfd3fb8c1aea2a4c171729c1c;Summa proof of solvency for CryptoExchange
40 changes: 24 additions & 16 deletions backend/src/apis/csv_parser.rs
Original file line number Diff line number Diff line change
@@ -1,32 +1,38 @@
use std::error::Error;
use std::fs::File;
use std::path::Path;
use std::{error::Error, fs::File, path::Path};

use ethers::{abi::AbiEncode, types::Bytes};
use serde::Deserialize;

use crate::contracts::generated::summa_contract::AddressOwnershipProof;

#[derive(Debug, Deserialize)]
struct Record {
chain: String,
address: String,
signature: String,
message: String,
}

pub fn parse_signature_csv<P: AsRef<Path>>(
path: P,
) -> Result<(Vec<String>, Vec<String>), Box<dyn Error>> {
) -> Result<Vec<AddressOwnershipProof>, Box<dyn Error>> {
let file = File::open(path)?;
let mut rdr = csv::ReaderBuilder::new().delimiter(b';').from_reader(file);

let mut signatures = Vec::<String>::new();
let mut addresses = Vec::<String>::new();
let mut address_ownership_proofs = Vec::<AddressOwnershipProof>::new();

for result in rdr.deserialize() {
let record: Record = result?;

signatures.push(record.signature);
addresses.push(record.address);
address_ownership_proofs.push(AddressOwnershipProof {
cex_address: record.address.to_string(),
chain: record.chain.to_string(),
signature: record.signature.parse()?,
message: Bytes::from(record.message.encode()),
});
}

Ok((addresses, signatures))
Ok(address_ownership_proofs)
}

#[cfg(test)]
Expand All @@ -35,15 +41,17 @@ mod tests {

#[test]
fn test_parse_csv_to_assets() {
// these signatures are from contracts/test/Summa.ts
let path = "src/apis/csv/signatures.csv";
let (addresses, signatures) = parse_signature_csv(path).unwrap();
let address_ownership = parse_signature_csv(path).unwrap();

assert_eq!(addresses[0], "0x70997970C51812dc3A010C7d01b50e0d17dc79C8");
let first_address_ownership = AddressOwnershipProof {
chain: "ETH".to_string(),
cex_address: "0x70997970C51812dc3A010C7d01b50e0d17dc79C8".to_string(),
signature:
("0x089b32327d332c295dc3b8873c205b72153211de6dc1c51235782b091cefb9d06d6df2661b86a7d441cd322f125b84901486b150e684221a7b7636eb8182af551b").parse().unwrap(),
message: "Summa proof of solvency for CryptoExchange".encode().into(),
};

assert_eq!(
signatures[0],
"0xb17a9e25265d3b88de7bfad81e7accad6e3d5612308ff83cc0fef76a34152b0444309e8fc3dea5139e49b6fc83a8553071a7af3d0cfd3fb8c1aea2a4c171729c1c"
);
assert_eq!(address_ownership[0], first_address_ownership);
}
}
2 changes: 1 addition & 1 deletion backend/src/apis/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
pub mod address_ownership;
mod csv_parser;
mod fetch;
pub mod ownership;
pub mod round;
37 changes: 0 additions & 37 deletions backend/src/apis/ownership.rs

This file was deleted.

4 changes: 2 additions & 2 deletions backend/src/apis/round.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,11 +80,11 @@ where
signer_key: &str,
chain_id: u64,
rpc_url: &str,
summasc_address: Address,
summa_sc_address: Address,
) -> Result<Round<LEVELS, N_ASSETS, N_BYTES>, Box<dyn Error>> {
Ok(Round {
snapshot: None,
signer: SummaSigner::new(&vec![], signer_key, chain_id, rpc_url, summasc_address),
signer: SummaSigner::new(&vec![], signer_key, chain_id, rpc_url, summa_sc_address),
})
}

Expand Down
15 changes: 8 additions & 7 deletions backend/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,11 @@ mod test {
abi::AbiEncode,
providers::Middleware,
types::{Address, Bytes, Filter, U256},
utils::{keccak256, Anvil},
utils::{keccak256, to_checksum, Anvil},
};
use halo2_proofs::halo2curves::bn256::Fr as Fp;

use crate::apis::{ownership::AddressOwnership, round::Round};
use crate::apis::{address_ownership::AddressOwnership, round::Round};
use crate::contracts::{
generated::{
inclusion_verifier::InclusionVerifier,
Expand Down Expand Up @@ -128,13 +128,13 @@ mod test {
// Dispatch proof of address ownership
let owned_addresses = vec![AddressOwnershipProof {
chain: "ETH".to_string(),
cex_address: cex_addr_1.to_string(),
cex_address: to_checksum(&cex_addr_1, None),
signature:
("0x089b32327d332c295dc3b8873c205b72153211de6dc1c51235782b091cefb9d06d6df2661b86a7d441cd322f125b84901486b150e684221a7b7636eb8182af551b").parse().unwrap(),
message: "Summa proof of solvency for CryptoExchange".encode().into(),
},AddressOwnershipProof {
chain: "ETH".to_string(),
cex_address: cex_addr_2.to_string(),
cex_address: to_checksum(&cex_addr_2, None),
signature:
("0xb17a9e25265d3b88de7bfad81e7accad6e3d5612308ff83cc0fef76a34152b0444309e8fc3dea5139e49b6fc83a8553071a7af3d0cfd3fb8c1aea2a4c171729c1c").parse().unwrap(),
message: "Summa proof of solvency for CryptoExchange".encode().into(),
Expand All @@ -145,11 +145,12 @@ mod test {
anvil.chain_id(),
anvil.endpoint().as_str(),
summa_contract.address(),
"src/apis/csv/signatures.csv",
)
.unwrap();

let ownership_submitted_result = address_ownership_client
.dispatch_proof_of_address_ownership(owned_addresses)
.dispatch_proof_of_address_ownership()
.await;

assert_eq!(ownership_submitted_result.is_ok(), true);
Expand All @@ -166,13 +167,13 @@ mod test {
AddressOwnershipProofSubmittedFilter {
address_ownership_proofs: vec![AddressOwnershipProof {
chain: "ETH".to_string(),
cex_address: cex_addr_1.to_string(),
cex_address: to_checksum(&cex_addr_1, None),
signature:
("0x089b32327d332c295dc3b8873c205b72153211de6dc1c51235782b091cefb9d06d6df2661b86a7d441cd322f125b84901486b150e684221a7b7636eb8182af551b").parse().unwrap(),
message: "Summa proof of solvency for CryptoExchange".encode().into(),
},AddressOwnershipProof {
chain: "ETH".to_string(),
cex_address: cex_addr_2.to_string(),
cex_address:to_checksum(&cex_addr_2, None),
signature:
("0xb17a9e25265d3b88de7bfad81e7accad6e3d5612308ff83cc0fef76a34152b0444309e8fc3dea5139e49b6fc83a8553071a7af3d0cfd3fb8c1aea2a4c171729c1c").parse().unwrap(),
message: "Summa proof of solvency for CryptoExchange".encode().into(),
Expand Down

0 comments on commit ce908f0

Please sign in to comment.