f30271ee9e
* Add test to understand flow of key storage * First commit * Committing to save trait stuff * Working naive design * Add keystore struct * Move keystore files into their own module * Add serde (de)serialize_with magic * Add keystore test * Fix tests * Add comments and minor fixes * Pass optional params to `to_keystore` function * Add `path` field to keystore * Add function to read Keystore from file * Add test vectors and fix Version serialization * Checksum params is empty object * Add public key to Keystore * Add function for saving keystore into file * Deleted account_manager main.rs * Move keystore module to validator_client * Add save_keystore method to validator_directory * Add load_keystore function. Minor refactorings * Fixed dependencies * Address some review comments * Add Password newtype; derive Zeroize * Fix test * Move keystore into own crate * Remove padding * Add error enum, zeroize more things * Fix comment * Add keystore builder * Remove keystore stuff from val client * Add more tests, comments * Add more comments, test vectors * Progress on improving JSON validation * More JSON verification * Start moving JSON into own mod * Remove old code * Add more tests, reader/writers * Tidy * Move keystore into own file * Move more logic into keystore file * Tidy * Tidy * Allow for odd-character hex * Add more json missing field checks * Use scrypt by default * Tidy, address comments * Test path and uuid in vectors * Fix comment * Add checks for kdf params * Enforce empty kdf message * Expose json_keystore mod * Split out encrypt/decrypt * Replace some password usage with slice * Expose PlainText struct * Expose consts, remove Password * Expose SALT_SIZE * Move dbg assert statement * Fix dodgy json test * Protect against n == 1 * Return error if n is not power of 2 * Add dklen checks * Add note about panics Co-authored-by: pawan <pawandhananjay@gmail.com>
109 lines
2.6 KiB
Rust
109 lines
2.6 KiB
Rust
#![cfg(test)]
|
|
|
|
use bls::Keypair;
|
|
use eth2_keystore::{Error, Keystore, KeystoreBuilder};
|
|
use std::fs::OpenOptions;
|
|
use tempfile::tempdir;
|
|
|
|
const GOOD_PASSWORD: &[u8] = &[42, 42, 42];
|
|
const BAD_PASSWORD: &[u8] = &[43, 43, 43];
|
|
|
|
#[test]
|
|
fn empty_password() {
|
|
assert_eq!(
|
|
KeystoreBuilder::new(&Keypair::random(), "".as_bytes(), "".into())
|
|
.err()
|
|
.unwrap(),
|
|
Error::EmptyPassword
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn string_round_trip() {
|
|
let keypair = Keypair::random();
|
|
|
|
let keystore = KeystoreBuilder::new(&keypair, GOOD_PASSWORD, "".into())
|
|
.unwrap()
|
|
.build()
|
|
.unwrap();
|
|
|
|
let json = keystore.to_json_string().unwrap();
|
|
let decoded = Keystore::from_json_str(&json).unwrap();
|
|
|
|
assert_eq!(
|
|
decoded.decrypt_keypair(BAD_PASSWORD).err().unwrap(),
|
|
Error::InvalidPassword,
|
|
"should not decrypt with bad password"
|
|
);
|
|
|
|
assert_eq!(
|
|
decoded.decrypt_keypair(GOOD_PASSWORD).unwrap(),
|
|
keypair,
|
|
"should decrypt with good password"
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn file() {
|
|
let keypair = Keypair::random();
|
|
let dir = tempdir().unwrap();
|
|
let path = dir.path().join("keystore.json");
|
|
|
|
let get_file = || {
|
|
OpenOptions::new()
|
|
.write(true)
|
|
.read(true)
|
|
.create(true)
|
|
.open(path.clone())
|
|
.expect("should create file")
|
|
};
|
|
|
|
let keystore = KeystoreBuilder::new(&keypair, GOOD_PASSWORD, "".into())
|
|
.unwrap()
|
|
.build()
|
|
.unwrap();
|
|
|
|
keystore
|
|
.to_json_writer(&mut get_file())
|
|
.expect("should write to file");
|
|
|
|
let decoded = Keystore::from_json_reader(&mut get_file()).expect("should read from file");
|
|
|
|
assert_eq!(
|
|
decoded.decrypt_keypair(BAD_PASSWORD).err().unwrap(),
|
|
Error::InvalidPassword,
|
|
"should not decrypt with bad password"
|
|
);
|
|
|
|
assert_eq!(
|
|
decoded.decrypt_keypair(GOOD_PASSWORD).unwrap(),
|
|
keypair,
|
|
"should decrypt with good password"
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn scrypt_params() {
|
|
let keypair = Keypair::random();
|
|
|
|
let keystore = KeystoreBuilder::new(&keypair, GOOD_PASSWORD, "".into())
|
|
.unwrap()
|
|
.build()
|
|
.unwrap();
|
|
|
|
let json = keystore.to_json_string().unwrap();
|
|
let decoded = Keystore::from_json_str(&json).unwrap();
|
|
|
|
assert_eq!(
|
|
decoded.decrypt_keypair(BAD_PASSWORD).err().unwrap(),
|
|
Error::InvalidPassword,
|
|
"should not decrypt with bad password"
|
|
);
|
|
|
|
assert_eq!(
|
|
decoded.decrypt_keypair(GOOD_PASSWORD).unwrap(),
|
|
keypair,
|
|
"should decrypt with good password"
|
|
);
|
|
}
|