|
| 1 | +use { |
| 2 | + super::CacaoError, |
| 3 | + alloy_primitives::{Address, FixedBytes}, |
| 4 | + alloy_providers::provider::{Provider, TempProvider}, |
| 5 | + alloy_rpc_types::{CallInput, CallRequest}, |
| 6 | + alloy_sol_types::{sol, SolCall}, |
| 7 | + alloy_transport_http::Http, |
| 8 | + url::Url, |
| 9 | +}; |
| 10 | + |
| 11 | +pub mod blockchain_api; |
| 12 | +pub mod get_rpc_url; |
| 13 | + |
| 14 | +pub const EIP1271: &str = "eip1271"; |
| 15 | + |
| 16 | +// https://eips.ethereum.org/EIPS/eip-1271 |
| 17 | +const MAGIC_VALUE: u32 = 0x1626ba7e; |
| 18 | +sol! { |
| 19 | + function isValidSignature( |
| 20 | + bytes32 _hash, |
| 21 | + bytes memory _signature) |
| 22 | + public |
| 23 | + view |
| 24 | + returns (bytes4 magicValue); |
| 25 | +} |
| 26 | + |
| 27 | +pub async fn verify_eip1271( |
| 28 | + signature: Vec<u8>, |
| 29 | + address: Address, |
| 30 | + hash: &[u8; 32], |
| 31 | + provider: Url, |
| 32 | +) -> Result<bool, CacaoError> { |
| 33 | + let provider = Provider::new(Http::new(provider)); |
| 34 | + |
| 35 | + let call_request = CallRequest { |
| 36 | + to: Some(address), |
| 37 | + input: CallInput::new( |
| 38 | + isValidSignatureCall { |
| 39 | + _hash: FixedBytes::from(hash), |
| 40 | + _signature: signature, |
| 41 | + } |
| 42 | + .abi_encode() |
| 43 | + .into(), |
| 44 | + ), |
| 45 | + ..Default::default() |
| 46 | + }; |
| 47 | + |
| 48 | + let result = provider.call(call_request, None).await.map_err(|e| { |
| 49 | + if let Some(error_response) = e.as_error_resp() { |
| 50 | + if error_response.message.starts_with("execution reverted:") { |
| 51 | + CacaoError::Verification |
| 52 | + } else { |
| 53 | + CacaoError::Eip1271Internal(e) |
| 54 | + } |
| 55 | + } else { |
| 56 | + CacaoError::Eip1271Internal(e) |
| 57 | + } |
| 58 | + })?; |
| 59 | + |
| 60 | + if result[..4] == MAGIC_VALUE.to_be_bytes().to_vec() { |
| 61 | + Ok(true) |
| 62 | + } else { |
| 63 | + Err(CacaoError::Verification) |
| 64 | + } |
| 65 | +} |
| 66 | + |
| 67 | +#[cfg(test)] |
| 68 | +mod test { |
| 69 | + use { |
| 70 | + super::*, |
| 71 | + crate::auth::cacao::signature::{eip191::eip191_bytes, strip_hex_prefix}, |
| 72 | + alloy_primitives::address, |
| 73 | + sha3::{Digest, Keccak256}, |
| 74 | + }; |
| 75 | + |
| 76 | + // Manual test. Paste address, signature, message, and project ID to verify |
| 77 | + // function |
| 78 | + #[tokio::test] |
| 79 | + #[ignore] |
| 80 | + async fn test_eip1271() { |
| 81 | + let address = address!("AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"); |
| 82 | + let signature = "xxx"; |
| 83 | + let signature = data_encoding::HEXLOWER_PERMISSIVE |
| 84 | + .decode(strip_hex_prefix(signature).as_bytes()) |
| 85 | + .map_err(|_| CacaoError::Verification) |
| 86 | + .unwrap(); |
| 87 | + let message = "xxx"; |
| 88 | + let hash = &Keccak256::new_with_prefix(eip191_bytes(message)).finalize()[..] |
| 89 | + .try_into() |
| 90 | + .unwrap(); |
| 91 | + let provider = "https://rpc.walletconnect.com/v1?chainId=eip155:1&projectId=xxx" |
| 92 | + .parse() |
| 93 | + .unwrap(); |
| 94 | + assert!(verify_eip1271(signature, address, hash, provider) |
| 95 | + .await |
| 96 | + .unwrap()); |
| 97 | + } |
| 98 | +} |
0 commit comments