Skip to content

Commit

Permalink
feat: dns resolver option
Browse files Browse the repository at this point in the history
  • Loading branch information
superical committed Jul 17, 2024
1 parent 9a9d9d6 commit e593abd
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 36 deletions.
26 changes: 13 additions & 13 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
"author": "",
"license": "Apache-2.0",
"dependencies": {
"@govtechsg/dnsprove": "^2.6.2",
"@govtechsg/dnsprove": "^2.8.0",
"@govtechsg/open-attestation": "^6.10.0-beta.3",
"axios": "^1.6.2",
"debug": "^4.3.1",
Expand Down
2 changes: 1 addition & 1 deletion src/common/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export const getDefaultProvider = (options: VerificationBuilderOptionsWithNetwor

// getProvider is a function to get an existing provider or to get a Default provider, when given the options
export const getProvider = (options: VerificationBuilderOptions): providers.Provider => {
return options.provider ?? getDefaultProvider(options);
return options.provider ?? getDefaultProvider(options as VerificationBuilderOptionsWithNetwork);
};

/**
Expand Down
28 changes: 21 additions & 7 deletions src/types/core.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { WrappedDocument, SignedWrappedDocument, v2, v3, v4 } from "@govtechsg/open-attestation";
import type { CustomDnsResolver } from "@govtechsg/dnsprove";
import { Resolver } from "did-resolver";
import { providers } from "ethers";
import { Reason } from "./error";
Expand All @@ -8,23 +9,36 @@ import { Reason } from "./error";
*/
export type PromiseCallback = (promises: Promise<VerificationFragment>[]) => void;

export interface VerificationBuilderOptionsWithProvider {
export type CustomDnsResolverOption = {
dnsResolvers?: CustomDnsResolver[];
};

export type VerificationBuilderOptionsWithProvider = {
provider: providers.Provider;
resolver?: Resolver;
}
} & CustomDnsResolverOption;

export interface VerificationBuilderOptionsWithNetwork {
export type VerificationBuilderOptionsWithNetwork = {
network: string;
resolver?: Resolver;
provider?: never;
}
} & CustomDnsResolverOption;

export type VerificationBuilderOptions = VerificationBuilderOptionsWithProvider | VerificationBuilderOptionsWithNetwork;
export type VerificationBuilderOptionsDnsDid = {
resolver?: Resolver;
network?: never;
provider?: never;
} & CustomDnsResolverOption;

export type VerificationBuilderOptions =
| VerificationBuilderOptionsWithProvider
| VerificationBuilderOptionsWithNetwork
| VerificationBuilderOptionsDnsDid;

export interface VerifierOptions {
export type VerifierOptions = {
provider: providers.Provider;
resolver?: Resolver;
}
} & CustomDnsResolverOption;

/**
* A verification fragment is the result of a verification
Expand Down
31 changes: 18 additions & 13 deletions src/verifiers/issuerIdentity/dnsDid/dnsDidProof.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { getData, utils, v2, v3, v4 } from "@govtechsg/open-attestation";
import { getDnsDidRecords } from "@govtechsg/dnsprove";
import { VerificationFragmentType, Verifier } from "../../../types/core";
import { CustomDnsResolver, getDnsDidRecords } from "@govtechsg/dnsprove";
import { VerificationFragmentType, Verifier, VerifierOptions } from "../../../types/core";
import { OpenAttestationDnsDidCode } from "../../../types/error";
import { withCodedErrorHandler } from "../../../common/errorHandler";
import { CodedError } from "../../../common/error";
Expand Down Expand Up @@ -43,11 +43,13 @@ const test: VerifierType["test"] = (document) => {
const verifyIssuerDnsDid = async ({
key,
location,
dnsResolvers,
}: {
key: string;
location: string;
dnsResolvers?: CustomDnsResolver[];
}): Promise<DnsDidVerificationStatus> => {
const records = await getDnsDidRecords(location);
const records = await getDnsDidRecords(location, dnsResolvers);
return {
location,
key,
Expand All @@ -56,7 +58,8 @@ const verifyIssuerDnsDid = async ({
};

const verifyV2 = async (
document: v2.SignedWrappedDocument
document: v2.SignedWrappedDocument,
options?: VerifierOptions
): Promise<OpenAttestationDnsDidIdentityProofVerificationFragment> => {
const documentData = getData(document);
const deferredVerificationStatus: Promise<DnsDidVerificationStatus>[] = documentData.issuers.map((issuer) => {
Expand Down Expand Up @@ -86,7 +89,7 @@ const verifyV2 = async (
OpenAttestationDnsDidCode.MALFORMED_IDENTITY_PROOF,
OpenAttestationDnsDidCode[OpenAttestationDnsDidCode.MALFORMED_IDENTITY_PROOF]
);
return verifyIssuerDnsDid({ key, location });
return verifyIssuerDnsDid({ key, location, dnsResolvers: options?.dnsResolvers });
});
const verificationStatus = await Promise.all(deferredVerificationStatus);

Expand All @@ -113,7 +116,8 @@ const verifyV2 = async (
};

const verifyV3 = async (
document: v3.SignedWrappedDocument
document: v3.SignedWrappedDocument,
options?: VerifierOptions
): Promise<OpenAttestationDnsDidIdentityProofVerificationFragment> => {
if (!utils.isSignedWrappedV3Document(document))
throw new CodedError(
Expand All @@ -123,7 +127,7 @@ const verifyV3 = async (
);
const location = document.openAttestationMetadata.identityProof.identifier;
const { key } = document.proof;
const verificationStatus = await verifyIssuerDnsDid({ key, location });
const verificationStatus = await verifyIssuerDnsDid({ key, location, dnsResolvers: options?.dnsResolvers });

if (ValidDnsDidVerificationStatus.guard(verificationStatus)) {
return {
Expand All @@ -147,7 +151,8 @@ const verifyV3 = async (
};

const verifyV4 = async (
document: v4.SignedWrappedDocument
document: v4.SignedWrappedDocument,
options?: VerifierOptions
): Promise<OpenAttestationDnsDidIdentityProofVerificationFragment> => {
if (!utils.isSignedWrappedV4Document(document))
throw new CodedError(
Expand All @@ -157,7 +162,7 @@ const verifyV4 = async (
);
const location = document.issuer.identityProof.identifier;
const { key } = document.proof;
const verificationStatus = await verifyIssuerDnsDid({ key, location });
const verificationStatus = await verifyIssuerDnsDid({ key, location, dnsResolvers: options?.dnsResolvers });

if (ValidDnsDidVerificationStatus.guard(verificationStatus)) {
return {
Expand All @@ -180,10 +185,10 @@ const verifyV4 = async (
};
};

const verify: VerifierType["verify"] = async (document) => {
if (utils.isSignedWrappedV2Document(document)) return verifyV2(document);
else if (utils.isSignedWrappedV3Document(document)) return verifyV3(document);
else if (utils.isSignedWrappedV4Document(document)) return verifyV4(document);
const verify: VerifierType["verify"] = async (document, options) => {
if (utils.isSignedWrappedV2Document(document)) return verifyV2(document, options);
else if (utils.isSignedWrappedV3Document(document)) return verifyV3(document, options);
else if (utils.isSignedWrappedV4Document(document)) return verifyV4(document, options);
throw new CodedError(
"Document does not match either v2, v3 or v4 formats. Consider using `utils.diagnose` from open-attestation to find out more.",
OpenAttestationDnsDidCode.UNRECOGNIZED_DOCUMENT,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ const resolveIssuerIdentity = async (
options: VerifierOptions
): Promise<DnsTxtVerificationStatus> => {
const network = await options.provider.getNetwork();
const records = await getDocumentStoreRecords(location);
const records = await getDocumentStoreRecords(location, options.dnsResolvers);
const matchingRecord = records.find(
(record) =>
record.addr.toLowerCase() === smartContractAddress.toLowerCase() &&
Expand Down
1 change: 1 addition & 0 deletions src/verifiers/verificationBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ export const verificationBuilder =
const verifierOptions: VerifierOptions = {
provider: getProvider(builderOptions),
resolver: builderOptions.resolver,
dnsResolvers: builderOptions.dnsResolvers,
};
const promises = verifiers.map((verifier) => {
if (verifier.test(document, verifierOptions)) {
Expand Down

0 comments on commit e593abd

Please sign in to comment.