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
use crate::cli::pcap::PcapDumpOpts;
use crate::cli::unit::UnitOpts;
use crate::cli::vanity::VanityOpts;
use crate::cli::wallet::WalletOpts;
use crate::debug::parse_pcap_log_file_to_csv;
use crate::node::node_with_autodiscovery;
use address::AddressOpts;
use anyhow::anyhow;
use clap::Clap;
use phrase::PhraseOpts;
use private::PrivateOpts;
use public::PublicOpts;
use seed::SeedOpts;
use std::io;
use std::io::Read;
use std::path::PathBuf;
use std::str::FromStr;
use tracing::Level;
use tracing_subscriber::EnvFilter;
mod address;
mod pcap;
mod phrase;
mod private;
mod public;
mod seed;
mod unit;
mod vanity;
mod wallet;
#[derive(Clap)]
#[clap(author, about, version)]
struct Opts {
#[clap(subcommand)]
command: Command,
#[clap(long)]
no_color: bool,
#[clap(long)]
log_level: Option<Level>,
}
#[derive(Clap)]
enum Command {
Node(NodeOpts),
Unit(UnitOpts),
Wallet(WalletOpts),
Phrase(PhraseOpts),
Seed(SeedOpts),
Private(PrivateOpts),
Public(PublicOpts),
Address(AddressOpts),
Vanity(VanityOpts),
Pcap(PcapDumpOpts),
Debug(DebugOpts),
}
#[derive(Clap)]
struct NodeOpts {
#[clap(short, long)]
override_peers: Option<Vec<String>>,
}
#[derive(Clap)]
struct DebugOpts {
#[clap(subcommand)]
command: DebugCommand,
}
#[derive(Clap)]
enum DebugCommand {
PcapLogToCSV(PcapLogToCsvArgs),
}
#[derive(Clap)]
struct PcapLogToCsvArgs {
src: PathBuf,
dst: PathBuf,
}
pub async fn run() -> anyhow::Result<()> {
let opts = Opts::parse();
let mut filter = EnvFilter::from_default_env();
if let Some(level) = opts.log_level {
filter = filter.add_directive(level.into());
}
let subscriber = tracing_subscriber::fmt::Subscriber::builder()
.with_env_filter(filter)
.with_ansi(!opts.no_color)
.finish();
tracing::subscriber::set_global_default(subscriber).expect("Could not initialize logger");
match opts.command {
#[cfg(feature = "node")]
Command::Node(o) => node_with_autodiscovery(o.override_peers).await,
#[cfg(not(feature = "node"))]
Command::Node(_) => panic!("Compile with the `node` feature to enable this."),
#[cfg(feature = "pcap")]
Command::Pcap(o) => o.handle().await,
#[cfg(not(feature = "pcap"))]
Command::Pcap(o) => panic!("Compile with the `pcap` feature to enable this."),
Command::Debug(debug) => match debug.command {
DebugCommand::PcapLogToCSV(huh) => parse_pcap_log_file_to_csv(&huh.src, &huh.dst),
},
Command::Wallet(wallet) => wallet.handle().await,
Command::Seed(seed) => seed.handle(),
Command::Private(private) => private.handle(),
Command::Public(public) => public.handle(),
Command::Phrase(phrase) => phrase.handle(),
Command::Address(address) => address.handle(),
Command::Unit(unit) => unit.handle(),
Command::Vanity(vanity) => vanity.handle().await,
}
}
#[derive(Copy, Clone)]
enum StringOrStdin<T>
where
T: FromStr,
<T as FromStr>::Err: std::fmt::Debug,
{
String(T),
Stdin,
}
impl<T> StringOrStdin<T>
where
T: FromStr,
<T as FromStr>::Err: std::fmt::Debug,
{
pub fn resolve(self) -> anyhow::Result<T>
where
T: FromStr,
<T as FromStr>::Err: std::fmt::Debug,
{
match self {
StringOrStdin::String(t) => Ok(t),
StringOrStdin::Stdin => {
let mut buffer = String::new();
io::stdin().read_to_string(&mut buffer)?;
Ok(T::from_str(buffer.trim())
.map_err(|e| anyhow!("Conversion from string failed: {:?}", e))?)
}
}
}
}
impl<T> FromStr for StringOrStdin<T>
where
T: FromStr,
<T as FromStr>::Err: std::fmt::Debug,
{
type Err = anyhow::Error;
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s.as_ref() {
"-" => Ok(StringOrStdin::Stdin),
x => match T::from_str(x) {
Ok(x) => Ok(StringOrStdin::String(x)),
Err(e) => Err(anyhow!("Could not parse string: {:?}", e)),
},
}
}
}