跳转到内容

@stdlib/dns

提供DNS名称的解析手段。

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

import "@stdlib/dns";

Structs

DNSResolveResult

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

函数

dnsStringToInternal

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

将一个 DNS 字符串转换为 Slicenull,如果不可能转换则返回 [null]。

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

dnsInternalNormalize

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

规范化 Slice 的内部 DNS 表示。 传入的 Slice 不能有任何引用,否则ext code 134将会抛出:Invalid argument

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

dnsInternalVerify

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

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

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

dnsExtractTopDomainLength

fun dnsExtractTopDomainLength(subdomain: Slice): Int;

计算顶域(top domain)在 子域(subdomain) Slice中的长度。

源码:

fun dnsExtractTopDomainLength(subdomain: Slice): Int {
let i: Int = 0;
let needBreak: Bool = false;
do {
let char: Int = subdomain.loadUint(8); // we do not check domain.length because it MUST contain \0 character
needBreak = char == 0;
if (!needBreak) {
i += 8;
}
} until (needBreak);
require(i != 0, "Invalid DNS name");
return i;
}

dnsExtractTopDomain

fun dnsExtractTopDomain(subdomain: Slice): Slice;

从 “子域 “中提取顶域 Slice

源码:

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

dnsResolveNext

fun dnsResolveNext(address: Address): Cell;

address Address解析为cell

源码:

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

dnsResolveWallet

fun dnsResolveWallet(address: Address): Cell;

将钱包 address Address解析为Cell

源码:

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

Traits

DNSResolver

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

  1. getter函数 dnsresolve(),对应于其FunC变体
  2. 虚函数 doResolveDNS(),它通过子域 Slice 位创建一个 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;
}
// Checks correctness
require(dnsInternalVerify(subdomain), "Invalid DNS name");
// Resolve
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};
}
}

示例用法:

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

资料来源