Skip to content

Commit

Permalink
Minor Refactoring to NS record PR (#608)
Browse files Browse the repository at this point in the history
* minor refactoring

* Fixed an ambiguous if condition
  • Loading branch information
thehenrytsai committed May 21, 2024
1 parent d38379a commit 6a153e4
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 10 deletions.
3 changes: 3 additions & 0 deletions packages/dids/src/did-error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@ export enum DidErrorCode {
/** The DID resolver was unable to find the DID document resulting from the resolution request. */
NotFound = 'notFound',

/** The DID resolver was unable to find the DID document in any authoritative gateway. */
NotFoundInAuthoritativeGateWay = 'notFoundInAuthoritativeGateWay',

/**
* The representation requested via the `accept` input metadata property is not supported by the
* DID method and/or DID resolver implementation.
Expand Down
24 changes: 14 additions & 10 deletions packages/dids/src/methods/did-dht.ts
Original file line number Diff line number Diff line change
Expand Up @@ -808,13 +808,14 @@ export class DidDhtDocument {
let bep44Message = await DidDhtDocument.pkarrGet({ gatewayUri, publicKeyBytes });

// Verify the signature of the BEP44 message and parse the value to a DNS packet.
let dnsPacket = await DidDhtUtils.parseBep44GetMessage({ bep44Message });
let dnsPacket: Packet | undefined = await DidDhtUtils.parseBep44GetMessage({ bep44Message });

// Look at the NS records in the DNS packet to find the resolution gateway URIs.
let resolutionGatewayUris = await DidDhtDocument.getAuthoritativeGatewayUris({ didUri, dnsPacket });
const resolutionGatewayUris = await DidDhtDocument.getAuthoritativeGatewayUris({ didUri, dnsPacket });

// Only do a second retrieval if the authoritative resolution gateway URIs are different from the given gateway URI.
if(!resolutionGatewayUris.includes(gatewayUri)) {
// Only do a second retrieval if authoritative resolution gateway URIs are specified and are different from the given gateway URI.
if(resolutionGatewayUris.length > 0 && !resolutionGatewayUris.includes(gatewayUri)) {
dnsPacket = undefined; // reset to `undefined` to use as a condition check for throwing an error
const accumulatedErrors = [];
for(const nsRecordGatewayUri of resolutionGatewayUris) {
try {
Expand All @@ -823,17 +824,20 @@ export class DidDhtDocument {
} catch (error: any) {
accumulatedErrors.push(`Failed retrieval from ${nsRecordGatewayUri}: ${error}`);

if(nsRecordGatewayUri == resolutionGatewayUris[resolutionGatewayUris.length - 1]) {
throw new Error(`DID document not found for: ${didUri}. Errors: ${accumulatedErrors.join('; ')}`);
}

// If the retrieval failed, try the next resolution gateway.
continue;
}

// If the retrieval was successful, break the loop.
break;
}

if(dnsPacket === undefined) {
throw new DidError(
DidErrorCode.NotFoundInAuthoritativeGateWay,
`DID document not found for: ${didUri} after looping through all authoritative gateways. Errors: ${accumulatedErrors.join('; ')}`
);
}
}

// Convert the DNS packet to a DID document and metadata.
Expand Down Expand Up @@ -1002,7 +1006,7 @@ export class DidDhtDocument {
* @param {object} params - The parameters to use when extracting gateway URIs from the DNS packet.
* @param {string} params.didUri - The DID URI corresponding to the DNS packet.
* @param {Packet} params.dnsPacket - The DNS packet containing potential NS records for resolution gateways.
* @returns {Promise<string[]>} Resolves to an array of gateway URIs if found, otherwise an empty array.
* @returns {Promise<string[]>} Resolves to an array of gateway URIs without trailing '.' if found, otherwise an empty array.
*/
public static async getAuthoritativeGatewayUris({ didUri, dnsPacket }: {
didUri: string;
Expand All @@ -1013,7 +1017,7 @@ export class DidDhtDocument {
for (const answer of dnsPacket?.answers ?? []) {
if (answer.type !== 'NS') continue;

if(answer.name.endsWith(`.${DidDhtDocument.getUniqueDidSuffix(didUri)}.`)) {
if (answer.name.endsWith(`.${DidDhtDocument.getUniqueDidSuffix(didUri)}.`)) {
const gatewayUri = answer.data.slice(0, -1); // Remove trailing dot
authoritativeGatewayUris.push(gatewayUri);
break;
Expand Down

0 comments on commit 6a153e4

Please sign in to comment.