Skip to content

Commit 1f31742

Browse files
chore(iota-names): some cleanup and improvements (#6530)
Co-authored-by: Thoralf-M <[email protected]>
1 parent b2223c1 commit 1f31742

File tree

8 files changed

+77
-99
lines changed

8 files changed

+77
-99
lines changed

crates/iota-graphql-rpc/src/types/query.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -541,7 +541,8 @@ impl Query {
541541
.extend()
542542
}
543543

544-
/// Resolves an IOTA-Names `domain` name to an address, if it has been bound.
544+
/// Resolves an IOTA-Names `domain` name to an address, if it has been
545+
/// bound.
545546
async fn resolve_iota_names_address(
546547
&self,
547548
ctx: &Context<'_>,

crates/iota-indexer/src/lib.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -174,9 +174,9 @@ pub struct IotaNamesOptions {
174174
#[arg(default_value_t = IotaNamesConfig::default().object_id)]
175175
#[arg(long = "iota-names-object-id")]
176176
pub object_id: ObjectID,
177-
#[arg(default_value_t = IotaNamesConfig::default().payment_package_address)]
178-
#[arg(long = "iota-names-payment-package-address")]
179-
pub payment_package_address: IotaAddress,
177+
#[arg(default_value_t = IotaNamesConfig::default().payments_package_address)]
178+
#[arg(long = "iota-names-payments-package-address")]
179+
pub payments_package_address: IotaAddress,
180180
#[arg(default_value_t = IotaNamesConfig::default().registry_id)]
181181
#[arg(long = "iota-names-registry-id")]
182182
pub registry_id: ObjectID,
@@ -193,15 +193,15 @@ impl From<IotaNamesOptions> for IotaNamesConfig {
193193
let IotaNamesOptions {
194194
package_address,
195195
object_id,
196-
payment_package_address,
196+
payments_package_address,
197197
registry_id,
198198
reverse_registry_id,
199199
subdomain_proxy_package_id,
200200
} = options;
201201
Self {
202202
package_address,
203203
object_id,
204-
payment_package_address,
204+
payments_package_address,
205205
registry_id,
206206
reverse_registry_id,
207207
subdomain_proxy_package_id,
@@ -214,15 +214,15 @@ impl From<IotaNamesConfig> for IotaNamesOptions {
214214
let IotaNamesConfig {
215215
package_address,
216216
object_id,
217-
payment_package_address,
217+
payments_package_address,
218218
registry_id,
219219
reverse_registry_id,
220220
subdomain_proxy_package_id,
221221
} = config;
222222
Self {
223223
package_address,
224224
object_id,
225-
payment_package_address,
225+
payments_package_address,
226226
registry_id,
227227
reverse_registry_id,
228228
subdomain_proxy_package_id,

crates/iota-names/src/config.rs

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,22 +15,29 @@ use crate::Domain;
1515
pub const LEAF_EXPIRATION_TIMESTAMP: u64 = 0;
1616
pub const DEFAULT_TLD: &str = "iota";
1717
pub const ACCEPTED_SEPARATORS: [char; 2] = ['.', '*'];
18-
pub const IOTA_NEW_FORMAT_SEPARATOR: char = '@';
18+
pub const IOTA_AT_FORMAT_SEPARATOR: char = '@';
1919

2020
#[derive(Clone, Debug, Serialize, Deserialize, Eq, PartialEq)]
2121
#[serde(rename_all = "kebab-case")]
2222
pub struct IotaNamesConfig {
23+
/// Address of the `iota_names` package.
2324
pub package_address: IotaAddress,
25+
/// ID of the `IotaNames` object.
2426
pub object_id: ObjectID,
25-
pub payment_package_address: IotaAddress,
27+
/// Address of the `payments` package.
28+
pub payments_package_address: IotaAddress,
29+
/// ID of the registry table.
2630
pub registry_id: ObjectID,
31+
/// ID of the reverse registry table.
2732
pub reverse_registry_id: ObjectID,
33+
/// Address of the `subdomain_proxy` package.
2834
pub subdomain_proxy_package_id: ObjectID,
2935
}
3036

3137
impl Default for IotaNamesConfig {
3238
fn default() -> Self {
33-
// TODO change to mainnet
39+
// TODO change to mainnet https://github.com/iotaledger/iota/issues/6532
40+
// TODO change to testnet https://github.com/iotaledger/iota/issues/6531
3441
Self::devnet()
3542
}
3643
}
@@ -39,15 +46,15 @@ impl IotaNamesConfig {
3946
pub fn new(
4047
package_address: IotaAddress,
4148
object_id: ObjectID,
42-
payment_package_address: IotaAddress,
49+
payments_package_address: IotaAddress,
4350
registry_id: ObjectID,
4451
reverse_registry_id: ObjectID,
4552
subdomain_proxy_package_id: ObjectID,
4653
) -> Self {
4754
Self {
4855
package_address,
4956
object_id,
50-
payment_package_address,
57+
payments_package_address,
5158
registry_id,
5259
reverse_registry_id,
5360
subdomain_proxy_package_id,
@@ -56,10 +63,9 @@ impl IotaNamesConfig {
5663

5764
pub fn from_chain(chain: &Chain) -> Self {
5865
match chain {
59-
// Chain::Mainnet => IotaNamesConfig::mainnet(),
60-
// Chain::Testnet => IotaNamesConfig::testnet(),
66+
Chain::Mainnet => todo!("https://github.com/iotaledger/iota/issues/6532"),
67+
Chain::Testnet => todo!("https://github.com/iotaledger/iota/issues/6531"),
6168
Chain::Unknown => IotaNamesConfig::devnet(),
62-
_ => todo!("uncomment Mainnet and Testnet when IOTA-Names is published there"),
6369
}
6470
}
6571

@@ -84,15 +90,17 @@ impl IotaNamesConfig {
8490
.unwrap()
8591
}
8692

87-
// TODO add mainnet() and testnet()
93+
// TODO add mainnet https://github.com/iotaledger/iota/issues/6532
8894

89-
// Create a config based on the package and object ids published on devnet
95+
// TODO add testnet https://github.com/iotaledger/iota/issues/6531
96+
97+
// Create a config based on the package and object ids published on devnet.
9098
pub fn devnet() -> Self {
9199
const PACKAGE_ADDRESS: &str =
92100
"0xe27899d691184f66821f8fed5e7c26f3c65b26921947956435a655c8d7efc573";
93101
const OBJECT_ID: &str =
94102
"0xdad5289ef0d64f8f3b4d72522907f3f67109fa00bfbcba2dd03c68084f1dfc89";
95-
const PAYMENT_PACKAGE_ADDRESS: &str =
103+
const PAYMENTS_PACKAGE_ADDRESS: &str =
96104
"0x8e1d3fafb70764eccc2e6b61812daf0a4db40db3c5cea515bf4d390f11016030";
97105
const REGISTRY_ID: &str =
98106
"0xff608b2b0d500b4d0cb25ff165bc3e01fce9bf3ef7fb002840b814d304a08b2a";
@@ -103,15 +111,15 @@ impl IotaNamesConfig {
103111

104112
let package_address = IotaAddress::from_str(PACKAGE_ADDRESS).unwrap();
105113
let object_id = ObjectID::from_str(OBJECT_ID).unwrap();
106-
let payment_package_address = IotaAddress::from_str(PAYMENT_PACKAGE_ADDRESS).unwrap();
114+
let payments_package_address = IotaAddress::from_str(PAYMENTS_PACKAGE_ADDRESS).unwrap();
107115
let registry_id = ObjectID::from_str(REGISTRY_ID).unwrap();
108116
let reverse_registry_id = ObjectID::from_str(REVERSE_REGISTRY_ID).unwrap();
109117
let subdomain_proxy_package_id = ObjectID::from_str(SUBDOMAIN_PROXY_PACKAGE_ID).unwrap();
110118

111119
Self::new(
112120
package_address,
113121
object_id,
114-
payment_package_address,
122+
payments_package_address,
115123
registry_id,
116124
reverse_registry_id,
117125
subdomain_proxy_package_id,

crates/iota-names/src/domain.rs

Lines changed: 18 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use move_core_types::{ident_str, identifier::IdentStr, language_storage::StructT
88
use serde::{Deserialize, Serialize};
99

1010
use crate::{
11-
config::{ACCEPTED_SEPARATORS, DEFAULT_TLD, IOTA_NEW_FORMAT_SEPARATOR},
11+
config::{ACCEPTED_SEPARATORS, DEFAULT_TLD, IOTA_AT_FORMAT_SEPARATOR},
1212
error::IotaNamesError,
1313
};
1414

@@ -18,13 +18,6 @@ pub struct Domain {
1818
labels: Vec<String>,
1919
}
2020

21-
// impl std::fmt::Display for Domain {
22-
// fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
23-
// let labels = self.labels.iter().rev().cloned().collect::<Vec<_>>();
24-
// write!(f, "{}", labels.join("."))
25-
// }
26-
// }
27-
2821
// impl FromStr for Domain {
2922
// type Err = anyhow::Error;
3023

@@ -54,7 +47,7 @@ impl FromStr for Domain {
5447
}
5548
let separator = separator(s)?;
5649

57-
let formatted_string = convert_from_new_format(s, &separator)?;
50+
let formatted_string = convert_from_at_format(s, &separator)?;
5851

5952
let labels = formatted_string
6053
.split(separator)
@@ -140,6 +133,9 @@ impl Domain {
140133
/// Returns the number of labels including TLD.
141134
///
142135
/// ```
136+
/// use std::str::FromStr;
137+
///
138+
/// use iota_names::domain::Domain;
143139
/// assert_eq!(
144140
/// Domain::from_str("test.example.iota").unwrap().num_labels(),
145141
/// 3
@@ -176,7 +172,7 @@ impl Domain {
176172
let _tld = labels.pop();
177173
let sld = labels.pop().unwrap();
178174

179-
format!("{}{}{}", labels.join(sep), IOTA_NEW_FORMAT_SEPARATOR, sld)
175+
format!("{}{IOTA_AT_FORMAT_SEPARATOR}{sld}", labels.join(sep))
180176
}
181177
}
182178

@@ -212,8 +208,8 @@ fn separator(s: &str) -> Result<char, IotaNamesError> {
212208
/// Converts @label ending to label{separator}iota ending.
213209
///
214210
/// E.g. `@example` -> `example.iota` | `test@example` -> `test.example.iota`
215-
fn convert_from_new_format(s: &str, separator: &char) -> Result<String, IotaNamesError> {
216-
let mut splits = s.split(IOTA_NEW_FORMAT_SEPARATOR);
211+
fn convert_from_at_format(s: &str, separator: &char) -> Result<String, IotaNamesError> {
212+
let mut splits = s.split(IOTA_AT_FORMAT_SEPARATOR);
217213

218214
let Some(before) = splits.next() else {
219215
return Err(IotaNamesError::InvalidSeparator);
@@ -239,9 +235,14 @@ fn convert_from_new_format(s: &str, separator: &char) -> Result<String, IotaName
239235
Ok(parts.join(&separator.to_string()))
240236
}
241237

238+
/// Checks the validity of a label according to these rules:
239+
/// - length must be in [MIN_LABEL_LENGTH..MAX_LABEL_LENGTH]
240+
/// - must contain only '0'..'9', 'a'..'z' and '-'
241+
/// - must not start or end with '-'
242242
pub fn validate_label(label: &str) -> Result<&str, IotaNamesError> {
243243
const MIN_LABEL_LENGTH: usize = 1;
244244
const MAX_LABEL_LENGTH: usize = 63;
245+
245246
let bytes = label.as_bytes();
246247
let len = bytes.len();
247248

@@ -254,39 +255,16 @@ pub fn validate_label(label: &str) -> Result<&str, IotaNamesError> {
254255
}
255256

256257
for (i, character) in bytes.iter().enumerate() {
257-
let is_valid_character = match character {
258-
b'a'..=b'z' => true,
259-
b'0'..=b'9' => true,
260-
b'-' if i != 0 && i != len - 1 => true,
261-
_ => false,
262-
};
263-
264-
if !is_valid_character {
265-
match character {
266-
b'-' => return Err(IotaNamesError::InvalidHyphens),
267-
_ => return Err(IotaNamesError::InvalidUnderscore),
268-
}
258+
match character {
259+
b'a'..=b'z' | b'0'..=b'9' => continue,
260+
b'-' if i == 0 || i == len - 1 => return Err(IotaNamesError::HyphensAsFirstOrLastChar),
261+
_ => return Err(IotaNamesError::InvalidLabelChar),
269262
};
270263
}
264+
271265
Ok(label)
272266
}
273267

274-
// fn parse_domain_label(label: &str) -> anyhow::Result<String> {
275-
// anyhow::ensure!(
276-
// label.len() >= MIN_LABEL_LEN && label.len() <= MAX_LABEL_LEN,
277-
// "label length outside allowed range
278-
// [{MIN_LABEL_LEN}..{MAX_LABEL_LEN}]: {}", label.len()
279-
// );
280-
// let regex =
281-
// regex::Regex::new("^[a-zA-Z0-9][a-zA-Z0-9-]+[a-zA-Z0-9]$").unwrap();
282-
283-
// anyhow::ensure!(
284-
// regex.is_match(label),
285-
// "invalid characters in domain: {label}"
286-
// );
287-
// Ok(label.to_owned())
288-
// }
289-
290268
#[cfg(test)]
291269
mod tests {
292270
use super::*;

crates/iota-names/src/error.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,20 @@ use serde::{Deserialize, Serialize};
66

77
#[derive(thiserror::Error, Debug, Serialize, Deserialize, Clone, Eq, PartialEq)]
88
pub enum IotaNamesError {
9-
#[error("Name Service: String length: {0} exceeds maximum allowed length: {1}")]
9+
#[error("String length: {0} exceeds maximum allowed length: {1}")]
1010
ExceedsMaxLength(usize, usize),
11-
#[error("Name Service: String length: {0} outside of valid range: [{1}, {2}]")]
11+
#[error("String length: {0} outside of valid range: [{1}, {2}]")]
1212
InvalidLength(usize, usize, usize),
13-
#[error("Name Service: Hyphens are not allowed as the first or last character")]
14-
InvalidHyphens,
15-
#[error("Name Service: Only lowercase letters, numbers, and hyphens are allowed")]
16-
InvalidUnderscore,
17-
#[error("Name Service: Domain must contain at least one label")]
13+
#[error("Hyphens are not allowed as the first or last character")]
14+
HyphensAsFirstOrLastChar,
15+
#[error("Only lowercase letters, numbers, and hyphens are allowed as label characters")]
16+
InvalidLabelChar,
17+
#[error("Domain must contain at least one label")]
1818
LabelsEmpty,
19-
#[error("Name Service: Domain must include only one separator")]
19+
#[error("Domain must include only one separator")]
2020
InvalidSeparator,
21-
#[error("Name Service: Name has expired")]
21+
#[error("Name has expired")]
2222
NameExpired,
23-
#[error("Name Service: Malformed object for {0}")]
23+
#[error("Malformed object for {0}")]
2424
MalformedObject(ObjectID),
2525
}

crates/iota-names/src/lib.rs

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,7 @@ use serde::{Deserialize, Serialize};
1717

1818
use self::domain::Domain;
1919

20-
pub const MIN_LABEL_LEN: usize = 3;
21-
pub const MAX_LABEL_LEN: usize = 63;
22-
23-
/// An object to manage a second-level domain (SLD)
20+
/// An object to manage a second-level domain (SLD).
2421
#[derive(Debug, Clone, Eq, PartialEq, Serialize, Deserialize)]
2522
pub struct IotaNamesRegistration {
2623
id: ObjectID,
@@ -63,7 +60,9 @@ pub trait IotaNamesNft {
6360

6461
fn expiration_timestamp_ms(&self) -> u64;
6562

66-
fn expiration_time(&self) -> SystemTime;
63+
fn expiration_time(&self) -> SystemTime {
64+
UNIX_EPOCH + Duration::from_millis(self.expiration_timestamp_ms())
65+
}
6766

6867
fn has_expired(&self) -> bool {
6968
self.expiration_time() <= SystemTime::now()
@@ -90,10 +89,6 @@ impl IotaNamesNft for IotaNamesRegistration {
9089
self.expiration_timestamp_ms
9190
}
9291

93-
fn expiration_time(&self) -> SystemTime {
94-
UNIX_EPOCH + Duration::from_millis(self.expiration_timestamp_ms())
95-
}
96-
9792
fn image_url(&self) -> &str {
9893
&self.image_url
9994
}
@@ -119,10 +114,6 @@ impl IotaNamesNft for SubdomainRegistration {
119114
self.nft.expiration_timestamp_ms()
120115
}
121116

122-
fn expiration_time(&self) -> SystemTime {
123-
self.nft.expiration_time()
124-
}
125-
126117
fn image_url(&self) -> &str {
127118
self.nft.image_url()
128119
}

0 commit comments

Comments
 (0)