The DidKeyUtils class provides utility functions to support operations in the DID Key method.

Constructors

Properties

JWK_TO_MULTICODEC: {
    [key: string]: string;
} = ...

A mapping from JSON Web Key (JWK) property descriptors to multicodec names.

This mapping is used to convert keys in JWK (JSON Web Key) format to multicodec format.

Type declaration

  • [key: string]: string

Remarks

The keys of this object are strings that describe the JOSE key type and usage, such as 'Ed25519:public', 'Ed25519:private', etc. The values are the corresponding multicodec names used to represent these key types.

Example

const multicodecName = JWK_TO_MULTICODEC['Ed25519:public'];
// Returns 'ed25519-pub', the multicodec name for an Ed25519 public key
MULTICODEC_PUBLIC_KEY_LENGTH: Record<number, number> = ...

Defines the expected byte lengths for public keys associated with different cryptographic algorithms, indexed by their multicodec code values.

MULTICODEC_TO_JWK: {
    [key: string]: Jwk;
} = ...

A mapping from multicodec names to their corresponding JOSE (JSON Object Signing and Encryption) representations. This mapping facilitates the conversion of multicodec key formats to JWK (JSON Web Key) formats.

Type declaration

  • [key: string]: Jwk

Remarks

The keys of this object are multicodec names, such as 'ed25519-pub', 'ed25519-priv', etc. The values are objects representing the corresponding JWK properties for that key type.

Example

const joseKey = MULTICODEC_TO_JWK['ed25519-pub'];
// Returns a partial JWK for an Ed25519 public key

Methods

  • Converts a JWK (JSON Web Key) to a Multicodec code and name.

    Parameters

    • params: {
          jwk: Jwk;
      }

      The parameters for the conversion.

      • jwk: Jwk

        The JSON Web Key to be converted.

    Returns Promise<MulticodecDefinition<number>>

    A promise that resolves to a Multicodec definition.

    Example

    const jwk: Jwk = { crv: 'Ed25519', kty: 'OKP', x: '...' };
    const { code, name } = await DidKeyUtils.jwkToMulticodec({ jwk });
  • Returns the appropriate public key compressor for the specified cryptographic curve.

    Parameters

    • curve: string

      The cryptographic curve to use for the key conversion.

    Returns ((params) => Promise<Uint8Array>)

    A public key compressor for the specified curve.

      • (params): Promise<Uint8Array>
      • Converts a public key to its compressed form.

        Parameters

        • params: {
              publicKeyBytes: Uint8Array;
          }

          The parameters for the public key compression.

          • publicKeyBytes: Uint8Array

            The public key as a Uint8Array.

        Returns Promise<Uint8Array>

        A Promise that resolves to the compressed public key as a Uint8Array.

  • Returns the appropriate key converter for the specified cryptographic curve.

    Parameters

    • curve: string

      The cryptographic curve to use for the key conversion.

    Returns AsymmetricKeyConverter

    An AsymmetricKeyConverter for the specified curve.

  • Converts a Multicodec code or name to parial JWK (JSON Web Key).

    Parameters

    • params: {
          code?: number;
          name?: string;
      }

      The parameters for the conversion.

      • Optional code?: number

        Optional Multicodec code to convert.

      • Optional name?: string

        Optional Multicodec name to convert.

    Returns Promise<Jwk>

    A promise that resolves to a JOSE format key.

    Example

    const partialJwk = await DidKeyUtils.multicodecToJwk({ name: 'ed25519-pub' });
    
  • Converts a public key in JWK (JSON Web Key) format to a multibase identifier.

    Parameters

    • params: {
          publicKey: Jwk;
      }

      The parameters for the conversion.

      • publicKey: Jwk

        The public key in JWK format.

    Returns Promise<string>

    A promise that resolves to the multibase identifier.

    Remarks

    Note: All secp public keys are converted to compressed point encoding before the multibase identifier is computed.

    Per Multicodec table: Public keys for Elliptic Curve cryptography algorithms (e.g., secp256k1, secp256k1r1, secp384r1, etc.) are always represented with compressed point encoding (e.g., secp256k1-pub, p256-pub, p384-pub, etc.).

    Per RFC 8812: "As a compressed point encoding representation is not defined for JWK elliptic curve points, the uncompressed point encoding defined there MUST be used. The x and y values represented MUST both be exactly 256 bits, with any leading zeros preserved."

    Example

    const publicKey = { crv: 'Ed25519', kty: 'OKP', x: '...' };
    const multibaseId = await DidKeyUtils.publicKeyToMultibaseId({ publicKey });