getPrimaryNameByAddress method

Future<String?> getPrimaryNameByAddress(
  1. String address
)

Returns the primary name for the given account address.

Implementation

Future<String?> getPrimaryNameByAddress(String address) async {
  final ansResource = await aptosClient.getAccountResource(
    contractAddress,
    "$contractAddress::domains::ReverseLookupRegistryV1",
  );
  final handle = ansResource["data"]["registry"]["handle"];
  final domainsTableItemRequest = TableItem(
    "address",
    "$contractAddress::domains::NameRecordKeyV1",
    address
  );

  try {
    final item = await aptosClient.queryTableItem(handle, domainsTableItemRequest);
    final vec = item["subdomain_name"]["vec"] as List;
    return vec.isNotEmpty ? "${vec.first}.${item["domain_name"]}" : item["domain_name"];
  } catch (e) {
    // response is 404 error - meaning item not found
    dynamic err = e;
    if (err.response?.statusCode == 404) {
      return null;
    }
    rethrow;
  }
}