Skip to content

Commit

Permalink
feat(w3c): add convenience methods to vc and vp (#1477)
Browse files Browse the repository at this point in the history
Signed-off-by: Timo Glastra <[email protected]>
  • Loading branch information
TimoGlastra authored Jun 10, 2023
1 parent e0c991f commit 83cbfe3
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 2 deletions.
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
}
}

0 comments on commit 83cbfe3

Please sign in to comment.