Resolve an AT handle to it's DID record.
at_lookup.ts
44 lines 1.1 kB view raw
1/** 2 * @file AT Protocol handle resolver 3 * @description Resolves AT Protocol handles to DIDs using DNS TXT records 4 */ 5 6import dns from "node:dns"; 7import process from "node:process"; 8 9const DOT = "."; 10 11/** 12 * The AT Protocol DNS TXT record prefix 13 * @see https://atproto.com/specs/handle#dns-txt-method 14 */ 15const at_prefix = "_atproto"; 16 17/** 18 * Callback function for DNS TXT record resolution 19 * @param {NodeJS.ErrnoException | null} err - Error object if resolution failed, null otherwise 20 * @param {string[][]} addresses - Array of TXT record values returned from DNS lookup 21 * @throws {NodeJS.ErrnoException} Throws the error if DNS resolution fails 22 */ 23function resolveCb(err: NodeJS.ErrnoException | null, addresses: string[][]) { 24 if (err) { 25 throw err; 26 } 27 28 const address = addresses[0][0]; 29 console.info(address); 30} 31 32const input = process.argv; 33 34if (input.length !== 3) { 35 throw new Error("Please provide a handle to resolve."); 36} 37 38const host = input.pop(); 39 40/** 41 * Get the did record that corresponds to an internet handle 42 * see https://atproto.com/specs/handle#dns-txt-method 43 */ 44dns.resolveTxt(at_prefix + DOT + host, resolveCb);