tendermint_config/
node_key.rs1use std::{fs, path::Path};
4
5use serde::{Deserialize, Serialize};
6use tendermint::{node, private_key::PrivateKey, public_key::PublicKey};
7
8use crate::{error::Error, prelude::*};
9
10#[derive(Serialize, Deserialize)]
12pub struct NodeKey {
13 pub priv_key: PrivateKey,
15}
16
17impl NodeKey {
18 pub fn parse_json<T: AsRef<str>>(json_string: T) -> Result<Self, Error> {
20 let res = serde_json::from_str(json_string.as_ref()).map_err(Error::serde_json)?;
21 Ok(res)
22 }
23
24 pub fn load_json_file<P>(path: &P) -> Result<Self, Error>
26 where
27 P: AsRef<Path>,
28 {
29 let json_string = fs::read_to_string(path)
30 .map_err(|e| Error::file_io(format!("{}", path.as_ref().display()), e))?;
31
32 Self::parse_json(json_string)
33 }
34
35 pub fn public_key(&self) -> PublicKey {
37 #[allow(unreachable_patterns)]
38 match &self.priv_key {
39 PrivateKey::Ed25519(signing_key) => PublicKey::Ed25519(signing_key.verification_key()),
40 _ => unreachable!(),
41 }
42 }
43
44 pub fn node_id(&self) -> node::Id {
46 #[allow(unreachable_patterns)]
47 match &self.public_key() {
48 PublicKey::Ed25519(pubkey) => node::Id::from(*pubkey),
49 _ => unreachable!(),
50 }
51 }
52}