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 error;
8pub mod name;
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::name::Name;
20
21/// An object to manage a second-level name (SLN).
22#[derive(Debug, Clone, Eq, PartialEq, Serialize, Deserialize)]
23pub struct IotaNamesRegistration {
24    id: ObjectID,
25    name: Name,
26    name_str: String,
27    expiration_timestamp_ms: u64,
28}
29
30/// An object to manage a subname.
31#[derive(Debug, Clone, Eq, PartialEq, Serialize, Deserialize)]
32pub struct SubnameRegistration {
33    id: ObjectID,
34    nft: IotaNamesRegistration,
35}
36
37impl SubnameRegistration {
38    pub fn into_inner(self) -> IotaNamesRegistration {
39        self.nft
40    }
41}
42
43/// Unifying trait for [`IotaNamesRegistration`] and [`SubnameRegistration`]
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 name(&self) -> &Name;
58
59    fn name_str(&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 name(&self) -> &Name {
79        &self.name
80    }
81
82    fn name_str(&self) -> &str {
83        &self.name_str
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 SubnameRegistration {
96    const MODULE: &IdentStr = ident_str!("subname_registration");
97    const TYPE_NAME: &IdentStr = ident_str!("SubnameRegistration");
98
99    fn name(&self) -> &Name {
100        self.nft.name()
101    }
102
103    fn name_str(&self) -> &str {
104        self.nft.name_str()
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}