Skip to content
This repository has been archived by the owner on Oct 19, 2024. It is now read-only.

Commit

Permalink
fix(eip712): handle fields with raw idents (#2315)
Browse files Browse the repository at this point in the history
* chore: bump syn to 2.0, fix breaking changes

* chore: update ethers-contract-derive to use syn 2

* chore: move eip712 into ethers-contract-derive

* fix: temp disable prettyplease formatting

* chore: update prettyplease

* fix mergings

* fix

* ci: disable live-tests since it doesn't do anything

* update

* fix(eip712): handle fields with raw idents
  • Loading branch information
DaniPopes authored Apr 10, 2023
1 parent 36a2037 commit 5a85223
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 40 deletions.
64 changes: 32 additions & 32 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 7 additions & 3 deletions ethers-contract/ethers-contract-derive/src/eip712.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,8 +138,12 @@ fn parse_fields(input: &DeriveInput) -> Result<Vec<(String, ParamType)>> {

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
Expand All @@ -150,7 +154,7 @@ fn parse_fields(input: &DeriveInput) -> Result<Vec<(String, ParamType)>> {
None => crate::utils::find_parameter_type(&f.ty)?,
};

fields.push((field_name, field_type));
fields.push((name, ty));
}

Ok(fields)
Expand Down
28 changes: 23 additions & 5 deletions ethers-contract/tests/it/eip712.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use ethers_core::{
};

#[test]
fn test_derive_eip712() {
fn derive_eip712() {
#[derive(Debug, Clone, Eip712, EthAbiType)]
#[eip712(
name = "Radicle",
Expand Down Expand Up @@ -43,7 +43,7 @@ fn test_derive_eip712() {
}

#[test]
fn test_struct_hash() {
fn struct_hash() {
#[derive(Debug, Clone, Eip712, EthAbiType)]
#[eip712(
name = "Radicle",
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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(
Expand All @@ -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)")
);
}

0 comments on commit 5a85223

Please sign in to comment.