mirror of
https://github.com/d0zingcat/solana-agent-kit.git
synced 2026-05-13 23:16:55 +00:00
added domain methods
This commit is contained in:
0
src/tools/get_all_active_tlds.ts
Normal file
0
src/tools/get_all_active_tlds.ts
Normal file
0
src/tools/get_all_registered_all_domains.ts
Normal file
0
src/tools/get_all_registered_all_domains.ts
Normal file
23
src/tools/get_domains_on_tld.ts
Normal file
23
src/tools/get_domains_on_tld.ts
Normal 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}`);
|
||||
}
|
||||
}
|
||||
21
src/tools/get_main_domain.ts
Normal file
21
src/tools/get_main_domain.ts
Normal 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
21
src/tools/lookup_owner.ts
Normal 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}`);
|
||||
}
|
||||
}
|
||||
42
src/tools/resolve_domain.ts
Normal file
42
src/tools/resolve_domain.ts
Normal 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}`);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user