Skip to main content

xrpl_common_stdlib/objects/generated/
did.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 DID objects in any ledger.
13pub trait DIDFields: LedgerObjectCommonFields {
14    /// The account that controls the DID.
15    fn account(&self) -> Result<AccountID> {
16        ledger_object::get_field(self.get_slot_num(), sfield::Account)
17    }
18
19    /// The W3C standard DID document associated with the DID. The `DIDDocument` field isn't checked
20    /// for validity and is limited to a maximum length of 256 bytes.
21    fn did_document(&self) -> Result<Option<StandardBlob>> {
22        ledger_object::get_field_optional(self.get_slot_num(), sfield::DIDDocument)
23    }
24
25    /// The Universal Resource Identifier that points to the corresponding DID document or the data
26    /// associated with the DID. This field can be an HTTP(S) URL or IPFS URI. This field isn't
27    /// checked for validity and is limited to a maximum length of 256 bytes.
28    fn uri(&self) -> Result<Option<UriBlob>> {
29        ledger_object::get_field_optional(self.get_slot_num(), sfield::URI)
30    }
31
32    /// The public attestations of identity credentials associated with the DID. The `Data` field
33    /// isn't checked for validity and is limited to a maximum length of 256 bytes.
34    fn data(&self) -> Result<Option<StandardBlob>> {
35        ledger_object::get_field_optional(self.get_slot_num(), sfield::Data)
36    }
37
38    /// A hint indicating which page of the sender's owner directory links to this entry, in case
39    /// the directory consists of multiple pages.
40    fn owner_node(&self) -> Result<u64> {
41        ledger_object::get_field(self.get_slot_num(), sfield::OwnerNode)
42    }
43
44    /// The identifying hash of the transaction that most recently modified this object.
45    fn previous_txn_id(&self) -> Result<Hash256> {
46        ledger_object::get_field(self.get_slot_num(), sfield::PreviousTxnID)
47    }
48
49    /// The index of the ledger that contains the transaction that most recently modified this
50    /// object.
51    fn previous_txn_lgr_seq(&self) -> Result<u32> {
52        ledger_object::get_field(self.get_slot_num(), sfield::PreviousTxnLgrSeq)
53    }
54}
55
56/// Trait providing access to fields specific to the current DID object.
57pub trait CurrentDIDFields: CurrentLedgerObjectCommonFields {
58    /// The account that controls the DID.
59    fn account(&self) -> Result<AccountID> {
60        current_ledger_object::get_field(sfield::Account)
61    }
62
63    /// The W3C standard DID document associated with the DID. The `DIDDocument` field isn't checked
64    /// for validity and is limited to a maximum length of 256 bytes.
65    fn did_document(&self) -> Result<Option<StandardBlob>> {
66        current_ledger_object::get_field_optional(sfield::DIDDocument)
67    }
68
69    /// The Universal Resource Identifier that points to the corresponding DID document or the data
70    /// associated with the DID. This field can be an HTTP(S) URL or IPFS URI. This field isn't
71    /// checked for validity and is limited to a maximum length of 256 bytes.
72    fn uri(&self) -> Result<Option<UriBlob>> {
73        current_ledger_object::get_field_optional(sfield::URI)
74    }
75
76    /// The public attestations of identity credentials associated with the DID. The `Data` field
77    /// isn't checked for validity and is limited to a maximum length of 256 bytes.
78    fn data(&self) -> Result<Option<StandardBlob>> {
79        current_ledger_object::get_field_optional(sfield::Data)
80    }
81
82    /// A hint indicating which page of the sender's owner directory links to this entry, in case
83    /// the directory consists of multiple pages.
84    fn owner_node(&self) -> Result<u64> {
85        current_ledger_object::get_field(sfield::OwnerNode)
86    }
87
88    /// The identifying hash of the transaction that most recently modified this object.
89    fn previous_txn_id(&self) -> Result<Hash256> {
90        current_ledger_object::get_field(sfield::PreviousTxnID)
91    }
92
93    /// The index of the ledger that contains the transaction that most recently modified this
94    /// object.
95    fn previous_txn_lgr_seq(&self) -> Result<u32> {
96        current_ledger_object::get_field(sfield::PreviousTxnLgrSeq)
97    }
98}
99
100#[derive(Debug, Clone, Copy, Eq, PartialEq)]
101pub struct DID {
102    pub(crate) slot_num: i32,
103}
104
105impl DID {
106    /// Binds this handle to a host-managed slot holding a DID ledger object.
107    pub fn new(slot_num: i32) -> Self {
108        Self { slot_num }
109    }
110}
111
112impl LedgerObjectCommonFields for DID {
113    fn get_slot_num(&self) -> i32 {
114        self.slot_num
115    }
116}
117
118impl DIDFields for DID {}
119
120#[cfg(test)]
121mod tests {
122    use super::*;
123    use crate::host::host_bindings_trait::MockHostBindings;
124    use crate::host::setup_mock;
125    use crate::objects::test_utils::*;
126
127    #[test]
128    fn read_all_fields() {
129        let mut mock = MockHostBindings::new();
130        mock_all_fields_present(&mut mock);
131        let _guard = setup_mock(mock);
132
133        let obj = DID::new(0);
134
135        assert!(obj.account().is_ok());
136        assert!(obj.owner_node().is_ok());
137        assert!(obj.previous_txn_id().is_ok());
138        assert!(obj.previous_txn_lgr_seq().is_ok());
139        assert!(obj.did_document().is_ok());
140        assert!(obj.uri().is_ok());
141        assert!(obj.data().is_ok());
142    }
143}