Skip to content

Commit

Permalink
feat(credentials.service): resuable transfromToCredoPresentation()
Browse files Browse the repository at this point in the history
Signed-off-by: jrhender <[email protected]>
  • Loading branch information
jrhender committed Sep 19, 2024
1 parent 1df4985 commit c70a602
Showing 1 changed file with 21 additions and 13 deletions.
34 changes: 21 additions & 13 deletions apps/vc-api/src/vc-api/credentials/credentials.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,7 @@ import { transformVerificationResult } from './utils/verification-result-transfo
*/
@Injectable()
export class CredentialsService implements CredentialVerifier {
constructor(
private didService: DIDService,
private credoService: CredoService
) {}
constructor(private didService: DIDService, private credoService: CredoService) {}

async issueCredential(issueDto: IssueCredentialDto): Promise<VerifiableCredentialDto> {
const verificationMethod = await this.getVerificationMethodForDid(
Expand Down Expand Up @@ -104,9 +101,7 @@ export class CredentialsService implements CredentialVerifier {
(await this.getVerificationMethodForDid(provePresentationDto.presentation.holder)).id;

const signPresentationOption: W3cSignPresentationOptions<ClaimFormat.LdpVp> = {
presentation: JsonTransformer.fromJSON(provePresentationDto.presentation, W3cPresentation, {
validate: false
}),
presentation: this.transformToCredoPresentation(provePresentationDto.presentation, W3cPresentation),
challenge: provePresentationDto.options.challenge,
verificationMethod: verificationMethodId,
format: ClaimFormat.LdpVp,
Expand Down Expand Up @@ -148,12 +143,7 @@ export class CredentialsService implements CredentialVerifier {
options: VerifyOptionsDto
): Promise<VerificationResultDto> {
const w3cVerifyPresentationOptions: W3cVerifyPresentationOptions = {
presentation: JsonTransformer.fromJSON(vp, W3cJsonLdVerifiablePresentation, {
// Credo is expecting the the verifiableCredential property in a VP
// However, this property is optional in both the v1.1 and v2 VC data models
// For example, it isn't present if the VP is only for authentication
validate: !(vp.verifiableCredential === undefined)
}),
presentation: this.transformToCredoPresentation(vp, W3cJsonLdVerifiablePresentation),
challenge: options.challenge ?? vp.proof.challenge
};
const verifyPresentation = await this.credoService.agent.w3cCredentials.verifyPresentation(
Expand All @@ -171,4 +161,22 @@ export class CredentialsService implements CredentialVerifier {

return didDoc.verificationMethod[0];
}

/**
* Credo is expecting the the verifiableCredential property in a presentation.
* However, this property is optional in both the v1.1 and v2 VC data models.
* For example, it isn't present if the presentation is only for authentication.
* Credo issue to resolve underlying cause: https://github.com/openwallet-foundation/credo-ts/issues/2038
* @param presentation presentation to transform to a Credo type
* @param targetClass the desired Credo type
* @returns instance of targetClass
*/
private transformToCredoPresentation<T extends W3cPresentation>(
presentation,
targetClass: new (...args: unknown[]) => T
): T {
return JsonTransformer.fromJSON(presentation, targetClass, {
validate: !(presentation.verifiableCredential === undefined)
});
}
}

0 comments on commit c70a602

Please sign in to comment.