Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update @web5/credentials README #349

Merged
merged 10 commits into from
Dec 13, 2023
Merged

Update @web5/credentials README #349

merged 10 commits into from
Dec 13, 2023

Conversation

nitro-neal
Copy link
Contributor

Credentials

The Credentials package enables the creation, signing, verification, and general processing of Verifiable Credentials (VCs). It also has full Presentation Exchange support.

Verifiable Credential

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.

Usage:

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.
  • expirationDate?: (optinal) Expiration Date
class StreetCredibility {
  constructor(localRespect, legit) {
    this.localRespect = localRespect;
    this.legit = legit;
  }
}

const vc = new VerifiableCredential({
  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 { DidKeyMethod } from '@web5/dids';
const issuer = await DidKeyMethod.create();

Then sign the VC using the did object

const vcJwt = 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 })

Presentation Exchange

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

Features

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

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})

Copy link

codesandbox bot commented Dec 11, 2023

Review or Edit in CodeSandbox

Open the branch in Web EditorVS CodeInsiders

Open Preview

Copy link
Contributor

github-actions bot commented Dec 11, 2023

TBDocs Report

✅ No errors or warnings

@web5/api

  • Project entry file: packages/api/src/index.ts

TBDocs Report Updated at 2023-12-13T19:33:12Z 4ad3ef5

@mistermoe
Copy link
Contributor

yo @nitro-neal ! i think we're missing an await before vc.sign and VerifiableCredential.create

Copy link

codecov bot commented Dec 11, 2023

Codecov Report

Merging #349 (4ad3ef5) into main (78c2de2) will decrease coverage by 0.07%.
Report is 3 commits behind head on main.
The diff coverage is n/a.

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #349      +/-   ##
==========================================
- Coverage   92.80%   92.73%   -0.07%     
==========================================
  Files          76       71       -5     
  Lines       17267    16473     -794     
  Branches     1607     1521      -86     
==========================================
- Hits        16025    15277     -748     
+ Misses       1215     1174      -41     
+ Partials       27       22       -5     
Components Coverage Δ
api 96.93% <ø> (+0.20%) ⬆️
common 97.78% <ø> (ø)
credentials ∅ <ø> (∅)
crypto 100.00% <ø> (ø)
dids 91.87% <ø> (ø)
agent 88.08% <ø> (ø)
identity-agent 56.81% <ø> (ø)
proxy-agent 58.43% <ø> (ø)
user-agent 55.22% <ø> (ø)

@frankhinek
Copy link
Contributor

We could also add docs for using the Jwt class but this functionality is going to end up moving over to @web5/crypto after this and this are done.

packages/credentials/README.md Outdated Show resolved Hide resolved
packages/credentials/README.md Outdated Show resolved Hide resolved
packages/credentials/README.md Outdated Show resolved Hide resolved
packages/credentials/README.md Outdated Show resolved Hide resolved
packages/credentials/README.md Outdated Show resolved Hide resolved
packages/credentials/tests/presentation-exchange.spec.ts Outdated Show resolved Hide resolved
packages/credentials/tests/presentation-exchange.spec.ts Outdated Show resolved Hide resolved
@frankhinek frankhinek linked an issue Dec 12, 2023 that may be closed by this pull request
@frankhinek frankhinek changed the title update readme Update @web5/crypto README Dec 13, 2023
@frankhinek frankhinek added the documentation Improvements or additions to documentation label Dec 13, 2023
@frankhinek frankhinek changed the title Update @web5/crypto README Update @web5/credentials README Dec 13, 2023
@nitro-neal nitro-neal merged commit edc8054 into main Dec 13, 2023
30 checks passed
@nitro-neal nitro-neal deleted the update-cred-readme-2 branch December 13, 2023 20:05
finn-block pushed a commit that referenced this pull request Mar 19, 2024
* update readme

* updates

* Update packages/credentials/README.md

Co-authored-by: Frank Hinek <[email protected]>

* Update packages/credentials/README.md

Co-authored-by: Frank Hinek <[email protected]>

* Update packages/credentials/README.md

Co-authored-by: Frank Hinek <[email protected]>

* Update packages/credentials/README.md

Co-authored-by: Frank Hinek <[email protected]>

* Update packages/credentials/README.md

Co-authored-by: Frank Hinek <[email protected]>

* Update packages/credentials/tests/presentation-exchange.spec.ts

Co-authored-by: Frank Hinek <[email protected]>

* Update packages/credentials/tests/presentation-exchange.spec.ts

Co-authored-by: Frank Hinek <[email protected]>

* minor readme updates

---------

Co-authored-by: Frank Hinek <[email protected]>
Co-authored-by: Moe Jangda <[email protected]>
finn-block pushed a commit that referenced this pull request Mar 19, 2024
* update readme

* updates

* Update packages/credentials/README.md

Co-authored-by: Frank Hinek <[email protected]>

* Update packages/credentials/README.md

Co-authored-by: Frank Hinek <[email protected]>

* Update packages/credentials/README.md

Co-authored-by: Frank Hinek <[email protected]>

* Update packages/credentials/README.md

Co-authored-by: Frank Hinek <[email protected]>

* Update packages/credentials/README.md

Co-authored-by: Frank Hinek <[email protected]>

* Update packages/credentials/tests/presentation-exchange.spec.ts

Co-authored-by: Frank Hinek <[email protected]>

* Update packages/credentials/tests/presentation-exchange.spec.ts

Co-authored-by: Frank Hinek <[email protected]>

* minor readme updates

---------

Co-authored-by: Frank Hinek <[email protected]>
Co-authored-by: Moe Jangda <[email protected]>
finn-block pushed a commit that referenced this pull request Mar 19, 2024
* update readme

* updates

* Update packages/credentials/README.md

Co-authored-by: Frank Hinek <[email protected]>

* Update packages/credentials/README.md

Co-authored-by: Frank Hinek <[email protected]>

* Update packages/credentials/README.md

Co-authored-by: Frank Hinek <[email protected]>

* Update packages/credentials/README.md

Co-authored-by: Frank Hinek <[email protected]>

* Update packages/credentials/README.md

Co-authored-by: Frank Hinek <[email protected]>

* Update packages/credentials/tests/presentation-exchange.spec.ts

Co-authored-by: Frank Hinek <[email protected]>

* Update packages/credentials/tests/presentation-exchange.spec.ts

Co-authored-by: Frank Hinek <[email protected]>

* minor readme updates

---------

Co-authored-by: Frank Hinek <[email protected]>
Co-authored-by: Moe Jangda <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Update @web5/credentials README for v0.4.0
3 participants