Skip to main content

xrpl_common_stdlib/objects/generated/
credential.rs

1// GENERATED -- do not hand-edit. Run scripts/generate-ledger-objects.sh to regenerate.
2
3use crate::host::Result;
4use crate::objects::traits::CurrentLedgerObjectCommonFields;
5use crate::objects::traits::LedgerObjectCommonFields;
6use crate::objects::{current_ledger_object, ledger_object};
7use crate::sfield;
8use crate::types::account_id::AccountID;
9use crate::types::blob::{StandardBlob, UriBlob};
10use crate::types::uint::Hash256;
11
12/// Trait providing access to fields specific to Credential objects in any ledger.
13pub trait CredentialFields: LedgerObjectCommonFields {
14    /// The account that this credential is for.
15    fn subject(&self) -> Result<AccountID> {
16        ledger_object::get_field(self.get_slot_num(), sfield::Subject)
17    }
18
19    /// The account that issued this credential.
20    fn issuer(&self) -> Result<AccountID> {
21        ledger_object::get_field(self.get_slot_num(), sfield::Issuer)
22    }
23
24    /// Arbitrary data defining the type of credential this entry represents. The minimum length is
25    /// 1 byte and the maximum length is 64 bytes.
26    fn credential_type(&self) -> Result<StandardBlob> {
27        ledger_object::get_field(self.get_slot_num(), sfield::CredentialType)
28    }
29
30    /// The Expiration field (Optional).
31    fn expiration(&self) -> Result<Option<u32>> {
32        ledger_object::get_field_optional(self.get_slot_num(), sfield::Expiration)
33    }
34
35    /// Arbitrary additional data about the credential, for example a URL where a W3C-formatted
36    /// Verifiable Credential can be retrieved.
37    fn uri(&self) -> Result<Option<UriBlob>> {
38        ledger_object::get_field_optional(self.get_slot_num(), sfield::URI)
39    }
40
41    /// A hint indicating which page of the issuer's directory links to this entry, in case the
42    /// directory consists of multiple pages.
43    fn issuer_node(&self) -> Result<u64> {
44        ledger_object::get_field(self.get_slot_num(), sfield::IssuerNode)
45    }
46
47    /// A hint indicating which page of the subject's owner directory links to this entry, in case
48    /// the directory consists of multiple pages.
49    fn subject_node(&self) -> Result<Option<u64>> {
50        ledger_object::get_field_optional(self.get_slot_num(), sfield::SubjectNode)
51    }
52
53    /// The identifying hash of the transaction that most recently modified this entry.
54    fn previous_txn_id(&self) -> Result<Hash256> {
55        ledger_object::get_field(self.get_slot_num(), sfield::PreviousTxnID)
56    }
57
58    /// The index of the ledger that contains the transaction that most recently modified this
59    /// entry.
60    fn previous_txn_lgr_seq(&self) -> Result<u32> {
61        ledger_object::get_field(self.get_slot_num(), sfield::PreviousTxnLgrSeq)
62    }
63}
64
65/// Trait providing access to fields specific to the current Credential object.
66pub trait CurrentCredentialFields: CurrentLedgerObjectCommonFields {
67    /// The account that this credential is for.
68    fn subject(&self) -> Result<AccountID> {
69        current_ledger_object::get_field(sfield::Subject)
70    }
71
72    /// The account that issued this credential.
73    fn issuer(&self) -> Result<AccountID> {
74        current_ledger_object::get_field(sfield::Issuer)
75    }
76
77    /// Arbitrary data defining the type of credential this entry represents. The minimum length is
78    /// 1 byte and the maximum length is 64 bytes.
79    fn credential_type(&self) -> Result<StandardBlob> {
80        current_ledger_object::get_field(sfield::CredentialType)
81    }
82
83    /// The Expiration field (Optional).
84    fn expiration(&self) -> Result<Option<u32>> {
85        current_ledger_object::get_field_optional(sfield::Expiration)
86    }
87
88    /// Arbitrary additional data about the credential, for example a URL where a W3C-formatted
89    /// Verifiable Credential can be retrieved.
90    fn uri(&self) -> Result<Option<UriBlob>> {
91        current_ledger_object::get_field_optional(sfield::URI)
92    }
93
94    /// A hint indicating which page of the issuer's directory links to this entry, in case the
95    /// directory consists of multiple pages.
96    fn issuer_node(&self) -> Result<u64> {
97        current_ledger_object::get_field(sfield::IssuerNode)
98    }
99
100    /// A hint indicating which page of the subject's owner directory links to this entry, in case
101    /// the directory consists of multiple pages.
102    fn subject_node(&self) -> Result<Option<u64>> {
103        current_ledger_object::get_field_optional(sfield::SubjectNode)
104    }
105
106    /// The identifying hash of the transaction that most recently modified this entry.
107    fn previous_txn_id(&self) -> Result<Hash256> {
108        current_ledger_object::get_field(sfield::PreviousTxnID)
109    }
110
111    /// The index of the ledger that contains the transaction that most recently modified this
112    /// entry.
113    fn previous_txn_lgr_seq(&self) -> Result<u32> {
114        current_ledger_object::get_field(sfield::PreviousTxnLgrSeq)
115    }
116}
117
118#[derive(Debug, Clone, Copy, Eq, PartialEq)]
119pub struct Credential {
120    pub(crate) slot_num: i32,
121}
122
123impl Credential {
124    /// Binds this handle to a host-managed slot holding a Credential ledger object.
125    pub fn new(slot_num: i32) -> Self {
126        Self { slot_num }
127    }
128}
129
130impl LedgerObjectCommonFields for Credential {
131    fn get_slot_num(&self) -> i32 {
132        self.slot_num
133    }
134}
135
136impl CredentialFields for Credential {}
137
138#[cfg(test)]
139mod tests {
140    use super::*;
141    use crate::host::host_bindings_trait::MockHostBindings;
142    use crate::host::setup_mock;
143    use crate::objects::test_utils::*;
144
145    #[test]
146    fn read_all_fields() {
147        let mut mock = MockHostBindings::new();
148        mock_all_fields_present(&mut mock);
149        let _guard = setup_mock(mock);
150
151        let obj = Credential::new(0);
152
153        assert!(obj.subject().is_ok());
154        assert!(obj.issuer().is_ok());
155        assert!(obj.credential_type().is_ok());
156        assert!(obj.issuer_node().is_ok());
157        assert!(obj.previous_txn_id().is_ok());
158        assert!(obj.previous_txn_lgr_seq().is_ok());
159        assert!(obj.expiration().is_ok());
160        assert!(obj.uri().is_ok());
161        assert!(obj.subject_node().is_ok());
162    }
163
164    #[test]
165    fn optional_fields_none() {
166        let mut mock = MockHostBindings::new();
167        mock_all_fields_not_found(&mut mock);
168        let _guard = setup_mock(mock);
169
170        let obj = Credential::new(0);
171
172        assert!(obj.expiration().unwrap().is_none());
173        assert!(obj.subject_node().unwrap().is_none());
174    }
175}