Available on crate feature
signed-message
only.Expand description
EIP-712 message signing and verification.
This module provides the SignedMessage
struct for signing and verifying messages according
to the EIP-712 standard.
Available functions for interacting with messages:
sign
: Signs a message using the EIP-712 standard.recover_signer_address
: Recovers the signer’s address from a signed message.verify
: Convenience wrapper overrecover_signer_address
to verify the signer’s address.
To use a Rust struct as a message, it must implement the ToSolStruct
trait.
Refer to the example below for more details.
§Example
use thegraph_core::signed_message::{sign, verify, ToSolStruct};
// Create a signer instance
let signer = thegraph_core::alloy::signers::local::PrivateKeySigner::random();
// Define the EIP-712 domain separator
const DOMAIN: Eip712Domain = eip712_domain! {
name: "Example domain",
version: "1",
chain_id: 1,
verifying_contract: address!("a83682bbe91c0d2d48a13fd751b2da8e989fe421"),
salt: b256!("66eb090e6dbb9668c7d32c0ee7ba5e8f08d84385804485d316dd5f5692273593"),
};
// Define a message struct
#[derive(Clone, Debug)]
struct Message {
addr: Address,
hash: [u8; 32],
}
// Define the message equivalent solidity struct
thegraph_core::alloy::sol! {
struct MessageSol {
address addr;
bytes32 hash;
}
}
// Implement the ToSolStruct trait for the message struct
impl ToSolStruct<MessageSol> for Message {
fn to_sol_struct(&self) -> MessageSol {
MessageSol {
addr: self.addr,
hash: self.hash.into(),
}
}
}
// Create a message instance with some data
let message = Message {
addr: address!("03f6d2a3d8c3413de72c193386f1894e1ddc2b6b"),
hash: *keccak256(b"Hello, world!"),
};
// Sign the message
let signed_message = sign(&signer, &DOMAIN, message).expect("sign_message failed");
// Verify the signed message
assert!(verify(&DOMAIN, &signed_message, &signer.address()).is_ok());
Structs§
- Message
Hash - Message hash according to EIP-712
hashStruct
. - Recover
Signer Error - Errors that can occur when recovering the signer’s address of a message.
- Signature
Bytes - The EIP-712 ECDSA signature bytes.
- Signed
Message - EIP-712 signed message
Enums§
- Signing
Error - Errors that can occur when signing a message.
- Verification
Error - Errors that can occur when verifying the signer’s address of a message.
Traits§
- ToSol
Struct - A conversion trait for converting a type into a solidity struct representation