Module signed_message

Source
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:

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§

MessageHash
Message hash according to EIP-712 hashStruct.
RecoverSignerError
Errors that can occur when recovering the signer’s address of a message.
SignatureBytes
The EIP-712 ECDSA signature bytes.
SignedMessage
EIP-712 signed message

Enums§

SigningError
Errors that can occur when signing a message.
VerificationError
Errors that can occur when verifying the signer’s address of a message.

Traits§

ToSolStruct
A conversion trait for converting a type into a solidity struct representation

Functions§

recover_signer_address
Recover the signer’s address an EIP-712 signed message
sign
Signs a message using the EIP-712 standard
verify
Verify the signer’s address of an EIP-712 signed message