Skip to content

Commit

Permalink
feat: dns resolvers (#61)
Browse files Browse the repository at this point in the history
* feat: separate dns resolvers

* feat: add ali dns resolver

* fix: update paths
  • Loading branch information
superical authored Jul 17, 2024
1 parent 6294609 commit e1e943f
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 25 deletions.
36 changes: 11 additions & 25 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import axios from "axios";
import { OpenAttestationDNSTextRecord, OpenAttestationDNSTextRecordT } from "./records/dnsTxt";
import { OpenAttestationDnsDidRecord, OpenAttestationDnsDidRecordT } from "./records/dnsDid";
import { getLogger } from "./util/logger";
import { CodedError, DnsproveStatusCode } from "./common/error";
import { cloudflareDnsResolver, googleDnsResolver } from "./util/dns-resolvers";

const { trace } = getLogger("index");

Expand All @@ -24,24 +24,7 @@ interface GenericObject {

export type CustomDnsResolver = (domain: string) => Promise<IDNSQueryResponse>;

export const defaultDnsResolvers: CustomDnsResolver[] = [
async (domain) => {
const { data } = await axios({
method: "GET",
url: `https://dns.google/resolve?name=${domain}&type=TXT`,
});

return data;
},
async (domain) => {
const { data } = await axios({
method: "GET",
url: `https://cloudflare-dns.com/dns-query?name=${domain}&type=TXT`,
headers: { accept: "application/dns-json", contentType: "application/json", connection: "keep-alive" },
});
return data;
},
];
export const defaultDnsResolvers: CustomDnsResolver[] = [googleDnsResolver, cloudflareDnsResolver];

/**
* Returns true for strings that are openattestation records
Expand Down Expand Up @@ -176,21 +159,22 @@ export const parseDnsDidResults = (recordSet: IDNSRecord[] = [], dnssec: boolean
/**
* Queries a given domain and parses the results to retrieve openattestation document store records if any
* @param domain e.g: "example.openattestation.com"
* @param customDnsResolvers
* @example
* > getDocumentStoreRecords("example.openattestation.com")
* > [ { type: 'openatts',
net: 'ethereum',
netId: '3',
addr: '0x2f60375e8144e16Adf1979936301D8341D58C36C',
dnssec: true } ]
net: 'ethereum',
netId: '3',
addr: '0x2f60375e8144e16Adf1979936301D8341D58C36C',
dnssec: true } ]
*/
export const getDocumentStoreRecords = async (
domain: string,
customDnsResolvers?: CustomDnsResolver[]
): Promise<OpenAttestationDNSTextRecord[]> => {
trace(`Received request to resolve ${domain}`);

const dnsResolvers = customDnsResolvers || defaultDnsResolvers;
const dnsResolvers = customDnsResolvers ?? defaultDnsResolvers;

const results = await queryDns(domain, dnsResolvers);
const answers = results.Answer || [];
Expand All @@ -206,7 +190,7 @@ export const getDnsDidRecords = async (
): Promise<OpenAttestationDnsDidRecord[]> => {
trace(`Received request to resolve ${domain}`);

const dnsResolvers = customDnsResolvers || defaultDnsResolvers;
const dnsResolvers = customDnsResolvers ?? defaultDnsResolvers;

const results = await queryDns(domain, dnsResolvers);
const answers = results.Answer || [];
Expand All @@ -217,3 +201,5 @@ export const getDnsDidRecords = async (
};

export { OpenAttestationDNSTextRecord, OpenAttestationDnsDidRecord };

export * from "./util/dns-resolvers";
11 changes: 11 additions & 0 deletions src/util/dns-resolvers/ali-dns-resolver.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import axios from "axios";
import { CustomDnsResolver } from "../..";

export const aliDnsResolver: CustomDnsResolver = async (domain) => {
const { data } = await axios({
method: "GET",
url: `https://dns.alidns.com/resolve?name=${domain}&type=16`,
});

return data;
};
11 changes: 11 additions & 0 deletions src/util/dns-resolvers/cloudflare-dns-resolver.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import axios from "axios";
import { CustomDnsResolver } from "../..";

export const cloudflareDnsResolver: CustomDnsResolver = async (domain) => {
const { data } = await axios({
method: "GET",
url: `https://cloudflare-dns.com/dns-query?name=${domain}&type=TXT`,
headers: { accept: "application/dns-json", contentType: "application/json", connection: "keep-alive" },
});
return data;
};
11 changes: 11 additions & 0 deletions src/util/dns-resolvers/google-dns-resolver.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import axios from "axios";
import { CustomDnsResolver } from "../..";

export const googleDnsResolver: CustomDnsResolver = async (domain) => {
const { data } = await axios({
method: "GET",
url: `https://dns.google/resolve?name=${domain}&type=TXT`,
});

return data;
};
3 changes: 3 additions & 0 deletions src/util/dns-resolvers/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export * from "./google-dns-resolver";
export * from "./cloudflare-dns-resolver";
export * from "./ali-dns-resolver";

0 comments on commit e1e943f

Please sign in to comment.