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

Commit

Permalink
fix: support of vp referencing in descriptor_map's path.
Browse files Browse the repository at this point in the history
Signed-off-by: Volodymyr Kubiv <[email protected]>
  • Loading branch information
vkubiv committed Oct 25, 2022
1 parent 919b90a commit ce5e1b3
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 23 deletions.
36 changes: 13 additions & 23 deletions pkg/doc/presexch/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,28 +156,29 @@ func selectVC(typelessVerifiable interface{},
var err error

for {
vc, err = selectByPath(builder, typelessVerifiable, mapping.Path, opts)
typelessVerifiable, err = selectByPath(builder, typelessVerifiable, mapping.Path)
if err != nil {
return nil, fmt.Errorf("failed to select vc from submission: %w", err)
}

if mapping.PathNested == nil {
break
if mapping.PathNested != nil {
mapping = mapping.PathNested
continue
}

mapping = mapping.PathNested
var credBits []byte

var vcBytes []byte

vcBytes, err = vc.MarshalJSON()
credBits, err = json.Marshal(typelessVerifiable)
if err != nil {
return nil, fmt.Errorf("failed to marshal vc: %w", err)
return nil, fmt.Errorf("failed to marshal credential: %w", err)
}

err = json.Unmarshal(vcBytes, &typelessVerifiable)
vc, err = verifiable.ParseCredential(credBits, opts.CredentialOptions...)
if err != nil {
return nil, fmt.Errorf("failed to unmarshal vc: %w", err)
return nil, fmt.Errorf("failed to parse credential: %w", err)
}

break
}

return vc, nil
Expand Down Expand Up @@ -263,8 +264,7 @@ func descriptorIDs(input []*InputDescriptor) []string {
// [The Input Descriptor Mapping Object] MUST include a path property, and its value MUST be a JSONPath
// string expression that selects the credential to be submit in relation to the identified Input Descriptor
// identified, when executed against the top-level of the object the Presentation Submission is embedded within.
func selectByPath(builder gval.Language, vp interface{}, jsonPath string,
options *MatchOptions) (*verifiable.Credential, error) {
func selectByPath(builder gval.Language, vp interface{}, jsonPath string) (interface{}, error) {
path, err := builder.NewEvaluable(jsonPath)
if err != nil {
return nil, fmt.Errorf("failed to build new json path evaluator: %w", err)
Expand All @@ -275,17 +275,7 @@ func selectByPath(builder gval.Language, vp interface{}, jsonPath string,
return nil, fmt.Errorf("failed to evaluate json path [%s]: %w", jsonPath, err)
}

credBits, err := json.Marshal(cred)
if err != nil {
return nil, fmt.Errorf("failed to marshal credential: %w", err)
}

vc, err := verifiable.ParseCredential(credBits, options.CredentialOptions...)
if err != nil {
return nil, fmt.Errorf("failed to parse credential: %w", err)
}

return vc, nil
return cred, nil
}

func stringsContain(s []string, val string) bool {
Expand Down
40 changes: 40 additions & 0 deletions pkg/doc/presexch/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"github.com/stretchr/testify/require"

"github.com/hyperledger/aries-framework-go/component/storageutil/mem"

"github.com/hyperledger/aries-framework-go/pkg/doc/ldcontext"
. "github.com/hyperledger/aries-framework-go/pkg/doc/presexch"
jsonldsig "github.com/hyperledger/aries-framework-go/pkg/doc/signature/jsonld"
Expand Down Expand Up @@ -153,6 +154,45 @@ func TestPresentationDefinition_Match(t *testing.T) {
require.Equal(t, expectedNested.ID, result.ID)
})

t.Run("match with self referencing", func(t *testing.T) {
uri := randomURI()
contextLoader := createTestDocumentLoader(t, uri)
agent := newAgent(t)

expected := newSignedJWTVC(t, agent, []string{uri})

defs := &PresentationDefinition{
InputDescriptors: []*InputDescriptor{{
ID: uuid.New().String(),
Schema: []*Schema{{
URI: fmt.Sprintf("%s#%s", verifiable.ContextID, verifiable.VCType),
}},
}},
}

matched, err := defs.Match(newVP(t,
&PresentationSubmission{DescriptorMap: []*InputDescriptorMapping{{
ID: defs.InputDescriptors[0].ID,
Path: "$",
PathNested: &InputDescriptorMapping{
ID: defs.InputDescriptors[0].ID,
Path: "$.verifiableCredential[0]",
},
}}},
expected,
), contextLoader,
WithCredentialOptions(
verifiable.WithJSONLDDocumentLoader(contextLoader),
verifiable.WithPublicKeyFetcher(verifiable.NewVDRKeyResolver(agent.VDRegistry()).PublicKeyFetcher()),
),
)
require.NoError(t, err)
require.Len(t, matched, 1)
result, ok := matched[defs.InputDescriptors[0].ID]
require.True(t, ok)
require.Equal(t, expected.ID, result.ID)
})

t.Run("match one signed credential", func(t *testing.T) {
uri := randomURI()
contextLoader := createTestDocumentLoader(t, uri)
Expand Down

0 comments on commit ce5e1b3

Please sign in to comment.