tendermint_config/
priv_validator_key.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
//! Validator private keys

use std::{fs, path::Path};

use serde::{Deserialize, Serialize};
use tendermint::{
    account,
    private_key::PrivateKey,
    public_key::{PublicKey, TendermintKey},
};

use crate::{error::Error, prelude::*};

/// Validator private key
#[derive(Serialize, Deserialize)] // JSON custom serialization for priv_validator_key.json
pub struct PrivValidatorKey {
    /// Address
    pub address: account::Id,

    /// Public key
    pub pub_key: PublicKey,

    /// Private key
    pub priv_key: PrivateKey,
}

impl PrivValidatorKey {
    /// Parse `priv_validator_key.json`
    pub fn parse_json<T: AsRef<str>>(json_string: T) -> Result<Self, Error> {
        let result =
            serde_json::from_str::<Self>(json_string.as_ref()).map_err(Error::serde_json)?;

        // Validate that the parsed key type is usable as a consensus key
        TendermintKey::new_consensus_key(result.priv_key.public_key())
            .map_err(Error::tendermint)?;

        Ok(result)
    }

    /// Load `node_key.json` from a file
    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)
    }

    /// Get the consensus public key for this validator private key
    pub fn consensus_pubkey(&self) -> TendermintKey {
        TendermintKey::new_consensus_key(self.priv_key.public_key()).unwrap()
    }
}