Language
Libraries
@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

Converts a DNS string to a Slice?.

Signature:

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

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

dnsInternalNormalize

Normalizes the internal DNS representation of the Slice.

Signature:

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

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

dnsInternalVerify

Verifies the internal DNS representation of the subdomain Slice.

Signature:

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

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

dnsExtractTopDomainLength

Calculates length of a top domain in the subdomain Slice.

Signature:

fun dnsExtractTopDomainLength(subdomain: Slice): Int;

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

Extracts top domain from a subdomain Slice.

Signature:

fun dnsExtractTopDomain(subdomain: Slice): Slice;

Source code:

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

dnsResolveNext

Resolves an address Address into a Cell.

Signature:

fun dnsResolveNext(address: Address): Cell;

Source code:

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

dnsResolveWallet

Resolves a wallet address Address into a Cell.

Signature:

fun dnsResolveWallet(address: Address): 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 dnsresolve, which corresponds to it's FunC variant (opens in a new tab).
  2. virtual 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