tendermint_config/
node_key.rsuse std::{fs, path::Path};
use serde::{Deserialize, Serialize};
use tendermint::{node, private_key::PrivateKey, public_key::PublicKey};
use crate::{error::Error, prelude::*};
#[derive(Serialize, Deserialize)]
pub struct NodeKey {
pub priv_key: PrivateKey,
}
impl NodeKey {
pub fn parse_json<T: AsRef<str>>(json_string: T) -> Result<Self, Error> {
let res = serde_json::from_str(json_string.as_ref()).map_err(Error::serde_json)?;
Ok(res)
}
pub fn load_json_file<P>(path: &P) -> Result<Self, Error>
where
P: AsRef<Path>,
{
let json_string = fs::read_to_string(path)
.map_err(|e| Error::file_io(format!("{}", path.as_ref().display()), e))?;
Self::parse_json(json_string)
}
pub fn public_key(&self) -> PublicKey {
#[allow(unreachable_patterns)]
match &self.priv_key {
PrivateKey::Ed25519(signing_key) => PublicKey::Ed25519(signing_key.verification_key()),
_ => unreachable!(),
}
}
pub fn node_id(&self) -> node::Id {
#[allow(unreachable_patterns)]
match &self.public_key() {
PublicKey::Ed25519(pubkey) => node::Id::from(*pubkey),
_ => unreachable!(),
}
}
}