casper_client/types/
account.rs

1use std::fmt::{self, Display, Formatter};
2
3use itertools::Itertools;
4use serde::{Deserialize, Serialize};
5
6use casper_types::{account::AccountHash, URef};
7
8#[cfg(doc)]
9use crate::types::Contract;
10use crate::types::NamedKey;
11
12/// A representation of a public key and weight which can be associated with a given [`Account`] or
13/// [`Contract`].
14///
15/// Note, the "key" in this case is represented by an [`AccountHash`].
16#[derive(Copy, Clone, PartialOrd, Ord, PartialEq, Eq, Hash, Serialize, Deserialize, Debug)]
17#[serde(deny_unknown_fields)]
18pub struct AssociatedKey {
19    account_hash: AccountHash,
20    weight: u8,
21}
22
23impl AssociatedKey {
24    /// Returns the account hash of this associated key.
25    pub fn account_hash(&self) -> &AccountHash {
26        &self.account_hash
27    }
28
29    /// Returns the weight attributed to this associated key.
30    pub fn weight(&self) -> u8 {
31        self.weight
32    }
33}
34
35impl Display for AssociatedKey {
36    fn fmt(&self, formatter: &mut Formatter) -> fmt::Result {
37        write!(
38            formatter,
39            "associated-key {{ {}, weight: {} }}",
40            self.account_hash, self.weight
41        )
42    }
43}
44
45/// Thresholds that have to be met when executing an action of a certain type.
46#[derive(Copy, Clone, PartialOrd, Ord, PartialEq, Eq, Hash, Serialize, Deserialize, Debug)]
47#[serde(deny_unknown_fields)]
48pub struct ActionThresholds {
49    deployment: u8,
50    key_management: u8,
51}
52
53impl ActionThresholds {
54    /// Returns the deployment threshold.
55    pub fn deployment(&self) -> u8 {
56        self.deployment
57    }
58
59    /// Returns the key-management threshold.
60    pub fn key_management(&self) -> u8 {
61        self.key_management
62    }
63}
64
65impl Display for ActionThresholds {
66    fn fmt(&self, formatter: &mut Formatter) -> fmt::Result {
67        write!(
68            formatter,
69            "action-thresholds {{ deployment: {}, key-management: {} }}",
70            self.deployment, self.key_management
71        )
72    }
73}
74
75/// Structure representing a user's account, stored in global state.
76#[derive(Clone, PartialOrd, Ord, PartialEq, Eq, Hash, Serialize, Deserialize, Debug)]
77#[serde(deny_unknown_fields)]
78pub struct Account {
79    account_hash: AccountHash,
80    named_keys: Vec<NamedKey>,
81    main_purse: URef,
82    associated_keys: Vec<AssociatedKey>,
83    action_thresholds: ActionThresholds,
84}
85
86impl Account {
87    /// Returns the account hash (the hash of the public key) of the owner of the account.
88    pub fn account_hash(&self) -> &AccountHash {
89        &self.account_hash
90    }
91
92    /// Returns the named keys of the account.
93    pub fn named_keys(&self) -> impl Iterator<Item = &NamedKey> {
94        self.named_keys.iter()
95    }
96
97    /// Returns the main purse of the account.
98    pub fn main_purse(&self) -> &URef {
99        &self.main_purse
100    }
101
102    /// Returns the associated-keys of the account.
103    pub fn associated_keys(&self) -> impl Iterator<Item = &AssociatedKey> {
104        self.associated_keys.iter()
105    }
106
107    /// Returns the action-thresholds of the account.
108    pub fn action_thresholds(&self) -> ActionThresholds {
109        self.action_thresholds
110    }
111}
112
113impl Display for Account {
114    fn fmt(&self, formatter: &mut Formatter) -> fmt::Result {
115        write!(
116            formatter,
117            "account {{ {}, main-purse {}, {}, named keys {{{}}}, associated-keys {{{}}} }}",
118            self.account_hash,
119            self.main_purse,
120            self.action_thresholds,
121            self.named_keys.iter().format(", "),
122            self.associated_keys.iter().format(", ")
123        )
124    }
125}