iota_names/
lib.rs

1// Copyright (c) Mysten Labs, Inc.
2// Modifications Copyright (c) 2025 IOTA Stiftung
3// SPDX-License-Identifier: Apache-2.0
4
5pub mod config;
6pub mod constants;
7pub mod domain;
8pub mod error;
9pub mod registry;
10
11use std::time::{Duration, SystemTime, UNIX_EPOCH};
12
13use iota_types::base_types::ObjectID;
14use move_core_types::{
15    account_address::AccountAddress, ident_str, identifier::IdentStr, language_storage::StructTag,
16};
17use serde::{Deserialize, Serialize};
18
19use self::domain::Domain;
20
21/// An object to manage a second-level domain (SLD).
22#[derive(Debug, Clone, Eq, PartialEq, Serialize, Deserialize)]
23pub struct IotaNamesRegistration {
24    id: ObjectID,
25    domain: Domain,
26    domain_name: String,
27    expiration_timestamp_ms: u64,
28}
29
30/// An object to manage a subdomain.
31#[derive(Debug, Clone, Eq, PartialEq, Serialize, Deserialize)]
32pub struct SubdomainRegistration {
33    id: ObjectID,
34    nft: IotaNamesRegistration,
35}
36
37impl SubdomainRegistration {
38    pub fn into_inner(self) -> IotaNamesRegistration {
39        self.nft
40    }
41}
42
43/// Unifying trait for [`IotaNamesRegistration`] and [`SubdomainRegistration`]
44pub trait IotaNamesNft {
45    const MODULE: &IdentStr;
46    const TYPE_NAME: &IdentStr;
47
48    fn type_(package_id: AccountAddress) -> StructTag {
49        StructTag {
50            address: package_id,
51            module: Self::MODULE.into(),
52            name: Self::TYPE_NAME.into(),
53            type_params: Vec::new(),
54        }
55    }
56
57    fn domain(&self) -> &Domain;
58
59    fn domain_name(&self) -> &str;
60
61    fn expiration_timestamp_ms(&self) -> u64;
62
63    fn expiration_time(&self) -> SystemTime {
64        UNIX_EPOCH + Duration::from_millis(self.expiration_timestamp_ms())
65    }
66
67    fn has_expired(&self) -> bool {
68        self.expiration_time() <= SystemTime::now()
69    }
70
71    fn id(&self) -> ObjectID;
72}
73
74impl IotaNamesNft for IotaNamesRegistration {
75    const MODULE: &IdentStr = ident_str!("iota_names_registration");
76    const TYPE_NAME: &IdentStr = ident_str!("IotaNamesRegistration");
77
78    fn domain(&self) -> &Domain {
79        &self.domain
80    }
81
82    fn domain_name(&self) -> &str {
83        &self.domain_name
84    }
85
86    fn expiration_timestamp_ms(&self) -> u64 {
87        self.expiration_timestamp_ms
88    }
89
90    fn id(&self) -> ObjectID {
91        self.id
92    }
93}
94
95impl IotaNamesNft for SubdomainRegistration {
96    const MODULE: &IdentStr = ident_str!("subdomain_registration");
97    const TYPE_NAME: &IdentStr = ident_str!("SubdomainRegistration");
98
99    fn domain(&self) -> &Domain {
100        self.nft.domain()
101    }
102
103    fn domain_name(&self) -> &str {
104        self.nft.domain_name()
105    }
106
107    fn expiration_timestamp_ms(&self) -> u64 {
108        self.nft.expiration_timestamp_ms()
109    }
110
111    fn id(&self) -> ObjectID {
112        self.id
113    }
114}