Ethereum Name Service 백서

Ethereum Name Service

원문

bitpro가 원문에서 글자만 모은 내용이에요. 표·그림·수식은 빠지거나 깨질 수 있어요. 전체 75쪽 중 21쪽

avatar', 'https://example.com/avatar.png'], }) // Read them back using viem's built-in ENS functions const ethAddr = await client.getEnsAddress({ name }) const avatar = await client.getEnsText({ name, key: 'avatar' }) ``` #### Sharing Records Between Names Make `wallet.eth` serve the same record as `alice.eth`, then undo it: ```ts [Viem] import { namehash, toHex } from 'viem' import { packetToBytes } from 'viem/ens' // Link wallet.eth to the record alice.eth currently uses await wallet.writeContract({ address: resolverAddress, abi: permissionedResolverAbi, functionName: 'linkToNode', args: [ toHex(packetToBytes('wallet.eth')), // source: the name to link namehash('alice.eth'), // target: whose record to use ], }) // Unlink wallet.eth again (it then serves the default record) await wallet.writeContract({ address: resolverAddress, abi: permissionedResolverAbi, functionName: 'linkToRecord', args: [toHex(packetToBytes('wallet.eth')), 0n], }) ``` `linkToNode` reverts with `InvalidRecord` if the target name has no record yet. Both functions require the root-only `ROLE_LINK`. #### Delegating a Single Text Key A name owner can grant a dApp permission to set only a specific text record, for example allowing it to update the `avatar` key without giving access to any other records: ```ts [Viem] import { encodeFunctionData, toHex } from 'viem' import { packetToBytes } from 'viem/ens' // Encode a setText call for the key to delegate. // Only the selector and the key matter; name and value are ignored. const setter = encodeFunctionData({ abi: permissionedResolverAbi, functionName: 'setText', args: ['0x', 'avatar', ''], }) // Grant the dApp permission to set the "avatar" text key await wallet.writeContract({ address: resolverAddress, abi: permissionedResolverAbi, functionName: 'grantSetterRoles', args: [setter, dappAddress], }) // The dApp can now set the avatar record on names served by this resolver... await dappWallet.writeContract({ address: resolverAddress, abi: permissionedResolverAbi, functionName: 'setText', args: [ toHex(packetToBytes('alice.eth')), 'avatar', 'https://example.com/avatar.png', ], }) // ...but attempting to set any other key will revert // setText(dnsName, 'description', '...') → reverts with EACUnauthorizedAccountRoles ``` <FrenCallout fren="earl" variant="warning"> Argument-scoped grants apply to **every name** served by the resolver instance: the dApp above can set the `avatar` key on all of them. To give an account access to records of specific names only, serve those names from their own resolver instance. </FrenCallout> To grant access to all text keys instead of one, grant `ROLE_SET_TEXT` resolver-wide: ```ts [Viem] const ROLE_SET_TEXT = 1n << 4n await wallet.writeContract({ address: resolverAddress, abi: permissionedResolverAbi, functionName: 'grantRootRoles', args: [ROLE_SET_TEXT, dappAddress], }) ``` #### Revoking Permissions Argument-scoped roles are revoked with `revokeRoles` on the argument's resource, root-scoped roles with `revokeRootRoles`: ```ts [Viem] import { keccak256, toHex } from 'viem' const ROLE_SET_TEXT = 1n << 4n // Revoke the dApp's permission for the "avatar" text key. // The resource of a string argument is keccak256 of its bytes. const resource = BigInt(keccak256(toHex('avatar'))) await wallet.writeContract({ address: resolverAddress, abi: permissionedResolverAbi, functionName: 'revokeRoles', args: [resource, ROLE_SET_TEXT, dappAddress], }) // Revoke resolver-wide ROLE_SET_TEXT (all text keys) await wallet.writeContract({ address: resolverAddress, abi: permissionedResolverAbi, functionName: 'revokeRootRoles', args: [ROLE_SET_TEXT, dappAddress], }) ``` #### Locking a Record Type Permanently Because each role has a corresponding admin role, an owner can make a record type permanently immutable by revoking both the role and its admin. Without the admin role, nobody can grant the role back. Locking the setter alone does not freeze what a name serves: a `ROLE_LINK` holder could relink the name to a different record, and a `ROLE_UPGRADE` holder could replace the resolver implementation entirely. A full lock therefore revokes all three roles with their admins: ```ts [Viem] import { toHex } from 'viem' import { packetToBytes } from 'viem/ens' const ROLE_SET_CONTENTHASH = 1n << 8n const ROLE_LINK = 1n << 28n const ROLE_UPGRADE = 1n << 124n // Each role together with its admin counterpart (admin = role << 128) const LOCK_ROLES = ROLE_SET_CONTENTHASH | (ROLE_SET_CONTENTHASH << 128n) | ROLE_LINK | (ROLE_LINK << 128n) | ROLE_UPGRADE | (ROLE_UPGRADE << 128n) // Set the content hash one final time await wallet.writeContract({ address: resolverAddress, abi: permissionedResolverAbi, functionName: 'setContenthash', args: [toHex(packetToBytes('alice.eth')), contenthashBytes], // your encoded content hash }) // Permanently lock: revoke the setter, link, and upgrade roles with their admins await wallet.writeContract({ address: resolverAddress, abi: permissionedResolverAbi, functionName: 'revokeRootRoles', args: [LOCK_ROLES, ownerAddress], // your address }) // No one can set a content hash, relink names, or upgrade this resolver anymore ``` <FrenCallout fren="earl" variant="warning"> This is irreversible. Once a role and its admin are revoked, there is no way to restore the ability to use that role. Roles are revoked per account: the lock is only complete once no other account holds the revoked roles or their admins. </FrenCallout> #### Resetting a Name To give a name a clean slate, unlink it. The next write mints a fresh empty record for it: ```ts [Viem] const dnsName = toHex(packetToBytes('alice.eth')) // Unlink: alice.eth now serves the default record await wallet.writeContract({ address: resolverAddress, abi: permissionedResolverAbi, functionName: 'linkToRecord', args: [dnsName, 0n], }) // The first write after unlinking creates a new empty record await wallet.writeContract({ address: resolverAddress, abi: permissionedResolverAbi, functionName: 'setText', args: [dnsName, 'avatar', 'https://example.com/new-avatar.png'], }) ``` The old record is untouched by this: any other names linked to it keep serving its values. ### Reference #### Write Functions <ContractReference functions={[ { name: 'initialize', description: 'Initialize the resolver proxy. Grants each Grant on ROOT_RESOURCE exactly as passed and executes the calls as a multicall with role checks skipped.', params: [ { name: 'grants', type: 'Grant[]', description: 'Accounts and role bitmaps granted on ROOT_RESOURCE (Grant = { account, roleBitmap })', }, { name: 'calls', type: 'bytes[]', description: 'Encoded calls to execute during initialization', }, ], }, { name: 'setAddress', description: 'Set an address record. Requires ROLE_SET_ADDRESS on the coin type resource or root. Reverts with InvalidEVMAddress if an EVM coin type gets an address that is not 0 or 20 bytes.', params: [ { name: 'name', type: 'bytes', description: 'The DNS-encoded name' }, { name: 'coinType', type: 'uint256', description: 'The coin type (e.g. 60 for Ethereum)', }, { name: 'addressBytes', type: 'bytes', description: 'The encoded address', }, ], }, { name: 'setText', description: 'Set a text record. Requires ROLE_SET_TEXT on the key resource or root.', params: [ { name: 'name', type: 'bytes', description: 'The DNS-encoded name' }, { name: 'key', type: 'string', description: 'The text record key' }, { name: 'value', type: 'string', description: 'The text record value' }, ], }, { name: 'setData', description: 'Set a data record. Requires ROLE_SET_DATA on the key resource or root.', params: [ { name: 'name', type: 'bytes', description: 'The DNS-encoded name' }, { name: 'key', type: 'string', description: 'The data key' }, { name: 'value', type: 'bytes', description: 'The data value' }, ], }, { name: 'setContenthash', description: 'Set the content hash. Requires ROLE_SET_CONTENTHASH on root.', params: [ { name: 'name', type: 'bytes', description: 'The DNS-encoded name' }, { name: 'hash', type: 'bytes', description: 'The content hash' }, ], }, { name: 'setName', description: 'Set the reverse name. Requires ROLE_SET_NAME on root.', params: [ { name: 'name', type: 'bytes', description: 'The DNS-encoded name' }, { name: 'primaryName', type: 'string', description: 'The primary name', }, ], }, { name: 'setABI', description: 'Set an ABI record. Requires ROLE_SET_ABI on the content type resource or root. Reverts with InvalidContentType unless contentType is a single bit.', params: [ { name: 'name', type: 'bytes', description: 'The DNS-encoded name' }, { name: 'contentType', type: 'uint256', description: 'ABI content type (must be a power of 2)', }, { name: 'data', type: 'bytes', description: 'The ABI data' }, ], }, { name: 'setInterface', description: 'Set an interface implementer record. Requires ROLE_SET_INTERFACE on the interface ID resource or root.', params: [ { name: 'name', type: 'bytes', description: 'The DNS-encoded name' }, { name: 'interfaceId', type: 'bytes4', description: 'The EIP-165 interface ID', }, { name: 'implementer', type: 'address', description: 'Contract implementing the interface', }, ], }, { name: 'linkToNode', description: 'Link a name to the record another node currently uses. Requires ROLE_LINK on root. Reverts with InvalidRecord if the target has no record.', params: [ { name: 'sourceName', type: 'bytes', description: 'The DNS-encoded name to link', }, { name: 'targetNode', type: 'bytes32', description: 'The namehash whose record to use', }, ], }, { name: 'linkToRecord', description: 'Link a name to a record by ID, or unlink it with ID 0. Requires ROLE_LINK on root. Reverts with InvalidRecord if the record does not exist yet.', params: [ { name: 'sourceName', type: 'bytes', description: 'The DNS-encoded name to link', }, { name: 'recordId', type: 'uint256', description: 'The record ID, or 0 to unlink', }, ], }, { name: 'grantSetterRoles', description: 'Grant the argument-scoped role encoded in a setter call. The caller must hold the corresponding admin role.', params: [ { name: 'setter', type: 'bytes', description: 'ABI-encoded setter calldata identifying the setter and its argument', }, { name: 'account', type: 'address', description: 'Account to grant the role to', }, ], returns: [ { name: 'success', type: 'bool', description: 'True if an authorization was changed', }, ], }, { name: 'multicall', description: "Execute multiple write operations in a single transaction. Reverts with the first failing call's error.", params: [ { name: 'calls', type: 'bytes[]', description: 'Array of encoded function calls', }, ], returns: [ { name: 'results', type: 'bytes[]', description: 'Array of return data from each call', }, ], }, { name: 'multicallWithNodeCheck', description: 'Same as multicall. The node parameter is ignored and exists for interface compatibility.', params: [ { name: 'node', type: 'bytes32', description: 'Ignored' }, { name: 'calls', type: 'bytes[]', description: 'Array of encoded function calls', }, ], returns: [ { name: 'results', type: 'bytes[]', description: 'Array of return data from each call', }, ], }, ]} /> #### View Functions Record values are read through `resolve()` with the profile calldata from the [Supported Record Types](#supported-record-types) table. The contract exposes no standalone getters for record values. <ContractReference functions={[ { name: 'resolve', description: 'Resolve a name by dispatching to the requested resolver profile. Implements IExtendedResolver. The node argument inside the profile calldata is ignored; the node is derived from the name. A multicall as profile calldata resolves each inner call.', params: [ { name: 'name', type: 'bytes', description: 'DNS-encoded name to resolve', }, { name: 'data', type: 'bytes', description: 'Encoded resolver profile call (e.g. addr, text)', }, ], returns: [ { name: 'result', type: 'bytes', description: 'Encoded result of the profile call', }, ], }, { name: 'getRecordId', description: 'Get the record a node is linked to.', params: [ { name: 'node', type: 'bytes32', description: 'The namehash to look up', }, ], returns: [ { name: 'recordId', type: 'uint256', description: 'The record ID, or 0 if the node is not linked', }, ], }, { name: 'getRecordCount', description: 'Get the number of records created on this resolver.', returns: [ { name: 'count', type: 'uint256', description: 'Number of created records', }, ], }, { name: 'decodeSetter', description: "Decode setter calldata into its argument, the argument's EAC resource, and the corresponding role. Reverts with UnsupportedResolverProfile for selectors without argument scoping.", params: [ { name: 'setter', type: 'bytes', description: 'ABI-encoded setter calldata', }, ], returns: [ { name: 'arg', type: 'bytes', description: 'The setter argument' }, { name: 'resource', type: 'uint256', description: 'The EAC resource' }, { name: 'roleBitmap', type: 'uint256', description: 'The corresponding role bit', }, ], }, { name: 'isContractNamer', description: 'Whether an account is authorized to name this contract (holds ROLE_CAN_NAME on root).', params: [ { name: 'namer', type: 'address', description: 'The account to check' }, ], returns: [ { name: 'allowed', type: 'bool', description: 'True if the account holds ROLE_CAN_NAME', }, ], }, ]} /> #### Events <ContractReference functions={[ { name: 'ResolverCreated', description: 'The resolver was created or initialized.', params: [], }, { name: 'Linked', description: 'A name was linked to a record, either explicitly or by a first write. Record ID 0 means the name was unlinked.', params: [ { name: 'recordId', type: 'uint256', indexed: true, description: 'The record ID', }, { name: 'node', type: 'bytes32', indexed: true, description: 'The namehash of the name', }, { name: 'name', type: 'bytes', description: 'The DNS-encoded name' }, ], }, { name: 'AddressUpdated', description: 'An address record changed.', params: [ { name: 'recordId', type: 'uint256', indexed: true, description: 'The record ID', }, { name: 'coinType', type: 'uint256', description: 'The coin type' }, { name: 'addressBytes', type: 'bytes', description: 'The encoded address', }, ], }, { name: 'TextUpdated', description: 'A text record changed.', params: [ { name: 'recordId', type: 'uint256', indexed: true, description: 'The record ID', }, { name: 'keyHash', type: 'string', indexed: true, description: 'The key, hashed in the topic (indexed strings are stored as their keccak256 hash)', }, { name: 'key', type: 'string', description: 'The text key' }, { name: 'value', type: 'string', description: 'The text value' }, ], }, { name: 'DataUpdated', description: 'A data record changed.', params: [ { name: 'recordId', type: 'uint256', indexed: true, description: 'The record ID', }, { name: 'keyHash', type: 'string', indexed: true, description: 'The key, hashed in the topic (indexed strings are stored as their keccak256 hash)', }, { name: 'key', type: 'string', description: 'The data key' }, { name: 'value', type: 'bytes', description: 'The data value' }, ], }, { name: 'ContenthashUpdated', description: 'The content hash changed.', params: [ { name: 'recordId', type: 'uint256', indexed: true, description: 'The record ID', }, { name: 'hash', type: 'bytes', description: 'The content hash' }, ], }, { name: 'NameUpdated', description: 'The reverse name changed.', params: [ { name: 'recordId', type: 'uint256', indexed: true, description: 'The record ID', }, { name: 'primaryName', type: 'string', description: 'The primary name', }, ], }, { name: 'ABIUpdated', description: 'An ABI record changed.', params: [ { name: 'recordId', type: 'uint256', indexed: true, description: 'The record ID', }, { name: 'contentType', type: 'uint256', indexed: true, description: 'The content type', }, ], }, { name: 'InterfaceUpdated', description: 'An interface implementer record changed.', params: [ { name: 'recordId', type: 'uint256', indexed: true, description: 'The record ID', }, { name: 'interfaceId', type: 'bytes4', indexed: true, description: 'The interface ID', }, { name: 'implementer', type: 'address', description: 'The implementer address', }, ], }, { name: 'ResourceArgument', description: 'A resource with no current role holders received a grant through grantSetterRoles. Associates the resource with its setter argument. Can re-fire after full revocation.', params: [ { name: 'resource', type: 'uint256', indexed: true, description: 'The EAC resource', }, { name: 'arg', type: 'bytes', description: 'The setter argument' }, ], }, ]} /> ## Registry Hierarchy ENSv2 replaces ENSv1's single flat registry with a hierarchical model where each name can have its own registry for managing subnames. This page explains the tree structure, how resolution works within it, and the implications for name ownership. ### From Flat to Hierarchical ENSv1 used a simple architecture, where a single flat registry maintained a mapping from all names to their owner and resolver addresses. Hierarchical ownership was enforced through the use of [namehash](/resolution/names#namehash) to calculate IDs for subnames. This has the advantage of simplicity, but means that ownership rules are enforced by a single, non-upgradeable contract, and changes in the status of a parent name do not automatically ripple down to affect subnames. ```mermaid --- title: ENSv1 Structure --- flowchart LR subgraph Registry direction LR root["<b>&lt;root></b><hr>owner: 0x0123..."] eth["<b>eth</b><hr>owner: 0x1234..."] montoya["<b>montoya.eth</b><hr>owner: 0x5678...<br/>• resolver: 0x6789..."] inigo["<b>inigo.montoya.eth</b><hr>owner: 0x7890...<br/>• resolver: 0x890A..."] domingo["<b>domingo.montoya.eth</b><hr>owner: 0x90AB...<br/>• resolver: 0x0ABC..."] end resolvers@{label: "Resolvers", shape: processes} Registry --> resolvers ``` In ENSv2, registries are hierarchical: each name can have a resolver and a subregistry: ```mermaid --- title: ENSv2 Structure --- flowchart TB root@{label: "<b>&lt;root></b><hr>owner: 0x0123...</b>"} eth@{label: "<b>eth</b><hr>owner: 0x1234..."} montoya@{label: "<b>montoya.eth</b><hr>owner: 0x5678..."} inigo@{label: "<b>Resolver 2</b><hr>owner: 0x7890...<br>addr(60): 0x7890...", shape: notch-rect} domingo@{label: "<b>domingo.montoya.eth</b><hr>owner: 0x90AB..."} montoyaResolver@{label: "<b>Resolver 1</b><hr>owner: 0x5678...<br>addr(60): 0x5678...", shape: notch-rect} root-- eth --> eth eth-- montoya --> montoya eth-- montoya --> montoyaResolver montoya-- inigo --> inigo montoya-- domingo --> domingo ``` Here, registries are shown as rectangles, while resolvers are shown as notched rectangles. Note that the registry for `.eth` has both a subregistry and a resolver defined for `montoya.eth`. Note also that there's a resolver defined for `inigo.montoya.eth` but no subregistry, while `domingo.montoya.eth` has a subregistry but no resolver. A name only needs to have a subregistry defined if it wants the ability to create subnames, and it only needs a resolver defined if it wants to define records to resolve for that name or its subnames. ### Names as Chains of Entries A full name like `inigo.montoya.eth` does not exist as a single on-chain object. Instead, it's a chain of entries across registries: `inigo` is an entry in the `montoya.eth` registry, `montoya` is an entry in the `.eth` registry, and these are linked by the `subregistry` field on each entry. This means that on-chain, a
프로젝트로 돌아가기