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

feat(w3c): add convenience methods to vc and vp #1477

Merged
Merged
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,13 @@ import type { W3cCredentialOptions } from '../../models/credential/W3cCredential

import { ValidateNested } from 'class-validator'

import { IsInstanceOrArrayOfInstances, SingleOrArray, asArray, mapSingleOrArray } from '../../../../utils'
import {
IsInstanceOrArrayOfInstances,
SingleOrArray,
asArray,
mapSingleOrArray,
JsonTransformer,
} from '../../../../utils'
import { ClaimFormat } from '../../models/ClaimFormat'
import { W3cCredential } from '../../models/credential/W3cCredential'

Expand Down Expand Up @@ -31,10 +37,22 @@ export class W3cJsonLdVerifiableCredential extends W3cCredential {
return proofArray.map((proof) => proof.type)
}

public toJson() {
return JsonTransformer.toJSON(this)
}

/**
* The {@link ClaimFormat} of the credential. For JSON-LD credentials this is always `ldp_vc`.
*/
public get claimFormat(): ClaimFormat.LdpVc {
return ClaimFormat.LdpVc
}

/**
* Get the encoded variant of the W3C Verifiable Credential. For JSON-LD credentials this is
* a JSON object.
*/
public get encoded() {
return this.toJson()
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import type { LinkedDataProofOptions } from './LinkedDataProof'
import type { W3cPresentationOptions } from '../../models/presentation/W3cPresentation'

import { SingleOrArray, IsInstanceOrArrayOfInstances } from '../../../../utils'
import { SingleOrArray, IsInstanceOrArrayOfInstances, JsonTransformer, asArray } from '../../../../utils'
import { ClaimFormat } from '../../models'
import { W3cPresentation } from '../../models/presentation/W3cPresentation'

import { LinkedDataProof, LinkedDataProofTransformer } from './LinkedDataProof'
Expand All @@ -21,4 +22,28 @@ export class W3cJsonLdVerifiablePresentation extends W3cPresentation {
@LinkedDataProofTransformer()
@IsInstanceOrArrayOfInstances({ classType: LinkedDataProof })
public proof!: SingleOrArray<LinkedDataProof>

public get proofTypes(): Array<string> {
const proofArray = asArray(this.proof) ?? []
return proofArray.map((proof) => proof.type)
}

public toJson() {
return JsonTransformer.toJSON(this)
}

/**
* The {@link ClaimFormat} of the presentation. For JSON-LD credentials this is always `ldp_vp`.
*/
public get claimFormat(): ClaimFormat.LdpVp {
return ClaimFormat.LdpVp
}

/**
* Get the encoded variant of the W3C Verifiable Presentation. For JSON-LD presentations this is
* a JSON object.
*/
public get encoded() {
return this.toJson()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -109,4 +109,12 @@ export class W3cJwtVerifiableCredential {
public get claimFormat(): ClaimFormat.JwtVc {
return ClaimFormat.JwtVc
}

/**
* Get the encoded variant of the W3C Verifiable Credential. For JWT credentials this is
* a JWT string.
*/
public get encoded() {
return this.serializedJwt
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import type { W3cPresentation } from '../models'

import { Jwt } from '../../../crypto/jose/jwt/Jwt'
import { AriesFrameworkError } from '../../../error'
import { ClaimFormat } from '../models'

import { getPresentationFromJwtPayload } from './presentationTransformer'

Expand Down Expand Up @@ -78,4 +79,19 @@ export class W3cJwtVerifiablePresentation {
public get holderId() {
return this.presentation.holderId
}

/**
* The {@link ClaimFormat} of the presentation. For JWT presentations this is always `jwt_vp`.
*/
public get claimFormat(): ClaimFormat.JwtVp {
return ClaimFormat.JwtVp
}

/**
* Get the encoded variant of the W3C Verifiable Presentation. For JWT presentations this is
* a JWT string.
*/
public get encoded() {
return this.serializedJwt
}
}