multiversx_sdk/
validator.rs

1use core::str;
2
3use anyhow::Result;
4use multiversx_chain_core::types::BLSKey;
5
6#[derive(Clone, Debug)]
7pub struct Validator {
8    pub private_key: Vec<u8>,
9    pub public_key: BLSKey,
10}
11
12impl Validator {
13    pub fn from_pem_file(file_path: &str) -> Result<Self> {
14        let contents = std::fs::read_to_string(file_path)?;
15        Self::from_pem_file_contents(contents)
16    }
17
18    pub fn from_pem_file_contents(contents: String) -> Result<Self> {
19        let pem = pem::parse(contents).expect("Failed to parse PEM file");
20        let public_key_str = pem
21            .tag()
22            .rsplit(' ')
23            .next()
24            .expect("Failed to extract public key from PEM file");
25
26        let public_key = hex::decode(public_key_str)?;
27        let private_key = pem.contents();
28
29        Ok(Validator {
30            private_key: private_key.to_vec(),
31            public_key: BLSKey::from_vec(public_key).expect("bad public key length"),
32        })
33    }
34}