Writing a Resolver
编写解析器
Resolvers are specified in EIP137. A resolver must implement the following method: EIP137中描述了解析器的详细信息。一个解析器必须实现以下方法:
function supportsInterface(bytes4 interfaceID) constant returns (bool);
supportsInterface is defined in EIP165, and allows callers to determine if a resolver supports a particular record type. Record types are specified as a set of one or more methods that a resolver must implement together. Currently defined record types include:
supportsInterface在EIP165中定义,它可以被调用者用来确定一个解析器是否支持某个特定的记录类型。记录类型是指解析器必须一起实现的一个或多个方法的集合。当前定义的记录类型包括:
| Record type | Function(s) | Interface ID | Defined in |
|---|---|---|---|
| Ethereum address | addr | 0x3b3b57de | EIP137 |
| ENS Name | name | 0x691f3431 | EIP181 |
| ABI specification | ABI | 0x2203ab56 | EIP205 |
| Public key | pubkey | 0xc8690233 | EIP619 |
| Text records | text | 0x59d1d43c | EIP634 |
| Content hash | contenthash | 0xbc1c58d1 | |
| 记录类型 | 函数 | 接口ID | 定义文档 |
| :--- | :--- | :--- | :--- |
| 以太坊地址 | addr | 0x3b3b57de | EIP137 |
| ENS域名 | name | 0x691f3431 | EIP181 |
| ABI规范 | ABI | 0x2203ab56 | EIP205 |
| 公钥 | pubkey | 0xc8690233 | EIP619 |
| 文本记录 | text | 0x59d1d43c | EIP634 |
| 内容散列 | contenthash | 0xbc1c58d1 |
supportsInterface must also return true for the interfaceID value 0x01ffc9a7, which is the interface ID of supportsInterface itself.
supportsInterface本身的接口ID为0x01ffc9a7,当interfaceID值为0x01ffc9a7时,supportsInterface也必须返回true。
Additionally, the content interface was used as a defacto standard for Swarm hashes, and has an interface ID of 0xd8389dc5. New implementations should use contenthash instead.
此外,content接口被用作Swarm散列事实上的标准,它的接口ID为0xd8389dc5。现在新的内容散列应该使用contenthash接口来实现。
Example Resolver
解析器示例
A simple resolver that supports only the addr type might look something like this: 一个只支持addr类型的简易解析器,看起来就像这样:
contract SimpleResolver {
function supportsInterface(bytes4 interfaceID) constant returns (bool) {
return interfaceID == 0x3b3b57de;
}
function addr(bytes32 nodeID) constant returns (address) {
return address(this);
}
}
2
3
4
5
6
7
8
9
这个简易解析器总是返回自己的地址作为所有查询的结果。尽管解析器采用不同的机制应该返回相同的结果,但实际上解析器可以按照需要采用任何机制来确定返回的结果,并且应该尽可能降低gas费用。