feat: added langchain class, refactored methods to use tlb parser, wrote and ran tests

This commit is contained in:
adpthegreat
2024-12-21 20:53:00 +01:00
parent beaf061929
commit cdec8ed32d
15 changed files with 884 additions and 154 deletions

View File

@@ -1,42 +1,22 @@
import { Buffer } from "buffer";
import { PublicKey } from "@solana/web3.js";
import {
getNameAccountKeySync,
NAME_PROGRAM_ID,
} from "@bonfida/spl-name-service";
import { TldParser } from "@onsol/tldparser";
import { SolanaAgentKit } from "../index";
import { PublicKey } from "@solana/web3.js";
/**
* Resolve a domain to its public key
* Resolve all domains for a given agent and domain
* @param agent SolanaAgentKit instance
* @param domain Domain name to resolve
* @returns Associated public key or null if not found
* @returns Promise resolving to the domain or undefined if not found
*/
export async function resolveAllDomains(
agent: SolanaAgentKit,
domain: string
): Promise<PublicKey | null> {
): Promise<PublicKey | undefined> {
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));
let tld = new TldParser(agent.connection).getOwnerFromDomainTld(domain)
return tld;
} catch (error: any) {
throw new Error(`Domain resolution failed: ${error.message}`);
}
}