-
Notifications
You must be signed in to change notification settings - Fork 85
/
Copy pathgenerate.rs
115 lines (93 loc) · 3.2 KB
/
generate.rs
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
use clap::{arg, command};
use super::super::config::{
locator, network,
secret::{self, Secret},
};
use crate::{commands::global, print::Print};
#[derive(thiserror::Error, Debug)]
pub enum Error {
#[error(transparent)]
Config(#[from] locator::Error),
#[error(transparent)]
Secret(#[from] secret::Error),
#[error(transparent)]
Network(#[from] network::Error),
#[error("An identity with the name '{0}' already exists")]
IdentityAlreadyExists(String),
}
#[derive(Debug, clap::Parser, Clone)]
#[group(skip)]
#[allow(clippy::struct_excessive_bools)]
pub struct Cmd {
/// Name of identity
pub name: String,
/// Do not fund address
#[arg(long)]
pub no_fund: bool,
/// Optional seed to use when generating seed phrase.
/// Random otherwise.
#[arg(long, conflicts_with = "default_seed")]
pub seed: Option<String>,
/// Output the generated identity as a secret key
#[arg(long, short = 's')]
pub as_secret: bool,
#[command(flatten)]
pub config_locator: locator::Args,
/// When generating a secret key, which `hd_path` should be used from the original `seed_phrase`.
#[arg(long)]
pub hd_path: Option<usize>,
/// Generate the default seed phrase. Useful for testing.
/// Equivalent to --seed 0000000000000000
#[arg(long, short = 'd', conflicts_with = "seed")]
pub default_seed: bool,
#[command(flatten)]
pub network: network::Args,
/// Fund generated key pair
#[arg(long, default_value = "false")]
pub fund: bool,
/// Overwrite existing identity if it already exists.
#[arg(long)]
pub overwrite: bool,
}
impl Cmd {
pub async fn run(&self, global_args: &global::Args) -> Result<(), Error> {
let print = Print::new(global_args.quiet);
if self.config_locator.read_identity(&self.name).is_ok() {
if !self.overwrite {
return Err(Error::IdentityAlreadyExists(self.name.clone()));
}
print.exclaimln(format!("Overwriting identity '{}'", &self.name));
}
if !self.fund {
print.warnln(
"Behavior of `generate` will change in the \
future, and it will no longer fund by default. If you want to fund please \
provide `--fund` flag. If you don't need to fund your keys in the future, ignore this \
warning. It can be suppressed with -q flag.",
);
}
let seed_phrase = if self.default_seed {
Secret::test_seed_phrase()
} else {
Secret::from_seed(self.seed.as_deref())
}?;
let secret = if self.as_secret {
seed_phrase.private_key(self.hd_path)?.into()
} else {
seed_phrase
};
self.config_locator.write_identity(&self.name, &secret)?;
if !self.no_fund {
let addr = secret.public_key(self.hd_path)?;
let network = self.network.get(&self.config_locator)?;
network
.fund_address(&addr)
.await
.map_err(|e| {
tracing::warn!("fund_address failed: {e}");
})
.unwrap_or_default();
}
Ok(())
}
}