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

Replace sephereon/PEX presentation definitions with web5 js VCs implementation #112

Merged
merged 7 commits into from
Dec 7, 2023
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions packages/protocol/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,6 @@
},
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i think this PR necessitates a version bump - would you be able to do that? the readme has a section on how to add a changeset and automatically bump the version.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

automagic nice!

"dependencies": {
"@noble/hashes": "1.3.2",
"@sphereon/pex": "2.1.0",
"@sphereon/pex-models": "2.0.3",
"@web5/common": "0.2.1",
"@web5/credentials": "0.3.2",
"@web5/crypto": "0.2.2",
Expand Down
9 changes: 3 additions & 6 deletions packages/protocol/src/message-kinds/rfq.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
import type { MessageKind, MessageKindModel, MessageMetadata, ResourceModel } from '../types.js'

import { Offering } from '../resource-kinds/index.js'
import { VerifiableCredential } from '@web5/credentials'
import { VerifiableCredential, PresentationExchange } from '@web5/credentials'
import { Message } from '../message.js'
import { PEXv2 } from '@sphereon/pex'

const pex = new PEXv2()

/**
* Options passed to {@link Rfq.create}
Expand Down Expand Up @@ -77,9 +74,9 @@ export class Rfq extends Message<'rfq'> {
* @throws if rfq's claims do not fulfill the offering's requirements
*/
async verifyClaims(offering: Offering | ResourceModel<'offering'>) {
const { areRequiredCredentialsPresent } = pex.evaluateCredentials(offering.data.requiredClaims, this.claims)
const credentials = PresentationExchange.selectCredentials(this.claims, offering.data.requiredClaims)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

does this method return credentials or error encapsulated in an object?

Copy link
Contributor

@nitro-neal nitro-neal Dec 6, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  /**
   * Selects credentials that satisfy a given presentation definition.
   *
   * @param {string[]} vcJwts The list of Verifiable Credentials to select from.
   * @param {PresentationDefinitionV2} presentationDefinition The Presentation Definition to match against.
   * @return {string[]} selectedVcJwts A list of Verifiable Credentials that satisfy the Presentation Definition.
   */
  public static selectCredentials(
    vcJwts: string[],
    presentationDefinition: PresentationDefinitionV2
  ): string[] {
    this.resetPex();
    const selectResults: SelectResults = this.pex.selectFrom(presentationDefinition, vcJwts);
    return selectResults.verifiableCredential as string[] ?? [];
  }
  

it retuns the list of Verifiable Credentials that can satisfy the Presentation Definition.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

actually might be helpful to return the error. this way we can surface it back up to the caller

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I propose we use exceptions for error cases. Two benefits I can think of:

  1. The return type is scoped to the semantic use case, which makes naming more cohesive and self-evident (ex. List<Credentials> as opposed to SelectCredentialsResponse (which would include a credentials and errors arrays))
  2. By decoupling the error from the type, we have flexibility to introduce deeper call stacks abstracted behind the selectCredentials down the road, which could bubble-up exceptions, rather than having to then concern ourselves with passing errors up the callstack in return types

The counter argument is it makes the consumer have to use try/catch which can become verbose, but it isn't much more verbose than the subsequent if (selectCredentialsResponse.errors?.length) { ...


if (areRequiredCredentialsPresent === 'error') {
if (!credentials.length) {
throw new Error(`claims do not fulfill the offering's requirements`)
}

Expand Down
5 changes: 4 additions & 1 deletion packages/protocol/src/resource-kinds/offering.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { ResourceKindModel, ResourceMetadata } from '../types.js'
import { Resource } from '../resource.js'
import type { PresentationDefinitionV2 } from '@web5/credentials'

/**
* Options passed to {@link Offering.create}
Expand Down Expand Up @@ -64,7 +65,9 @@ export class Offering extends Resource<'offering'> {
}

/** Articulates the claim(s) required when submitting an RFQ for this offering. */
get requiredClaims() {
// TODO: Remove type annotation once type alias replaced with direct export in @web5/credentials
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is good to go now, right? Do we have to wait on a release to be cut? If so, and that may be some time, then I'm cool with leaving this comment, opening and issue, and going ahead with a merge here

// [Link to the PR](https://github.com/TBD54566975/web5-js/pull/336)
get requiredClaims(): PresentationDefinitionV2 {
return this.data.requiredClaims
}
}
2 changes: 1 addition & 1 deletion packages/protocol/src/types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { Schema as JsonSchema } from 'ajv'
import type { PresentationDefinitionV2 } from '@sphereon/pex-models'
import type { PresentationDefinitionV2 } from '@web5/credentials'

export { JsonSchema }

Expand Down
6 changes: 0 additions & 6 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading