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