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

chore: add CreateDisplayCredentialBytes #3580

Merged
merged 2 commits into from
May 9, 2023
Merged
Show file tree
Hide file tree
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
51 changes: 51 additions & 0 deletions pkg/doc/verifiable/credential_sdjwt.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/hyperledger/aries-framework-go/pkg/doc/sdjwt/common"
"github.com/hyperledger/aries-framework-go/pkg/doc/sdjwt/holder"
"github.com/hyperledger/aries-framework-go/pkg/doc/sdjwt/issuer"
json2 "github.com/hyperledger/aries-framework-go/pkg/doc/util/json"
)

type marshalDisclosureOpts struct {
Expand Down Expand Up @@ -389,6 +390,56 @@ func (vc *Credential) CreateDisplayCredential( // nolint:funlen,gocyclo
return newVC, nil
}

// CreateDisplayCredentialMap creates, for SD-JWT credentials, a Credential whose selective-disclosure subject fields
// are replaced with the disclosure data.
//
// Options may be provided to filter the disclosures that will be included in the display credential. If a disclosure is
// not included, the associated claim will not be present in the returned credential.
//
// If the calling Credential is not an SD-JWT credential, this method returns the credential itself.
func (vc *Credential) CreateDisplayCredentialMap( // nolint:funlen,gocyclo
opts ...DisplayCredentialOption,
) (map[string]interface{}, error) {
options := &displayCredOpts{}

for _, opt := range opts {
opt(options)
}

if options.displayAll && len(options.displayGiven) > 0 {
return nil, fmt.Errorf("incompatible options provided")
}

if vc.SDJWTHashAlg == "" || vc.JWT == "" {
bytes, err := vc.MarshalJSON()
if err != nil {
return nil, err
}

return json2.ToMap(bytes)
}

credClaims, err := unmarshalJWSClaims(vc.JWT, false, nil)
if err != nil {
return nil, fmt.Errorf("unmarshal VC JWT claims: %w", err)
}

credClaims.refineFromJWTClaims()

useDisclosures := filterDisclosureList(vc.SDJWTDisclosures, options)

newVCObj, err := common.GetDisclosedClaims(useDisclosures, credClaims.VC)
if err != nil {
return nil, fmt.Errorf("assembling disclosed claims into vc: %w", err)
}

if subj, ok := newVCObj["credentialSubject"].(map[string]interface{}); ok {
clearEmpty(subj)
}

return newVCObj, nil
}

func filterDisclosureList(disclosures []*common.DisclosureClaim, options *displayCredOpts) []*common.DisclosureClaim {
if options.displayAll {
return disclosures
Expand Down
15 changes: 15 additions & 0 deletions pkg/doc/verifiable/credential_sdjwt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,21 @@ func TestCreateDisplayCredential(t *testing.T) {
require.Equal(t, expectedFields, subj[0].CustomFields)
})

t.Run("not a SD-JWT credential map", func(t *testing.T) {
vc2, err := parseTestCredential(t, []byte(jwtTestCredential))
require.NoError(t, err)

displayVC, err := vc2.CreateDisplayCredentialMap(DisplayAllDisclosures())
require.NoError(t, err)
require.NotEmpty(t, displayVC)
})

t.Run("display all claims map", func(t *testing.T) {
displayVC, err := vc.CreateDisplayCredentialMap(DisplayAllDisclosures())
require.NoError(t, err)
require.NotEmpty(t, displayVC)
})

t.Run("display no claims", func(t *testing.T) {
displayVC, err := vc.CreateDisplayCredential()
require.NoError(t, err)
Expand Down