Skip to content

Commit

Permalink
Rename DidUri to Did
Browse files Browse the repository at this point in the history
Signed-off-by: Frank Hinek <[email protected]>
  • Loading branch information
frankhinek committed Feb 3, 2024
1 parent ab117f1 commit c469775
Show file tree
Hide file tree
Showing 9 changed files with 77 additions and 77 deletions.
58 changes: 29 additions & 29 deletions packages/dids/src/did-uri.ts → packages/dids/src/did.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* The `DidUri` class represents a Decentralized Identifier (DID) Uniform Resource Identifier (URI).
* The `Did` class represents a Decentralized Identifier (DID) Uniform Resource Identifier (URI).
*
* This class provides a method for parsing a DID URI string into its component parts, as well as a
* method for serializing a DID URI object into a string.
Expand All @@ -15,15 +15,15 @@
*
* @see {@link https://www.w3.org/TR/did-core/#did-syntax | DID Core Specification, § DID Syntax}
*/
export class DidUri {
export class Did {
/** Regular expression pattern for matching the method component of a DID URI. */
static readonly METHOD_PATTERN = '([a-z0-9]+)';
/** Regular expression pattern for matching percent-encoded characters in a method identifier. */
static readonly PCT_ENCODED_PATTERN = '(?:%[0-9a-fA-F]{2})';
/** Regular expression pattern for matching the characters allowed in a method identifier. */
static readonly ID_CHAR_PATTERN = `(?:[a-zA-Z0-9._-]|${DidUri.PCT_ENCODED_PATTERN})`;
static readonly ID_CHAR_PATTERN = `(?:[a-zA-Z0-9._-]|${Did.PCT_ENCODED_PATTERN})`;
/** Regular expression pattern for matching the method identifier component of a DID URI. */
static readonly METHOD_ID_PATTERN = `((?:${DidUri.ID_CHAR_PATTERN}*:)*(${DidUri.ID_CHAR_PATTERN}+))`;
static readonly METHOD_ID_PATTERN = `((?:${Did.ID_CHAR_PATTERN}*:)*(${Did.ID_CHAR_PATTERN}+))`;
/** Regular expression pattern for matching the path component of a DID URI. */
static readonly PATH_PATTERN = `(/[^#?]*)?`;
/** Regular expression pattern for matching the query component of a DID URI. */
Expand All @@ -32,7 +32,7 @@ export class DidUri {
static readonly FRAGMENT_PATTERN = `(#.*)?`;
/** Regular expression pattern for matching all of the components of a DID URI. */
static readonly DID_URI_PATTERN = new RegExp(
`^did:(?<method>${DidUri.METHOD_PATTERN}):(?<id>${DidUri.METHOD_ID_PATTERN})(?<path>${DidUri.PATH_PATTERN})(?<query>${DidUri.QUERY_PATTERN})(?<fragment>${DidUri.FRAGMENT_PATTERN})$`
`^did:(?<method>${Did.METHOD_PATTERN}):(?<id>${Did.METHOD_ID_PATTERN})(?<path>${Did.PATH_PATTERN})(?<query>${Did.QUERY_PATTERN})(?<fragment>${Did.FRAGMENT_PATTERN})$`
);

/**
Expand Down Expand Up @@ -94,7 +94,7 @@ export class DidUri {
params?: Record<string, string>;

/**
* Constructs a new `DidUri` instance.
* Constructs a new `DidUri` instance from individual components.
*
* @param params - An object containing the parameters to be included in the DID URI.
* @param params.method - The name of the DID method.
Expand Down Expand Up @@ -126,49 +126,49 @@ export class DidUri {
*
* @example
* ```ts
* const didUri = DidUri.parse('did:example:123?service=agent&relativeRef=/credentials#degree');
* const did = Did.parse('did:example:123?service=agent&relativeRef=/credentials#degree');
*
* console.log(didUri.uri) // Output: 'did:example:123'
* console.log(didUri.method) // Output: 'example'
* console.log(didUri.id) // Output: '123'
* console.log(didUri.query) // Output: 'service=agent&relativeRef=/credentials'
* console.log(didUri.fragment) // Output: 'degree'
* console.log(didUri.params) // Output: { service: 'agent', relativeRef: '/credentials' }
* console.log(did.uri) // Output: 'did:example:123'
* console.log(did.method) // Output: 'example'
* console.log(did.id) // Output: '123'
* console.log(did.query) // Output: 'service=agent&relativeRef=/credentials'
* console.log(did.fragment) // Output: 'degree'
* console.log(did.params) // Output: { service: 'agent', relativeRef: '/credentials' }
* ```
*
* @params didUriString - The DID URI string to be parsed.
* @returns A `DidUri` object representing the parsed DID URI, or `null` if the input string is not a valid DID URI.
* @params didUri - The DID URI string to be parsed.
* @returns A `Did` object representing the parsed DID URI, or `null` if the input string is not a valid DID URI.
*/
static parse(didUriString: string): DidUri | null {
static parse(didUri: string): Did | null {
// Return null if the input string is empty or not provided.
if (!didUriString) return null;
if (!didUri) return null;

// Execute the regex pattern on the input string to extract URI components.
const match = DidUri.DID_URI_PATTERN.exec(didUriString);
const match = Did.DID_URI_PATTERN.exec(didUri);

// If the pattern does not match, or if the required groups are not found, return null.
if (!match || !match.groups) return null;

// Extract the method, id, params, path, query, and fragment from the regex match groups.
const { method, id, path, query, fragment } = match.groups;

// Initialize a new DidUri object with the uri, method and id.
const didUri: DidUri = {
// Initialize a new Did object with the uri, method and id.
const did: Did = {
uri: `did:${method}:${id}`,
method,
id,
};

// If path is present, add it to the DidUri object.
if (path) didUri.path = path;
// If path is present, add it to the Did object.
if (path) did.path = path;

// If query is present, add it to the DidUri object, removing the leading '?'.
if (query) didUri.query = query.slice(1);
// If query is present, add it to the Did object, removing the leading '?'.
if (query) did.query = query.slice(1);

// If fragment is present, add it to the DidUri object, removing the leading '#'.
if (fragment) didUri.fragment = fragment.slice(1);
// If fragment is present, add it to the Did object, removing the leading '#'.
if (fragment) did.fragment = fragment.slice(1);

// If query params are present, parse them into a key-value object and add to the DidUri object.
// If query params are present, parse them into a key-value object and add to the Did object.
if (query) {
const parsedParams = {} as Record<string, string>;
// Split the query string by '&' to get individual parameter strings.
Expand All @@ -178,9 +178,9 @@ export class DidUri {
const [key, value] = pair.split('=');
parsedParams[key] = value;
}
didUri.params = parsedParams;
did.params = parsedParams;
}

return didUri;
return did;
}
}
2 changes: 1 addition & 1 deletion packages/dids/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export * from './did.js';
export * from './did-error.js';
export * from './did-uri.js';

export * from './methods/did-dht.js';
export * from './methods/did-ion.js';
Expand Down
6 changes: 3 additions & 3 deletions packages/dids/src/methods/did-dht.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import type {
DidVerificationMethod,
} from '../types/did-core.js';

import { DidUri } from '../did-uri.js';
import { Did } from '../did.js';
import { DidMethod } from './did-method.js';
import { DidError, DidErrorCode } from '../did-error.js';
import { DidVerificationRelationship } from '../types/did-core.js';
Expand Down Expand Up @@ -628,7 +628,7 @@ export class DidDht extends DidMethod {
methodId?: string;
}): Promise<DidVerificationMethod | undefined> {

const parsedDid = DidUri.parse(didDocument.id);
const parsedDid = Did.parse(didDocument.id);
if (parsedDid && parsedDid.method !== this.methodName) {
throw new Error(`Method not supported: ${parsedDid.method}`);
}
Expand Down Expand Up @@ -1417,7 +1417,7 @@ class DidDhtUtils {
didUri: string
}): Uint8Array {
// Parse the DID URI.
const parsedDid = DidUri.parse(didUri);
const parsedDid = Did.parse(didUri);

// Verify that the DID URI is valid.
if (!parsedDid) {
Expand Down
6 changes: 3 additions & 3 deletions packages/dids/src/methods/did-ion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import type {
PortableDidVerificationMethod,
} from '../methods/did-method.js';

import { DidUri } from '../did-uri.js';
import { Did } from '../did.js';
import { DidMethod } from '../methods/did-method.js';
import { DidError, DidErrorCode } from '../did-error.js';
import { EMPTY_DID_RESOLUTION_RESULT } from '../resolver/did-resolver.js';
Expand Down Expand Up @@ -340,7 +340,7 @@ export class DidIon extends DidMethod {
methodId?: string;
}): Promise<DidVerificationMethod | undefined> {
// Verify the DID method is supported.
const parsedDid = DidUri.parse(didDocument.id);
const parsedDid = Did.parse(didDocument.id);
if (parsedDid && parsedDid.method !== this.methodName) {
throw new DidError(DidErrorCode.MethodNotSupported, `Method not supported: ${parsedDid.method}`);
}
Expand Down Expand Up @@ -449,7 +449,7 @@ export class DidIon extends DidMethod {
*/
public static async resolve(didUri: string, options: DidResolutionOptions = {}): Promise<DidResolutionResult> {
// Attempt to parse the DID URI.
const parsedDid = DidUri.parse(didUri);
const parsedDid = Did.parse(didUri);

// If parsing failed, the DID is invalid.
if (!parsedDid) {
Expand Down
6 changes: 3 additions & 3 deletions packages/dids/src/methods/did-jwk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import { LocalKeyManager } from '@web5/crypto';
import type { BearerDid, DidCreateOptions, DidCreateVerificationMethod, DidMetadata, PortableDid } from './did-method.js';
import type { DidDocument, DidResolutionOptions, DidResolutionResult, DidVerificationMethod } from '../types/did-core.js';

import { DidUri } from '../did-uri.js';
import { Did } from '../did.js';
import { DidMethod } from './did-method.js';
import { DidError, DidErrorCode } from '../did-error.js';
import { EMPTY_DID_RESOLUTION_RESULT } from '../resolver/did-resolver.js';
Expand Down Expand Up @@ -275,7 +275,7 @@ export class DidJwk extends DidMethod {
methodId?: string;
}): Promise<DidVerificationMethod | undefined> {
// Verify the DID method is supported.
const parsedDid = DidUri.parse(didDocument.id);
const parsedDid = Did.parse(didDocument.id);
if (parsedDid && parsedDid.method !== this.methodName) {
throw new DidError(DidErrorCode.MethodNotSupported, `Method not supported: ${parsedDid.method}`);
}
Expand All @@ -295,7 +295,7 @@ export class DidJwk extends DidMethod {
*/
public static async resolve(didUri: string, _options?: DidResolutionOptions): Promise<DidResolutionResult> {
// Attempt to parse the DID URI.
const parsedDid = DidUri.parse(didUri);
const parsedDid = Did.parse(didUri);

// Attempt to decode the Base64URL-encoded JWK.
let publicKey: Jwk | undefined;
Expand Down
8 changes: 4 additions & 4 deletions packages/dids/src/methods/did-key.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import {
import type { BearerDid, DidCreateOptions, DidCreateVerificationMethod, DidMetadata, PortableDid } from './did-method.js';
import type { DidDocument, DidResolutionOptions, DidResolutionResult, DidVerificationMethod } from '../types/did-core.js';

import { DidUri } from '../did-uri.js';
import { Did } from '../did.js';
import { DidMethod } from './did-method.js';
import { DidError, DidErrorCode } from '../did-error.js';
import { getVerificationMethodTypes } from '../utils.js';
Expand Down Expand Up @@ -446,7 +446,7 @@ export class DidKey extends DidMethod {
methodId?: string;
}): Promise<DidVerificationMethod | undefined> {
// Verify the DID method is supported.
const parsedDid = DidUri.parse(didDocument.id);
const parsedDid = Did.parse(didDocument.id);
if (parsedDid && parsedDid.method !== this.methodName) {
throw new DidError(DidErrorCode.MethodNotSupported, `Method not supported: ${parsedDid.method}`);
}
Expand Down Expand Up @@ -523,7 +523,7 @@ export class DidKey extends DidMethod {
* If there are only three components set the version to the string
* value 1 and use the last value as the multibaseValue.
*/
const parsedDid = DidUri.parse(didUri);
const parsedDid = Did.parse(didUri);
if (!parsedDid) {
throw new DidError(DidErrorCode.InvalidDid, `Invalid DID URI: ${didUri}`);
}
Expand Down Expand Up @@ -1005,7 +1005,7 @@ export class DidKey extends DidMethod {
* @returns `true` if the DID URI meets the `did:key` method's structural requirements, `false` otherwise.
*
*/
private static validateIdentifier(parsedDid: DidUri): boolean {
private static validateIdentifier(parsedDid: Did): boolean {
const { method, id: multibaseValue } = parsedDid;
const [ scheme ] = parsedDid.uri.split(':', 1);

Expand Down
4 changes: 2 additions & 2 deletions packages/dids/src/methods/did-web.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { DidDocument, DidResolutionOptions, DidResolutionResult } from '../types/did-core.js';

import { DidUri } from '../did-uri.js';
import { Did } from '../did.js';
import { DidMethod } from './did-method.js';
import { EMPTY_DID_RESOLUTION_RESULT } from '../resolver/did-resolver.js';

Expand Down Expand Up @@ -40,7 +40,7 @@ export class DidWeb extends DidMethod {
*/
public static async resolve(didUri: string, _options?: DidResolutionOptions): Promise<DidResolutionResult> {
// Attempt to parse the DID URI.
const parsedDid = DidUri.parse(didUri);
const parsedDid = Did.parse(didUri);

// If parsing failed, the DID is invalid.
if (!parsedDid) {
Expand Down
6 changes: 3 additions & 3 deletions packages/dids/src/resolver/did-resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import type { KeyValueStore } from '@web5/common';
import type { DidMethodResolver } from '../methods/did-method.js';
import type { DidDereferencingOptions, DidDereferencingResult, DidResolutionOptions, DidResolutionResult, DidResource } from '../types/did-core.js';

import { DidUri } from '../did-uri.js';
import { Did } from '../did.js';
import { DidErrorCode } from '../did-error.js';
import { DidResolverCacheNoop } from './resolver-cache-noop.js';

Expand Down Expand Up @@ -115,7 +115,7 @@ export class DidResolver {
*/
public async resolve(didUri: string, options?: DidResolutionOptions): Promise<DidResolutionResult> {

const parsedDid = DidUri.parse(didUri);
const parsedDid = Did.parse(didUri);
if (!parsedDid) {
return {
...EMPTY_DID_RESOLUTION_RESULT,
Expand Down Expand Up @@ -181,7 +181,7 @@ export class DidResolver {
): Promise<DidDereferencingResult> {

// Validate the given `didUrl` confirms to the DID URL syntax.
const parsedDidUrl = DidUri.parse(didUrl);
const parsedDidUrl = Did.parse(didUrl);

if (!parsedDidUrl) {
return {
Expand Down
Loading

0 comments on commit c469775

Please sign in to comment.