bitpro가 원문에서 글자만 모은 내용이에요. 표·그림·수식은 빠지거나 깨질 수 있어요. 전체 75쪽 중 9쪽
ployments page](/learn/deployments).
:::
The public resolver is a general-purpose ENS resolver that is suitable for most user needs.
It permits the owner of a name to update their records, includes permissions, and stores its data on layer-1 ethereum.
Most ENS names registered through the ENS Manager will use the latest version of the public resolver by default.
Names that resolve to a supported public resolver are editable from within the ENS Manager.
If you'd like to take a peek under the hood you can view the [public resolver source code](https://github.com/ensdomains/ens-contracts/blob/staging/contracts/resolvers/PublicResolver.sol) on GitHub.
### Features
The public resolver supports the following features:
* [EIP-137](https://eips.ethereum.org/EIPS/eip-137) - Contract address interface (`addr()`)
* [EIP-165](https://eips.ethereum.org/EIPS/eip-165) - Interface Detection (`supportsInterface()`)
* [EIP-181](https://eips.ethereum.org/EIPS/eip-181) - Reverse Resolution (`name()`)
* [EIP-205](https://eips.ethereum.org/EIPS/eip-205) - ABI Resolution for contracts (`ABI()`)
* [EIP-619](https://eips.ethereum.org/EIPS/eip-619) - SECP256k1 public keys (`pubkey()`)
* [EIP-634](https://eips.ethereum.org/EIPS/eip-634) - Text records (`text()`)
* [EIP-1577](https://eips.ethereum.org/EIPS/eip-1577) - Content hash resolution (`contenthash()`)
* [EIP-2304](https://eips.ethereum.org/EIPS/eip-2304) - Multicoin support (`addr()`)
* [ENSIP-19](/ensip/19#default-address) - Default EVM address resolution
:::note
While the `PublicResolver` provides a default implementation, remember that custom resolvers exist. It is therefore not recommended to hardcode any addresses. To ensure a safe implementation, always use the `supportsInterface()` method to check for the existence of a specific interface. See [Interacting with a Resolver](/resolvers/interacting) for how to do this.
:::
### Default EVM Address Resolution
The latest public resolver supports the notion of a "default EVM address" for a name, represented by coinType `0`. If this is set, it will be returned by the `addr(bytes32 node, uint256 coinType)` function regardless of the `coinType` parameter, unless overridden by a more specific address.
This pairs nicely with the default reverse registrar when dealing with [L2 primary names](/web/reverse#l2-primary-names).
## Resolvers Quickstart
At the heart of every ENS name is its resolver. A resolver is a smart contract that implements a specific set of Resolver features (see [Resolver Interface](/resolvers/interfaces)).
The resolvers smart contract functions have control over the resolution process of a ["node"](/resolution/names#namehash) (a name or subdomain) and onwards (subdomains of itself).
### Basic Resolver
A naive but very plausible example of a resolver is the following.
```solidity
contract MyResolver {
function addr(bytes32 node) external pure returns (address) {
return 0x225f137127d9067788314bc7fcc1f36746a3c3B5;
}
function supportsInterface(
bytes4 interfaceID
) external pure returns (bool) {
return
interfaceID == this.addr.selector ||
interfaceID == this.supportsInterface.selector;
}
}
```
Notice how the above would always return the same address regardless of the name it is queried for.
If you want to [write your own resolver resolver](/resolvers/writing), or see the [interface reference](/resolvers/interfaces).
### Public Resolver
The default resolver for all names is the Public Resolver, a swiss army knife of resolvers, written by the ENS Labs team, feature-packed with everything your everyday user might need.
You can read more about the [Public Resolver](/resolvers/public).
### Interacting with a resolver
Depending on the resolver in charge of a name, certain frontend apps will be able to interact with them as well.
This means you can set your favourite records, upgrade your name to different logic, and more, from your dApp of choice.
Are you writing a dApp and want to build this? Checkout the [Interacting with a Resolver](/resolvers/interacting) section.
### Offchain Resolution
Although by default ENS resolution is done onchain. You can leverage the power of CCIP Read to redirect resolution to an offchain gateway.
More about writing a CCIP Read-enabled resolver [here](/resolvers/ccip-read).
import { InterfaceDetails } from '../../components/InterfaceDetails'
import { universalresolver_methods } from '../../data/universal-resolver'
## Universal Resolver \[A swiss army knife for resolution.]
### Overview
:::note
Application developers typically don't need to interact with the universal resolver directly. This guide is directed at lower level-library developers.
:::
The Universal Resolver is a smart contract that simplifies the process of resolving ENS names. It's the recommended way to implement ENS resolution in modern libraries.
Adopting the universal resolver will make the transition to [ENSv2](https://ens.domains/ensv2) seamless.
### Test Case
You might be asking yourself, "How do I know if my application is already using the Universal Resolver?"
To answer that question, we've prepared a test case. If you're using the Universal Resolver, your app should resolve `ur.integration-tests.eth` to `0x2222222222222222222222222222222222222222`. If it's not, it will resolve to `0x1111111111111111111111111111111111111111`.
### Implementation Guide
The Universal Resolver should be treated as the canonical entrypoint to ENS for name resolution. `0xeEeEEEeE14D718C2B47D9923Deab1335E144EeEe` is the official deployment address on Ethereum Mainnet and testnets, which is a proxy contract owned by ENS DAO that will be upgraded to support [ENSv2](https://ens.domains/ensv2) in the future.
This guide assumes that your application already supports [CCIP Read](https://eips.ethereum.org/EIPS/eip-3668).
#### Forward Resolution
To resolve one or more records for a name, use the `resolve(bytes name, bytes data)` method which returns `(bytes data, address resolver)`.
The `name` argument is the [DNS-encoded](/resolution/names#dns) version of the name. Make sure to [normalize](/resolution/names#normalize) the name first, as well! For example, given the name `My.Name.eth`:
1. Normalize:
* `My.Name.eth` -> `my.name.eth`
2. DNS Encode:
* `my.name.eth` -> `0x026d79046e616d650365746800`
The `data` argument is a single ABI-encoded call to the resolver for that name, or a multicall encoded via the following interface:
```solidity
interface IMulticallable {
function multicall(bytes[] calldata data) external view returns (bytes[] memory results);
}
```
For example, if you want to resolve the ETH address and description text record for `nick.eth`, your logic to generate the `data` parameter would look something like this:
```ts
import { namehash, normalize } from 'viem/ens'
import { encodeFunctionData, parseAbi } from 'viem/utils'
const simpleResolverAbi = parseAbi([
'function addr(bytes32 node) view returns (address)',
'function text(bytes32 node, string key) view returns (string)',
])
const multicallAbi = parseAbi([
'function multicall(bytes[] data) returns (bytes[] results)',
])
const name = normalize('nick.eth')
const node = namehash(name)
const resolverCalls = [
{
abi: simpleResolverAbi,
functionName: 'addr',
args: [node],
},
{
abi: simpleResolverAbi,
functionName: 'text',
args: [node, 'description'],
},
] as const
const data = encodeFunctionData({
abi: multicallAbi,
functionName: 'multicall',
args: [resolverCalls.map((call) => encodeFunctionData(call))],
})
```
Learn about [standard resolver methods](/resolvers/interfaces) for ABI-encoding that should be added to `simpleResolverAbi` in production.
The response you'll get back from `resolve` will have to be decoded against the multicall ABI, then further decoded against the base resolver ABI. This results in the ETH address and value of the description text record. The rest of the logic would look something like this:
```ts
import { createPublicClient, decodeFunctionResult, http, toHex } from 'viem'
import { mainnet } from 'viem/chains'
import { packetToBytes } from 'viem/ens'
// ...adding from the above example
const dnsEncodedName = toHex(packetToBytes(name))
const universalResolverAbi = parseAbi([
'error ResolverNotFound(bytes name)',
'error ResolverNotContract(bytes name, address resolver)',
'error UnsupportedResolverProfile(bytes4 selector)',
'error ResolverError(bytes errorData)',
'error ReverseAddressMismatch(string primary, bytes primaryAddress)',
'error HttpError(uint16 status, string message)',
'function resolve(bytes name, bytes data) view returns (bytes result, address resolver)',
'function reverse(bytes lookupAddress, uint256 coinType) view returns (string primary, address resolver, address reverseResolver)',
])
const client = createPublicClient({
chain: mainnet,
transport: http(),
})
const resolveRes = await client.readContract({
abi: universalResolverAbi,
address: '0xeEeEEEeE14D718C2B47D9923Deab1335E144EeEe',
functionName: 'resolve',
args: [dnsEncodedName, data],
})
const decodedMulticall = decodeFunctionResult({
abi: multicallAbi,
functionName: 'multicall',
data: resolveRes[0],
})
const decodedRes = decodedMulticall.map((res, i) =>
decodeFunctionResult({
abi: simpleResolverAbi,
functionName: resolverCalls[i].functionName,
data: res,
})
)
// decodedRes[0] = "0xb8c2C29ee19D8307cb7255e1Cd9CbDE883A267d5"
// decodedRes[1] = "Lead developer of ENS & Ethereum Foundation alum. Certified rat tickler. he/him."
```
#### Reverse Resolution
To reverse-resolve an address to an ENS name (go from address to name), call the `reverse (bytes lookupAddress, uint256 coinType)` method which returns `(string primary, address resolver, address reverseResolver)`.
The `lookupAddress` argument is the Ethereum address of the account you want to fetch the primary name for. The `coinType` argument defines the chain you'd like to fetch the reverse record from. See [Multichain Addresses](/web/resolution#multichain-addresses) for more information about the `coinType` argument.
`reverse` internally checks that the name forward resolves to the address you're looking up, so your implementation doesn't need to do any additional checks and should be quite straightforward. It might look something like this:
```ts
import { createPublicClient, http, parseAbi } from 'viem'
import { mainnet } from 'viem/chains'
const address = '0xb8c2C29ee19D8307cb7255e1Cd9CbDE883A267d5'
const universalResolverAbi = parseAbi([
'error ResolverNotFound(bytes name)',
'error ResolverNotContract(bytes name, address resolver)',
'error UnsupportedResolverProfile(bytes4 selector)',
'error ResolverError(bytes errorData)',
'error ReverseAddressMismatch(string primary, bytes primaryAddress)',
'error HttpError(uint16 status, string message)',
'function resolve(bytes name, bytes data) view returns (bytes result, address resolver)',
'function reverse(bytes lookupAddress, uint256 coinType) view returns (string primary, address resolver, address reverseResolver)',
])
const client = createPublicClient({
chain: mainnet,
transport: http(),
})
const [primaryName] = await client.readContract({
abi: universalResolverAbi,
address: '0xeEeEEEeE14D718C2B47D9923Deab1335E144EeEe',
functionName: 'reverse',
args: [address, 60n],
})
// primaryName = "nick.eth"
```
#### Batch Gateways
The Universal Resolver utilizes a batch gateway to perform parallel [EIP-3668](https://eips.ethereum.org/EIPS/eip-3668) (CCIP-Read) requests and mitigate offchain server issues. [`CCIPBatcher.sol`](https://github.com/ensdomains/ens-contracts/blob/staging/contracts/ccipRead/CCIPBatcher.sol) is the recommended batch gateway client implementation. The protocol and server implementation is defined in [ENSIP-21: Batch Gateway Offchain Lookup Protocol](/ensip/21/) (BGOLP).
The latest batch gateways can be queried from the Universal Resolver via `batchGateways()` which returns `(string[] gateways)`.
1. **External Batch Gateway** `https://ccip-v3.ens.xyz` is a trustless service operated by ENS Labs which receives batch gateway requests, performs the supplied CCIP-Read requests in parallel, bundles up the responses or failures, and replies to the caller. This service acts like an open proxy. This mechanism functions in **ALL** clients that support CCIP-Read.
2. **Local Batch Gateway** `x-batch-gateway:true` is a [special-purpose URL](/ensip/21/) which notifies ENSIP-21 aware clients that the `OffchainLookup` is a BGOLP request and can be handled locally without using the **External Batch Gateway** service. The client follows the same process but the requests originate locally without an additional network hop to an external service. For unaware clients, the URL is ignored by the [Client Lookup Protocol](https://eips.ethereum.org/EIPS/eip-3668#client-lookup-protocol) in EIP-3668.
:::note
Currently only [viem](https://github.com/wevm/viem/blob/main/src/utils/ens/localBatchGatewayRequest.ts) supports ENSIP-21.
:::
## Writing a Resolver
Every ENS name has a resolver, which is responsible for resolving information about a name.
Resolvers are a core part of the ENS protocol. They give each name, represented as a ["node"](/resolution/names#namehash), the power to control the resolution process for itself and all of its subnames. Resolvers were originally standardized in [EIP 137](https://eips.ethereum.org/EIPS/eip-137), but have since received a few updates such as [EIP 181](https://eips.ethereum.org/EIPS/eip-181), [EIP 2304](https://eips.ethereum.org/EIPS/eip-2304), and [ENSIP-10](/ensip/10).
You can find the latest default resolver implementation, called the Public Resolver, on [GitHub](https://github.com/ensdomains/ens-contracts/blob/mainnet/contracts/resolvers/PublicResolver.sol) and [Etherscan](/learn/deployments).
### Resolver Interface
You can view an extended list of resolver methods [here](/resolvers/interfaces), however a simple interface might look something like this:
```solidity
interface IMyResolver {
function supportsInterface(bytes4 interfaceId) external view returns (bool);
function addr(bytes32 node) external view returns (address payable);
function addr(bytes32 node, uint256 coinType) external view returns (bytes memory);
function contenthash(bytes32 node) external view returns (bytes memory);
function text(bytes32 node, string calldata key) external view returns (string memory);
function setAddr(bytes32 node, address addr) external;
function setAddr(bytes32 node, uint256 coinType, bytes calldata a) external;
function setContenthash(bytes32 node, bytes calldata hash) external;
function setText(bytes32 node, string calldata key, string calldata value) external;
}
```
### Wildcard Resolution
In [ENSIP-10](/ensip/10) a new `resolve()` method was added to the resolver interface to allow for wildcard resolution.
```solidity
interface IExtendedResolver {
/**
* @dev Performs ENS name resolution for the supplied name and resolution data.
* @param name The name to resolve, in normalised and DNS-encoded form.
* @param data The resolution data, as specified in ENSIP-10.
* @return The result of resolving the name.
*/
function resolve(
bytes memory name,
bytes memory data
) external view returns (bytes memory);
}
```
:::note
Don't forget to add `0x9061b923` to your [EIP-165](https://eips.ethereum.org/) `supportsInterface()` implementation.
:::
### Onchain Resolvers
By default, ENS names use the [Public Resolver](/resolvers/public) which stores all data onchain. An extremely basic resolver that stores a mapping of ENS names to addresses might look like this:
```solidity
contract OnchainResolver {
mapping(bytes32 node => address addr) public addr;
function setAddr(bytes32 node, address _addr) external {
addr[node] = _addr;
}
function supportsInterface(
bytes4 interfaceID
) external pure returns (bool) {
return
interfaceID == OnchainResolver.supportsInterface.selector ||
// function addr(bytes32 node) external view returns (address)
interfaceID == 0x3b3b57de;
}
}
```
:::note
This is not secure since it doesn't implement any form of access control for updating a name's address, and is only suitable for demonstration purposes.
:::
Since the mapping is stored internally, it costs gas for the owner of a name to update their address. This is great for a maximal decentralization, but is not always practical.
### Offchain Resolvers
An offchain resolver is a resolver that implements CCIP Read to defer a name's resolution to an HTTP server. This server can then load data from any source including offchain databases, APIs, or other blockchains. [Learn more about CCIP Read](/resolvers/ccip-read).
An equivalent offchain resolver to the above onchain example looks something like this:
```solidity
contract OffchainResolver {
string public url =
"https://docs.ens.domains/api/example/basic-gateway";
error OffchainLookup(
address sender,
string[] urls,
bytes callData,
bytes4 callbackFunction,
bytes extraData
);
function addr(bytes32 node) external view returns (address) {
bytes memory callData = abi.encodeWithSelector(
OffchainResolver.addr.selector,
node
);
string[] memory urls = new string[](1);
urls[0] = url;
revert OffchainLookup(
address(this),
urls,
callData,
OffchainResolver.addrCallback.selector,
abi.encode(callData, address(this))
);
}
function addrCallback(
bytes calldata response,
bytes calldata
) external pure returns (address) {
address _addr = abi.decode(response, (address));
return _addr;
}
function supportsInterface(
bytes4 interfaceID
) external pure returns (bool) {
return
interfaceID == OffchainResolver.supportsInterface.selector ||
interfaceID == OffchainResolver.addr.selector;
}
}
```
:::note
This is not secure since it doesn't implement any form of verification in the callback function, and is only suitable for demonstration purposes.
:::
Any ENS name that sets its resolver to this contract would resolve to whatever address the Gateway returns, which can be changed at any time offchain for free. See the [gateway code here](https://github.com/ensdomains/docs/blob/master/functions/api/example/basic-gateway.ts).
For the same functionality to work with subnames, you'd need to implement the `resolve()` method from ENSIP-10. A feature-complete example can be found [here](https://github.com/ensdomains/ccip-tools/blob/master/contracts/OffchainResolver.sol), and easily deployed via [https://ccip-tools.pages.dev](https://ccip-tools.pages.dev).
import { Card } from '../../components/ui/Card'
## Resolution
The process by which we load information about a name is called resolution. It's a simple process, but it's important to understand.
Here is a diagram of some of the contracts involved when resolving a name.
<Card>
<img src="/img/resolution-diagram-1.png" alt="Diagram of the ENS resolution process" />
</Card>
The resolution process involves multiple parts. Most notably the [Registry](/registry/ens), multiple Registrars ([ETH Registrar](/registry/eth), [DNS Registrar](/registry/dns), [Reverse Registrar](/registry/reverse), etc)
and the concept of a [Resolver](/resolvers/quickstart).
### How to resolve
Here is a little peek at what happens under the hood of your favourite library when you do a name lookup.
#### 1. Find the Resolver
Every name has a "resolver". A resolver is simply a contract that implements the [resolver specification](/resolvers/quickstart) and can be queried for information about a name.
To get the resolver responsible for a name, you can query [The Registry](/registry/ens) for the `resolver` of a name.
:::code-group
```solidity [Solidity]
ENS.resolver(bytes32 node) view returns (address)
```
```tsx [Wagmi]
// https://wagmi.sh/react/api/hooks/useEnsResolver
import { normalize } from 'viem/ens'
import { useEnsResolver } from 'wagmi'
export const MyResolver = () => {
const { data: myResolver } = useEnsResolver({
name: normalize('nick.eth'), //프로젝트로 돌아가기