ethers-rs/examples/sign.rs

27 lines
909 B
Rust
Raw Permalink Normal View History

// use the eyre crate for easy idiomatic error handling
use eyre::Result;
// use the ethers_core rand for rng
use ethers_core::rand::thread_rng;
// use the ethers_signers crate to manage LocalWallet and Signer
use ethers_signers::{LocalWallet, Signer};
2020-05-24 16:14:27 +00:00
// Use the `tokio::main` macro for using async on the main function
#[tokio::main]
async fn main() -> Result<()> {
// Generate a random wallet
let wallet = LocalWallet::new(&mut thread_rng());
// Declare the message you want to sign.
2020-05-24 16:14:27 +00:00
let message = "Some data";
// sign message from your wallet and print out signature produced.
let signature = wallet.sign_message(message).await?;
println!("Produced signature {signature}");
2020-05-24 16:14:27 +00:00
// verify the signature produced from your wallet.
signature.verify(message, wallet.address()).unwrap();
println!("Verified signature produced by {:?}!", wallet.address());
Ok(())
2020-05-24 16:14:27 +00:00
}