Skip to content

Commit

Permalink
fix: use Alloy address recovery
Browse files Browse the repository at this point in the history
  • Loading branch information
chris13524 committed Apr 28, 2024
1 parent 628b3e3 commit 989f68b
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 29 deletions.
3 changes: 1 addition & 2 deletions relay_rpc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ license = "Apache-2.0"
[features]
default = ["cacao"]
cacao = [
"dep:k256",
"dep:alloy-provider",
"dep:alloy-primitives",
"dep:erc6492",
Expand Down Expand Up @@ -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 }
Expand All @@ -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]
Expand Down
34 changes: 9 additions & 25 deletions relay_rpc/src/auth/cacao/signature/eip191.rs
Original file line number Diff line number Diff line change
@@ -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<u8> {
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 {
Expand Down Expand Up @@ -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]
Expand All @@ -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]
Expand All @@ -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]
Expand All @@ -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());
}
}
4 changes: 2 additions & 2 deletions relay_rpc/src/auth/cacao/signature/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 989f68b

Please sign in to comment.