diem_types/account_config/resources/
account.rs

1// Copyright (c) The Diem Core Contributors
2// SPDX-License-Identifier: Apache-2.0
3
4// Copyright 2021 Conflux Foundation. All rights reserved.
5// Conflux is free software and distributed under GNU General Public License.
6// See http://www.gnu.org/licenses/
7
8use crate::{
9    account_config::{
10        constants::ACCOUNT_MODULE_NAME, KeyRotationCapabilityResource,
11        WithdrawCapabilityResource,
12    },
13    event::EventHandle,
14};
15use move_core_types::move_resource::MoveResource;
16#[cfg(any(test, feature = "fuzzing"))]
17use proptest_derive::Arbitrary;
18use serde::{Deserialize, Serialize};
19
20/// A Rust representation of an Account resource.
21/// This is not how the Account is represented in the VM but it's a convenient
22/// representation.
23#[derive(Debug, Serialize, Deserialize)]
24#[cfg_attr(any(test, feature = "fuzzing"), derive(Arbitrary))]
25pub struct AccountResource {
26    authentication_key: Vec<u8>,
27    withdrawal_capability: Option<WithdrawCapabilityResource>,
28    key_rotation_capability: Option<KeyRotationCapabilityResource>,
29    received_events: EventHandle,
30    sent_events: EventHandle,
31    sequence_number: u64,
32}
33
34impl AccountResource {
35    /// Constructs an Account resource.
36    pub fn new(
37        sequence_number: u64, authentication_key: Vec<u8>,
38        withdrawal_capability: Option<WithdrawCapabilityResource>,
39        key_rotation_capability: Option<KeyRotationCapabilityResource>,
40        sent_events: EventHandle, received_events: EventHandle,
41    ) -> Self {
42        AccountResource {
43            sequence_number,
44            withdrawal_capability,
45            key_rotation_capability,
46            authentication_key,
47            sent_events,
48            received_events,
49        }
50    }
51
52    /// Return the sequence_number field for the given AccountResource
53    pub fn sequence_number(&self) -> u64 { self.sequence_number }
54
55    /// Returns if this account has delegated its withdrawal capability
56    pub fn has_delegated_withdrawal_capability(&self) -> bool {
57        self.withdrawal_capability.is_none()
58    }
59
60    /// Returns if this account has delegated its key rotation capability
61    pub fn has_delegated_key_rotation_capability(&self) -> bool {
62        self.key_rotation_capability.is_none()
63    }
64
65    /// Return the authentication_key field for the given AccountResource
66    pub fn authentication_key(&self) -> &[u8] { &self.authentication_key }
67
68    /// Return the sent_events handle for the given AccountResource
69    pub fn sent_events(&self) -> &EventHandle { &self.sent_events }
70
71    /// Return the received_events handle for the given AccountResource
72    pub fn received_events(&self) -> &EventHandle { &self.received_events }
73}
74
75impl MoveResource for AccountResource {
76    const MODULE_NAME: &'static str = ACCOUNT_MODULE_NAME;
77    const STRUCT_NAME: &'static str = ACCOUNT_MODULE_NAME;
78}