diff --git a/src/tools/get_all_active_tlds.ts b/src/tools/get_all_active_tlds.ts index e69de29..929fbfc 100644 --- a/src/tools/get_all_active_tlds.ts +++ b/src/tools/get_all_active_tlds.ts @@ -0,0 +1,65 @@ +import { + NameRegistryState, + getAllDomains, + ROOT_DOMAIN_ACCOUNT, +} from "@bonfida/spl-name-service"; +import { Connection, PublicKey } from "@solana/web3.js"; +import { SolanaAgentKit } from "../index"; + +/** + * Get all active top-level domains (TLDs) in the Solana name service + * @param agent SolanaAgentKit instance + * @returns Array of active TLD strings + */ +export async function getAllDomainsTLDs( + agent: SolanaAgentKit +): Promise { + try { + // Get the root domain record + const rootAccount = await NameRegistryState.retrieve( + agent.connection, + ROOT_DOMAIN_ACCOUNT + ); + + if (!rootAccount) { + throw new Error("Root domain account not found"); + } + + // Fetch all TLD records under the root domain + const tldAccounts = await agent.connection.getProgramAccounts( + new PublicKey("namesLPneVptA9Z5rqUDD9tMTWEJwofgaYwp8cawRkX"), + { + filters: [ + { + memcmp: { + offset: 0, + bytes: rootAccount.registry.owner.toBase58(), + }, + }, + ], + } + ); + + // Parse the TLD names from the accounts + const tlds: string[] = []; + for (const account of tldAccounts) { + const registry = await NameRegistryState.retrieve( + agent.connection, + account.pubkey + ); + + if (registry && registry.registry.data) { + const tldName = Buffer.from(registry.registry.data) + .toString() + .replace(/\0/g, ""); + if (tldName) { + tlds.push(tldName); + } + } + } + + return tlds; + } catch (error: any) { + throw new Error(`Failed to fetch TLDs: ${error.message}`); + } +} diff --git a/src/tools/get_all_registered_all_domains.ts b/src/tools/get_all_registered_all_domains.ts index e69de29..dc0f0a4 100644 --- a/src/tools/get_all_registered_all_domains.ts +++ b/src/tools/get_all_registered_all_domains.ts @@ -0,0 +1,36 @@ +import { getAllDomains } from "@bonfida/spl-name-service"; +import { SolanaAgentKit } from "../agent"; +import { PublicKey } from "@solana/web3.js"; +import { getAllDomainsTLDs } from "./get_all_active_tlds"; + +/** + * Get all registered domains across all TLDs + * @param agent SolanaAgentKit instance + * @returns Array of all registered domain names with their TLDs + */ +export async function getAllRegisteredAllDomains( + agent: SolanaAgentKit +): Promise { + try { + // First get all TLDs + const tlds = await getAllDomainsTLDs(agent); + const allDomains: string[] = []; + + // For each TLD, fetch all registered domains + for (const tld of tlds) { + const domains = await getAllDomains( + agent.connection, + new PublicKey("namesLPneVptA9Z5rqUDD9tMTWEJwofgaYwp8cawRkX"), + ); + + // Add domains with TLD suffix + domains.forEach((domain) => { + allDomains.push(`${domain}.${tld}`); + }); + } + + return allDomains; + } catch (error: any) { + throw new Error(`Failed to fetch all registered domains: ${error.message}`); + } +} diff --git a/src/tools/lookup_owner.ts b/src/tools/lookup_owner.ts index 9243467..cffd264 100644 --- a/src/tools/lookup_owner.ts +++ b/src/tools/lookup_owner.ts @@ -14,7 +14,7 @@ export async function getOwnedAllDomains( ): Promise { try { const domains = await getAllDomains(agent.connection, owner); - return domains.map(domain => domain.name); + return domains.map(domain => domain.toString()); } catch (error: any) { throw new Error(`Failed to fetch owned domains: ${error.message}`); }