EIP-712 Rust Utils
Overview
The eip_712_utils crate is a Rust library providing utilities for EIP-712 message signing. This library is designed for simple creation, signing, and validation of EIP-712 structured data, particularly for NFTs (Non-Fungible Tokens).
Features
- Generate EIP-712 compliant structured data
- Hash structured data as per EIP-712 specifications
- Sign EIP-712 structured data
Installation
cargo add eip_712_utils
Usage
Generate and Sign Structured Data
API Reference
Modules
eip712
This module provides the core functionality for EIP-712 encoding and hashing.
-
EIP712
- Represents the EIP-712 structured data.
- Methods:
builder() -> EIP712Builder: Returns a new builder for EIP712.domain(name, version, chain_id, verifying_contract): Sets the domain parameters.custom_field((field_name, fields)): Adds custom fields to the EIP-712 structure. Here you can use your NFT specific Data. Imporant to note that this field will be used as the root node of the hashed data. If it's not provided, the root node used will beEIP712Domain.message(message): Sets the message data. Simplest way is to usejson!fromserdeas shown in the example.build() -> EIP712: Builds the EIP-712 structured data.
-
EIP712Domain
- Represents the EIP-712 domain.
- Fields:
name,version,chain_id,verifying_contract.
-
FieldType
- Represents a field type in the EIP-712 structure.
- Fields:
name,type_.
-
MessageTypes
- Represents the types of messages in the EIP-712 structure.
-
hash_structured_data(data: EIP712) -> Result<H256, Error>
- Hashes the EIP-712 structured data.
- Parameters:
data- The EIP-712 structured data. - Returns: The hash of the structured data.
nft_helpers
This module contains helper functions specifically for NFT-related operations.
- hash_structured_data_string(data: String) -> Result<H256, Error>
- Hashes EIP-712 structured data provided as a JSON string.
- Parameters:
data- The JSON string representing the EIP-712 structured data. - Returns: The hash of the structured data.
signing
This module provides utilities for signing EIP-712 messages.
- sign_message(message: &Message, private_key: &SecretKey) -> Signature
- Signs the given EIP-712 message.
- Parameters:
message- The EIP-712 message to sign,private_key- The private key used to sign the message. - Returns: The signature of the message.
Running Tests
Run the tests with:
Contributing
Contributions are welcome! Please open an issue or submit a pull request on GitHub.
License
This project is licensed under the MIT License.