Skip to content

Commit

Permalink
feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
reinkrul committed Nov 27, 2023
1 parent 1190f18 commit 14a888c
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 28 deletions.
17 changes: 3 additions & 14 deletions vcr/pe/presentation_submission.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,21 +177,10 @@ func (b *PresentationSubmissionBuilder) Build(format string) (PresentationSubmis
// Resolve returns a map where each of the input descriptors is mapped to the corresponding VerifiableCredential.
// If an input descriptor can't be mapped to a VC, an error is returned.
// This function is specified by https://identity.foundation/presentation-exchange/#processing-of-submission-entries
func (s PresentationSubmission) Resolve(envelope interface{}) (map[string]vc.VerifiableCredential, error) {
switch envelope.(type) {
case []interface{}:
// list of VPs
case map[string]interface{}:
// single VP (JSON)
case string:
// single VP (JWT)
default:
return nil, errors.New("invalid Presentation Exchange envelope")
}

func (s PresentationSubmission) Resolve(envelope Envelope) (map[string]vc.VerifiableCredential, error) {
result := make(map[string]vc.VerifiableCredential)
for _, inputDescriptor := range s.DescriptorMap {
resolvedCredential, err := resolveCredential(nil, inputDescriptor, envelope)
resolvedCredential, err := resolveCredential(nil, inputDescriptor, envelope.asInterface)
if err != nil {
return nil, fmt.Errorf("unable to resolve credential for input descriptor '%s': %w", inputDescriptor.Id, err)
}
Expand Down Expand Up @@ -261,7 +250,7 @@ func resolveCredential(path []string, mapping InputDescriptorMappingObject, valu
// The Presentation Definitions are passed in the envelope, as specified by the PEX specification.
// It assumes credentials of the presentations only map in 1 way to the input descriptors.
func (s PresentationSubmission) Validate(envelope Envelope, definition PresentationDefinition) (map[string]vc.VerifiableCredential, error) {
actualCredentials, err := s.Resolve(envelope.Interface)
actualCredentials, err := s.Resolve(envelope)
if err != nil {
return nil, fmt.Errorf("resolve credentials from presentation submission: %w", err)
}
Expand Down
14 changes: 7 additions & 7 deletions vcr/pe/presentation_submission_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ func TestPresentationSubmission_Resolve(t *testing.T) {
var submission PresentationSubmission
require.NoError(t, json.Unmarshal([]byte(submissionJSON), &submission))

credentials, err := submission.Resolve(toEnvelope(t, vp).Interface)
credentials, err := submission.Resolve(toEnvelope(t, vp))

require.NoError(t, err)
assert.Len(t, credentials, 1)
Expand Down Expand Up @@ -241,7 +241,7 @@ func TestPresentationSubmission_Resolve(t *testing.T) {
var submission PresentationSubmission
require.NoError(t, json.Unmarshal([]byte(submissionJSON), &submission))

credentials, err := submission.Resolve(toEnvelope(t, vp).Interface)
credentials, err := submission.Resolve(toEnvelope(t, vp))

require.NoError(t, err)
assert.Len(t, credentials, 2)
Expand Down Expand Up @@ -284,7 +284,7 @@ func TestPresentationSubmission_Resolve(t *testing.T) {
var submission PresentationSubmission
require.NoError(t, json.Unmarshal([]byte(submissionJSON), &submission))

credentials, err := submission.Resolve(toEnvelope(t, []interface{}{vp1, vp2}).Interface)
credentials, err := submission.Resolve(toEnvelope(t, []interface{}{vp1, vp2}))

require.NoError(t, err)
assert.Len(t, credentials, 2)
Expand All @@ -309,7 +309,7 @@ func TestPresentationSubmission_Resolve(t *testing.T) {
var submission PresentationSubmission
require.NoError(t, json.Unmarshal([]byte(submissionJSON), &submission))

credentials, err := submission.Resolve(toEnvelope(t, vp).Interface)
credentials, err := submission.Resolve(toEnvelope(t, vp))

require.EqualError(t, err, "unable to resolve credential for input descriptor '1': path '$.verifiableCredential' does not reference a credential")
assert.Nil(t, credentials)
Expand All @@ -332,7 +332,7 @@ func TestPresentationSubmission_Resolve(t *testing.T) {
var submission PresentationSubmission
require.NoError(t, json.Unmarshal([]byte(submissionJSON), &submission))

credentials, err := submission.Resolve(toEnvelope(t, vp).Interface)
credentials, err := submission.Resolve(toEnvelope(t, vp))

require.ErrorContains(t, err, "unable to resolve credential for input descriptor '1': invalid JSON-LD credential at path")
assert.Nil(t, credentials)
Expand All @@ -355,7 +355,7 @@ func TestPresentationSubmission_Resolve(t *testing.T) {
var submission PresentationSubmission
require.NoError(t, json.Unmarshal([]byte(submissionJSON), &submission))

credentials, err := submission.Resolve(toEnvelope(t, vp).Interface)
credentials, err := submission.Resolve(toEnvelope(t, vp))

require.ErrorContains(t, err, "unable to resolve credential for input descriptor '1': invalid JSON-LD presentation at path")
assert.Nil(t, credentials)
Expand All @@ -378,7 +378,7 @@ func TestPresentationSubmission_Resolve(t *testing.T) {
var submission PresentationSubmission
require.NoError(t, json.Unmarshal([]byte(submissionJSON), &submission))

credentials, err := submission.Resolve(toEnvelope(t, vp).Interface)
credentials, err := submission.Resolve(toEnvelope(t, vp))

assert.EqualError(t, err, "unable to resolve credential for input descriptor '1': value of Go type 'string' at path '$.verifiableCredential.expirationDate' can't be decoded using format 'ldp_vc'")
assert.Nil(t, credentials)
Expand Down
8 changes: 5 additions & 3 deletions vcr/pe/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,11 @@ import (
"github.com/nuts-foundation/go-did/vc"
)

// Envelope is a parsed Presentation Exchange envelope, containing zero or more Verifiable Presentations that are referenced by the Presentation Submission.
type Envelope struct {
Interface interface{}
// Presentations contains the Verifiable Presentations that were parsed from the envelope.
Presentations []vc.VerifiablePresentation
asInterface interface{}
}

// ParseEnvelope parses a Presentation Exchange envelope, which is a JSON type that encompasses zero or more Verifiable Presentations.
Expand All @@ -46,7 +48,7 @@ func ParseEnvelope(envelopeBytes []byte) (*Envelope, error) {
return nil, err
}
return &Envelope{
Interface: asInterface,
asInterface: asInterface,
Presentations: presentations,
}, nil
}
Expand All @@ -56,7 +58,7 @@ func ParseEnvelope(envelopeBytes []byte) (*Envelope, error) {
return nil, err
}
return &Envelope{
Interface: asInterface,
asInterface: asInterface,
Presentations: []vc.VerifiablePresentation{*presentation},
}, nil
}
Expand Down
8 changes: 4 additions & 4 deletions vcr/pe/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func TestParseEnvelope(t *testing.T) {
presentation := test.CreateJWTPresentation(t, did.MustParseDID("did:example:1"), credential.ValidNutsOrganizationCredential(t))
envelope, err := ParseEnvelope([]byte(presentation.Raw()))
require.NoError(t, err)
require.Equal(t, presentation.ID.String(), envelope.Interface.(map[string]interface{})["id"])
require.Equal(t, presentation.ID.String(), envelope.asInterface.(map[string]interface{})["id"])
require.Len(t, envelope.Presentations, 1)
})
t.Run("invalid JWT", func(t *testing.T) {
Expand All @@ -44,7 +44,7 @@ func TestParseEnvelope(t *testing.T) {
t.Run("JSON object", func(t *testing.T) {
envelope, err := ParseEnvelope([]byte(`{"id": "value"}`))
require.NoError(t, err)
require.Equal(t, map[string]interface{}{"id": "value"}, envelope.Interface)
require.Equal(t, map[string]interface{}{"id": "value"}, envelope.asInterface)
require.Len(t, envelope.Presentations, 1)
})
t.Run("invalid VP as JSON object", func(t *testing.T) {
Expand All @@ -55,7 +55,7 @@ func TestParseEnvelope(t *testing.T) {
t.Run("JSON array with objects", func(t *testing.T) {
envelope, err := ParseEnvelope([]byte(`[{"id": "value"}]`))
require.NoError(t, err)
require.Equal(t, []interface{}{map[string]interface{}{"id": "value"}}, envelope.Interface)
require.Equal(t, []interface{}{map[string]interface{}{"id": "value"}}, envelope.asInterface)
require.Len(t, envelope.Presentations, 1)
})
t.Run("JSON array with JWTs", func(t *testing.T) {
Expand All @@ -64,7 +64,7 @@ func TestParseEnvelope(t *testing.T) {
listJSON, _ := json.Marshal(presentations)
envelope, err := ParseEnvelope(listJSON)
require.NoError(t, err)
require.Len(t, envelope.Interface, 2)
require.Len(t, envelope.asInterface, 2)
require.Len(t, envelope.Presentations, 2)
})
t.Run("invalid VPs list as JSON", func(t *testing.T) {
Expand Down

0 comments on commit 14a888c

Please sign in to comment.