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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
use crate::{Error, Result};
pub use ed25519_dalek::{Keypair, PublicKey, SecretKey, KEYPAIR_LENGTH, PUBLIC_KEY_LENGTH};
use hex::{decode, encode};
use std::path::Path;
use tokio::fs;
const REWARD_PUBLIC_KEY_FILENAME: &str = "reward_public_key";
const REWARD_SECRET_KEY_FILENAME: &str = "reward_secret_key";
const NETWORK_KEYPAIR_FILENAME: &str = "network_keypair";
pub async fn store_network_keypair(
root_dir: &Path,
keypair_as_bytes: [u8; KEYPAIR_LENGTH],
) -> Result<()> {
let keypair_path = root_dir.join(NETWORK_KEYPAIR_FILENAME);
fs::write(keypair_path, encode(keypair_as_bytes)).await?;
Ok(())
}
pub async fn get_network_keypair(root_dir: &Path) -> Result<Option<Keypair>> {
let path = root_dir.join(NETWORK_KEYPAIR_FILENAME);
if !path.is_file() {
return Ok(None);
}
let keypair_hex_bytes = fs::read(path).await?;
let keypair_bytes = decode(keypair_hex_bytes).map_err(|err| {
Error::Logic(format!(
"Couldn't hex-decode network keypair bytes: {}",
err
))
})?;
let keypair = Keypair::from_bytes(&keypair_bytes)
.map_err(|_| Error::Logic("Config error: Invalid network keypair bytes".to_string()))?;
Ok(Some(keypair))
}
pub async fn store_new_reward_keypair(root_dir: &Path, keypair: &Keypair) -> Result<()> {
let secret_key_path = root_dir.join(REWARD_SECRET_KEY_FILENAME);
let public_key_path = root_dir.join(REWARD_PUBLIC_KEY_FILENAME);
fs::write(secret_key_path, encode(keypair.secret.to_bytes())).await?;
fs::write(public_key_path, encode(keypair.public.to_bytes())).await?;
Ok(())
}
pub async fn get_reward_pk(root_dir: &Path) -> Result<Option<PublicKey>> {
let path = root_dir.join(REWARD_PUBLIC_KEY_FILENAME);
if !path.is_file() {
return Ok(None);
}
let pk_hex_bytes = fs::read(path).await?;
let pk_bytes = decode(pk_hex_bytes).map_err(|err| {
Error::Logic(format!(
"Couldn't hex-decode Ed25519 public key bytes: {}",
err
))
})?;
let pk = PublicKey::from_bytes(&pk_bytes)
.map_err(|_| Error::Logic("Config error: Invalid Ed25519 public key bytes".to_string()))?;
Ok(Some(pk))
}
#[cfg(test)]
mod test {
use super::{
get_network_keypair, get_reward_pk, store_network_keypair, store_new_reward_keypair,
};
use anyhow::{anyhow, Result};
use rand::{distributions::Alphanumeric, rngs::OsRng, thread_rng, Rng};
use tempdir::TempDir;
#[tokio::test]
async fn pubkey_to_and_from_file() -> Result<()> {
let mut rng = OsRng;
let keypair = ed25519_dalek::Keypair::generate(&mut rng);
let root = create_temp_root()?;
let root_dir = root.path();
store_new_reward_keypair(root_dir, &keypair).await?;
let pk_result = get_reward_pk(root_dir).await?;
assert_eq!(pk_result, Some(keypair.public));
Ok(())
}
#[tokio::test]
async fn keypair_to_and_from_file() -> Result<()> {
let mut rng = OsRng;
let keypair = ed25519_dalek::Keypair::generate(&mut rng);
let root = create_temp_root()?;
let root_dir = root.path();
let keypair_result = get_network_keypair(root_dir).await?;
assert!(keypair_result.is_none());
store_network_keypair(root_dir, keypair.to_bytes()).await?;
let keypair_result = get_network_keypair(root_dir).await?;
if let Some(kp) = keypair_result {
assert_eq!(kp.public, keypair.public);
Ok(())
} else {
Err(anyhow!("Network keypair was not read from file"))
}
}
fn create_temp_root() -> Result<TempDir> {
let rand_dir_name: String = thread_rng().sample_iter(&Alphanumeric).take(10).collect();
TempDir::new(&rand_dir_name).map_err(|e| anyhow!("Failed to create temp dir: {}", e))
}
}