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

Commit

Permalink
chore: add CreateDisplayCredentialBytes (#3580)
Browse files Browse the repository at this point in the history
* chore: add CreateDisplayCredentialBytes

* feat: use map
  • Loading branch information
skynet2 authored May 9, 2023
1 parent 4b06af1 commit bc155de
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 0 deletions.
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

0 comments on commit bc155de

Please sign in to comment.