npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@web5/credentials

v1.0.1

Published

Verifiable Credentials

Downloads

3,764

Readme

@web5/credentials

The @web5/credentials package provides the following functionality:

Table of Contents

VerifiableCredential

VC Features

  • Create Verifiable Credentials with flexible data types.
  • Sign credentials using decentralized identifiers (DIDs).
  • Verify the integrity and authenticity of VCs encoded as JSON Web Tokens (JWTs).
  • Parse JWT representations of VCs into VerifiableCredential instances.

VC Usage

Along with the credentials package you will need command and dids for most of the Verifiable Credentials operations

npm install @web5/common
npm install @web5/dids
npm install @web5/credentials

Then to import:

import { VerifiableCredential, VerifiablePresentation, PresentationExchange } from '@web5/credentials';

Creating a Verifiable Credential

Create a new VerifiableCredential with the following parameters:

  • type: Type of the credential.
  • issuer: Issuer URI.
  • subject: Subject URI.
  • data: Credential data.
  • issuanceDate? (Optional) The Issuance Date. Defaults to current date if not specified
  • expirationDate?: (Optional) Expiration Date
  • evidence?: (Optional) Evidence can be included by an issuer to provide the verifier with additional supporting information in a verifiable credential.
class StreetCredibility {
  constructor(localRespect, legit) {
    this.localRespect = localRespect;
    this.legit = legit;
  }
}

const vc = await VerifiableCredential.create({
  type: "StreetCred",
  issuer: "did:example:issuer",
  subject: "did:example:subject",
  data: new StreetCredibility("high", true)
});

Signing a Verifiable Credential

Sign a VerifiableCredential with a DID:

  • did: The did that is signing the VC

First create a Did object as follows:

import { DidKey } from '@web5/dids';
const issuer: BearerDid = await DidKey.create();

Then sign the VC using the did object

const vcJwt = await vc.sign({ did: issuer });

Verifying a Verifiable Credential

Verify the integrity and authenticity of a Verifiable Credential

  • vcJwt: The VC in JWT format as a String.
try {
  await VerifiableCredential.verify({ vcJwt: signedVcJwt })
  console.log("VC Verification successful!")
} catch (e: Error) {
  console.log("VC Verification failed: ${e.message}")
}

Parsing a JWT into a Verifiable Credential

Parse a JWT into a VerifiableCredential instance

vcJwt: The VC JWT as a String.

const vc = VerifiableCredential.parseJwt({ vcJwt: signedVcJwt })

VerifiablePresentation

VP Features

  • Create Verifiable Presentation with flexible data types.
  • Sign presentations using decentralized identifiers (DIDs).
  • Verify the integrity and authenticity of VPs encoded as JSON Web Tokens (JWTs).
  • Parse JWT representations of VPs into VerifiablePresentation instances.

VP Usage

Creating a Verifiable Presentation

Create a new VerifiablePresentation with the following parameters:

  • holder: The holder URI of the presentation, as a string..
  • vcJwts: The JWTs of the credentials to be included in the presentation.
  • type: Optional type of the presentation, can be a string or an array of strings.
  • additionalData: Optional additional data to be included in the presentation.
const vp = await VerifiablePresentation.create({
  type: 'PresentationSubmission',
  holder: 'did:ex:holder',
  vcJwts: vcJwts,
  additionalData: { 'arbitrary': 'data' }
});

Signing a Verifiable Presentation

Sign a VerifiablePresentation with a DID:

  • did: The did that is signing the VP

Sign the VP using the did object

const vpJwt = await vp.sign({ did: issuer });

Verifying a Verifiable Presentation

Verify the integrity and authenticity of a Verifiable Presentation

  • vpJwt: The VP in JWT format as a String.
try {
  await VerifiablePresentation.verify({ vpJwt: signedVpJwt })
  console.log("VP Verification successful!")
} catch (e: Error) {
  console.log("VP Verification failed: ${e.message}")
}

Parsing a JWT into a Verifiable Presentation

Parse a JWT into a VerifiablePresentation instance

vpJwt: The VP JWT as a String.

const parsedVp = VerifiablePresentation.parseJwt({ vcJwt: signedVcJwt })

PresentationExchange

PresentationExchange is designed to facilitate the creation of a Verifiable Presentation by providing tools to select and validate Verifiable Credentials against defined criteria.

PEX Features

  • Select credentials that satisfy a given presentation definition.
  • Validate if a Verifiable Credential JWT satisfies a Presentation Definition.
  • Validate input descriptors within Presentation Definitions.

PEX Usage

Selecting Credentials

Select Verifiable Credentials that meet the criteria of a given presentation definition.

  • vcJwts: The list of Verifiable Credentials to select from.
  • presentationDefinition The Presentation Definition to match against.

This returns a list of the vcJwts that are acceptable in the presentation definition.

const selectedCredentials = PresentationExchange.selectCredentials({
    vcJwts: signedVcJwts,
    presentationDefinition: presentationDefinition
})

Satisfying a Presentation Definition

Validate if a Verifiable Credential JWT satisfies the given presentation definition. Will return an error if the evaluation results in warnings or errors.

  • vcJwts: The list of Verifiable Credentials to select from.
  • presentationDefinition The Presentation Definition to match against.
try {
  PresentationExchange.satisfiesPresentationDefinition({ vcJwts: signedVcJwts, presentationDefinition: presentationDefinition })
  console.log("vcJwts satisfies Presentation Definition!")
} catch (e: Error) {
  console.log("Verification failed: ${e.message}")
}

Create Presentation From Credentials

Creates a presentation from a list of Verifiable Credentials that satisfy a given presentation definition. This function initializes the Presentation Exchange (PEX) process, validates the presentation definition, evaluates the credentials against the definition, and finally constructs the presentation result if the evaluation is successful.

  • vcJwts: The list of Verifiable Credentials to select from.
  • presentationDefinition The Presentation Definition to match against.
const presentationResult = PresentationExchange.createPresentationFromCredentials({ vcJwts: signedVcJwts, presentationDefinition: presentationDefinition })

Validate Definition

This method validates whether an object is usable as a presentation definition or not.

  • presentationDefinition The Presentation Definition to validate
const valid = PresentationExchange.validateDefinition({ presentationDefinition })

Validate Submission

This method validates whether an object is usable as a presentation submission or not.

  • presentationSubmission The Presentation Submission to validate
const valid = PresentationExchange.validateSubmission({ presentationSubmission })

Validate Presentation

Evaluates a presentation against a presentation definition.

  • presentationDefinition The Presentation Definition to validate
  • presentation The Presentation
const evaluationResults = PresentationExchange.evaluatePresentation({ presentationDefinition, presentation })