-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor backend following updated contracts (#152)
- Loading branch information
Showing
30 changed files
with
5,233 additions
and
5,259 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
chain;asset_name;amount | ||
ETH;ETH;556863 | ||
ETH;USDT;556863 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,49 +1,118 @@ | ||
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, U256}, | ||
}; | ||
use serde::Deserialize; | ||
|
||
use crate::contracts::generated::summa_contract::{AddressOwnershipProof, Asset}; | ||
|
||
#[derive(Debug, Deserialize)] | ||
struct Record { | ||
struct SignatureRecord { | ||
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 address_ownership_proofs = Vec::<AddressOwnershipProof>::new(); | ||
|
||
for result in rdr.deserialize() { | ||
let record: SignatureRecord = result?; | ||
|
||
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(address_ownership_proofs) | ||
} | ||
|
||
#[derive(Debug, Deserialize)] | ||
struct AssetRecord { | ||
chain: String, | ||
asset_name: String, | ||
amount: String, | ||
} | ||
|
||
pub fn parse_asset_csv<P: AsRef<Path>, const N_ASSETS: usize>( | ||
path: P, | ||
) -> Result<[Asset; N_ASSETS], 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 assets_vec = Vec::with_capacity(N_ASSETS); | ||
|
||
for result in rdr.deserialize() { | ||
let record: Record = result?; | ||
let record: AssetRecord = result?; | ||
|
||
signatures.push(record.signature); | ||
addresses.push(record.address); | ||
assets_vec.push(Asset { | ||
asset_name: record.asset_name, | ||
chain: record.chain, | ||
amount: U256::from_dec_str(&record.amount)?, | ||
}); | ||
} | ||
|
||
Ok((addresses, signatures)) | ||
let assets_array: [Asset; N_ASSETS] = assets_vec.try_into().map_err(|v: Vec<Asset>| { | ||
format!( | ||
"The number of assets in CSV file does not match the expected count {:?}", | ||
v | ||
) | ||
})?; | ||
|
||
Ok(assets_array) | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
|
||
#[test] | ||
fn test_parse_csv_to_assets() { | ||
// these signatures are from contracts/test/Summa.ts | ||
fn test_parse_csv_to_signature() { | ||
let path = "src/apis/csv/signatures.csv"; | ||
let (addresses, signatures) = parse_signature_csv(path).unwrap(); | ||
let address_ownership = parse_signature_csv(path).unwrap(); | ||
|
||
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!(addresses[0], "0x70997970C51812dc3A010C7d01b50e0d17dc79C8"); | ||
assert_eq!(address_ownership[0], first_address_ownership); | ||
} | ||
|
||
#[test] | ||
fn test_parse_csv_to_assets() { | ||
let path = "src/apis/csv/assets.csv"; | ||
let assets = parse_asset_csv::<&str, 2>(path).unwrap(); | ||
|
||
assert_eq!( | ||
signatures[0], | ||
"0xb17a9e25265d3b88de7bfad81e7accad6e3d5612308ff83cc0fef76a34152b0444309e8fc3dea5139e49b6fc83a8553071a7af3d0cfd3fb8c1aea2a4c171729c1c" | ||
assets[0], | ||
Asset { | ||
chain: "ETH".to_string(), | ||
asset_name: "ETH".to_string(), | ||
amount: U256::from(556863), | ||
} | ||
); | ||
assert_eq!( | ||
assets[1], | ||
Asset { | ||
chain: "ETH".to_string(), | ||
asset_name: "USDT".to_string(), | ||
amount: U256::from(556863), | ||
} | ||
); | ||
} | ||
} |
Oops, something went wrong.