Skip to content

@stdlib/dns

提供解析 DNS 名称的方法。

要使用此库,请导入 @stdlib/dns

solidity
import "@stdlib/dns";

结构体

DNSResolveResult

solidity
struct DNSResolveResult {
    prefix: Int;
    record: Cell?;
}

函数

dnsStringToInternal

solidity
@name(dns_string_to_internal)
native dnsStringToInternal(str: String): Slice?;

将 DNS 字符串转换为 Slicenull,如果无法转换则返回 null

源代码 (FunC): dns.fc#L1

dnsInternalNormalize

solidity
@name(dns_internal_normalize)
native dnsInternalNormalize(src: Slice): Slice;

规范化 Slice 的内部 DNS 表示。

源代码 (FunC): dns.fc#L125

dnsInternalVerify

solidity
@name(dns_internal_verify)
native dnsInternalVerify(subdomain: Slice): Bool;

验证子域 Slice 的内部 DNS 表示。

源代码 (FunC): dns.fc#L81

dnsExtractTopDomainLength

solidity
fun dnsExtractTopDomainLength(subdomain: Slice): Int;

计算 subdomain Slice 中顶级域名的长度。

源代码:

solidity
fun dnsExtractTopDomainLength(subdomain: Slice): Int {
    let i: Int = 0;
    let needBreak: Bool = false;
    do {
        let char: Int = subdomain.loadUint(8); // 我们不检查 domain.length 因为它必须包含 \0 字符
        needBreak = char == 0;
        if (!needBreak) {
            i += 8;
        }
    } until (needBreak);
    require(i != 0, "Invalid DNS name");
    return i;
}

dnsExtractTopDomain

solidity
fun dnsExtractTopDomain(subdomain: Slice): Slice;

subdomain Slice 中提取顶级域名。

源代码:

solidity
fun dnsExtractTopDomain(subdomain: Slice): Slice {
    let len: Int = dnsExtractTopDomainLength(subdomain);
    return subdomain.loadBits(len);
}

dnsResolveNext

solidity
fun dnsResolveNext(address: Address): Cell;

address Address 解析为 Cell

源代码:

solidity
fun dnsResolveNext(address: Address): Cell {
    return beginCell()
        .storeUint(0xba93, 16)
        .storeAddress(address)
        .endCell();
}

dnsResolveWallet

solidity
fun dnsResolveWallet(address: Address): Cell;

将钱包 address Address 解析为 Cell

源代码:

solidity
fun dnsResolveWallet(address: Address): Cell {
    return beginCell()
        .storeUint(0x9fd3, 16)
        .storeAddress(address)
        .storeUint(0, 8)
        .endCell();
}

特性

DNSResolver

特性 DNSResolver 提供两个用于 DNS 解析的辅助函数:

  1. getter 函数 dnsresolve(),对应其 FunC 变体
  2. 虚函数 doResolveDNS(),从子域 Slice 位创建一个结构体 DNSResolveResult

源代码:

solidity
trait DNSResolver {
    get fun dnsresolve(subdomain: Slice, category: Int): DNSResolveResult {
        // 规范化
        let delta: Int = 0;
        if (subdomain.preloadUint(8) == 0) {
            subdomain.loadUint(8); // 跳过第一个字节
            delta += 8;
        }

        // 检查正确性
        require(dnsInternalVerify(subdomain), "Invalid DNS name");

        // 解析
        let res: DNSResolveResult = self.doResolveDNS(subdomain, category);
        return DNSResolveResult{prefix: res.prefix + delta, record: res.record};
    }
    virtual fun doResolveDNS(subdomain: Slice, category: Int): DNSResolveResult {
        return DNSResolveResult{prefix: subdomain.bits(), record: null};
    }
}

使用示例:

solidity
import "@stdlib/dns";

contract ExampleContract with DNSResolver {
    // 现在,此合约有一个:
    // 1. dnsresolve getter 函数
    // 2. doResolveDNS 虚函数
}

源代码