From 14502f405ff82054c47977a1f148131861635751 Mon Sep 17 00:00:00 2001 From: SteveRogers Date: Wed, 25 Dec 2024 14:05:06 +0800 Subject: [PATCH 1/2] docs: add comment of ens resolver --- packages/web3-eth-ens/src/resolver.ts | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/packages/web3-eth-ens/src/resolver.ts b/packages/web3-eth-ens/src/resolver.ts index d0b4f5f20ea..4d3ed701a47 100644 --- a/packages/web3-eth-ens/src/resolver.ts +++ b/packages/web3-eth-ens/src/resolver.ts @@ -29,19 +29,36 @@ import { namehash } from './utils.js'; // https://github.com/ensdomains/resolvers/blob/master/contracts/PublicResolver.sol export class Resolver { + /** + * The ENS registry instance used to resolve ENS names. + */ private readonly registry: Registry; + /** + * Creates a new instance of the `Resolver` class. + * @param registry - An instance of the ENS `Registry`. + */ public constructor(registry: Registry) { this.registry = registry; } + /** + * Retrieves the resolver contract for the specified ENS name. + * @param ENSName - The ENS name to resolve. + * @returns A Promise that resolves to the resolver contract. + */ private async getResolverContractAdapter(ENSName: string) { // TODO : (Future 4.1.0 TDB) cache resolver contract if frequently queried same ENS name, refresh cache based on TTL and usage, also limit cache size, optional cache with a flag return this.registry.getResolver(ENSName); } - // https://eips.ethereum.org/EIPS/eip-165 - // eslint-disable-next-line class-methods-use-this + /** + * Checks if a resolver contract supports a specific interface. + * Relative EIP: https://eips.ethereum.org/EIPS/eip-165 + * @param resolverContract - The resolver contract instance. + * @param methodName - The method name to check for interface support. + * @throws ResolverMethodMissingError if the interface is not supported. + */ public async checkInterfaceSupport( resolverContract: Contract, methodName: string, @@ -129,3 +146,4 @@ export class Resolver { return resolverContract.methods.name(namehash(reverseName)).call(); } } + From 74c90071b5fdf9a8d50573f3bc0ed6863d782bdd Mon Sep 17 00:00:00 2001 From: SteveRogers Date: Wed, 25 Dec 2024 14:06:29 +0800 Subject: [PATCH 2/2] docs: add comment of other funcs for ENS resolver --- packages/web3-eth-ens/src/resolver.ts | 42 ++++++++++++++++++++++++++- 1 file changed, 41 insertions(+), 1 deletion(-) diff --git a/packages/web3-eth-ens/src/resolver.ts b/packages/web3-eth-ens/src/resolver.ts index 4d3ed701a47..e4e48fcbfda 100644 --- a/packages/web3-eth-ens/src/resolver.ts +++ b/packages/web3-eth-ens/src/resolver.ts @@ -80,6 +80,12 @@ export class Resolver { ); } + /** + * Checks if a resolver contract supports a specific interface by its ID. + * @param ENSName - The ENS name to resolve. + * @param interfaceId - The interface ID to check. + * @returns A Promise that resolves to a boolean indicating support. + */ public async supportsInterface(ENSName: string, interfaceId: string) { const resolverContract = await this.getResolverContractAdapter(ENSName); @@ -96,7 +102,12 @@ export class Resolver { return resolverContract.methods.supportsInterface(interfaceIdParam).call(); } - // eslint-disable-next-line @typescript-eslint/no-inferrable-types + /** + * Retrieves the address associated with an ENS name for a specific coin type. + * @param ENSName - The ENS name to resolve. + * @param coinType - The coin type (default is 60 for Ethereum). + * @returns A Promise that resolves to the address. + */ public async getAddress(ENSName: string, coinType: number = 60) { const resolverContract = await this.getResolverContractAdapter(ENSName); @@ -105,6 +116,11 @@ export class Resolver { return resolverContract.methods.addr(namehash(ENSName), coinType).call(); } + /** + * Retrieves the public key associated with an ENS name. + * @param ENSName - The ENS name to resolve. + * @returns A Promise that resolves to the public key. + */ public async getPubkey(ENSName: string) { const resolverContract = await this.getResolverContractAdapter(ENSName); @@ -113,6 +129,11 @@ export class Resolver { return resolverContract.methods.pubkey(namehash(ENSName)).call(); } + /** + * Retrieves the content hash associated with an ENS name. + * @param ENSName - The ENS name to resolve. + * @returns A Promise that resolves to the content hash. + */ public async getContenthash(ENSName: string) { const resolverContract = await this.getResolverContractAdapter(ENSName); @@ -121,6 +142,13 @@ export class Resolver { return resolverContract.methods.contenthash(namehash(ENSName)).call(); } + /** + * Sets the address for an ENS name. + * @param ENSName - The ENS name to set the address for. + * @param address - The address to set. + * @param txConfig - The transaction configuration options. + * @returns A Promise that resolves when the transaction is complete. + */ public async setAddress(ENSName: string, address: Address, txConfig: PayableCallOptions) { const resolverContract = await this.getResolverContractAdapter(ENSName); await this.checkInterfaceSupport(resolverContract, methodsInInterface.setAddr); @@ -128,6 +156,12 @@ export class Resolver { return resolverContract.methods.setAddr(namehash(ENSName), address).send(txConfig); } + /** + * Retrieves a text record for an ENS name. + * @param ENSName - The ENS name to resolve. + * @param key - The text record key. + * @returns A Promise that resolves to the text record value. + */ public async getText(ENSName: string, key: string) { const resolverContract = await this.getResolverContractAdapter(ENSName); await this.checkInterfaceSupport(resolverContract, methodsInInterface.text); @@ -135,6 +169,12 @@ export class Resolver { return resolverContract.methods.text(namehash(ENSName), key).call(); } + /** + * Retrieves the ENS name associated with an address. + * @param address - The address to resolve. + * @param checkInterfaceSupport - Whether to check for interface support (default: true). + * @returns A Promise that resolves to the ENS name. + */ public async getName(address: string, checkInterfaceSupport = true) { const reverseName = `${address.toLowerCase().substring(2)}.addr.reverse`;