Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add comment for ens resolver #7428

Open
wants to merge 2 commits into
base: 4.x
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 61 additions & 3 deletions packages/web3-eth-ens/src/resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<typeof PublicResolverAbi>,
methodName: string,
Expand All @@ -63,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);

Expand All @@ -79,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);

Expand All @@ -88,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);

Expand All @@ -96,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);

Expand All @@ -104,20 +142,39 @@ 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);

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);

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`;

Expand All @@ -129,3 +186,4 @@ export class Resolver {
return resolverContract.methods.name(namehash(reverseName)).call();
}
}