logo
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
//! Auth info.

use super::{Fee, SignerInfo};
use crate::{prost_ext::MessageExt, proto, Error, ErrorReport, Result};

/// [`AuthInfo`] describes the fee and signer modes that are used to sign a transaction.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct AuthInfo {
    /// Defines the signing modes for the required signers.
    ///
    /// The number and order of elements must match the required signers from transaction
    /// [`Body`][`super::Body`]’s messages. The first element is the primary signer and the one
    /// which pays the [`Fee`].
    pub signer_infos: Vec<SignerInfo>,

    /// [`Fee`] and gas limit for the transaction.
    ///
    /// The first signer is the primary signer and the one which pays the fee.
    /// The fee can be calculated based on the cost of evaluating the body and doing signature
    /// verification of the signers. This can be estimated via simulation.
    pub fee: Fee,
}

impl AuthInfo {
    /// Convert to a Protocol Buffers representation.
    pub fn into_proto(self) -> proto::cosmos::tx::v1beta1::AuthInfo {
        self.into()
    }

    /// Encode this type using Protocol Buffers.
    pub fn into_bytes(self) -> Result<Vec<u8>> {
        self.into_proto().to_bytes()
    }
}

impl TryFrom<proto::cosmos::tx::v1beta1::AuthInfo> for AuthInfo {
    type Error = ErrorReport;

    fn try_from(proto: proto::cosmos::tx::v1beta1::AuthInfo) -> Result<AuthInfo> {
        Ok(AuthInfo {
            signer_infos: proto
                .signer_infos
                .into_iter()
                .map(TryFrom::try_from)
                .collect::<Result<_, _>>()?,
            fee: proto
                .fee
                .ok_or(Error::MissingField { name: "fee" })?
                .try_into()?,
        })
    }
}

impl From<AuthInfo> for proto::cosmos::tx::v1beta1::AuthInfo {
    fn from(auth_info: AuthInfo) -> proto::cosmos::tx::v1beta1::AuthInfo {
        proto::cosmos::tx::v1beta1::AuthInfo {
            signer_infos: auth_info.signer_infos.into_iter().map(Into::into).collect(),
            fee: Some(auth_info.fee.into()),
        }
    }
}