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 Readme for credentials package #265

Merged
merged 1 commit into from
Nov 7, 2023
Merged

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:

  • signOptions: The sign options used to sign the credential.

First create a SignOptions object as follows:

import { Ed25519, Jose } from '@web5/crypto';
import { DidKeyMethod } from '@web5/dids';

const issuer = await DidKeyMethod.create();
const privateKey = (await Jose.jwkToKey({ key: issuer.keySet.verificationMethodKeys![0].privateKeyJwk! })).keyMaterial;

const signOptions = {
  issuerDid: issuer.did,
  subjectDid: "did:example:subject",
  kid: `${issuer.did}#${issuer.did.split(':')[2]}`,
  signer: async (data) => await Ed25519.sign({ data, key: privateKey })
};

Then sign the VC using the signoptions object

const vcJwt = vc.sign(signOptions)

Verifying a Verifiable Credential

Verify the integrity and authenticity of a VC JWT

  • vcJwt: The VC in JWT format as a String.
try {
  await VerifiableCredential.verify(vcJwt)
  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)

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,
    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, 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, presentationDefinition)

Validate Definition

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

const valid = PresentationExchange.validateDefinition(presentationDefinition)

Validate Submission

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

const valid = PresentationExchange.validateSubmission(presentationSubmission)

Validate Presentation

Evaluates a presentation against a presentation definition.

const evaluationResults = PresentationExchange.evaluatePresentation(presentationDefinition, presentation)

Copy link

codesandbox bot commented Nov 6, 2023

Review or Edit in CodeSandbox

Open the branch in Web EditorVS CodeInsiders

Open Preview

Copy link
Contributor

github-actions bot commented Nov 6, 2023

TBDocs Report

🛑 Errors: 0
⚠️ Warnings: 23

@web5/api

  • Project entry file: packages/api/src/index.ts
📄 File: node_modules/abstract-level/types/abstract-level.d.ts
⚠️ compiler:TS2304: Cannot find name 'this'. #L143
⚠️ compiler:TS2304: Cannot find name 'this'. #L173
⚠️ compiler:TS2304: Cannot find name 'this'. #L187
⚠️ compiler:TS2304: Cannot find name 'this'. #L201
⚠️ compiler:TS2304: Cannot find name 'this'. #L218
📄 File: packages/api/src/did-api.ts
⚠️ extractor:ae-undocumented: Missing documentation for "agent". #L49
⚠️ extractor:ae-undocumented: Missing documentation for "connectedDid". #L50
📄 File: packages/api/src/dwn-api.ts
⚠️ extractor:ae-undocumented: Missing documentation for "agent". #L185
⚠️ extractor:ae-undocumented: Missing documentation for "connectedDid". #L186
📄 File: packages/api/src/protocol.ts
⚠️ extractor:ae-undocumented: Missing documentation for "_agent". #L30
⚠️ extractor:ae-undocumented: Missing documentation for "_metadata". #L31
⚠️ extractor:ae-undocumented: Missing documentation for "_protocolsConfigureMessage". #L32
📄 File: packages/api/src/record.ts
⚠️ extractor:ae-undocumented: Missing documentation for "_agent". #L76
⚠️ extractor:ae-undocumented: Missing documentation for "_attestation". #L77
⚠️ extractor:ae-undocumented: Missing documentation for "_contextId". #L78
⚠️ extractor:ae-undocumented: Missing documentation for "_descriptor". #L79
⚠️ extractor:ae-undocumented: Missing documentation for "_encodedData". #L80
⚠️ extractor:ae-undocumented: Missing documentation for "_encryption". #L81
⚠️ extractor:ae-undocumented: Missing documentation for "_readableStream". #L82
⚠️ extractor:ae-undocumented: Missing documentation for "_recordId". #L83
📄 File: packages/api/src/vc-api.ts
⚠️ extractor:ae-undocumented: Missing documentation for "agent". #L9
⚠️ extractor:ae-undocumented: Missing documentation for "connectedDid". #L10
📄 File: packages/api/src/web5.ts
⚠️ extractor:ae-undocumented: Missing documentation for "connectedDid". #L89

Updated @ 2023-11-06T19:55:22.677Z - Commit: 68b0630

Copy link

codecov bot commented Nov 6, 2023

Codecov Report

Merging #265 (0ceb323) into main (a2758b9) will increase coverage by 0.00%.
The diff coverage is 100.00%.

@@           Coverage Diff           @@
##             main     #265   +/-   ##
=======================================
  Coverage   90.69%   90.69%           
=======================================
  Files          70       70           
  Lines       13679    13686    +7     
  Branches     1343     1343           
=======================================
+ Hits        12406    12413    +7     
  Misses       1247     1247           
  Partials       26       26           
Components Coverage Δ
api 95.30% <ø> (ø)
common 95.00% <ø> (ø)
credentials 94.49% <100.00%> (+0.06%) ⬆️
crypto 100.00% <ø> (ø)
dids 88.75% <ø> (ø)
agent 88.17% <ø> (ø)
identity-agent 59.05% <ø> (ø)
proxy-agent 58.59% <ø> (ø)
user-agent 57.36% <ø> (ø)

@frankhinek frankhinek merged commit 50be4bd into main Nov 7, 2023
23 of 24 checks passed
@frankhinek frankhinek deleted the update-cred-readme branch November 7, 2023 14:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants