From fbd6ac600226dbe5435c4dd76a3d8322ac855952 Mon Sep 17 00:00:00 2001 From: adpthegreat Date: Fri, 20 Dec 2024 19:25:30 +0100 Subject: [PATCH] added domain methods --- src/tools/get_all_active_tlds.ts | 0 src/tools/get_all_registered_all_domains.ts | 0 src/tools/get_domains_on_tld.ts | 23 +++++++++++ src/tools/get_main_domain.ts | 21 +++++++++++ src/tools/lookup_owner.ts | 21 +++++++++++ src/tools/resolve_domain.ts | 42 +++++++++++++++++++++ 6 files changed, 107 insertions(+) create mode 100644 src/tools/get_all_active_tlds.ts create mode 100644 src/tools/get_all_registered_all_domains.ts create mode 100644 src/tools/get_domains_on_tld.ts create mode 100644 src/tools/get_main_domain.ts create mode 100644 src/tools/lookup_owner.ts create mode 100644 src/tools/resolve_domain.ts diff --git a/src/tools/get_all_active_tlds.ts b/src/tools/get_all_active_tlds.ts new file mode 100644 index 0000000..e69de29 diff --git a/src/tools/get_all_registered_all_domains.ts b/src/tools/get_all_registered_all_domains.ts new file mode 100644 index 0000000..e69de29 diff --git a/src/tools/get_domains_on_tld.ts b/src/tools/get_domains_on_tld.ts new file mode 100644 index 0000000..045f44a --- /dev/null +++ b/src/tools/get_domains_on_tld.ts @@ -0,0 +1,23 @@ +import { SolanaAgentKit } from "../agent"; +import { getOwnedAllDomains } from "./lookup_owner"; +import { PublicKey } from "@solana/web3.js"; + +/** + * Get all domains owned by an address for a specific TLD + * @param agent SolanaAgentKit instance + * @param owner Owner's public key + * @param tld Top-level domain (e.g., "sol") + * @returns Array of owned domain names for the specified TLD + */ +export async function getOwnedDomainsForTLD( + agent: SolanaAgentKit, + owner: PublicKey, + tld: string +): Promise { + try { + const allDomains = await getOwnedAllDomains(agent, owner); + return allDomains.filter(domain => domain.endsWith(`.${tld}`)); + } catch (error: any) { + throw new Error(`Failed to fetch domains for TLD: ${error.message}`); + } +} diff --git a/src/tools/get_main_domain.ts b/src/tools/get_main_domain.ts new file mode 100644 index 0000000..775c343 --- /dev/null +++ b/src/tools/get_main_domain.ts @@ -0,0 +1,21 @@ +import { getFavoriteDomain as _getFavoriteDomain } from "@bonfida/spl-name-service"; +import { PublicKey } from "@solana/web3.js"; + + +/** + * Get the user's main/favorite domain for a SolanaAgentKit instance + * @param agent SolanaAgentKit instance + * @param owner Owner's public key + * @returns Promise resolving to the main domain name or null if not set + */ +export async function getMainAllDomainsDomain( + agent: any, + owner: PublicKey +): Promise { + try { + const favDomain = await _getFavoriteDomain(agent.connection, owner); + return favDomain.stale ? null : favDomain.reverse; + } catch (error: any) { + throw new Error(`Failed to fetch main domain: ${error.message}`); + } +} \ No newline at end of file diff --git a/src/tools/lookup_owner.ts b/src/tools/lookup_owner.ts new file mode 100644 index 0000000..9243467 --- /dev/null +++ b/src/tools/lookup_owner.ts @@ -0,0 +1,21 @@ +import { getAllDomains } from "@bonfida/spl-name-service"; +import { SolanaAgentKit } from "../agent"; +import { PublicKey } from "@solana/web3.js"; + +/** + * Get all domains owned by a specific address + * @param agent SolanaAgentKit instance + * @param owner Owner's public key + * @returns Array of owned domain names + */ +export async function getOwnedAllDomains( + agent: SolanaAgentKit, + owner: PublicKey +): Promise { + try { + const domains = await getAllDomains(agent.connection, owner); + return domains.map(domain => domain.name); + } catch (error: any) { + throw new Error(`Failed to fetch owned domains: ${error.message}`); + } +} diff --git a/src/tools/resolve_domain.ts b/src/tools/resolve_domain.ts new file mode 100644 index 0000000..3ea5e41 --- /dev/null +++ b/src/tools/resolve_domain.ts @@ -0,0 +1,42 @@ +import { Buffer } from "buffer"; +import { PublicKey } from "@solana/web3.js"; +import { + getNameAccountKeySync, + NAME_PROGRAM_ID, +} from "@bonfida/spl-name-service"; +import { SolanaAgentKit } from "../index"; + +/** + * Resolve a domain to its public key + * @param agent SolanaAgentKit instance + * @param domain Domain name to resolve + * @returns Associated public key or null if not found + */ +export async function resolveAllDomains( + agent: SolanaAgentKit, + domain: string +): Promise { + try { + // Convert domain name to buffer for hashing + const hashedDomain = Buffer.from(domain); + + // Get the name account key using the new sync method + const nameAccountKey = getNameAccountKeySync( + hashedDomain, + undefined, // nameClass + undefined // nameParent + ); + + // Get the account info to retrieve the owner + const owner = await agent.connection.getAccountInfo(nameAccountKey); + + if (!owner) { + return null; + } + + // The owner's public key is stored in the data buffer starting at offset 32 + return new PublicKey(owner.data.slice(32, 64)); + } catch (error: any) { + throw new Error(`Domain resolution failed: ${error.message}`); + } +}