2021-05-31 13:38:39 +00:00
|
|
|
// use the anyhow crate for easy idiomatic error handling
|
2020-09-20 15:17:02 +00:00
|
|
|
use anyhow::Result;
|
2021-05-31 13:38:39 +00:00
|
|
|
// 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
|
|
|
|
2021-05-31 13:38:39 +00:00
|
|
|
// Use the `tokio::main` macro for using async on the main function
|
2020-09-20 15:17:02 +00:00
|
|
|
#[tokio::main]
|
|
|
|
async fn main() -> Result<()> {
|
2021-05-31 13:38:39 +00:00
|
|
|
// Generate a random wallet
|
|
|
|
let wallet = LocalWallet::new(&mut thread_rng());
|
2021-06-01 14:36:02 +00:00
|
|
|
|
2021-05-31 13:38:39 +00:00
|
|
|
// Declare the message you want to sign.
|
2020-05-24 16:14:27 +00:00
|
|
|
let message = "Some data";
|
|
|
|
|
2021-05-31 13:38:39 +00:00
|
|
|
// sign message from your wallet and print out signature produced.
|
2020-09-20 15:17:02 +00:00
|
|
|
let signature = wallet.sign_message(message).await?;
|
2020-05-24 16:14:27 +00:00
|
|
|
println!("Produced signature {}", signature);
|
|
|
|
|
2021-05-31 13:38:39 +00:00
|
|
|
// verify the signature produced from your wallet.
|
2020-06-17 06:45:15 +00:00
|
|
|
signature.verify(message, wallet.address()).unwrap();
|
2020-06-17 06:38:04 +00:00
|
|
|
println!("Verified signature produced by {:?}!", wallet.address());
|
2020-09-20 15:17:02 +00:00
|
|
|
|
|
|
|
Ok(())
|
2020-05-24 16:14:27 +00:00
|
|
|
}
|