ether_link/
signature.rs

1pub struct EthereumSignature {
2    pub expected_address: String,
3    pub signed_message: String,
4    pub signature: String,
5}
6
7impl EthereumSignature {
8    pub fn new(expected_address: String, signed_message: String, signature: String) -> Self {
9        Self { expected_address, signed_message, signature }
10    }
11    // Verify the signature of a message
12    pub fn verify_ethereum_signature(&self) -> Result<bool, String> {
13        use ethers::core::types::{Address, Signature};
14        use ethers::utils::hash_message;
15        use std::str::FromStr;
16
17        // Parse the signature and address
18        let signature = Signature::from_str(&self.signature).map_err(|e| e.to_string())?;
19        let expected_address = Address::from_str(&self.expected_address).map_err(|e| e.to_string())?;
20        
21        // Hash the message as per Ethereum signed message format
22        let message_hash = hash_message(&self.signed_message);
23        
24        // Recover the address that signed the message
25        let recovered_address = signature.recover(message_hash).map_err(|e| e.to_string())?;
26        
27        // Compare the recovered address with the expected address
28        Ok(recovered_address == expected_address)
29    }
30}