Skip to content

Commit 43923e1

Browse files
authoredMar 26, 2021
feat: add option to keep payload fields when creating JWT VC/VP (#431)
fixes #394
1 parent 9e23a10 commit 43923e1

File tree

2 files changed

+86
-3
lines changed

2 files changed

+86
-3
lines changed
 

‎__tests__/shared/verifiableData.ts

+64
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { TAgent, IDIDManager, IIdentifier, IDataStore } from '../../packages/core/src'
22
import { IDataStoreORM } from '../../packages/data-store/src'
33
import { ICredentialIssuer } from '../../packages/credential-w3c/src'
4+
import { decodeJWT } from 'did-jwt'
45

56
type ConfiguredAgent = TAgent<IDIDManager & ICredentialIssuer & IDataStore & IDataStoreORM>
67

@@ -69,6 +70,34 @@ export default (testContext: {
6970
expect(verifiableCredential).toHaveProperty('issuanceDate')
7071
expect(verifiableCredential['@context']).toEqual(['https://www.w3.org/2018/credentials/v1'])
7172
expect(verifiableCredential['type']).toEqual(['VerifiableCredential'])
73+
74+
const token = verifiableCredential.proof.jwt
75+
const { payload } = decodeJWT(token)
76+
expect(payload.vc.credentialSubject.id).not.toBeDefined()
77+
})
78+
79+
it('should create verifiable credential keeping original fields', async () => {
80+
expect.assertions(5)
81+
const verifiableCredential = await agent.createVerifiableCredential({
82+
credential: {
83+
issuer: { id: identifier.did },
84+
credentialSubject: {
85+
id: 'did:web:example.com',
86+
you: 'Rock',
87+
},
88+
},
89+
proofFormat: 'jwt',
90+
removeOriginalFields: false,
91+
})
92+
93+
expect(verifiableCredential).toHaveProperty('proof.jwt')
94+
expect(verifiableCredential).toHaveProperty('issuanceDate')
95+
expect(verifiableCredential['@context']).toEqual(['https://www.w3.org/2018/credentials/v1'])
96+
expect(verifiableCredential['type']).toEqual(['VerifiableCredential'])
97+
98+
const token = verifiableCredential.proof.jwt
99+
const { payload } = decodeJWT(token)
100+
expect(payload.vc.credentialSubject.id).toEqual('did:web:example.com')
72101
})
73102

74103
it('should create verifiable presentation', async () => {
@@ -142,6 +171,41 @@ export default (testContext: {
142171

143172
const verifiablePresentation2 = await agent.dataStoreGetVerifiablePresentation({ hash })
144173
expect(verifiablePresentation).toEqual(verifiablePresentation2)
174+
175+
const token = verifiablePresentation.proof.jwt
176+
const { payload } = decodeJWT(token)
177+
expect(payload.holder).not.toBeDefined()
178+
})
179+
180+
it('should create verifiable presentation (simple) keeping original fields', async () => {
181+
const verifiableCredential = await agent.createVerifiableCredential({
182+
credential: {
183+
issuer: { id: identifier.did },
184+
credentialSubject: {
185+
id: 'did:web:example.com',
186+
you: 'Rock',
187+
},
188+
},
189+
proofFormat: 'jwt',
190+
})
191+
192+
const verifiablePresentation = await agent.createVerifiablePresentation({
193+
presentation: {
194+
holder: identifier.did,
195+
verifier: [],
196+
verifiableCredential: [verifiableCredential],
197+
},
198+
proofFormat: 'jwt',
199+
removeOriginalFields: false,
200+
})
201+
202+
expect(verifiablePresentation).toHaveProperty('proof.jwt')
203+
expect(verifiablePresentation['@context']).toEqual(['https://www.w3.org/2018/credentials/v1'])
204+
expect(verifiablePresentation['type']).toEqual(['VerifiablePresentation'])
205+
206+
const token = verifiablePresentation.proof.jwt
207+
const { payload } = decodeJWT(token)
208+
expect(payload.holder).toEqual(identifier.did)
145209
})
146210

147211
it('should query for credentials', async () => {

‎packages/credential-w3c/src/action-handler.ts

+22-3
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,12 @@ export interface ICreateVerifiablePresentationArgs {
7272
* Currently, only JWT is supported
7373
*/
7474
proofFormat: EncodingFormat
75+
76+
/**
77+
* Remove payload members during JWT-JSON transformation. Defaults to `true`.
78+
* See https://www.w3.org/TR/vc-data-model/#jwt-encoding
79+
*/
80+
removeOriginalFields?: boolean
7581
}
7682

7783
/**
@@ -119,6 +125,12 @@ export interface ICreateVerifiableCredentialArgs {
119125
* Currently, only JWT is supported
120126
*/
121127
proofFormat: EncodingFormat
128+
129+
/**
130+
* Remove payload members during JWT-JSON transformation. Defaults to `true`.
131+
* See https://www.w3.org/TR/vc-data-model/#jwt-encoding
132+
*/
133+
removeOriginalFields?: boolean
122134
}
123135

124136
/**
@@ -215,7 +227,11 @@ export class CredentialIssuer implements IAgentPlugin {
215227
//FIXME: Throw an `unsupported_format` error if the `args.proofFormat` is not `jwt`
216228
const signer = (data: string | Uint8Array) => context.agent.keyManagerSignJWT({ kid: key.kid, data })
217229
debug('Signing VP with', identifier.did)
218-
const jwt = await createVerifiablePresentationJwt(presentation, { did: identifier.did, signer })
230+
const jwt = await createVerifiablePresentationJwt(
231+
presentation,
232+
{ did: identifier.did, signer },
233+
{ removeOriginalFields: args.removeOriginalFields },
234+
)
219235
//FIXME: flagging this as a potential privacy leak.
220236
debug(jwt)
221237
const verifiablePresentation = normalizePresentation(jwt)
@@ -256,8 +272,11 @@ export class CredentialIssuer implements IAgentPlugin {
256272
if (key.type === 'Ed25519') {
257273
alg = 'EdDSA'
258274
}
259-
260-
const jwt = await createVerifiableCredentialJwt(credential, { did: identifier.did, signer, alg })
275+
const jwt = await createVerifiableCredentialJwt(
276+
credential,
277+
{ did: identifier.did, signer, alg },
278+
{ removeOriginalFields: args.removeOriginalFields },
279+
)
261280
//FIXME: flagging this as a potential privacy leak.
262281
debug(jwt)
263282
const verifiableCredential = normalizeCredential(jwt)

0 commit comments

Comments
 (0)
Please sign in to comment.