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

fix: presentation with empty credential fix #3319

Merged
Merged
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
fix: presentation with empty credential fix
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>
Baha Shaaban committed Aug 8, 2022

Unverified

No user is associated with the committer email.
commit d84c044e4e2a1c9feb5a63693fe0b5e4ae297aa0
11 changes: 8 additions & 3 deletions pkg/doc/verifiable/presentation.go
Original file line number Diff line number Diff line change
@@ -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.
45 changes: 45 additions & 0 deletions pkg/doc/verifiable/presentation_test.go
Original file line number Diff line number Diff line change
@@ -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)