Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: dns resolvers #61

Merged
merged 3 commits into from
Jul 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;

HJunyuan marked this conversation as resolved.
Show resolved Hide resolved
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";
Loading