Crates.io | eip_712_utils |
lib.rs | eip_712_utils |
version | 0.1.0 |
source | src |
created_at | 2024-06-21 10:18:45.64148 |
updated_at | 2024-06-21 10:18:45.64148 |
description | A Rust library providing utilities for EIP-712 message signing |
homepage | https://github.com/Aion-Studio/eip-712-utils |
repository | https://github.com/Aion-Studio/eip-712-utils |
max_upload_size | |
id | 1279367 |
size | 40,550 |
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).
Generate EIP-712 compliant structured data
Hash structured data as per EIP-712 specifications
Sign EIP-712 structured data
cargo add eip_712_utils
#[cfg(test)]
mod tests {
use rustc_hex::ToHex;
use serde_json::json;
use super::*;
#[test]
fn generate_hash_and_sign_it() {
let name = "AionRisingNFTs";
let version = "0.0.1";
let chain_id = "0x7A69";
let verifying_contract = "0x037eDa3aDB1198021A9b2e88C22B464fD38db3f3";
let token_id = "0x1";
let amount = "0x1";
let to = "0x7FA9385bE102ac3EAc297483Dd6233D62b3e1496";
let nonce = "0x1";
let custom_fields = vec![
FieldType {
name: "tokenId".to_string(),
type_: "uint256".to_string(),
},
FieldType {
name: "amount".to_string(),
type_: "uint256".to_string(),
},
FieldType {
name: "to".to_string(),
type_: "address".to_string(),
},
FieldType {
name: "nonce".to_string(),
type_: "uint256".to_string(),
},
];
let data: EIP712 = EIP712::builder()
.domain(name, version, chain_id, verifying_contract)
.custom_field(("NFTData".to_string(), custom_fields))
.message(json!({
"tokenId": token_id,
"amount": amount,
"to": to,
"nonce": nonce
}))
.build();
let hashed_structured_data = hash_structured_data(data).unwrap().to_hex::<String>();
let private_key = 659918_u32;
let signature = sign_message(&hashed_structured_data, private_key);
let expected_signature = "b9c658f86d985ad0502584c70ea520cf68523e4013786f83f216de093ef9467e453d27fe627278ab0c8425906843a706f66a9c3120b37e88ac722aa217a04fcf1b";
assert_eq!(signature, expected_signature);
}
}
eip712
This module provides the core functionality for EIP-712 encoding and hashing.
EIP712
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 be EIP712Domain
.message(message)
: Sets the message data. Simplest way is to use json!
from serde
as shown in the example.build() -> EIP712
: Builds the EIP-712 structured data.EIP712Domain
name
, version
, chain_id
, verifying_contract
.FieldType
name
, type_
.MessageTypes
hash_structured_data(data: EIP712) -> Result<H256, Error>
data
- The EIP-712 structured data.nft_helpers
This module contains helper functions specifically for NFT-related operations.
data
- The JSON string representing the EIP-712 structured data.signing
This module provides utilities for signing EIP-712 messages.
message
- The EIP-712 message to sign, private_key
- The private key used to sign the message.Run the tests with:
cargo test
Contributions are welcome! Please open an issue or submit a pull request on GitHub.
This project is licensed under the MIT License.