18 releases (6 stable)

2.0.2 Mar 28, 2023
1.5.0 Mar 1, 2023
1.0.2 Nov 27, 2022
0.17.0 Jul 28, 2022
0.2.1 Nov 29, 2021

#2384 in Magic Beans

Download history 2964/week @ 2023-10-22 3022/week @ 2023-10-29 2209/week @ 2023-11-05 3845/week @ 2023-11-12 3386/week @ 2023-11-19 2755/week @ 2023-11-26 2681/week @ 2023-12-03 2206/week @ 2023-12-10 2737/week @ 2023-12-17 1356/week @ 2023-12-24 1710/week @ 2023-12-31 2601/week @ 2024-01-07 3000/week @ 2024-01-14 1831/week @ 2024-01-21 2746/week @ 2024-01-28 2975/week @ 2024-02-04

10,665 downloads per month
Used in 10 crates (8 directly)

MIT/Apache

1MB
19K SLoC

EIP-712 Derive Macro

This crate provides a derive macro Eip712 that is used to encode a rust struct into a payload hash, according to https://eips.ethereum.org/EIPS/eip-712

The trait used to derive the macro is found in ethers_core::transaction::eip712::Eip712 Both the derive macro and the trait must be in context when using

This derive macro requires the #[eip712] attributes to be included for specifying the domain separator used in encoding the hash.

NOTE: In addition to deriving Eip712 trait, the EthAbiType trait must also be derived. This allows the struct to be parsed into ethers_core::abi::Token for encoding.

Optional Eip712 Parameters

The only optional parameter is salt, which accepts a string that is hashed using keccak256 and stored as bytes.

Example Usage

use ethers_contract::EthAbiType;
use ethers_derive_eip712::*;
use ethers_core::types::{transaction::eip712::Eip712, H160};

#[derive(Debug, Eip712, EthAbiType)]
#[eip712(
    name = "Radicle",
    version = "1",
    chain_id = 1,
    verifying_contract = "0x0000000000000000000000000000000000000000"
    // salt is an optional parameter
    salt = "my-unique-spice"
)]
pub struct Puzzle {
    pub organization: H160,
    pub contributor: H160,
    pub commit: String,
    pub project: String,
}

let puzzle = Puzzle {
    organization: "0000000000000000000000000000000000000000"
        .parse::<H160>()
        .expect("failed to parse address"),
    contributor: "0000000000000000000000000000000000000000"
        .parse::<H160>()
        .expect("failed to parse address"),
    commit: "5693b7019eb3e4487a81273c6f5e1832d77acb53".to_string(),
    project: "radicle-reward".to_string(),
};

let hash = puzzle.encode_eip712().unwrap();

Limitations

At the moment, the derive macro does not recursively encode nested Eip712 structs.

There is an Inner helper attribute #[eip712] for fields that will eventually be used to determine if there is a nested eip712 struct. However, this work is not yet complete.

Dependencies

~12–24MB
~338K SLoC