跳转到内容

@stdlib/dns

提供DNS名称的解析手段。

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

import "@stdlib/dns";

结构

DNSResolveResult

struct DNSResolveResult {
prefixInt;
recordCell?;
}

功能

dnsStringToInternal

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

将 DNS 字符串转换为[slice][slice]或null,如果不可能的话。

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

dnsInternalNormalize

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

将 [Slice][片] 的内部 DNS 表示规范化。 传递的 [Slice][slice]必须没有任何引用,否则将产生退出代码 134异常:“无效参数”。

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

dnsInternalVerify

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

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

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

dnsExtractTopDomainLength

fun dnsExtractTopDomainLength(subdomain: Slice):Int

计算顶域在 “子域”[][片]中的长度。

源代码

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

dnsExtractTopDomain

fun dnsExtractTopDomain(subdomain: Slice):Slice

从 “子域 “中提取顶域 [][片]。

源代码

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

dnsResolveNext

fun dnsResolveNext(address: Address):Cell

地址地址解析为[单元格][单元格]。

源代码

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

dnsResolveWallet

fun dnsResolveWallet(address: Address):Cell

将钱包 addressAddress解析为[Cell][cell]。

源代码

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

特质

DNSResolver

Trait DNSResolver 为 DNS 解析提供了两个辅助函数:

  1. getter函数 dnsresolve(),对应于其FunC变体
  2. 虚拟函数 doResolveDNS(),由子域 [][片] 位创建结构 DNSResolveResult

源代码

trait DNSResolver {
get fun dnsresolve(subdomain: Slice, category: Int):DNSResolveResult {
// Normalize
let delta: Int = 0;
if (subdomain.preloadUint(8) == 0) {
subdomain.loadUint(8); // Skip first byte
delta += 8;
} // Check correctity
// 检查正确性
require(dnsInternalVerify(subdomain), "Invalid DNS name");
// 解析
let resDNSResolveResult = 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};
}
}

使用示例

import "@stdlib/dns";
contract ExampleContract with DNSResolver {
// Now, this contract has a:
// 1. dnsresolve getter function
// 2. doResolveDNS virtual function
}

资料来源