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 pub fn verify(&self) -> Result<bool, String> {
13 use ethers::core::types::{Address, Signature};
14 use ethers::utils::hash_message;
15 use std::str::FromStr;
16
17 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 let message_hash = hash_message(&self.signed_message);
23
24 let recovered_address = signature.recover(message_hash).map_err(|e| e.to_string())?;
26
27 Ok(recovered_address == expected_address)
29 }
30}