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

feat: Add options to VDR Accept #3439

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pkg/framework/aries/api/vdr/vdr.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ type Registry interface {
type VDR interface {
Read(did string, opts ...DIDMethodOption) (*did.DocResolution, error)
Create(did *did.Doc, opts ...DIDMethodOption) (*did.DocResolution, error)
Accept(method string) bool
Accept(method string, opts ...DIDMethodOption) bool
Update(did *did.Doc, opts ...DIDMethodOption) error
Deactivate(did string, opts ...DIDMethodOption) error
Close() error
Expand Down
7 changes: 6 additions & 1 deletion pkg/mock/vdr/mock_vdr.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
type MockVDR struct {
AcceptValue bool
StoreErr error
AcceptFunc func(method string, opts ...vdrapi.DIDMethodOption) bool
ReadFunc func(didID string, opts ...vdrapi.DIDMethodOption) (*did.DocResolution, error)
CreateFunc func(did *did.Doc, opts ...vdrapi.DIDMethodOption) (*did.DocResolution, error)
UpdateFunc func(didDoc *did.Doc, opts ...vdrapi.DIDMethodOption) error
Expand Down Expand Up @@ -60,7 +61,11 @@ func (m *MockVDR) Deactivate(didID string, opts ...vdrapi.DIDMethodOption) error
}

// Accept did.
func (m *MockVDR) Accept(method string) bool {
func (m *MockVDR) Accept(method string, opts ...vdrapi.DIDMethodOption) bool {
if m.AcceptFunc != nil {
return m.AcceptFunc(method, opts...)
}

return m.AcceptValue
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/vdr/httpbinding/vdr.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func New(endpointURL string, opts ...Option) (*VDR, error) {
}

// Accept did method - attempt to resolve any method.
func (v *VDR) Accept(method string) bool {
func (v *VDR) Accept(method string, opts ...vdrapi.DIDMethodOption) bool {
return v.accept(method)
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/vdr/key/vdr.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func New() *VDR {
}

// Accept accepts did:key method.
func (v *VDR) Accept(method string) bool {
func (v *VDR) Accept(method string, opts ...vdrapi.DIDMethodOption) bool {
return method == DIDMethod
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/vdr/peer/vdr.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,6 @@ func (v *VDR) Deactivate(did string, opts ...vdrapi.DIDMethodOption) error {
}

// Accept did method.
func (v *VDR) Accept(method string) bool {
func (v *VDR) Accept(method string, opts ...vdrapi.DIDMethodOption) bool {
return method == DIDMethod
}
26 changes: 20 additions & 6 deletions pkg/vdr/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ import (
"github.com/hyperledger/aries-framework-go/pkg/vdr/peer"
)

const didAcceptOpt = "didAcceptOpt"

// Option is a vdr instance option.
type Option func(opts *Registry)

Expand Down Expand Up @@ -45,8 +47,12 @@ func (r *Registry) Resolve(did string, opts ...vdrapi.DIDMethodOption) (*diddoc.
return nil, err
}

// create accept options with did and add existing options
acceptOpts := []vdrapi.DIDMethodOption{vdrapi.WithOption(didAcceptOpt, did)}
acceptOpts = append(acceptOpts, opts...)

// resolve did method
method, err := r.resolveVDR(didMethod)
method, err := r.resolveVDR(didMethod, acceptOpts...)
if err != nil {
return nil, err
}
Expand All @@ -71,8 +77,12 @@ func (r *Registry) Update(didDoc *diddoc.Doc, opts ...vdrapi.DIDMethodOption) er
return err
}

// create accept options with did and add existing options
acceptOpts := []vdrapi.DIDMethodOption{vdrapi.WithOption(didAcceptOpt, didDoc.ID)}
acceptOpts = append(acceptOpts, opts...)

// resolve did method
method, err := r.resolveVDR(didMethod)
method, err := r.resolveVDR(didMethod, acceptOpts...)
if err != nil {
return err
}
Expand All @@ -87,8 +97,12 @@ func (r *Registry) Deactivate(did string, opts ...vdrapi.DIDMethodOption) error
return err
}

// create accept options with did and add existing options
acceptOpts := []vdrapi.DIDMethodOption{vdrapi.WithOption(didAcceptOpt, did)}
acceptOpts = append(acceptOpts, opts...)

// resolve did method
method, err := r.resolveVDR(didMethod)
method, err := r.resolveVDR(didMethod, acceptOpts...)
if err != nil {
return err
}
Expand All @@ -105,7 +119,7 @@ func (r *Registry) Create(didMethod string, did *diddoc.Doc,
opt(docOpts)
}

method, err := r.resolveVDR(didMethod)
method, err := r.resolveVDR(didMethod, opts...)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -143,9 +157,9 @@ func (r *Registry) Close() error {
return nil
}

func (r *Registry) resolveVDR(method string) (vdrapi.VDR, error) {
func (r *Registry) resolveVDR(method string, opts ...vdrapi.DIDMethodOption) (vdrapi.VDR, error) {
for _, v := range r.vdr {
if v.Accept(method) {
if v.Accept(method, opts...) {
return v, nil
}
}
Expand Down
77 changes: 77 additions & 0 deletions pkg/vdr/registry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,24 @@ func TestRegistry_Resolve(t *testing.T) {
require.NoError(t, err)
})

t.Run("test opts passed to accept", func(t *testing.T) {
registry := New(WithVDR(&mockvdr.MockVDR{
AcceptFunc: func(method string, opts ...vdrapi.DIDMethodOption) bool {
acceptOpts := &vdrapi.DIDMethodOpts{Values: make(map[string]interface{})}
// Apply options
for _, opt := range opts {
opt(acceptOpts)
}

require.NotNil(t, acceptOpts.Values["k1"])
require.NotNil(t, acceptOpts.Values[didAcceptOpt])
return true
},
}))
_, err := registry.Resolve("1:id:123", vdrapi.WithOption("k1", "v1"))
require.NoError(t, err)
})

t.Run("test success", func(t *testing.T) {
registry := New(WithVDR(&mockvdr.MockVDR{AcceptValue: true}))
_, err := registry.Resolve("1:id:123")
Expand Down Expand Up @@ -154,6 +172,25 @@ func TestRegistry_Update(t *testing.T) {
require.NoError(t, err)
})

t.Run("test opts passed to accept", func(t *testing.T) {
registry := New(WithVDR(&mockvdr.MockVDR{
AcceptFunc: func(method string, opts ...vdrapi.DIDMethodOption) bool {
acceptOpts := &vdrapi.DIDMethodOpts{Values: make(map[string]interface{})}
// Apply options
for _, opt := range opts {
opt(acceptOpts)
}

require.NotNil(t, acceptOpts.Values["k1"])
require.NotNil(t, acceptOpts.Values[didAcceptOpt])
return true
},
}))

err := registry.Update(&did.Doc{ID: "1:id:123"}, vdrapi.WithOption("k1", "v1"))
require.NoError(t, err)
})

t.Run("test success", func(t *testing.T) {
registry := New(WithVDR(&mockvdr.MockVDR{AcceptValue: true}))
err := registry.Update(&did.Doc{ID: "1:id:123"})
Expand Down Expand Up @@ -205,6 +242,25 @@ func TestRegistry_Deactivate(t *testing.T) {
require.NoError(t, err)
})

t.Run("test opts passed to accept", func(t *testing.T) {
registry := New(WithVDR(&mockvdr.MockVDR{
AcceptFunc: func(method string, opts ...vdrapi.DIDMethodOption) bool {
acceptOpts := &vdrapi.DIDMethodOpts{Values: make(map[string]interface{})}
// Apply options
for _, opt := range opts {
opt(acceptOpts)
}

require.NotNil(t, acceptOpts.Values["k1"])
require.NotNil(t, acceptOpts.Values[didAcceptOpt])
return true
},
}))

err := registry.Deactivate("1:id:123", vdrapi.WithOption("k1", "v1"))
require.NoError(t, err)
})

t.Run("test success", func(t *testing.T) {
registry := New(WithVDR(&mockvdr.MockVDR{AcceptValue: true}))
err := registry.Deactivate("1:id:123")
Expand Down Expand Up @@ -232,6 +288,27 @@ func TestRegistry_Create(t *testing.T) {
_, err := registry.Create("id", &did.Doc{VerificationMethod: []did.VerificationMethod{{ID: "key1"}}})
require.NoError(t, err)
})

t.Run("test opts passed to accept", func(t *testing.T) {
registry := New(WithVDR(&mockvdr.MockVDR{
AcceptFunc: func(method string, opts ...vdrapi.DIDMethodOption) bool {
acceptOpts := &vdrapi.DIDMethodOpts{Values: make(map[string]interface{})}
// Apply options
for _, opt := range opts {
opt(acceptOpts)
}

require.NotNil(t, acceptOpts.Values["k1"])
return true
},
}))

_, err := registry.Create("id",
&did.Doc{VerificationMethod: []did.VerificationMethod{{ID: "key1"}}},
vdrapi.WithOption("k1", "v1"))
require.NoError(t, err)
})

t.Run("with KMS opts - test opts is passed ", func(t *testing.T) {
registry := New(WithVDR(&mockvdr.MockVDR{
AcceptValue: true,
Expand Down
2 changes: 1 addition & 1 deletion pkg/vdr/web/vdr.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func New() *VDR {
}

// Accept method of the VDR interface.
func (v *VDR) Accept(method string) bool {
func (v *VDR) Accept(method string, opts ...vdrapi.DIDMethodOption) bool {
return method == namespace
}

Expand Down