VerifiableCredential represents a digitally verifiable credential according to the W3C Verifiable Credentials Data Model.

It provides functionalities to sign, verify, and create credentials, offering a concise API to work with JWT representations of verifiable credentials and ensuring that the signatures and claims within those JWTs can be validated.

Constructors

Properties

Accessors

Methods

Constructors

Properties

vcDataModel: ICredential

The [VcDataModel] instance representing the core data model of a verifiable credential.

Accessors

  • get issuer(): string
  • The issuer of the credential.

    Returns string

  • get subject(): string
  • The subject of the credential.

    Returns string

  • get type(): string
  • The type of the credential.

    Returns string

Methods

  • Signs the verifiable credential and returns it as a signed JWT.

    Parameters

    Returns Promise<string>

    The JWT representing the signed verifiable credential.

    Example

    const vcJwt = verifiableCredential.sign({ did: myDid });
    
  • Converts the current object to its JSON representation.

    Returns string

    The JSON representation of the object.

  • Create a [VerifiableCredential] based on the provided parameters.

    Parameters

    Returns Promise<VerifiableCredential>

    A [VerifiableCredential] instance.

    Example

    const vc = await VerifiableCredential.create({
    type: 'StreetCredibility',
    issuer: 'did:ex:issuer',
    subject: 'did:ex:subject',
    data: { 'arbitrary': 'data' }
    })
  • Parses a JWT into a [VerifiableCredential] instance.

    Parameters

    • vcJwt: {
          vcJwt: string;
      }

      The verifiable credential JWT as a [String].

      • vcJwt: string

    Returns VerifiableCredential

    A [VerifiableCredential] instance derived from the JWT.

    Example

    const vc = VerifiableCredential.parseJwt({ vcJwt: signedVcJwt })
    
  • Verifies the integrity and authenticity of a Verifiable Credential (VC) encoded as a JSON Web Token (JWT).

    This function performs several crucial validation steps to ensure the trustworthiness of the provided VC:

    • Parses and validates the structure of the JWT.
    • Ensures the presence of critical header elements alg and kid in the JWT header.
    • Resolves the Decentralized Identifier (DID) and retrieves the associated DID Document.
    • Validates the DID and establishes a set of valid verification method IDs.
    • Identifies the correct Verification Method from the DID Document based on the kid parameter.
    • Verifies the JWT's signature using the public key associated with the Verification Method.

    If any of these steps fail, the function will throw a [Error] with a message indicating the nature of the failure:

    • exp MUST represent the expirationDate property, encoded as a UNIX timestamp (NumericDate).
    • iss MUST represent the issuer property of a verifiable credential or the holder property of a verifiable presentation.
    • nbf MUST represent issuanceDate, encoded as a UNIX timestamp (NumericDate).
    • jti MUST represent the id property of the verifiable credential or verifiable presentation.
    • sub MUST represent the id property contained in the credentialSubject.

    Once the verifications are successful, when recreating the VC data model object, this function will:

    • If exp is present, the UNIX timestamp MUST be converted to an [XMLSCHEMA11-2] date-time, and MUST be used to set the value of the expirationDate property of credentialSubject of the new JSON object.
    • If iss is present, the value MUST be used to set the issuer property of the new credential JSON object or the holder property of the new presentation JSON object.
    • If nbf is present, the UNIX timestamp MUST be converted to an [XMLSCHEMA11-2] date-time, and MUST be used to set the value of the issuanceDate property of the new JSON object.
    • If sub is present, the value MUST be used to set the value of the id property of credentialSubject of the new credential JSON object.
    • If jti is present, the value MUST be used to set the value of the id property of the new JSON object.

    Parameters

    • param: {
          vcJwt: string;
      }

      The parameters for the verification process.

      • vcJwt: string

        The Verifiable Credential in JWT format as a [string].

    Returns Promise<{
        issuer: string;
        subject: string;
        vc: ICredential;
    }>

    Example

    try {
    VerifiableCredential.verify({ vcJwt: signedVcJwt })
    console.log("VC Verification successful!")
    } catch (e: Error) {
    console.log("VC Verification failed: ${e.message}")
    }

    Throws

    Error if the verification fails at any step, providing a message with failure details.

    Throws

    Error if critical JWT header elements are absent.