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

Commit

Permalink
feat: use map
Browse files Browse the repository at this point in the history
  • Loading branch information
skynet2 committed May 9, 2023
1 parent 7b82144 commit f50986c
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 14 deletions.
21 changes: 11 additions & 10 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,16 +390,16 @@ func (vc *Credential) CreateDisplayCredential( // nolint:funlen,gocyclo
return newVC, nil
}

// CreateDisplayCredentialBytes creates, for SD-JWT credentials, a Credential whose selective-disclosure subject fields
// 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) CreateDisplayCredentialBytes( // nolint:funlen,gocyclo
func (vc *Credential) CreateDisplayCredentialMap( // nolint:funlen,gocyclo
opts ...DisplayCredentialOption,
) ([]byte, error) {
) (map[string]interface{}, error) {
options := &displayCredOpts{}

for _, opt := range opts {
Expand All @@ -410,7 +411,12 @@ func (vc *Credential) CreateDisplayCredentialBytes( // nolint:funlen,gocyclo
}

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

return json2.ToMap(bytes)
}

credClaims, err := unmarshalJWSClaims(vc.JWT, false, nil)
Expand All @@ -431,12 +437,7 @@ func (vc *Credential) CreateDisplayCredentialBytes( // nolint:funlen,gocyclo
clearEmpty(subj)
}

vcBytes, err := json.Marshal(&newVCObj)
if err != nil {
return nil, fmt.Errorf("marshalling vc object to JSON: %w", err)
}

return vcBytes, nil
return newVCObj, nil
}

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

t.Run("not a SD-JWT credential bytes", func(t *testing.T) {
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.CreateDisplayCredentialBytes(DisplayAllDisclosures())
displayVC, err := vc2.CreateDisplayCredentialMap(DisplayAllDisclosures())
require.NoError(t, err)
require.NotEmpty(t, displayVC)
})

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

0 comments on commit f50986c

Please sign in to comment.