penumbra_custody/
request.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
use penumbra_governance::ValidatorVoteBody;
use penumbra_proto::{custody::v1 as pb, DomainType};
use penumbra_stake::validator::Validator;
use penumbra_transaction::TransactionPlan;

use crate::PreAuthorization;

/// A transaction authorization request submitted to a custody service for approval.
#[derive(Debug, Clone)]
pub struct AuthorizeRequest {
    /// The transaction plan to authorize.
    pub plan: TransactionPlan,
    /// Optionally, pre-authorization data, if required by the custodian.
    pub pre_authorizations: Vec<PreAuthorization>,
}

impl DomainType for AuthorizeRequest {
    type Proto = pb::AuthorizeRequest;
}

impl TryFrom<pb::AuthorizeRequest> for AuthorizeRequest {
    type Error = anyhow::Error;
    fn try_from(value: pb::AuthorizeRequest) -> Result<Self, Self::Error> {
        Ok(Self {
            plan: value
                .plan
                .ok_or_else(|| anyhow::anyhow!("missing plan"))?
                .try_into()?,
            pre_authorizations: value
                .pre_authorizations
                .into_iter()
                .map(TryInto::try_into)
                .collect::<Result<Vec<_>, _>>()?,
        })
    }
}

impl From<AuthorizeRequest> for pb::AuthorizeRequest {
    fn from(value: AuthorizeRequest) -> pb::AuthorizeRequest {
        Self {
            plan: Some(value.plan.into()),
            pre_authorizations: value
                .pre_authorizations
                .into_iter()
                .map(Into::into)
                .collect(),
        }
    }
}

/// A validator definition authorization request submitted to a custody service for approval.
#[derive(Debug, Clone)]
pub struct AuthorizeValidatorDefinitionRequest {
    /// The validator definition to authorize.
    pub validator_definition: Validator,
    /// Optionally, pre-authorization data, if required by the custodian.
    pub pre_authorizations: Vec<PreAuthorization>,
}

impl DomainType for AuthorizeValidatorDefinitionRequest {
    type Proto = pb::AuthorizeValidatorDefinitionRequest;
}

impl TryFrom<pb::AuthorizeValidatorDefinitionRequest> for AuthorizeValidatorDefinitionRequest {
    type Error = anyhow::Error;
    fn try_from(value: pb::AuthorizeValidatorDefinitionRequest) -> Result<Self, Self::Error> {
        Ok(Self {
            validator_definition: value
                .validator_definition
                .ok_or_else(|| anyhow::anyhow!("missing validator definition"))?
                .try_into()?,
            pre_authorizations: value
                .pre_authorizations
                .into_iter()
                .map(TryInto::try_into)
                .collect::<Result<Vec<_>, _>>()?,
        })
    }
}

impl From<AuthorizeValidatorDefinitionRequest> for pb::AuthorizeValidatorDefinitionRequest {
    fn from(value: AuthorizeValidatorDefinitionRequest) -> pb::AuthorizeValidatorDefinitionRequest {
        Self {
            validator_definition: Some(value.validator_definition.into()),
            pre_authorizations: value
                .pre_authorizations
                .into_iter()
                .map(Into::into)
                .collect(),
        }
    }
}

/// A validator vote authorization request submitted to a custody service for approval.
#[derive(Debug, Clone)]
pub struct AuthorizeValidatorVoteRequest {
    /// The transaction plan to authorize.
    pub validator_vote: ValidatorVoteBody,
    /// Optionally, pre-authorization data, if required by the custodian.
    pub pre_authorizations: Vec<PreAuthorization>,
}

impl DomainType for AuthorizeValidatorVoteRequest {
    type Proto = pb::AuthorizeValidatorVoteRequest;
}

impl TryFrom<pb::AuthorizeValidatorVoteRequest> for AuthorizeValidatorVoteRequest {
    type Error = anyhow::Error;
    fn try_from(value: pb::AuthorizeValidatorVoteRequest) -> Result<Self, Self::Error> {
        Ok(Self {
            validator_vote: value
                .validator_vote
                .ok_or_else(|| anyhow::anyhow!("missing validator vote"))?
                .try_into()?,
            pre_authorizations: value
                .pre_authorizations
                .into_iter()
                .map(TryInto::try_into)
                .collect::<Result<Vec<_>, _>>()?,
        })
    }
}

impl From<AuthorizeValidatorVoteRequest> for pb::AuthorizeValidatorVoteRequest {
    fn from(value: AuthorizeValidatorVoteRequest) -> pb::AuthorizeValidatorVoteRequest {
        Self {
            validator_vote: Some(value.validator_vote.into()),
            pre_authorizations: value
                .pre_authorizations
                .into_iter()
                .map(Into::into)
                .collect(),
        }
    }
}