snarkos_aot/
key.rs

1use std::{path::PathBuf, str::FromStr};
2
3use anyhow::{bail, Result};
4use clap::Args;
5
6use crate::{Network, PrivateKey};
7
8/// A command line argument for specifying the account private key of the node.
9/// Done by a private key or a private key file.
10#[derive(Debug, Args, Clone)]
11#[group(required = true, multiple = false)]
12pub struct Key<N: Network> {
13    /// Specify the account private key of the node
14    #[clap(long = "private-key")]
15    pub private_key: Option<PrivateKey<N>>,
16    /// Specify the account private key of the node
17    #[clap(long = "private-key-file")]
18    pub private_key_file: Option<PathBuf>,
19}
20
21impl<N: Network> Key<N> {
22    pub fn try_get(self) -> Result<PrivateKey<N>> {
23        match (self.private_key, self.private_key_file) {
24            (Some(key), None) => Ok(key),
25            (None, Some(file)) => {
26                let raw = std::fs::read_to_string(file)?.trim().to_string();
27                Ok(PrivateKey::from_str(&raw)?)
28            }
29            // clap should make this unreachable, but serde might not
30            _ => bail!("Either `private-key` or `private-key-file` must be set"),
31        }
32    }
33}