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 resolver option #293

Merged
merged 1 commit into from
Jul 18, 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
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.9.0",
"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 { SignedWrappedDocument, v2, v3, WrappedDocument } 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
24 changes: 14 additions & 10 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 } 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 @@ -41,11 +41,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 @@ -54,7 +56,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 @@ -84,7 +87,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 @@ -111,7 +114,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 @@ -121,7 +125,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 @@ -144,9 +148,9 @@ const verifyV3 = async (
};
};

const verify: VerifierType["verify"] = async (document) => {
if (utils.isSignedWrappedV2Document(document)) return verifyV2(document);
else if (utils.isSignedWrappedV3Document(document)) return verifyV3(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);
throw new CodedError(
"Document does not match either v2 or v3 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
Loading