Skip to content

Instantly share code, notes, and snippets.

@mdtanrikulu
Created June 21, 2023 15:23
Show Gist options
  • Save mdtanrikulu/6888fb2986bd7679f4c557e18bf3105d to your computer and use it in GitHub Desktop.
Save mdtanrikulu/6888fb2986bd7679f4c557e18bf3105d to your computer and use it in GitHub Desktop.
dns wire format conversion
function dnsWireFormat(domain, ttl, type, cls, address) {
let labels = domain.split(".");
let output = "";
for (let label of labels) {
output += label.length.toString(16).padStart(2, '0');
for (let char of label) {
output += char.charCodeAt(0).toString(16);
}
}
output += "00";
output += type.toString(16).padStart(4, '0');
output += cls.toString(16).padStart(4, '0');
output += ttl.toString(16).padStart(8, '0');
let addrBytes = address.split(".");
output += addrBytes.length.toString(16).padStart(4, '0');
for (let byte of addrBytes) {
output += parseInt(byte).toString(16).padStart(2, '0');
}
return output;
}
// types: A: (1) NS: (2), CNAME: (5), SOA: (6), PTR: (12), MX: (15), and AAAA: (28)
// classes IN: (1), CS: (2), CH (3), HS: (4)
console.log(dnsWireFormat("tanrikulu.eth", 3600, 1, 1, "1.2.3.4"));
// 0974616e72696b756c7503657468000001000100000e10000401020304
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment