Reference
@stdlib/dns

@stdlib/dns

Provides means for resolution of DNS (opens in a new tab) names.

To use this library, import @stdlib/dns:

import "@stdlib/dns";

Structs

DNSResolveResult

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

Functions

dnsStringToInternal

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

Converts a DNS string to a Slice or null, if it's impossible.

Source code (FunC): dns.fc#L1 (opens in a new tab)

dnsInternalNormalize

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

Normalizes the internal DNS representation of the Slice.

Source code (FunC): dns.fc#L125 (opens in a new tab)

dnsInternalVerify

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

Verifies the internal DNS representation of the subdomain Slice.

Source code (FunC): dns.fc#L81 (opens in a new tab)

dnsExtractTopDomainLength

fun dnsExtractTopDomainLength(subdomain: Slice): Int;

Calculates length of a top domain in the subdomain Slice.

Source code:

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;

Extracts top domain from a subdomain Slice.

Source code:

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

dnsResolveNext

fun dnsResolveNext(address: Address): Cell;

Resolves an address Address into a Cell.

Source code:

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

dnsResolveWallet

fun dnsResolveWallet(address: Address): Cell;

Resolves a wallet address Address into a Cell.

Source code:

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

Traits

DNSResolver

Trait DNSResolver provides two helper functions for DNS resolution:

  1. getter function dnsresolve(), which corresponds to its FunC variant (opens in a new tab).
  2. virtual function doResolveDNS(), which creates a struct DNSResolveResult out of subdomain Slice bits.

Source code:

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};
    }
}

Usage example:

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

Sources