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

Commit bc155de

Browse files
authored
chore: add CreateDisplayCredentialBytes (#3580)
* chore: add CreateDisplayCredentialBytes * feat: use map
1 parent 4b06af1 commit bc155de

File tree

2 files changed

+66
-0
lines changed

2 files changed

+66
-0
lines changed

pkg/doc/verifiable/credential_sdjwt.go

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515
"github.com/hyperledger/aries-framework-go/pkg/doc/sdjwt/common"
1616
"github.com/hyperledger/aries-framework-go/pkg/doc/sdjwt/holder"
1717
"github.com/hyperledger/aries-framework-go/pkg/doc/sdjwt/issuer"
18+
json2 "github.com/hyperledger/aries-framework-go/pkg/doc/util/json"
1819
)
1920

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

393+
// CreateDisplayCredentialMap creates, for SD-JWT credentials, a Credential whose selective-disclosure subject fields
394+
// are replaced with the disclosure data.
395+
//
396+
// Options may be provided to filter the disclosures that will be included in the display credential. If a disclosure is
397+
// not included, the associated claim will not be present in the returned credential.
398+
//
399+
// If the calling Credential is not an SD-JWT credential, this method returns the credential itself.
400+
func (vc *Credential) CreateDisplayCredentialMap( // nolint:funlen,gocyclo
401+
opts ...DisplayCredentialOption,
402+
) (map[string]interface{}, error) {
403+
options := &displayCredOpts{}
404+
405+
for _, opt := range opts {
406+
opt(options)
407+
}
408+
409+
if options.displayAll && len(options.displayGiven) > 0 {
410+
return nil, fmt.Errorf("incompatible options provided")
411+
}
412+
413+
if vc.SDJWTHashAlg == "" || vc.JWT == "" {
414+
bytes, err := vc.MarshalJSON()
415+
if err != nil {
416+
return nil, err
417+
}
418+
419+
return json2.ToMap(bytes)
420+
}
421+
422+
credClaims, err := unmarshalJWSClaims(vc.JWT, false, nil)
423+
if err != nil {
424+
return nil, fmt.Errorf("unmarshal VC JWT claims: %w", err)
425+
}
426+
427+
credClaims.refineFromJWTClaims()
428+
429+
useDisclosures := filterDisclosureList(vc.SDJWTDisclosures, options)
430+
431+
newVCObj, err := common.GetDisclosedClaims(useDisclosures, credClaims.VC)
432+
if err != nil {
433+
return nil, fmt.Errorf("assembling disclosed claims into vc: %w", err)
434+
}
435+
436+
if subj, ok := newVCObj["credentialSubject"].(map[string]interface{}); ok {
437+
clearEmpty(subj)
438+
}
439+
440+
return newVCObj, nil
441+
}
442+
392443
func filterDisclosureList(disclosures []*common.DisclosureClaim, options *displayCredOpts) []*common.DisclosureClaim {
393444
if options.displayAll {
394445
return disclosures

pkg/doc/verifiable/credential_sdjwt_test.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -379,6 +379,21 @@ func TestCreateDisplayCredential(t *testing.T) {
379379
require.Equal(t, expectedFields, subj[0].CustomFields)
380380
})
381381

382+
t.Run("not a SD-JWT credential map", func(t *testing.T) {
383+
vc2, err := parseTestCredential(t, []byte(jwtTestCredential))
384+
require.NoError(t, err)
385+
386+
displayVC, err := vc2.CreateDisplayCredentialMap(DisplayAllDisclosures())
387+
require.NoError(t, err)
388+
require.NotEmpty(t, displayVC)
389+
})
390+
391+
t.Run("display all claims map", func(t *testing.T) {
392+
displayVC, err := vc.CreateDisplayCredentialMap(DisplayAllDisclosures())
393+
require.NoError(t, err)
394+
require.NotEmpty(t, displayVC)
395+
})
396+
382397
t.Run("display no claims", func(t *testing.T) {
383398
displayVC, err := vc.CreateDisplayCredential()
384399
require.NoError(t, err)

0 commit comments

Comments
 (0)