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
56
57
58
use crate::{
error::{Error, Kind},
node,
private_key::PrivateKey,
public_key::PublicKey,
};
use anomaly::format_err;
use serde::{Deserialize, Serialize};
use std::{fs, path::Path};
#[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> {
Ok(serde_json::from_str(json_string.as_ref())?)
}
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| {
format_err!(
Kind::Parse,
"couldn't open {}: {}",
path.as_ref().display(),
e
)
})?;
Self::parse_json(json_string)
}
pub fn public_key(&self) -> PublicKey {
match &self.priv_key {
PrivateKey::Ed25519(keypair) => keypair.public.into(),
}
}
pub fn node_id(&self) -> node::Id {
#[allow(unreachable_patterns)]
match &self.public_key() {
PublicKey::Ed25519(pubkey) => node::Id::from(*pubkey),
_ => unreachable!(),
}
}
}