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
use crate::commands::config::secret::Secret;

use super::super::{locator, secret};
use clap::arg;

#[derive(thiserror::Error, Debug)]
pub enum Error {
    #[error(transparent)]
    Config(#[from] locator::Error),

    #[error(transparent)]
    Secret(#[from] secret::Error),

    #[error(transparent)]
    StrKey(#[from] stellar_strkey::DecodeError),
}

#[derive(Debug, clap::Parser, Clone)]
#[group(skip)]
pub struct Cmd {
    /// Name of identity to lookup, default test identity used if not provided
    pub name: Option<String>,

    /// If identity is a seed phrase use this hd path, default is 0
    #[arg(long)]
    pub hd_path: Option<usize>,

    #[command(flatten)]
    pub locator: locator::Args,
}

impl Cmd {
    pub fn run(&self) -> Result<(), Error> {
        println!("{}", self.public_key()?);
        Ok(())
    }

    pub fn private_key(&self) -> Result<ed25519_dalek::SigningKey, Error> {
        Ok(if let Some(name) = &self.name {
            self.locator.read_identity(name)?
        } else {
            Secret::test_seed_phrase()?
        }
        .key_pair(self.hd_path)?)
    }

    pub fn public_key(&self) -> Result<stellar_strkey::ed25519::PublicKey, Error> {
        if let Some(Ok(key)) = self
            .name
            .as_deref()
            .map(stellar_strkey::ed25519::PublicKey::from_string)
        {
            Ok(key)
        } else {
            Ok(stellar_strkey::ed25519::PublicKey::from_payload(
                self.private_key()?.verifying_key().as_bytes(),
            )?)
        }
    }
}