Skip to content

Commit

Permalink
PEX: ldp_vp with 1 VC should not index credential property
Browse files Browse the repository at this point in the history
  • Loading branch information
reinkrul committed Nov 28, 2023
1 parent 44508d3 commit a575dcc
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 3 deletions.
10 changes: 10 additions & 0 deletions vcr/pe/presentation_submission.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 $.
Expand Down
37 changes: 34 additions & 3 deletions vcr/pe/presentation_submission_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -101,7 +132,7 @@ func TestPresentationSubmissionBuilder_Build(t *testing.T) {
"path_nested": {
"format": "ldp_vc",
"id": "Match ID=1",
"path": "$.verifiableCredential[0]"
"path": "$.verifiableCredential"
}
},
{
Expand All @@ -111,7 +142,7 @@ func TestPresentationSubmissionBuilder_Build(t *testing.T) {
"path_nested": {
"format": "ldp_vc",
"id": "Match ID=2",
"path": "$.verifiableCredential[0]"
"path": "$.verifiableCredential"
}
}
]
Expand Down
2 changes: 2 additions & 0 deletions vcr/pe/test/test_definitions.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ const PickOne = `
],
"input_descriptors": [
{
"id": "Match ID=1",
"name": "Pick 1",
"group": ["A"],
"constraints": {
Expand All @@ -47,6 +48,7 @@ const PickOne = `
}
},
{
"id": "Match ID=2",
"name": "Pick 2",
"group": ["A"],
"constraints": {
Expand Down

0 comments on commit a575dcc

Please sign in to comment.