cosm_utils/modules/auth/
model.rs

1use std::{fmt, str::FromStr};
2
3use cosmrs::proto::cosmos::auth::v1beta1::{BaseAccount, Params as CosmosParams};
4use cosmrs::{crypto::PublicKey, AccountId};
5use schemars::JsonSchema;
6use serde::{Deserialize, Serialize};
7
8use crate::chain::{error::ChainError, request::PaginationResponse};
9
10use super::error::AccountError;
11
12#[derive(Clone, Debug, Serialize, Deserialize, Eq, PartialEq, PartialOrd, Ord)]
13pub struct Address(AccountId);
14
15impl Address {
16    pub fn new(prefix: &str, bytes: &[u8]) -> Result<Self, AccountError> {
17        let account_id =
18            AccountId::new(prefix, bytes).map_err(|e| AccountError::AccountIdParse {
19                message: e.to_string(),
20            })?;
21
22        Ok(Self(account_id))
23    }
24
25    pub fn prefix(&self) -> &str {
26        self.0.prefix()
27    }
28
29    pub fn to_bytes(&self) -> Vec<u8> {
30        self.0.to_bytes()
31    }
32}
33
34impl AsRef<str> for Address {
35    fn as_ref(&self) -> &str {
36        self.0.as_ref()
37    }
38}
39
40impl fmt::Display for Address {
41    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
42        self.0.fmt(f)
43    }
44}
45
46impl FromStr for Address {
47    type Err = AccountError;
48
49    fn from_str(s: &str) -> Result<Self, Self::Err> {
50        Ok(Address(AccountId::from_str(s).map_err(|e| {
51            AccountError::Address {
52                message: e.to_string(),
53            }
54        })?))
55    }
56}
57
58impl From<AccountId> for Address {
59    fn from(account: AccountId) -> Address {
60        Address(account)
61    }
62}
63
64impl From<Address> for AccountId {
65    fn from(account: Address) -> AccountId {
66        account.0
67    }
68}
69
70impl From<Address> for String {
71    fn from(address: Address) -> Self {
72        address.0.into()
73    }
74}
75
76#[derive(Clone, Debug, Serialize, Deserialize, Eq, PartialEq)]
77pub struct Account {
78    /// Bech32 address of account
79    pub address: Address,
80
81    pub pubkey: Option<PublicKey>,
82
83    pub account_number: u64,
84
85    pub sequence: u64,
86}
87
88impl TryFrom<BaseAccount> for Account {
89    type Error = AccountError;
90
91    fn try_from(proto: BaseAccount) -> Result<Self, Self::Error> {
92        Ok(Account {
93            address: proto.address.parse()?,
94            pubkey: proto
95                .pub_key
96                .map(PublicKey::try_from)
97                .transpose()
98                .map_err(ChainError::crypto)?,
99            account_number: proto.account_number,
100            sequence: proto.sequence,
101        })
102    }
103}
104
105#[derive(Clone, Debug, Serialize, Deserialize, Eq, PartialEq)]
106pub struct AccountResponse {
107    pub account: Account,
108}
109
110#[derive(Clone, Debug, Serialize, Deserialize, Eq, PartialEq)]
111pub struct AccountsResponse {
112    pub accounts: Vec<Account>,
113
114    pub next: Option<PaginationResponse>,
115}
116
117#[derive(Clone, Debug, Serialize, Deserialize, JsonSchema, Eq, PartialEq, Hash)]
118pub struct ParamsResponse {
119    pub params: Option<Params>,
120}
121
122#[derive(Clone, Debug, Serialize, Deserialize, JsonSchema, Eq, PartialEq, Hash)]
123pub struct Params {
124    pub max_memo_characters: u64,
125    pub tx_sig_limit: u64,
126    pub tx_size_cost_per_byte: u64,
127    pub sig_verify_cost_ed25519: u64,
128    pub sig_verify_cost_secp256k1: u64,
129}
130
131impl From<CosmosParams> for Params {
132    fn from(p: CosmosParams) -> Self {
133        Self {
134            max_memo_characters: p.max_memo_characters,
135            tx_sig_limit: p.tx_sig_limit,
136            tx_size_cost_per_byte: p.tx_size_cost_per_byte,
137            sig_verify_cost_ed25519: p.sig_verify_cost_ed25519,
138            sig_verify_cost_secp256k1: p.sig_verify_cost_secp256k1,
139        }
140    }
141}
142
143impl From<Params> for CosmosParams {
144    fn from(p: Params) -> Self {
145        Self {
146            max_memo_characters: p.max_memo_characters,
147            tx_sig_limit: p.tx_sig_limit,
148            tx_size_cost_per_byte: p.tx_size_cost_per_byte,
149            sig_verify_cost_ed25519: p.sig_verify_cost_ed25519,
150            sig_verify_cost_secp256k1: p.sig_verify_cost_secp256k1,
151        }
152    }
153}