Skip to content

Commit

Permalink
feat: append context if missing (trustbloc#1741)
Browse files Browse the repository at this point in the history
  • Loading branch information
skynet2 authored Jul 4, 2024
1 parent b7a9286 commit 0a07a2a
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 3 deletions.
17 changes: 14 additions & 3 deletions pkg/doc/vc/vcutil/vcutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"fmt"
"strings"

"github.com/samber/lo"
"github.com/trustbloc/vc-go/verifiable"

vcsverifiable "github.com/trustbloc/vcs/pkg/doc/verifiable"
Expand Down Expand Up @@ -102,19 +103,29 @@ func CreateIssuer(issuerDID, issuerName string) *verifiable.Issuer {
}

// AppendSignatureTypeContext appends context for JSONWebSignature2020 and BbsBlsSignature2020.
func AppendSignatureTypeContext(context []string, signatureType vcsverifiable.SignatureType,
func AppendSignatureTypeContext(
context []string,
signatureType vcsverifiable.SignatureType,
) []string {
if signatureType == vcsverifiable.JSONWebSignature2020 {
return append(context, jsonWebSignature2020Context)
return appendIfMissing(context, jsonWebSignature2020Context)
}

if signatureType == vcsverifiable.BbsBlsSignature2020 {
return append(context, bbsBlsSignature2020Context)
return appendIfMissing(context, bbsBlsSignature2020Context)
}

return context
}

func appendIfMissing(contexts []string, context string) []string {
if lo.Contains(contexts, context) {
return contexts
}

return append(contexts, context)
}

// PrependCredentialPrefix prepends prefix to credential.ID.
func PrependCredentialPrefix(credential *verifiable.Credential, prefix string) *verifiable.Credential {
contents := credential.Contents()
Expand Down
22 changes: 22 additions & 0 deletions pkg/doc/vc/vcutil/vcutil_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"encoding/json"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/trustbloc/vc-go/verifiable"

Expand Down Expand Up @@ -206,3 +207,24 @@ func TestPrependCredentialPrefix(t *testing.T) {

require.Equal(t, "prefix_did:example:ebfeb1f712ebc6f1c276e12ec21", credential.Contents().ID)
}

func TestAppendIfMissing(t *testing.T) {
t.Run("missing", func(t *testing.T) {
assert.EqualValues(t, []string{
"abcd",
"https://w3c-ccg.github.io/lds-jws2020/contexts/lds-jws2020-v1.json",
}, AppendSignatureTypeContext([]string{
"abcd",
}, vcsverifiable.JSONWebSignature2020))
})

t.Run("already exists", func(t *testing.T) {
assert.EqualValues(t, []string{
"abcd",
"https://w3c-ccg.github.io/lds-jws2020/contexts/lds-jws2020-v1.json",
}, AppendSignatureTypeContext([]string{
"abcd",
"https://w3c-ccg.github.io/lds-jws2020/contexts/lds-jws2020-v1.json",
}, vcsverifiable.JSONWebSignature2020))
})
}

0 comments on commit 0a07a2a

Please sign in to comment.