Ethereum Name Service 백서

Ethereum Name Service

원문

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

getExpiry()`, `latestOwnerOf()`, etc. to build metadata from on-chain state. ### Parent Pointer Each name's `subregistry` field creates a forward pointer from parent to child. The registry also stores a backward pointer to its own parent via `setParent()` / `getParent()`, creating a two-way link at every level: <Card className="flex items-center justify-center gap-4 py-6"> <div className="border-grey-light flex items-center gap-2 rounded-lg border bg-white px-4 py-2 dark:bg-black"> <div className="text-center"> <div className="font-semibold">.eth</div> <div className="text-grey text-xs">parent</div> </div> </div> <div className="text-blue font-bold">⇄</div> <div className="border-grey-light flex items-center gap-2 rounded-lg border bg-white px-4 py-2 dark:bg-black"> <div className="text-center"> <div className="font-semibold">nick.eth</div> <div className="text-grey text-xs">child</div> </div> </div> <div className="text-blue font-bold">⇄</div> <div className="border-grey-light flex items-center gap-2 rounded-lg border bg-white px-4 py-2 dark:bg-black"> <div className="text-center"> <div className="font-semibold">sub.nick.eth</div> <div className="text-grey text-xs">grandchild</div> </div> </div> </Card> The backward pointer records both the parent registry's address and the label this registry is known by in the parent. The UniversalHelper's [`findCanonicalName()`](/ensv2/universal-resolver-v2#findcanonicalname) uses these backward pointers to walk up the tree, verifying at each step that `parent.getSubregistry(label)` points back to the current registry, and reconstructing the full name along the way. Once this pointer is set, the registry has committed to its position in the hierarchy: <FrenCallout fren="earl" variant="note" title="Definition – Canonical Parent"> The **canonical parent** of a registry is the parent registry set by calling `setParent(parent, label)`. It identifies where this registry sits in the hierarchy: as `label` under `parent`. </FrenCallout> However, as long as `ROLE_SET_PARENT` is still held on `ROOT_RESOURCE`, the pointer can be changed, effectively remounting the registry at a different position (e.g. moving from `nick.eth` to `nick.xyz`). To prevent this, the owner revokes `ROLE_SET_PARENT` and `ROLE_SET_PARENT_ADMIN` on `ROOT_RESOURCE`: ```ts [Viem] import { createWalletClient, http } from 'viem' import { mainnet } from 'viem/chains' const wallet = createWalletClient({ chain: mainnet, transport: http() }) const ROLE_SET_PARENT = 1n << 8n const ROLE_SET_PARENT_ADMIN = ROLE_SET_PARENT << 128n // Set the canonical parent: this registry is "sub" under the nick.eth registry await wallet.writeContract({ address: subRegistryAddress, abi: permissionedRegistryAbi, functionName: 'setParent', args: [parentRegistryAddress, 'sub'], }) // Lock the parent pointer permanently by revoking both the role and its admin await wallet.writeContract({ address: subRegistryAddress, abi: permissionedRegistryAbi, functionName: 'revokeRootRoles', args: [ROLE_SET_PARENT | ROLE_SET_PARENT_ADMIN, ownerAddress], }) ``` <FrenCallout fren="earl" variant="note" title="Definition – Locked Canonical Parent"> A registry has a **locked canonical parent** when it has a canonical parent and no account holds `ROLE_SET_PARENT` or `ROLE_SET_PARENT_ADMIN` on `ROOT_RESOURCE`, making the pointer permanent. </FrenCallout> ### Emancipation <FrenCallout fren="earl" variant="note" title="Definition – Emancipation"> A registry is **emancipated** when no account holds a dangerous role on its `ROOT_RESOURCE`. Every registry reports this via `isEmancipated()`. </FrenCallout> Roles in a registry are normally scoped to a single name, but a role granted on [`ROOT_RESOURCE`](/ensv2/enhanced-access-control#resources) applies to every name in the registry. That reach is what makes some roles dangerous: `ROLE_SET_RESOLVER` on `ROOT_RESOURCE` can change the resolver of every name, `ROLE_SET_SUBREGISTRY` can swap any name's subregistry, `ROLE_UNREGISTER` can delete any name, and `ROLE_UPGRADE` can replace the registry implementation entirely. These four roles and their admin counterparts are the **dangerous roles**: `isEmancipated()` returns `true` once none of them has an assignee on `ROOT_RESOURCE`. Operational roles are not dangerous and do not affect emancipation: `ROLE_REGISTRAR` can only register available names and `ROLE_RENEW` only extends expiry. This is how the `.eth` registry works: the [ETH Registrar](/ensv2/eth-registrar) holds both on `ROOT_RESOURCE`, while each name owner holds the roles defined by [`REGISTRATION_ROLE_BITMAP`](/ensv2/eth-registrar#roles-granted-at-registration). Emancipation is irreversible: re-granting a dangerous role requires its admin counterpart, which is itself a dangerous role that no account holds anymore. To see which roles still block emancipation, inspect the per-role assignee counts with [`roleCount(ROOT_RESOURCE)`](/ensv2/enhanced-access-control#bitmap-layout). And rely on `isEmancipated()` only after [verifying](/ensv2/verifiable-factory#on-chain-verification) which implementation the registry runs: an arbitrary contract can simply always return `true`. The migration-specific `WrapperRegistry` hardcodes the flag to `true`, with its root roles derived at migration time from the name's [Name Wrapper fuses](/ensv2/migration). #### Emancipation Across the Hierarchy Emancipation at one level is not sufficient if a higher level can still interfere. If the `.eth` registry is emancipated but the root registry is not, an account with `ROLE_SET_SUBREGISTRY` on the root registry's `ROOT_RESOURCE` could swap the `.eth` subregistry pointer. True emancipation requires the entire chain from root down to be secured: * **Root registry**: governed by ENS governance (trusted by design) * **.eth registry**: emancipated (only `ROLE_REGISTRAR` + `ROLE_RENEW` granted on `ROOT_RESOURCE`) * **2LD registries** (e.g., alice.eth): depends on the 2LD owner's configuration For a subname like `sub.alice.eth` to be fully emancipated, the `alice.eth` owner must emancipate their registry (revoke the dangerous roles on `ROOT_RESOURCE`). ### Code Examples Subname registries are deployed as UserRegistry proxies through the Verifiable Factory. See [Deploying a Registry Proxy](/ensv2/verifiable-factory#deploying-a-registry-proxy) for a code example. The examples below use the ETHRegistry, but the same functions are available on any PermissionedRegistry instance. #### Querying Name State `getState` returns the full state of a name in a single call: its registration status, expiry, current owner, token ID, and EAC resource. Both `getState` and `getStatus` accept any of a name's three identifiers (labelhash, tokenId, or resource) thanks to [anyId polymorphism](/ensv2/permissioned-registry#anyid-polymorphism): ```ts [Viem] import { createPublicClient, http, keccak256, toHex } from 'viem' import { mainnet } from 'viem/chains' const client = createPublicClient({ chain: mainnet, transport: http() }) const labelhash = BigInt(keccak256(toHex('alice'))) // Query the full state of a name const state = await client.readContract({ address: registryAddress, abi: permissionedRegistryAbi, functionName: 'getState', args: [labelhash], }) // state: { status, expiry, latestOwner, tokenId, resource } const STATUS = ['AVAILABLE', 'RESERVED', 'REGISTERED'] as const console.log(STATUS[state.status]) // "REGISTERED" console.log(state.latestOwner) // "0x..." console.log(state.expiry) // expiry as unix timestamp // anyId polymorphism: labelhash, tokenId, and resource all resolve // to the same name, so this returns the same result const sameState = await client.readContract({ address: registryAddress, abi: permissionedRegistryAbi, functionName: 'getState', args: [state.tokenId], }) ``` If you only need to check whether a name is available, `getStatus` is a lighter alternative that returns just the status enum: ```ts [Viem] const status = await client.readContract({ address: registryAddress, abi: permissionedRegistryAbi, functionName: 'getStatus', args: [labelhash], }) // 0 = AVAILABLE, 1 = RESERVED, 2 = REGISTERED ``` #### Granting and Revoking Roles A name owner can delegate specific capabilities to other accounts by granting roles on the name's labelhash. Multiple roles can be combined into a single call using bitwise OR: ```ts [Viem] import { createWalletClient, http, keccak256, toHex } from 'viem' import { mainnet } from 'viem/chains' const wallet = createWalletClient({ chain: mainnet, transport: http() }) const ROLE_SET_RESOLVER = 1n << 24n const ROLE_SET_SUBREGISTRY = 1n << 20n const labelhash = BigInt(keccak256(toHex('alice'))) // Grant a single role await wallet.writeContract({ address: registryAddress, abi: permissionedRegistryAbi, functionName: 'grantRoles', args: [labelhash, ROLE_SET_RESOLVER, operatorAddress], }) // Grant multiple roles at once using bitwise OR await wallet.writeContract({ address: registryAddress, abi: permissionedRegistryAbi, functionName: 'grantRoles', args: [labelhash, ROLE_SET_RESOLVER | ROLE_SET_SUBREGISTRY, operatorAddress], }) ``` To revoke a role, call `revokeRoles` with the same arguments. Only the specified role is removed; other grants on the same name remain active: ```ts [Viem] await wallet.writeContract({ address: registryAddress, abi: permissionedRegistryAbi, functionName: 'revokeRoles', args: [labelhash, ROLE_SET_RESOLVER, operatorAddress], }) // ROLE_SET_SUBREGISTRY remains active ``` ### Reference #### Write Functions <ContractReference functions={[ { name: 'register', description: 'Register or reserve a name. Pass owner as address(0) to reserve without an owner.', params: [ { name: 'label', type: 'string', description: 'The label to register (e.g. "alice" for alice.eth)' }, { name: 'owner', type: 'address', description: 'Owner address, or address(0) to reserve' }, { name: 'registry', type: 'IRegistry', description: 'Subregistry to set, or address(0)' }, { name: 'resolver', type: 'address', description: 'Resolver to set, or address(0)' }, { name: 'roleBitmap', type: 'uint256', description: 'Initial roles to grant the owner' }, { name: 'expiry', type: 'uint64', description: 'Expiry timestamp in seconds' }, ], returns: [ { name: 'tokenId', type: 'uint256', description: 'The token ID of the registered name' }, ], }, { name: 'unregister', description: 'Unregister a name, making it available. Requires ROLE_UNREGISTER.', params: [ { name: 'anyId', type: 'uint256', description: 'Labelhash, token ID, or resource of the name' }, ], }, { name: 'renew', description: 'Extend a name\'s expiry. Cannot reduce the current expiry. Requires ROLE_RENEW (root or name scope) while the name is unexpired; reviving an expired name requires ROLE_RENEW on ROOT_RESOURCE.', params: [ { name: 'anyId', type: 'uint256', description: 'Labelhash, token ID, or resource of the name' }, { name: 'newExpiry', type: 'uint64', description: 'New expiry timestamp (must be >= current)' }, ], }, { name: 'setSubregistry', description: 'Set the subregistry for a name. Pointing two names to the same registry creates a namespace alias.', params: [ { name: 'anyId', type: 'uint256', description: 'Labelhash, token ID, or resource of the name' }, { name: 'registry', type: 'IRegistry', description: 'Subregistry address' }, ], }, { name: 'setResolver', description: 'Set the resolver for a name. Requires ROLE_SET_RESOLVER.', params: [ { name: 'anyId', type: 'uint256', description: 'Labelhash, token ID, or resource of the name' }, { name: 'resolver', type: 'address', description: 'Resolver address' }, ], }, { name: 'setParent', description: 'Set this registry\'s canonical parent. Requires ROLE_SET_PARENT on ROOT_RESOURCE.', params: [ { name: 'parent', type: 'IRegistry', description: 'Parent registry address' }, { name: 'label', type: 'string', description: 'This registry\'s label within the parent' }, ], }, { name: 'setURI', description: 'Set the metadata URI and optional renderer. Requires ROLE_SET_URI on ROOT_RESOURCE.', params: [ { name: 'uri_', type: 'string', description: 'Static metadata URI' }, { name: 'renderer', type: 'IRegistryURIRenderer', description: 'Renderer contract, or address(0) for static URI only' }, ], }, { name: 'unsafeTransfer', description: 'Transfer a token without the two safe-transfer checks (emancipation and sole assignee). The owner must hold ROLE_CAN_TRANSFER_ADMIN, and the caller must be the owner or an approved operator.', params: [ { name: 'to', type: 'address', description: 'Receiver of the token' }, { name: 'tokenId', type: 'uint256', description: 'Token to transfer' }, { name: 'data', type: 'bytes', description: 'Forwarded to the receiver hook' }, ], }, { name: 'unsafeBatchTransfer', description: 'Batch variant of unsafeTransfer. All tokens must belong to the same owner.', params: [ { name: 'to', type: 'address', description: 'Receiver of the tokens' }, { name: 'tokenIds', type: 'uint256[]', description: 'Tokens to transfer' }, { name: 'data', type: 'bytes', description: 'Forwarded to the receiver hook' }, ], }, { name: 'grantRoles', description: 'Grant roles on a name. Triggers token regeneration.', params: [ { name: 'anyId', type: 'uint256', description: 'Labelhash, token ID, or resource of the name' }, { name: 'roleBitmap', type: 'uint256', description: 'Bitmap of roles to grant' }, { name: 'account', type: 'address', description: 'Account to grant roles to' }, ], returns: [ { name: 'success', type: 'bool', description: 'Whether the roles were updated' }, ], }, { name: 'revokeRoles', description: 'Revoke roles on a name. Triggers token regeneration.', params: [ { name: 'anyId', type: 'uint256', description: 'Labelhash, token ID, or resource of the name' }, { name: 'roleBitmap', type: 'uint256', description: 'Bitmap of roles to revoke' }, { name: 'account', type: 'address', description: 'Account to revoke roles from' }, ], returns: [ { name: 'success', type: 'bool', description: 'Whether the roles were updated' }, ], }, { name: 'grantRootRoles', description: 'Grant contract-wide roles on ROOT_RESOURCE.', params: [ { name: 'roleBitmap', type: 'uint256', description: 'Bitmap of roles to grant' }, { name: 'account', type: 'address', description: 'Account to grant roles to' }, ], returns: [ { name: 'success', type: 'bool', description: 'Whether the roles were updated' }, ], }, { name: 'revokeRootRoles', description: 'Revoke contract-wide roles on ROOT_RESOURCE.', params: [ { name: 'roleBitmap', type: 'uint256', description: 'Bitmap of roles to revoke' }, { name: 'account', type: 'address', description: 'Account to revoke roles from' }, ], returns: [ { name: 'success', type: 'bool', description: 'Whether the roles were updated' }, ], }, ]} /> #### View Functions <ContractReference functions={[ { name: 'getState', description: 'Complete state for a name: status, expiry, latestOwner, tokenId, resource.', params: [ { name: 'anyId', type: 'uint256', description: 'Labelhash, token ID, or resource' }, ], returns: [ { name: 'state', type: 'State', description: 'Struct with status, expiry, latestOwner, tokenId, resource' }, ], }, { name: 'getStatus', description: 'Get the registration status of a name.', params: [ { name: 'anyId', type: 'uint256', description: 'Labelhash, token ID, or resource' }, ], returns: [ { name: 'status', type: 'Status', description: 'AVAILABLE, RESERVED, or REGISTERED' }, ], }, { name: 'isEmancipated', description: 'True when no dangerous role has an assignee on ROOT_RESOURCE. See Emancipation.', returns: [ { name: 'emancipated', type: 'bool', description: 'Whether the registry is emancipated' }, ], }, { name: 'getExpiry', description: 'Get the expiry timestamp of a name.', params: [ { name: 'anyId', type: 'uint256', description: 'Labelhash, token ID, or resource' }, ], returns: [ { name: 'expiry', type: 'uint64', description: 'Expiry timestamp in seconds' }, ], }, { name: 'getTokenId', description: 'Get the current token ID of a name.', params: [ { name: 'anyId', type: 'uint256', description: 'Labelhash, token ID, or resource' }, ], returns: [ { name: 'tokenId', type: 'uint256', description: 'Current ERC1155 token ID' }, ], }, { name: 'getResource', description: 'Get the current EAC resource ID of a name.', params: [ { name: 'anyId', type: 'uint256', description: 'Labelhash, token ID, or resource' }, ], returns: [ { name: 'resource', type: 'uint256', description: 'Current EAC resource ID' }, ], }, { name: 'latestOwnerOf', description: 'Get the recorded owner of a token, ignoring expiry. Burned or stale token IDs return the zero address.', params: [ { name: 'tokenId', type: 'uint256', description: 'The token ID to query' }, ], returns: [ { name: 'owner', type: 'address', description: 'Owner address' }, ], }, { name: 'ownerOf', description: 'Get the owner of a token, or address(0) if expired or stale token ID.', params: [ { name: 'tokenId', type: 'uint256', description: 'The token ID to query' }, ], returns: [ { name: 'owner', type: 'address', description: 'Owner address, or address(0)' }, ], }, { name: 'getSubregistry', description: 'Get the subregistry for a label. Returns address(0) if expired.', params: [ { name: 'label', type: 'string', description: 'The label to query' }, ], returns: [ { name: 'registry', type: 'IRegistry', description: 'Subregistry address' }, ], }, { name: 'getResolver', description: 'Get the resolver for a label. Returns address(0) if expired.', params: [ { name: 'label', type: 'string', description: 'The label to query' }, ], returns: [ { name: 'resolver', type: 'address', description: 'Resolver address' }, ], }, { name: 'getParent', description: 'Get this registry\'s canonical parent registry and label.', returns: [ { name: 'parent', type: 'IRegistry', description: 'Parent registry address' }, { name: 'label', type: 'string', description: 'This registry\'s label within the parent' }, ], }, { name: 'hasRoles', description: 'Check whether an account holds the specified roles on a name.', params: [ { name: 'anyId', type: 'uint256', description: 'Labelhash, token ID, or resource' }, { name: 'roleBitmap', type: 'uint256', description: 'Bitmap of roles to check' }, { name: 'account', type: 'address', description: 'Account to check' }, ], returns: [ { name: 'result', type: 'bool', description: 'True if account holds all specified roles' }, ], }, { name: 'roles', description: 'Get the full role bitmap for an account on a name.', params: [ { name: 'anyId', type: 'uint256', description: 'Labelhash, token ID, or resource' }, { name: 'account', type: 'address', description: 'Account to query' }, ], returns: [ { name: 'roleBitmap', type: 'uint256', description: 'Full role bitmap' }, ], }, { name: 'roleCount', description: 'Get the packed assignee counts for all 64 roles on a name. Each nybble (4 bits) holds the count of accounts holding that role.', params: [ { name: 'anyId', type: 'uint256', description: 'Labelhash, token ID, or resource' }, ], returns: [ { name: 'counts', type: 'uint256', description: 'Packed assignee counts (nybble per role)' }, ], }, { name: 'hasAssignees', description: 'Check whether any accounts hold the specified roles on a name.', params: [ { name: 'anyId', type: 'uint256', description: 'Labelhash, token ID, or resource' }, { name: 'roleBitmap', type: 'uint256', description: 'Bitmap of roles to check' }, ], returns: [ { name: 'result', type: 'bool', description: 'True if any account holds the roles' }, ], }, { name: 'getAssigneeCount', description: 'Get per-role assignee counts for the specified roles on a name.', params: [ { name: 'anyId', type: 'uint256', description: 'Labelhash, token ID, or resource' }, { name: 'roleBitmap', type: 'uint256', description: 'B
프로젝트로 돌아가기