From a575dcc9401bdad3ece02d301b8d9f90975d1159 Mon Sep 17 00:00:00 2001 From: Rein Krul Date: Tue, 28 Nov 2023 07:19:03 +0100 Subject: [PATCH] PEX: ldp_vp with 1 VC should not index credential property --- vcr/pe/presentation_submission.go | 10 +++++++ vcr/pe/presentation_submission_test.go | 37 +++++++++++++++++++++++--- vcr/pe/test/test_definitions.go | 2 ++ 3 files changed, 46 insertions(+), 3 deletions(-) diff --git a/vcr/pe/presentation_submission.go b/vcr/pe/presentation_submission.go index fdd1bf6cdc..3ced0e08fa 100644 --- a/vcr/pe/presentation_submission.go +++ b/vcr/pe/presentation_submission.go @@ -147,6 +147,16 @@ func (b *PresentationSubmissionBuilder) Build(format string) (PresentationSubmis } } + // the verifiableCredential property in Verifiable Presentations can be a single VC or an array of VCs when represented in JSON. + // go-did always marshals a single VC as a single VC for JSON-LD VPs. So we might need to fix the mapping paths. + if format == vc.JSONLDPresentationProofFormat { + for _, signInstruction := range nonEmptySignInstructions { + if len(signInstruction.Mappings) == 1 { + signInstruction.Mappings[0].Path = "$.verifiableCredential" + } + } + } + index := 0 // last we create the descriptor map for the presentation submission // If there's only one sign instruction the Path will be $. diff --git a/vcr/pe/presentation_submission_test.go b/vcr/pe/presentation_submission_test.go index 8724e2b0ee..23e8a42462 100644 --- a/vcr/pe/presentation_submission_test.go +++ b/vcr/pe/presentation_submission_test.go @@ -53,7 +53,38 @@ func TestPresentationSubmissionBuilder_Build(t *testing.T) { vc2 := credentialToJSONLD(vc.VerifiableCredential{ID: &id2}) vc3 := credentialToJSONLD(vc.VerifiableCredential{ID: &id3}) - t.Run("1 presentation", func(t *testing.T) { + t.Run("1 presentation with 1 credential", func(t *testing.T) { + expectedJSON := ` + { + "id": "for-test", + "definition_id": "", + "descriptor_map": [ + { + "format": "ldp_vc", + "id": "Match ID=1", + "path": "$.verifiableCredential" + } + ] + }` + presentationDefinition := PresentationDefinition{} + _ = json.Unmarshal([]byte(test.PickOne), &presentationDefinition) + builder := presentationDefinition.PresentationSubmissionBuilder() + builder.AddWallet(holder1, []vc.VerifiableCredential{vc1, vc2}) + + submission, signInstructions, err := builder.Build("ldp_vp") + + require.NoError(t, err) + require.NotNil(t, signInstructions) + assert.Len(t, signInstructions, 1) + require.Len(t, submission.DescriptorMap, 1) + assert.Equal(t, "$.verifiableCredential", submission.DescriptorMap[0].Path) + + submission.Id = "for-test" // easier assertion + actualJSON, _ := json.MarshalIndent(submission, "", " ") + println(string(actualJSON)) + assert.JSONEq(t, expectedJSON, string(actualJSON)) + }) + t.Run("1 presentation with 2 credentials", func(t *testing.T) { expectedJSON := ` { "id": "for-test", @@ -101,7 +132,7 @@ func TestPresentationSubmissionBuilder_Build(t *testing.T) { "path_nested": { "format": "ldp_vc", "id": "Match ID=1", - "path": "$.verifiableCredential[0]" + "path": "$.verifiableCredential" } }, { @@ -111,7 +142,7 @@ func TestPresentationSubmissionBuilder_Build(t *testing.T) { "path_nested": { "format": "ldp_vc", "id": "Match ID=2", - "path": "$.verifiableCredential[0]" + "path": "$.verifiableCredential" } } ] diff --git a/vcr/pe/test/test_definitions.go b/vcr/pe/test/test_definitions.go index 3562eebb96..587a3d9e54 100644 --- a/vcr/pe/test/test_definitions.go +++ b/vcr/pe/test/test_definitions.go @@ -30,6 +30,7 @@ const PickOne = ` ], "input_descriptors": [ { + "id": "Match ID=1", "name": "Pick 1", "group": ["A"], "constraints": { @@ -47,6 +48,7 @@ const PickOne = ` } }, { + "id": "Match ID=2", "name": "Pick 2", "group": ["A"], "constraints": {