Skip to content
This repository was archived by the owner on Mar 27, 2024. It is now read-only.

Commit 1daefcc

Browse files
authored
fix: presentation with empty credential fix (#3319)
This change fixes how a presentation is marshalled with empty credential field which wrongly outputs: '"verifiableCredential": null' This change skips this field if the presentation Credential field is empty Signed-off-by: Baha Shaaban <baha.shaaban@securekey.com>
1 parent 1d1ff78 commit 1daefcc

2 files changed

Lines changed: 53 additions & 3 deletions

File tree

pkg/doc/verifiable/presentation.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -286,17 +286,22 @@ func (vp *Presentation) raw() (*rawPresentation, error) {
286286
return nil, err
287287
}
288288

289-
return &rawPresentation{
289+
rp := &rawPresentation{
290290
// TODO single value contexts should be compacted as part of Issue [#1730]
291291
// Not compacting now to support interoperability
292292
Context: vp.Context,
293293
ID: vp.ID,
294294
Type: typesToRaw(vp.Type),
295-
Credential: vp.credentials,
296295
Holder: vp.Holder,
297296
Proof: proof,
298297
CustomFields: vp.CustomFields,
299-
}, nil
298+
}
299+
300+
if len(vp.credentials) > 0 {
301+
rp.Credential = vp.credentials
302+
}
303+
304+
return rp, nil
300305
}
301306

302307
// rawPresentation is a basic verifiable credential.

pkg/doc/verifiable/presentation_test.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,19 @@ const validPresentation = `
5454
}
5555
`
5656

57+
const presentationWithoutCredentials = `
58+
{
59+
"@context": [
60+
"https://www.w3.org/2018/credentials/v1",
61+
"https://www.w3.org/2018/credentials/examples/v1",
62+
"https://trustbloc.github.io/context/vc/examples-v1.jsonld"
63+
],
64+
"id": "urn:uuid:3978344f-8596-4c3a-a978-8fcaba3903c5",
65+
"type": "VerifiablePresentation",
66+
"holder": "did:example:ebfeb1f712ebc6f1c276e12ec21"
67+
}
68+
`
69+
5770
const validPresentationWithCustomFields = `
5871
{
5972
"@context": [
@@ -130,6 +143,38 @@ func TestParsePresentation(t *testing.T) {
130143
require.Equal(t, "did:example:ebfeb1f712ebc6f1c276e12ec21", vp.Holder)
131144
})
132145

146+
t.Run("creates a new Verifiable Presentation from valid JSON without credentials", func(t *testing.T) {
147+
vp, err := newTestPresentation(t, []byte(presentationWithoutCredentials), WithPresStrictValidation())
148+
require.NoError(t, err)
149+
require.NotNil(t, vp)
150+
151+
// validate @context
152+
require.Equal(t, []string{
153+
"https://www.w3.org/2018/credentials/v1",
154+
"https://www.w3.org/2018/credentials/examples/v1",
155+
"https://trustbloc.github.io/context/vc/examples-v1.jsonld",
156+
}, vp.Context)
157+
158+
// check id
159+
require.Equal(t, "urn:uuid:3978344f-8596-4c3a-a978-8fcaba3903c5", vp.ID)
160+
161+
// check type
162+
require.Equal(t, []string{"VerifiablePresentation"}, vp.Type)
163+
164+
// check verifiableCredentials
165+
require.Nil(t, vp.Credentials())
166+
require.Empty(t, vp.Credentials())
167+
168+
// check holder
169+
require.Equal(t, "did:example:ebfeb1f712ebc6f1c276e12ec21", vp.Holder)
170+
171+
// check rawPresentation
172+
rp, err := vp.raw()
173+
require.NoError(t, err)
174+
175+
require.IsType(t, nil, rp.Credential)
176+
})
177+
133178
t.Run("creates a new Verifiable Presentation with custom/additional fields", func(t *testing.T) {
134179
verify := func(t *testing.T, vp *Presentation) {
135180
require.Len(t, vp.CustomFields, 1)

0 commit comments

Comments
 (0)