From 1daefcc2be6472de61ed7f9cae82aef55d85cb43 Mon Sep 17 00:00:00 2001 From: Baha <29608896+Baha-sk@users.noreply.github.com> Date: Mon, 8 Aug 2022 16:03:14 -0400 Subject: [PATCH] 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 --- pkg/doc/verifiable/presentation.go | 11 ++++-- pkg/doc/verifiable/presentation_test.go | 45 +++++++++++++++++++++++++ 2 files changed, 53 insertions(+), 3 deletions(-) diff --git a/pkg/doc/verifiable/presentation.go b/pkg/doc/verifiable/presentation.go index da2f2d625..e25cb93b0 100644 --- a/pkg/doc/verifiable/presentation.go +++ b/pkg/doc/verifiable/presentation.go @@ -286,17 +286,22 @@ func (vp *Presentation) raw() (*rawPresentation, error) { return nil, err } - return &rawPresentation{ + rp := &rawPresentation{ // TODO single value contexts should be compacted as part of Issue [#1730] // Not compacting now to support interoperability Context: vp.Context, ID: vp.ID, Type: typesToRaw(vp.Type), - Credential: vp.credentials, Holder: vp.Holder, Proof: proof, CustomFields: vp.CustomFields, - }, nil + } + + if len(vp.credentials) > 0 { + rp.Credential = vp.credentials + } + + return rp, nil } // rawPresentation is a basic verifiable credential. diff --git a/pkg/doc/verifiable/presentation_test.go b/pkg/doc/verifiable/presentation_test.go index ae09636b1..28211d560 100644 --- a/pkg/doc/verifiable/presentation_test.go +++ b/pkg/doc/verifiable/presentation_test.go @@ -54,6 +54,19 @@ const validPresentation = ` } ` +const presentationWithoutCredentials = ` +{ + "@context": [ + "https://www.w3.org/2018/credentials/v1", + "https://www.w3.org/2018/credentials/examples/v1", + "https://trustbloc.github.io/context/vc/examples-v1.jsonld" + ], + "id": "urn:uuid:3978344f-8596-4c3a-a978-8fcaba3903c5", + "type": "VerifiablePresentation", + "holder": "did:example:ebfeb1f712ebc6f1c276e12ec21" +} +` + const validPresentationWithCustomFields = ` { "@context": [ @@ -130,6 +143,38 @@ func TestParsePresentation(t *testing.T) { require.Equal(t, "did:example:ebfeb1f712ebc6f1c276e12ec21", vp.Holder) }) + t.Run("creates a new Verifiable Presentation from valid JSON without credentials", func(t *testing.T) { + vp, err := newTestPresentation(t, []byte(presentationWithoutCredentials), WithPresStrictValidation()) + require.NoError(t, err) + require.NotNil(t, vp) + + // validate @context + require.Equal(t, []string{ + "https://www.w3.org/2018/credentials/v1", + "https://www.w3.org/2018/credentials/examples/v1", + "https://trustbloc.github.io/context/vc/examples-v1.jsonld", + }, vp.Context) + + // check id + require.Equal(t, "urn:uuid:3978344f-8596-4c3a-a978-8fcaba3903c5", vp.ID) + + // check type + require.Equal(t, []string{"VerifiablePresentation"}, vp.Type) + + // check verifiableCredentials + require.Nil(t, vp.Credentials()) + require.Empty(t, vp.Credentials()) + + // check holder + require.Equal(t, "did:example:ebfeb1f712ebc6f1c276e12ec21", vp.Holder) + + // check rawPresentation + rp, err := vp.raw() + require.NoError(t, err) + + require.IsType(t, nil, rp.Credential) + }) + t.Run("creates a new Verifiable Presentation with custom/additional fields", func(t *testing.T) { verify := func(t *testing.T, vp *Presentation) { require.Len(t, vp.CustomFields, 1)