added domain methods

This commit is contained in:
adpthegreat
2024-12-20 19:25:30 +01:00
parent 8d35d9b4c0
commit fbd6ac6002
6 changed files with 107 additions and 0 deletions

View File

View File

@@ -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<string[]> {
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}`);
}
}

View File

@@ -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<string | null> {
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}`);
}
}

21
src/tools/lookup_owner.ts Normal file
View File

@@ -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<string[]> {
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}`);
}
}

View File

@@ -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<PublicKey | null> {
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}`);
}
}