use namada_core::key::{SchemeType, common};
use rand::{CryptoRng, Rng};
use serde::{Deserialize, Serialize};
use thiserror::Error;
use zeroize::Zeroizing;
use crate::StoredKeypair;
#[derive(Error, Debug)]
pub enum ReadError {
#[error("Failed decoding the wallet store: {0}")]
Decode(toml::de::Error),
#[error("Failed to read the wallet store from {0}: {1}")]
ReadWallet(String, String),
#[error("Failed to write the wallet store: {0}")]
StoreNewWallet(String),
#[error("Failed to decode a key: {0}")]
Decryption(crate::keys::DecryptionError),
}
pub struct ValidatorWallet {
pub store: ValidatorStore,
pub consensus_key: common::SecretKey,
pub eth_cold_key: common::SecretKey,
pub eth_hot_key: common::SecretKey,
pub tendermint_node_key: common::SecretKey,
}
#[derive(Serialize, Deserialize, Debug)]
pub struct ValidatorStore {
pub consensus_key: StoredKeypair<common::SecretKey>,
pub eth_cold_key: StoredKeypair<common::SecretKey>,
pub tendermint_node_key: StoredKeypair<common::SecretKey>,
pub validator_keys: crate::ValidatorKeys,
}
impl ValidatorStore {
pub fn decode(data: &str) -> Result<Self, toml::de::Error> {
toml::from_str(data)
}
pub fn encode(&self) -> String {
toml::to_string(self).expect(
"Serializing of validator pre-genesis wallet shouldn't fail",
)
}
}
pub fn gen_key_to_store(
scheme: SchemeType,
password: Option<Zeroizing<String>>,
rng: &mut (impl CryptoRng + Rng),
) -> (StoredKeypair<common::SecretKey>, common::SecretKey) {
let sk = crate::gen_secret_key(scheme, rng);
StoredKeypair::new(sk, password)
}
impl From<crate::keys::DecryptionError> for ReadError {
fn from(err: crate::keys::DecryptionError) -> Self {
ReadError::Decryption(err)
}
}