Created
October 14, 2024 06:59
-
-
Save crypt0miester/5884ced73574f6facd36974a48c3fdd6 to your computer and use it in GitHub Desktop.
set of functions that allows you to integrate domain names and have them show up where wallet ady is normally displayed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { MainDomain, NameAccountAndDomain, TldParser } from "@onsol/tldparser"; | |
import { Connection, PublicKey } from "@solana/web3.js"; | |
/** | |
* Validates and resolves an Eclipse address or domain to a PublicKey. | |
* Mostly used in searches. | |
* Ideally the value is debounced on typing | |
* | |
* This function can handle two types of input: | |
* 1. An Eclipse domain (e.g., "miester.turbo") | |
* 2. An Eclipse public key (as a base58 encoded string) | |
* | |
* For domains, it resolves the owner's public key. | |
* For public keys, it validates the key and returns it as a PublicKey object. | |
* | |
* @param {Connection} connection - A connection to an Eclipse cluster | |
* @param {string} domainOrPubkey - A Eclipse domain or public key string | |
* @returns {Promise<PublicKey | undefined>} The resolved PublicKey or undefined if not found or invalid | |
*/ | |
export async function validateEclipseAddress( | |
connection: Connection, | |
domainOrPubkey: string, | |
): Promise<PublicKey | undefined> { | |
if (domainOrPubkey.includes('.') && domainOrPubkey.split('.').length === 2) { | |
const parser = new TldParser(connection); | |
return await parser.getOwnerFromDomainTld(domainOrPubkey); | |
} | |
try { | |
const userAddress = new PublicKey(domainOrPubkey); | |
return PublicKey.isOnCurve(userAddress.toBytes()) ? userAddress : undefined; | |
} catch { | |
return undefined; | |
} | |
} | |
/** | |
* Retrieves the main domain associated with a given Eclipse public key. | |
* | |
* A user can set one of their domains as the "main" domain. This function | |
* fetches that main domain if it exists. | |
* | |
* @param {Connection} connection - A connection to an Eclipse cluster | |
* @param {PublicKey} publicKey - The public key to query for the main domain | |
* @returns {Promise<MainDomain | undefined>} The main domain associated with the public key or undefined if not found | |
*/ | |
export async function getUserMainDomain( | |
connection: Connection, | |
publicKey: PublicKey, | |
): Promise<MainDomain | undefined> { | |
const parser = new TldParser(connection); | |
try { | |
return await parser.getMainDomain(publicKey); | |
} catch { | |
return undefined; | |
} | |
} | |
/** | |
* Retrieves all domains associated with a given Eclipse public key. | |
* | |
* This function fetches all domains owned by the specified public key, | |
* regardless of whether they are set as the main domain or not. | |
* | |
* @param {Connection} connection - A connection to an Eclipse cluster | |
* @param {PublicKey} publicKey - The public key to query for domains | |
* @returns {Promise<NameAccountAndDomain[] | undefined>} An array of all domains associated with the public key or undefined if none found | |
*/ | |
export async function getUserDomains( | |
connection: Connection, | |
publicKey: PublicKey, | |
): Promise<NameAccountAndDomain[] | undefined> { | |
const parser = new TldParser(connection); | |
try { | |
const domains = await parser.getParsedAllUserDomains(publicKey); | |
return domains && domains.length > 0 ? domains : undefined; | |
} catch { | |
return undefined; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment