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

Commit

Permalink
fix: presentation with empty credential fix (#3319)
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
baha-ai committed Aug 8, 2022
1 parent 1d1ff78 commit 1daefcc
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 3 deletions.
11 changes: 8 additions & 3 deletions pkg/doc/verifiable/presentation.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
45 changes: 45 additions & 0 deletions pkg/doc/verifiable/presentation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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": [
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 1daefcc

Please sign in to comment.