eth_keystore

Function encrypt_key

Source
pub fn encrypt_key<P, R, B, S>(
    dir: P,
    rng: &mut R,
    pk: B,
    password: S,
    name: Option<&str>,
) -> Result<String, KeystoreError>
where P: AsRef<Path>, R: Rng + CryptoRng, B: AsRef<[u8]>, S: AsRef<[u8]>,
Expand description

Encrypts the given private key using the Scrypt password-based key derivation function, and stores it in the provided directory. On success, it returns the id (Uuid) generated for this keystore.

ยงExample

use eth_keystore::encrypt_key;
use rand::RngCore;
use std::path::Path;

let dir = Path::new("./keys");
let mut rng = rand::thread_rng();

// Construct a 32-byte random private key.
let mut private_key = vec![0u8; 32];
rng.fill_bytes(private_key.as_mut_slice());

// Since we specify a custom filename for the keystore, it will be stored in `$dir/my-key`
let name = encrypt_key(&dir, &mut rng, &private_key, "password_to_keystore", Some("my-key"))?;