Skip to main content

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_sdk_types::{Address, Identifier, ObjectId, StructTag};
14use serde::{Deserialize, Serialize};
15
16use self::name::Name;
17
18/// An object to manage a second-level name (SLN).
19#[derive(Debug, Clone, Eq, PartialEq, Serialize, Deserialize)]
20pub struct NameRegistration {
21    id: ObjectId,
22    name: Name,
23    name_str: String,
24    expiration_timestamp_ms: u64,
25}
26
27/// An object to manage a subname.
28#[derive(Debug, Clone, Eq, PartialEq, Serialize, Deserialize)]
29pub struct SubnameRegistration {
30    id: ObjectId,
31    nft: NameRegistration,
32}
33
34impl SubnameRegistration {
35    pub fn into_inner(self) -> NameRegistration {
36        self.nft
37    }
38}
39
40/// Unifying trait for [`NameRegistration`] and [`SubnameRegistration`]
41pub trait IotaNamesNft {
42    const MODULE: Identifier;
43    const TYPE_NAME: Identifier;
44
45    fn type_(package_id: Address) -> StructTag {
46        StructTag::new(package_id, Self::MODULE, Self::TYPE_NAME, vec![])
47    }
48
49    fn name(&self) -> &Name;
50
51    fn name_str(&self) -> &str;
52
53    fn expiration_timestamp_ms(&self) -> u64;
54
55    fn expiration_time(&self) -> SystemTime {
56        UNIX_EPOCH + Duration::from_millis(self.expiration_timestamp_ms())
57    }
58
59    fn has_expired(&self) -> bool {
60        self.expiration_time() <= SystemTime::now()
61    }
62
63    fn id(&self) -> ObjectId;
64}
65
66impl IotaNamesNft for NameRegistration {
67    const MODULE: Identifier = Identifier::from_static("name_registration");
68    const TYPE_NAME: Identifier = Identifier::from_static("NameRegistration");
69
70    fn name(&self) -> &Name {
71        &self.name
72    }
73
74    fn name_str(&self) -> &str {
75        &self.name_str
76    }
77
78    fn expiration_timestamp_ms(&self) -> u64 {
79        self.expiration_timestamp_ms
80    }
81
82    fn id(&self) -> ObjectId {
83        self.id
84    }
85}
86
87impl IotaNamesNft for SubnameRegistration {
88    const MODULE: Identifier = Identifier::from_static("subname_registration");
89    const TYPE_NAME: Identifier = Identifier::from_static("SubnameRegistration");
90
91    fn name(&self) -> &Name {
92        self.nft.name()
93    }
94
95    fn name_str(&self) -> &str {
96        self.nft.name_str()
97    }
98
99    fn expiration_timestamp_ms(&self) -> u64 {
100        self.nft.expiration_timestamp_ms()
101    }
102
103    fn id(&self) -> ObjectId {
104        self.id
105    }
106}