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
// Copyright (c) The Diem Core Contributors
// SPDX-License-Identifier: Apache-2.0

use diem_crypto::{ed25519::Ed25519PublicKey, x25519};

use diem_management::{config::ConfigPath, error::Error, secure_backend::ValidatorBackend};
use diem_types::{account_address::AccountAddress, waypoint::Waypoint};
use structopt::StructOpt;

#[derive(Debug, StructOpt)]
pub struct PrintAccount {
    #[structopt(flatten)]
    config: ConfigPath,
    /// The account name in storage
    #[structopt(long)]
    account_name: String,
    #[structopt(flatten)]
    validator_backend: ValidatorBackend,
}

impl PrintAccount {
    pub fn execute(self) -> Result<AccountAddress, Error> {
        let config = self
            .config
            .load()?
            .override_validator_backend(&self.validator_backend.validator_backend)?;

        let storage = config.validator_backend();
        let account_name = Box::leak(self.account_name.into_boxed_str());
        storage.account_address(account_name)
    }
}

#[derive(Debug, StructOpt)]
pub struct PrintKey {
    #[structopt(flatten)]
    config: ConfigPath,
    /// The key name in storage
    #[structopt(long)]
    key_name: String,
    #[structopt(flatten)]
    validator_backend: ValidatorBackend,
}

impl PrintKey {
    pub fn execute(self) -> Result<Ed25519PublicKey, Error> {
        let config = self
            .config
            .load()?
            .override_validator_backend(&self.validator_backend.validator_backend)?;

        let storage = config.validator_backend();
        let key_name = Box::leak(self.key_name.into_boxed_str());
        storage.ed25519_public_from_private(key_name)
    }
}

#[derive(Debug, StructOpt)]
pub struct PrintXKey {
    #[structopt(flatten)]
    config: ConfigPath,
    /// The key name in storage
    #[structopt(long)]
    key_name: String,
    #[structopt(flatten)]
    validator_backend: ValidatorBackend,
}

impl PrintXKey {
    pub fn execute(self) -> Result<x25519::PublicKey, Error> {
        let config = self
            .config
            .load()?
            .override_validator_backend(&self.validator_backend.validator_backend)?;

        let storage = config.validator_backend();
        let key_name = Box::leak(self.key_name.into_boxed_str());
        storage.x25519_public_from_private(key_name)
    }
}

#[derive(Debug, StructOpt)]
pub struct PrintWaypoint {
    #[structopt(flatten)]
    config: ConfigPath,
    /// The waypoint name in storage
    #[structopt(long)]
    waypoint_name: String,
    #[structopt(flatten)]
    validator_backend: ValidatorBackend,
}

impl PrintWaypoint {
    pub fn execute(self) -> Result<Waypoint, Error> {
        let config = self
            .config
            .load()?
            .override_validator_backend(&self.validator_backend.validator_backend)?;

        let storage = config.validator_backend();
        let waypoint_name = Box::leak(self.waypoint_name.into_boxed_str());
        storage.waypoint(waypoint_name)
    }
}