From 5d4481fde9f9409f128f8ad15693aa9099891884 Mon Sep 17 00:00:00 2001 From: DaniPopes <57450786+DaniPopes@users.noreply.github.com> Date: Thu, 30 Mar 2023 12:42:45 +0200 Subject: [PATCH] fix(eip712): handle fields with raw idents --- .../ethers-contract-derive/src/eip712.rs | 10 +++++-- ethers-contract/tests/it/eip712.rs | 28 +++++++++++++++---- 2 files changed, 30 insertions(+), 8 deletions(-) diff --git a/ethers-contract/ethers-contract-derive/src/eip712.rs b/ethers-contract/ethers-contract-derive/src/eip712.rs index ece0524e0..50f3451a9 100644 --- a/ethers-contract/ethers-contract-derive/src/eip712.rs +++ b/ethers-contract/ethers-contract-derive/src/eip712.rs @@ -138,8 +138,12 @@ fn parse_fields(input: &DeriveInput) -> Result> { let mut fields = Vec::with_capacity(named_fields.named.len()); for f in named_fields.named.iter() { - let field_name = f.ident.as_ref().unwrap().to_string().to_camel_case(); - let field_type = + // strip the raw identifier prefix + let name = f.ident.as_ref().unwrap().to_string(); + let s = name.strip_prefix("r#").unwrap_or(&name); + let name = s.to_camel_case(); + + let ty = match f.attrs.iter().find(|a| a.path().segments.iter().any(|s| s.ident == "eip712")) { // Found nested Eip712 Struct // TODO: Implement custom @@ -150,7 +154,7 @@ fn parse_fields(input: &DeriveInput) -> Result> { None => crate::utils::find_parameter_type(&f.ty)?, }; - fields.push((field_name, field_type)); + fields.push((name, ty)); } Ok(fields) diff --git a/ethers-contract/tests/it/eip712.rs b/ethers-contract/tests/it/eip712.rs index 82ded36ac..d89d9b26e 100644 --- a/ethers-contract/tests/it/eip712.rs +++ b/ethers-contract/tests/it/eip712.rs @@ -11,7 +11,7 @@ use ethers_core::{ }; #[test] -fn test_derive_eip712() { +fn derive_eip712() { #[derive(Debug, Clone, Eip712, EthAbiType)] #[eip712( name = "Radicle", @@ -43,7 +43,7 @@ fn test_derive_eip712() { } #[test] -fn test_struct_hash() { +fn struct_hash() { #[derive(Debug, Clone, Eip712, EthAbiType)] #[eip712( name = "Radicle", @@ -80,7 +80,7 @@ fn test_struct_hash() { } #[test] -fn test_derive_eip712_nested() { +fn derive_eip712_nested() { #[derive(Debug, Clone, Eip712, EthAbiType)] #[eip712( name = "MyDomain", @@ -126,7 +126,7 @@ fn test_derive_eip712_nested() { } #[test] -fn test_uniswap_v2_permit_hash() { +fn uniswap_v2_permit_hash() { // See examples/permit_hash.rs for comparison // the following produces the same permit_hash as in the example @@ -162,7 +162,7 @@ fn test_uniswap_v2_permit_hash() { } #[test] -fn test_domain_hash_constants() { +fn domain_hash_constants() { assert_eq!( EIP712_DOMAIN_TYPE_HASH, keccak256( @@ -174,3 +174,21 @@ fn test_domain_hash_constants() { keccak256("EIP712Domain(string name,string version,uint256 chainId,address verifyingContract,bytes32 salt)") ); } + +// https://t.me/ethers_rs/26844 +#[test] +fn raw_ident_fields() { + #[derive(Debug, Clone, Eip712, EthAbiType)] + #[eip712(name = "replica", version = "1", chain_id = 6666)] + pub struct Message { + pub title: String, + pub href: String, + pub r#type: String, + pub timestamp: U256, + } + + assert_eq!( + Message::type_hash().unwrap(), + keccak256("Message(string title,string href,string type,uint256 timestamp)") + ); +}