|
2 | 2 | // SPDX-License-Identifier: Apache-2.0
|
3 | 3 |
|
4 | 4 | use std::{
|
| 5 | + collections::BTreeMap, |
5 | 6 | str::FromStr,
|
6 | 7 | time::{Duration, SystemTime, UNIX_EPOCH},
|
7 | 8 | };
|
8 | 9 |
|
| 10 | +use anyhow::anyhow; |
9 | 11 | use chrono::{Utc, prelude::DateTime};
|
10 | 12 | use clap::Parser;
|
11 | 13 | use iota_graphql_rpc_client::simple_client::{GraphqlQueryVariable, SimpleClient};
|
12 | 14 | use iota_json::IotaJsonValue;
|
| 15 | +use iota_json_rpc_api::IndexerApiClient; |
13 | 16 | use iota_json_rpc_types::{
|
14 | 17 | IotaData, IotaObjectDataFilter, IotaObjectDataOptions, IotaObjectResponse,
|
15 | 18 | IotaObjectResponseQuery,
|
16 | 19 | };
|
17 | 20 | use iota_names::{
|
18 |
| - IotaNamesNft, IotaNamesRegistration, SubdomainRegistration, |
19 |
| - config::IotaNamesConfig, |
| 21 | + IotaNamesNft, IotaNamesRegistration, SubdomainRegistration, config::IotaNamesConfig, |
20 | 22 | domain::Domain,
|
21 |
| - registry::{RegistryEntry, ReverseRegistryEntry}, |
22 | 23 | };
|
23 | 24 | use iota_protocol_config::Chain;
|
24 | 25 | use iota_sdk::{IotaClient, wallet_context::WalletContext};
|
@@ -327,37 +328,60 @@ impl NameCommand {
|
327 | 328 | )
|
328 | 329 | }
|
329 | 330 | Self::GetUserData { domain, key } => {
|
330 |
| - let entry = get_registry_entry(&domain, context).await?; |
| 331 | + let client = context.get_client().await?; |
| 332 | + let mut res = client |
| 333 | + .http() |
| 334 | + .iota_names_lookup(&domain.to_string()) |
| 335 | + .await? |
| 336 | + .ok_or_else(|| anyhow!("no record found for {domain}"))? |
| 337 | + .data; |
331 | 338 |
|
332 | 339 | if let Some(key) = key {
|
333 |
| - let Some(value) = entry |
334 |
| - .name_record |
335 |
| - .data |
336 |
| - .contents |
337 |
| - .into_iter() |
338 |
| - .find(|entry| entry.key == key) |
339 |
| - else { |
| 340 | + if let Some((key, value)) = res.remove_entry(&key) { |
| 341 | + res = BTreeMap::new(); |
| 342 | + res.insert(key, value); |
| 343 | + } else { |
340 | 344 | anyhow::bail!("no value found for key `{key}`");
|
341 |
| - }; |
342 |
| - NameCommandResult::UserData(VecMap { |
343 |
| - contents: vec![value], |
344 |
| - }) |
345 |
| - } else { |
346 |
| - NameCommandResult::UserData(entry.name_record.data) |
| 345 | + } |
347 | 346 | }
|
| 347 | + NameCommandResult::UserData(res) |
348 | 348 | }
|
349 | 349 | Self::List { address } => {
|
350 |
| - let mut nfts = get_owned_nfts::<IotaNamesRegistration>(address, context).await?; |
351 |
| - let subdomain_nfts = |
352 |
| - get_owned_nfts::<SubdomainRegistration>(address, context).await?; |
353 |
| - nfts.extend(subdomain_nfts.into_iter().map(|nft| nft.into_inner())); |
| 350 | + let client = context.get_client().await?; |
| 351 | + let address = get_identity_address(address.map(KeyIdentity::Address), context)?; |
| 352 | + let mut cursor = None; |
| 353 | + let mut nfts = Vec::new(); |
| 354 | + loop { |
| 355 | + let res = client |
| 356 | + .http() |
| 357 | + .iota_names_find_all_registration_nfts( |
| 358 | + address, |
| 359 | + cursor, |
| 360 | + None, |
| 361 | + Some(IotaObjectDataOptions::new().with_bcs()), |
| 362 | + ) |
| 363 | + .await?; |
| 364 | + for obj in res.data { |
| 365 | + nfts.push(deserialize_move_object_from_bcs(obj)?); |
| 366 | + } |
| 367 | + if res.has_next_page { |
| 368 | + cursor = res.next_cursor; |
| 369 | + } else { |
| 370 | + break; |
| 371 | + } |
| 372 | + } |
354 | 373 | NameCommandResult::List(nfts)
|
355 | 374 | }
|
356 | 375 | Self::Lookup { domain } => {
|
357 |
| - let entry = get_registry_entry(&domain, context).await?; |
| 376 | + let client = context.get_client().await?; |
| 377 | + let res = client |
| 378 | + .http() |
| 379 | + .iota_names_lookup(&domain.to_string()) |
| 380 | + .await? |
| 381 | + .ok_or_else(|| anyhow!("no record found for {domain}"))?; |
358 | 382 | NameCommandResult::Lookup {
|
359 | 383 | domain,
|
360 |
| - target_address: entry.name_record.target_address, |
| 384 | + target_address: res.target_address, |
361 | 385 | }
|
362 | 386 | }
|
363 | 387 | Self::Register { domain, coin, opts } => {
|
@@ -458,13 +482,16 @@ impl NameCommand {
|
458 | 482 | )
|
459 | 483 | }
|
460 | 484 | Self::ReverseLookup { address } => {
|
| 485 | + let client = context.get_client().await?; |
461 | 486 | let address = get_identity_address(address.map(KeyIdentity::Address), context)?;
|
462 |
| - let entry = get_reverse_registry_entry(address, context).await?; |
| 487 | + let domain = client |
| 488 | + .http() |
| 489 | + .iota_names_reverse_lookup(address) |
| 490 | + .await? |
| 491 | + .map(|s| s.parse()) |
| 492 | + .transpose()?; |
463 | 493 |
|
464 |
| - NameCommandResult::ReverseLookup { |
465 |
| - address, |
466 |
| - domain: entry.map(|e| e.domain), |
467 |
| - } |
| 494 | + NameCommandResult::ReverseLookup { address, domain } |
468 | 495 | }
|
469 | 496 | Self::SetReverseLookup { domain, opts } => {
|
470 | 497 | // Check ownership of the name off-chain to avoid potentially wasting gas
|
@@ -887,7 +914,7 @@ pub enum NameCommandResult {
|
887 | 914 | address: IotaAddress,
|
888 | 915 | domain: Option<Domain>,
|
889 | 916 | },
|
890 |
| - UserData(VecMap<String, String>), |
| 917 | + UserData(BTreeMap<String, String>), |
891 | 918 | List(Vec<IotaNamesRegistration>),
|
892 | 919 | }
|
893 | 920 |
|
@@ -940,8 +967,8 @@ impl std::fmt::Display for NameCommandResult {
|
940 | 967 | let mut table_builder = TableBuilder::default();
|
941 | 968 | table_builder.set_header(["key", "value"]);
|
942 | 969 |
|
943 |
| - for entry in &entries.contents { |
944 |
| - table_builder.push_record([&entry.key, &entry.value]); |
| 970 | + for (key, value) in entries { |
| 971 | + table_builder.push_record([key, value]); |
945 | 972 | }
|
946 | 973 |
|
947 | 974 | let mut table = table_builder.build();
|
@@ -1101,36 +1128,6 @@ async fn get_proxy_nft_by_name(
|
1101 | 1128 | })
|
1102 | 1129 | }
|
1103 | 1130 |
|
1104 |
| -async fn get_registry_entry( |
1105 |
| - domain: &Domain, |
1106 |
| - context: &mut WalletContext, |
1107 |
| -) -> anyhow::Result<RegistryEntry> { |
1108 |
| - let client = context.get_client().await?; |
1109 |
| - let iota_names_config = get_iota_names_config(&client).await?; |
1110 |
| - let object_id = iota_names_config.record_field_id(domain); |
1111 |
| - |
1112 |
| - get_object_from_bcs(&client, object_id).await |
1113 |
| -} |
1114 |
| - |
1115 |
| -async fn get_reverse_registry_entry( |
1116 |
| - address: IotaAddress, |
1117 |
| - context: &mut WalletContext, |
1118 |
| -) -> anyhow::Result<Option<ReverseRegistryEntry>> { |
1119 |
| - let client = context.get_client().await?; |
1120 |
| - let iota_names_config = get_iota_names_config(&client).await?; |
1121 |
| - let object_id = iota_names_config.reverse_record_field_id(&address); |
1122 |
| - let response = client |
1123 |
| - .read_api() |
1124 |
| - .get_object_with_options(object_id, IotaObjectDataOptions::new().with_bcs()) |
1125 |
| - .await?; |
1126 |
| - |
1127 |
| - if response.data.is_some() { |
1128 |
| - Ok(Some(deserialize_move_object_from_bcs(response)?)) |
1129 |
| - } else { |
1130 |
| - Ok(None) |
1131 |
| - } |
1132 |
| -} |
1133 |
| - |
1134 | 1131 | async fn get_iota_names_config(client: &IotaClient) -> anyhow::Result<IotaNamesConfig> {
|
1135 | 1132 | let chain_identifier = client.read_api().get_chain_identifier().await?;
|
1136 | 1133 | let chain = ChainIdentifier::from_chain_short_id(&chain_identifier)
|
|
0 commit comments