Keystore Generation & Decryption

This is a functioning example of generating + encrypting a new keystore and decrypting a keystore using ethers-rs. This could particularly be helpful for applications that rely on local encrypted keys client side or server side.
This commit is contained in:
crypdough.eth 2023-01-23 13:26:17 -05:00 committed by GitHub
parent 0116130b87
commit c001406220
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 43 additions and 0 deletions

View File

@ -0,0 +1,43 @@
use ethers::prelude::*;
use ethers::core::rand::thread_rng;
use ethers::signers::{LocalWallet};
use dotenv::dotenv;
use eyre::Result;
#[tokio::main]
async fn main() -> Result<()>{
dotenv().ok();
let api_key = std::env::var("API_KEY").expect("expected environmental variable");
let encryption_path = std::env::var("ENCYPTION_PATH").expect("expected environmental variable");
let decryption_path = std::env::var("DECRYPTION_PATH").expect("expected environmental variable");
let password = std::env::var("PASSWORD").expect("expected environmental variable");
let ws = Ws::connect(api_key)
.await
.expect("no connection");
println!("Is Connected: {}", ws.ready());
//generates a brand new keystore with key
//name your keystore file, set the path, set the password
//*NOTE* -- pls don't store your passwords in a plain text file, this is just an example
let key_store = LocalWallet::new_keystore(encryption_path, &mut thread_rng(), &password, Some("my_encrypted_keys"))
.expect("key store fail");
let (your_signing_key, _) = key_store;
println!("your signing key is: {:?}", your_signing_key);
//decrypt your keystore given the filepath with the password you encrypted it with originally
let decrypt_key_store = LocalWallet::decrypt_keystore(decryption_path, &password)
.expect("decryption failed");
println!("your signing key is: {:?}", decrypt_key_store);
Ok(())
}