From 989f68bd38052c15d8cb1485d80eb1b764cbecae Mon Sep 17 00:00:00 2001 From: Chris Smith Date: Sat, 27 Apr 2024 20:26:07 -0400 Subject: [PATCH] fix: use Alloy address recovery --- relay_rpc/Cargo.toml | 3 +- relay_rpc/src/auth/cacao/signature/eip191.rs | 34 ++++++-------------- relay_rpc/src/auth/cacao/signature/mod.rs | 4 +-- 3 files changed, 12 insertions(+), 29 deletions(-) diff --git a/relay_rpc/Cargo.toml b/relay_rpc/Cargo.toml index 677a92a..0473b69 100644 --- a/relay_rpc/Cargo.toml +++ b/relay_rpc/Cargo.toml @@ -7,7 +7,6 @@ license = "Apache-2.0" [features] default = ["cacao"] cacao = [ - "dep:k256", "dep:alloy-provider", "dep:alloy-primitives", "dep:erc6492", @@ -35,7 +34,6 @@ chrono = { version = "0.4", default-features = false, features = [ regex = "1.7" once_cell = "1.16" jsonwebtoken = "8.1" -k256 = { version = "0.13", optional = true } sha2 = { version = "0.10.6" } url = "2" alloy-provider = { git = "https://github.com/alloy-rs/alloy.git", rev = "d68a6b7", optional = true } @@ -44,6 +42,7 @@ erc6492 = { git = "https://github.com/WalletConnect/erc6492.git", optional = tru strum = { version = "0.26", features = ["strum_macros", "derive"] } [dev-dependencies] +k256 = "0.13" tokio = { version = "1.35.1", features = ["test-util", "macros"] } [lints.clippy] diff --git a/relay_rpc/src/auth/cacao/signature/eip191.rs b/relay_rpc/src/auth/cacao/signature/eip191.rs index 33ad441..7fec4fa 100644 --- a/relay_rpc/src/auth/cacao/signature/eip191.rs +++ b/relay_rpc/src/auth/cacao/signature/eip191.rs @@ -1,36 +1,20 @@ use { super::CacaoError, - alloy_primitives::{Address, FixedBytes}, + alloy_primitives::{Address, Signature}, }; pub const EIP191: &str = "eip191"; -pub fn eip191_bytes(message: &str) -> Vec { - format!( - "\u{0019}Ethereum Signed Message:\n{}{}", - message.as_bytes().len(), - message - ) - .into() -} - pub fn verify_eip191( signature: &[u8], address: &Address, - hash: FixedBytes<32>, + message: &[u8], ) -> Result<(), CacaoError> { - use k256::ecdsa::{RecoveryId, Signature as Sig, VerifyingKey}; - - let sig = Sig::try_from(signature.get(..64).ok_or(CacaoError::Verification)?) - .map_err(|_| CacaoError::Verification)?; - let recovery_id = RecoveryId::try_from(signature.get(64).ok_or(CacaoError::Verification)? % 27) + let signature = Signature::try_from(signature).map_err(|_| CacaoError::Verification)?; + let add = signature + .recover_address_from_msg(message) .map_err(|_| CacaoError::Verification)?; - let recovered_key = VerifyingKey::recover_from_prehash(hash.as_slice(), &sig, recovery_id) - .map_err(|_| CacaoError::Verification)?; - - let add = Address::from_public_key(&recovered_key); - if &add == address { Ok(()) } else { @@ -61,7 +45,7 @@ mod tests { let message = "xxx"; let signature = sign_message(message, &private_key); let address = Address::from_private_key(&private_key); - verify_eip191(&signature, &address, eip191_hash_message(message)).unwrap(); + verify_eip191(&signature, &address, message.as_bytes()).unwrap(); } #[test] @@ -71,7 +55,7 @@ mod tests { let mut signature = sign_message(message, &private_key); *signature.first_mut().unwrap() = signature.first().unwrap().wrapping_add(1); let address = Address::from_private_key(&private_key); - assert!(verify_eip191(&signature, &address, eip191_hash_message(message)).is_err()); + assert!(verify_eip191(&signature, &address, message.as_bytes()).is_err()); } #[test] @@ -81,7 +65,7 @@ mod tests { let signature = sign_message(message, &private_key); let mut address = Address::from_private_key(&private_key); *address.0.first_mut().unwrap() = address.0.first().unwrap().wrapping_add(1); - assert!(verify_eip191(&signature, &address, eip191_hash_message(message)).is_err()); + assert!(verify_eip191(&signature, &address, message.as_bytes()).is_err()); } #[test] @@ -91,6 +75,6 @@ mod tests { let signature = sign_message(message, &private_key); let address = Address::from_private_key(&private_key); let message2 = "yyy"; - assert!(verify_eip191(&signature, &address, eip191_hash_message(message2)).is_err()); + assert!(verify_eip191(&signature, &address, message2.as_bytes()).is_err()); } } diff --git a/relay_rpc/src/auth/cacao/signature/mod.rs b/relay_rpc/src/auth/cacao/signature/mod.rs index 9b133b6..c04b077 100644 --- a/relay_rpc/src/auth/cacao/signature/mod.rs +++ b/relay_rpc/src/auth/cacao/signature/mod.rs @@ -4,7 +4,7 @@ use { get_rpc_url::GetRpcUrl, }, super::{Cacao, CacaoError}, - alloy_primitives::{eip191_hash_message, hex::FromHex, Address, Bytes}, + alloy_primitives::{hex::FromHex, Address, Bytes}, alloy_provider::{network::Ethereum, ReqwestProvider}, erc6492::verify_signature, serde::{Deserialize, Serialize}, @@ -39,7 +39,7 @@ impl Signature { EIP191 => { // Technically we can use EIP-6492 to verify EIP-191 signatures as well, // but since we know the signature type we can avoid an RPC request. - verify_eip191(&signature, &address, eip191_hash_message(message)) + verify_eip191(&signature, &address, message.as_bytes()) } EIP1271 | EIP6492 => { if let Some(provider) = provider {