Skip to main content

xrpl/models/ledger/objects/
did.rs

1use crate::models::ledger::objects::LedgerEntryType;
2use crate::models::FlagCollection;
3use crate::models::Model;
4use crate::models::NoFlags;
5use alloc::borrow::Cow;
6
7use serde::{Deserialize, Serialize};
8
9use serde_with::skip_serializing_none;
10
11use super::{CommonFields, LedgerObject};
12
13/// The `DID` object type holds references to, or data associated with, a single
14/// Decentralized Identifier (DID).
15///
16/// `<https://xrpl.org/docs/references/protocol/ledger-data/ledger-entry-types/did>`
17#[skip_serializing_none]
18#[derive(Debug, Serialize, Deserialize, PartialEq, Eq, Clone)]
19#[serde(rename_all = "PascalCase")]
20pub struct DID<'a> {
21    /// The base fields for all ledger object models.
22    ///
23    /// See Ledger Object Common Fields:
24    /// `<https://xrpl.org/ledger-entry-common-fields.html>`
25    #[serde(flatten)]
26    pub common_fields: CommonFields<'a, NoFlags>,
27    /// The account that controls the DID.
28    pub account: Cow<'a, str>,
29    /// The W3C standard DID document associated with the DID.
30    /// Limited to a maximum length of 256 bytes.
31    #[serde(rename = "DIDDocument")]
32    pub did_document: Option<Cow<'a, str>>,
33    /// The public attestations of identity credentials associated with the DID.
34    /// Limited to a maximum length of 256 bytes.
35    pub data: Option<Cow<'a, str>>,
36    /// The Universal Resource Identifier that points to the corresponding
37    /// DID document or the data associated with the DID.
38    /// Limited to a maximum length of 256 bytes.
39    #[serde(rename = "URI")]
40    pub uri: Option<Cow<'a, str>>,
41    /// A hint indicating which page of the owner directory links to this object.
42    pub owner_node: Cow<'a, str>,
43    /// The identifying hash of the transaction that most recently modified this object.
44    #[serde(rename = "PreviousTxnID")]
45    pub previous_txn_id: Cow<'a, str>,
46    /// The index of the ledger that contains the transaction that most recently
47    /// modified this object.
48    pub previous_txn_lgr_seq: u32,
49}
50
51impl<'a> Model for DID<'a> {}
52
53impl<'a> LedgerObject<NoFlags> for DID<'a> {
54    fn get_ledger_entry_type(&self) -> LedgerEntryType {
55        self.common_fields.get_ledger_entry_type()
56    }
57}
58
59impl<'a> DID<'a> {
60    pub fn new(
61        index: Option<Cow<'a, str>>,
62        ledger_index: Option<Cow<'a, str>>,
63        account: Cow<'a, str>,
64        did_document: Option<Cow<'a, str>>,
65        data: Option<Cow<'a, str>>,
66        uri: Option<Cow<'a, str>>,
67        owner_node: Cow<'a, str>,
68        previous_txn_id: Cow<'a, str>,
69        previous_txn_lgr_seq: u32,
70    ) -> Self {
71        Self {
72            common_fields: CommonFields {
73                flags: FlagCollection::default(),
74                ledger_entry_type: LedgerEntryType::DID,
75                index,
76                ledger_index,
77            },
78            account,
79            did_document,
80            data,
81            uri,
82            owner_node,
83            previous_txn_id,
84            previous_txn_lgr_seq,
85        }
86    }
87}
88
89#[cfg(test)]
90mod test_serde {
91    use super::*;
92    use alloc::borrow::Cow;
93
94    #[test]
95    fn test_serialize() {
96        let did = DID::new(
97            Some(Cow::from(
98                "46813BE38B798B3752CA590D44E7FEADB17485649074403AD1761A2835CE91FF",
99            )),
100            None,
101            Cow::from("rpfqJrXg5uidNo2ZsRhRY6TiF1cvYmV9Fg"),
102            Some(Cow::from("646F63")),
103            Some(Cow::from("617474657374")),
104            Some(Cow::from("6469645F6578616D706C65")),
105            Cow::from("0"),
106            Cow::from("A4C15DA185E6092DF5954FF62A1446220C61A5F60F0D93B4B09F708778E41120"),
107            4,
108        );
109        let serialized = serde_json::to_string(&did).unwrap();
110        let deserialized: DID = serde_json::from_str(&serialized).unwrap();
111        assert_eq!(did, deserialized);
112    }
113
114    #[test]
115    fn test_deserialize_from_json() {
116        let json = r#"{
117            "Account": "rpfqJrXg5uidNo2ZsRhRY6TiF1cvYmV9Fg",
118            "DIDDocument": "646F63",
119            "Data": "617474657374",
120            "Flags": 0,
121            "LedgerEntryType": "DID",
122            "OwnerNode": "0",
123            "PreviousTxnID": "A4C15DA185E6092DF5954FF62A1446220C61A5F60F0D93B4B09F708778E41120",
124            "PreviousTxnLgrSeq": 4,
125            "URI": "6469645F6578616D706C65",
126            "index": "46813BE38B798B3752CA590D44E7FEADB17485649074403AD1761A2835CE91FF"
127        }"#;
128
129        let did: DID = serde_json::from_str(json).unwrap();
130        assert_eq!(did.account, "rpfqJrXg5uidNo2ZsRhRY6TiF1cvYmV9Fg");
131        assert_eq!(did.did_document.as_deref(), Some("646F63"));
132        assert_eq!(did.data.as_deref(), Some("617474657374"));
133        assert_eq!(did.uri.as_deref(), Some("6469645F6578616D706C65"));
134        assert_eq!(did.owner_node, "0");
135        assert_eq!(did.previous_txn_lgr_seq, 4);
136    }
137}