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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
// Copyright (c) The Diem Core Contributors
// SPDX-License-Identifier: Apache-2.0

use diem_client::AccountAddress;
use diem_config::config::{Peer, PeerRole};
use diem_crypto::{
    ed25519::Ed25519PrivateKey, x25519, x25519::PUBLIC_KEY_SIZE, Uniform, ValidCryptoMaterial,
    ValidCryptoMaterialStringExt,
};
use diem_global_constants::OWNER_ACCOUNT;
use diem_management::{
    config::ConfigPath, error::Error, secure_backend::ValidatorBackend, storage::StorageWrapper,
};
use diem_types::{account_address::from_identity_public_key, PeerId};
use rand::SeedableRng;
use serde::Serialize;
use std::{
    collections::{HashMap, HashSet},
    fs::{read, File},
    io::Write,
    path::{Path, PathBuf},
    str::FromStr,
};
use structopt::StructOpt;

#[derive(Debug, StructOpt)]
pub struct GenerateKey {
    /// Location to store the key
    #[structopt(long)]
    key_file: PathBuf,
    #[structopt(long)]
    key_type: KeyType,
    #[structopt(long)]
    encoding: EncodingType,
}

#[derive(Clone, Copy, Debug, StructOpt)]
pub enum KeyType {
    Ed25519,
    X25519,
}

impl FromStr for KeyType {
    type Err = &'static str;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        match s.to_lowercase().as_str() {
            "ed25519" => Ok(KeyType::Ed25519),
            "x25519" => Ok(KeyType::X25519),
            _ => Err("Invalid key type"),
        }
    }
}

#[derive(Clone, Copy, Debug, StructOpt)]
pub enum EncodingType {
    BCS,
    Hex,
    Base64,
}

impl FromStr for EncodingType {
    type Err = &'static str;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        match s.to_lowercase().as_str() {
            "hex" => Ok(EncodingType::Hex),
            "bcs" => Ok(EncodingType::BCS),
            "base64" => Ok(EncodingType::Base64),
            _ => Err("Invalid encoding type"),
        }
    }
}

impl GenerateKey {
    pub fn execute(self) -> Result<(), Error> {
        let ed25519_key = &generate_ed25519_key();
        match self.key_type {
            KeyType::X25519 => {
                let key = x25519::PrivateKey::from_ed25519_private_bytes(&ed25519_key.to_bytes())
                    .map_err(|err| Error::UnexpectedError(err.to_string()))?;
                save_key(
                    &key,
                    "x22519 PrivateKey",
                    self.key_file.as_path(),
                    self.encoding,
                )
            }
            KeyType::Ed25519 => save_key(
                ed25519_key,
                "ed22519 PrivateKey",
                self.key_file.as_path(),
                self.encoding,
            ),
        }
    }
}

#[derive(Debug, StructOpt)]
pub struct ExtractPeersFromKeys {
    #[structopt(long, parse(try_from_str = parse_public_keys_hex))]
    keys: HashSet<x25519::PublicKey>,
    #[structopt(long)]
    output_file: Option<PathBuf>,
}

fn parse_public_key_hex(src: &str) -> Result<x25519::PublicKey, Error> {
    let input = src.trim();
    static HEX_SIZE: usize = PUBLIC_KEY_SIZE * 2;
    if input.len() != HEX_SIZE {
        return Err(Error::CommandArgumentError(
            "Invalid public key length, must be 64 hex characters".to_string(),
        ));
    }
    x25519::PublicKey::from_encoded_string(input)
        .map_err(|err| Error::UnableToParse("Key", err.to_string()))
}

fn parse_public_keys_hex(src: &str) -> Result<HashSet<x25519::PublicKey>, Error> {
    let input = src.trim();

    let strings: Vec<_> = input.split(',').collect();
    let mut keys: HashSet<_> = HashSet::new();
    for str in strings {
        keys.insert(parse_public_key_hex(str)?);
    }

    Ok(keys)
}

impl ExtractPeersFromKeys {
    pub fn execute(&self) -> Result<HashMap<PeerId, Peer>, Error> {
        let map = self
            .keys
            .iter()
            .map(|key| peer_from_public_key(*key))
            .collect();
        if let Some(output_file) = self.output_file.as_ref() {
            save_to_yaml(output_file, "Peers", &map)?;
        }
        Ok(map)
    }
}

#[derive(Debug, StructOpt)]
pub struct ExtractPeerFromFile {
    /// Location to read the key
    #[structopt(long)]
    key_file: PathBuf,
    #[structopt(long)]
    output_file: Option<PathBuf>,
    #[structopt(long)]
    encoding: EncodingType,
}

impl ExtractPeerFromFile {
    pub fn execute(self) -> Result<HashMap<PeerId, Peer>, Error> {
        let private_key = load_key::<x25519::PrivateKey>(self.key_file, self.encoding)?;
        let (peer_id, peer) = peer_from_public_key(private_key.public_key());
        let mut map = HashMap::new();
        map.insert(peer_id, peer);

        if let Some(output_file) = self.output_file.as_ref() {
            save_to_yaml(output_file, "Peer", &map)?;
        }
        Ok(map)
    }
}

#[derive(Debug, StructOpt)]
pub struct ExtractPeerFromStorage {
    #[structopt(flatten)]
    config: ConfigPath,
    /// The keys name in storage
    #[structopt(long)]
    key_name: String,
    #[structopt(flatten)]
    validator_backend: ValidatorBackend,
    #[structopt(long)]
    output_file: Option<PathBuf>,
}

impl ExtractPeerFromStorage {
    pub fn execute(self) -> Result<HashMap<PeerId, Peer>, Error> {
        let config = self
            .config
            .load()?
            .override_validator_backend(&self.validator_backend.validator_backend)?;
        let key_name = Box::leak(self.key_name.into_boxed_str());
        let storage = config.validator_backend();
        let peer_id = storage.account_address(OWNER_ACCOUNT)?;
        let public_key = storage.x25519_public_from_private(key_name)?;
        let (_, peer) = peer_from_public_key(public_key);
        let mut map = HashMap::new();
        map.insert(peer_id, peer);

        if let Some(output_file) = self.output_file.as_ref() {
            save_to_yaml(output_file, "Peer", &map)?;
        }
        Ok(map)
    }
}

fn peer_from_public_key(public_key: x25519::PublicKey) -> (AccountAddress, Peer) {
    let peer_id = from_identity_public_key(public_key);
    let mut public_keys = HashSet::new();
    public_keys.insert(public_key);
    (
        peer_id,
        Peer::new(Vec::new(), public_keys, PeerRole::Downstream),
    )
}

fn generate_ed25519_key() -> Ed25519PrivateKey {
    let mut rng = rand::rngs::StdRng::from_entropy();
    Ed25519PrivateKey::generate(&mut rng)
}

#[derive(Debug, StructOpt)]
struct ExtractKey {
    #[structopt(flatten)]
    config: ConfigPath,
    /// The keys name in storage
    #[structopt(long)]
    key_name: String,
    /// Location to store the key
    #[structopt(long)]
    key_file: PathBuf,
    #[structopt(long)]
    encoding: Option<EncodingType>,
    #[structopt(flatten)]
    validator_backend: ValidatorBackend,
}

impl ExtractKey {
    fn save_key<
        Key: ValidCryptoMaterial,
        F: FnOnce(StorageWrapper, &'static str) -> Result<Key, Error>,
    >(
        &self,
        load_key: F,
    ) -> Result<(), Error> {
        let config = self
            .config
            .load()?
            .override_validator_backend(&self.validator_backend.validator_backend)?;
        let encoding = self.encoding.unwrap_or(EncodingType::BCS);
        let storage = config.validator_backend();
        let key_name = Box::leak(self.key_name.clone().into_boxed_str());
        let key = load_key(storage, key_name)?;
        save_key(&key, key_name, &self.key_file, encoding)
    }
}

#[derive(Debug, StructOpt)]
pub struct ExtractPublicKey {
    #[structopt(flatten)]
    extract_key: ExtractKey,
    #[structopt(long)]
    key_type: Option<KeyType>,
}

impl ExtractPublicKey {
    pub fn execute(self) -> Result<(), Error> {
        match self.key_type {
            Some(KeyType::X25519) => self
                .extract_key
                .save_key(|storage, key_name| storage.x25519_public_from_private(key_name)),
            _ => self
                .extract_key
                .save_key(|storage, key_name| storage.ed25519_public_from_private(key_name)),
        }
    }
}

#[derive(Debug, StructOpt)]
pub struct ExtractPrivateKey {
    #[structopt(flatten)]
    extract_key: ExtractKey,
    #[structopt(long)]
    key_type: Option<KeyType>,
}

impl ExtractPrivateKey {
    pub fn execute(self) -> Result<(), Error> {
        match self.key_type {
            Some(KeyType::X25519) => self
                .extract_key
                .save_key(|storage, key_name| storage.x25519_private(key_name)),
            _ => self
                .extract_key
                .save_key(|storage, key_name| storage.ed25519_private(key_name)),
        }
    }
}

/// Loads a key to a file hex string encoded
pub fn load_key<Key: ValidCryptoMaterial>(
    path: PathBuf,
    encoding: EncodingType,
) -> Result<Key, Error> {
    let data = read(&path).map_err(|err| {
        Error::UnableToReadFile(path.to_str().unwrap().to_string(), err.to_string())
    })?;

    match encoding {
        EncodingType::BCS => {
            bcs::from_bytes(&data).map_err(|err| Error::BCS("Key".to_string(), err))
        }
        EncodingType::Hex => {
            let hex_string = String::from_utf8(data).unwrap();
            Key::from_encoded_string(hex_string.trim())
                .map_err(|err| Error::UnableToParse("Key", err.to_string()))
        }
        EncodingType::Base64 => {
            let string = String::from_utf8(data).unwrap();
            let bytes = base64::decode(string.trim())
                .map_err(|err| Error::UnableToParse("Key", err.to_string()))?;
            Key::try_from(bytes.as_slice())
                .map_err(|err| Error::UnexpectedError(format!("Failed to parse key {}", err)))
        }
    }
}

/// Saves a key to a file encoded in a string
pub fn save_key<Key: ValidCryptoMaterial>(
    key: &Key,
    key_name: &'static str,
    path: &Path,
    encoding: EncodingType,
) -> Result<(), Error> {
    let encoded = match encoding {
        EncodingType::Hex => hex::encode_upper(key.to_bytes()).into_bytes(),
        EncodingType::BCS => {
            bcs::to_bytes(key).map_err(|err| Error::BCS(key_name.to_string(), err))?
        }
        EncodingType::Base64 => base64::encode(key.to_bytes()).into_bytes(),
    };

    write_file(path, key_name, encoded.as_slice())
}

fn save_to_yaml<T: Serialize>(path: &Path, input_name: &str, item: &T) -> Result<(), Error> {
    let yaml =
        serde_yaml::to_string(item).map_err(|err| Error::UnexpectedError(err.to_string()))?;
    write_file(path, input_name, yaml.as_bytes())
}

fn write_file(path: &Path, input_name: &str, contents: &[u8]) -> Result<(), Error> {
    let mut file = File::create(path).map_err(|e| Error::IO(input_name.to_string(), e))?;
    file.write_all(contents)
        .map_err(|e| Error::IO(input_name.to_string(), e))?;
    Ok(())
}