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
use anyhow::Result;
use penumbra_custody::threshold::Terminal;

use crate::{
    config::{CustodyConfig, GovernanceCustodyConfig},
    terminal::ActualTerminal,
    App,
};

#[derive(Debug, clap::Subcommand)]
pub enum ThresholdCmd {
    /// Contribute to signing a transaction with threshold custody
    Sign,
}

impl ThresholdCmd {
    pub fn offline(&self) -> bool {
        match self {
            ThresholdCmd::Sign => true,
        }
    }

    #[tracing::instrument(skip(self, app))]
    pub async fn exec(&self, app: &mut App) -> Result<()> {
        let config = match app.config.custody.clone() {
            CustodyConfig::Threshold(config) => Some(config),
            CustodyConfig::Encrypted(config) => {
                let password = ActualTerminal.get_password().await?;
                config.convert_to_threshold(&password)?
            }
            _ => None, // If not threshold, we can't sign using threshold config
        };
        let governance_config = match &app.config.governance_custody {
            Some(GovernanceCustodyConfig::Threshold(governance_config)) => {
                Some(governance_config.clone())
            }
            None => config.clone(), // If no governance config, use regular one
            _ => None,              // If not threshold, we can't sign using governance config
        };
        match self {
            ThresholdCmd::Sign => {
                penumbra_custody::threshold::follow(
                    config.as_ref(),
                    governance_config.as_ref(),
                    &ActualTerminal,
                )
                .await
            }
        }
    }
}