Skip to content

Commit

Permalink
refactor: VDR support for deactivate/update
Browse files Browse the repository at this point in the history
closes hyperledger-archives#2423

Signed-off-by: Firas Qutishat <firas.qutishat@securekey.com>
  • Loading branch information
fqutishat authored and sudeshrshetty committed Oct 14, 2021
1 parent c24e1ec commit b71306f
Show file tree
Hide file tree
Showing 13 changed files with 339 additions and 8 deletions.
5 changes: 5 additions & 0 deletions pkg/framework/aries/api/vdr/vdr.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,19 @@ const DIDCommServiceType = "did-communication"
type Registry interface {
Resolve(did string, opts ...ResolveOption) (*did.DocResolution, error)
Create(method string, did *did.Doc, opts ...DIDMethodOption) (*did.DocResolution, error)
Update(did *did.Doc, opts ...DIDMethodOption) error
Deactivate(did string, opts ...DIDMethodOption) error
Close() error
}

// VDR verifiable data registry interface.
// TODO https://github.com/hyperledger/aries-framework-go/issues/2475
type VDR interface {
Read(did string, opts ...ResolveOption) (*did.DocResolution, error)
Create(keyManager kms.KeyManager, did *did.Doc, opts ...DIDMethodOption) (*did.DocResolution, error)
Accept(method string) bool
Update(did *did.Doc, opts ...DIDMethodOption) error
Deactivate(did string, opts ...DIDMethodOption) error
Close() error
}

Expand Down
32 changes: 26 additions & 6 deletions pkg/mock/vdr/mock_registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,14 @@ import (
// MockVDRegistry mock implementation of vdr
// to be used only for unit tests.
type MockVDRegistry struct {
CreateErr error
CreateValue *did.Doc
CreateFunc func(string, *did.Doc, ...vdrapi.DIDMethodOption) (*did.DocResolution, error)
ResolveErr error
ResolveValue *did.Doc
ResolveFunc func(didID string, opts ...vdrapi.ResolveOption) (*did.DocResolution, error)
CreateErr error
CreateValue *did.Doc
CreateFunc func(string, *did.Doc, ...vdrapi.DIDMethodOption) (*did.DocResolution, error)
UpdateFunc func(didDoc *did.Doc, opts ...vdrapi.DIDMethodOption) error
DeactivateFunc func(did string, opts ...vdrapi.DIDMethodOption) error
ResolveErr error
ResolveValue *did.Doc
ResolveFunc func(didID string, opts ...vdrapi.ResolveOption) (*did.DocResolution, error)
}

// Create mock implementation of create DID.
Expand Down Expand Up @@ -62,6 +64,24 @@ func (m *MockVDRegistry) Resolve(didID string, opts ...vdrapi.ResolveOption) (*d
return &did.DocResolution{DIDDocument: m.ResolveValue}, nil
}

// Update did.
func (m *MockVDRegistry) Update(didDoc *did.Doc, opts ...vdrapi.DIDMethodOption) error {
if m.UpdateFunc != nil {
return m.UpdateFunc(didDoc, opts...)
}

return nil
}

// Deactivate did.
func (m *MockVDRegistry) Deactivate(didID string, opts ...vdrapi.DIDMethodOption) error {
if m.DeactivateFunc != nil {
return m.DeactivateFunc(didID, opts...)
}

return nil
}

// Close frees resources being maintained by vdr.
func (m *MockVDRegistry) Close() error {
return nil
Expand Down
25 changes: 23 additions & 2 deletions pkg/mock/vdr/mock_vdr.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,11 @@ type MockVDR struct {
AcceptValue bool
StoreErr error
ReadFunc func(didID string, opts ...vdrapi.ResolveOption) (*did.DocResolution, error)
CreateFunc func(keyManager kms.KeyManager, did *did.Doc, opts ...vdrapi.DIDMethodOption) (*did.DocResolution, error)
CloseErr error
CreateFunc func(keyManager kms.KeyManager, did *did.Doc,
opts ...vdrapi.DIDMethodOption) (*did.DocResolution, error)
UpdateFunc func(didDoc *did.Doc, opts ...vdrapi.DIDMethodOption) error
DeactivateFunc func(did string, opts ...vdrapi.DIDMethodOption) error
CloseErr error
}

// Read did.
Expand All @@ -41,6 +44,24 @@ func (m *MockVDR) Create(keyManager kms.KeyManager, didDoc *did.Doc,
return nil, nil
}

// Update did.
func (m *MockVDR) Update(didDoc *did.Doc, opts ...vdrapi.DIDMethodOption) error {
if m.UpdateFunc != nil {
return m.UpdateFunc(didDoc, opts...)
}

return nil
}

// Deactivate did.
func (m *MockVDR) Deactivate(didID string, opts ...vdrapi.DIDMethodOption) error {
if m.DeactivateFunc != nil {
return m.DeactivateFunc(didID, opts...)
}

return nil
}

// Accept did.
func (m *MockVDR) Accept(method string) bool {
return m.AcceptValue
Expand Down
10 changes: 10 additions & 0 deletions pkg/vdr/httpbinding/vdr.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,16 @@ func (v *VDR) Close() error {
return nil
}

// Update did doc.
func (v *VDR) Update(didDoc *did.Doc, opts ...vdrapi.DIDMethodOption) error {
return fmt.Errorf("not supported")
}

// Deactivate did doc.
func (v *VDR) Deactivate(didID string, opts ...vdrapi.DIDMethodOption) error {
return fmt.Errorf("not supported")
}

// Option configures the peer vdr.
type Option func(opts *VDR)

Expand Down
22 changes: 22 additions & 0 deletions pkg/vdr/httpbinding/vdr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,25 @@ func TestVDR_Build(t *testing.T) {
require.Nil(t, result)
})
}

func TestUpdate(t *testing.T) {
t.Run("test update", func(t *testing.T) {
v, err := New("/did:example:334455")
require.NoError(t, err)

err = v.Update(nil)
require.Error(t, err)
require.Contains(t, err.Error(), "not supported")
})
}

func TestDeactivate(t *testing.T) {
t.Run("test deactivate", func(t *testing.T) {
v, err := New("/did:example:334455")
require.NoError(t, err)

err = v.Deactivate("")
require.Error(t, err)
require.Contains(t, err.Error(), "not supported")
})
}
17 changes: 17 additions & 0 deletions pkg/vdr/key/vdr.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,13 @@ SPDX-License-Identifier: Apache-2.0

package key

import (
"fmt"

diddoc "github.com/hyperledger/aries-framework-go/pkg/doc/did"
vdrapi "github.com/hyperledger/aries-framework-go/pkg/framework/aries/api/vdr"
)

const (

// DIDMethod did method.
Expand All @@ -32,3 +39,13 @@ func (v *VDR) Accept(method string) bool {
func (v *VDR) Close() error {
return nil
}

// Update did doc.
func (v *VDR) Update(didDoc *diddoc.Doc, opts ...vdrapi.DIDMethodOption) error {
return fmt.Errorf("not supported")
}

// Deactivate did doc.
func (v *VDR) Deactivate(didID string, opts ...vdrapi.DIDMethodOption) error {
return fmt.Errorf("not supported")
}
18 changes: 18 additions & 0 deletions pkg/vdr/key/vdr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,24 @@ func TestAccept(t *testing.T) {
})
}

func TestUpdate(t *testing.T) {
t.Run("test update", func(t *testing.T) {
v := New()
err := v.Update(nil)
require.Error(t, err)
require.Contains(t, err.Error(), "not supported")
})
}

func TestDeactivate(t *testing.T) {
t.Run("test deactivate", func(t *testing.T) {
v := New()
err := v.Deactivate("")
require.Error(t, err)
require.Contains(t, err.Error(), "not supported")
})
}

func TestClose(t *testing.T) {
t.Run("test success", func(t *testing.T) {
v := New()
Expand Down
12 changes: 12 additions & 0 deletions pkg/vdr/peer/vdr.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ package peer
import (
"fmt"

diddoc "github.com/hyperledger/aries-framework-go/pkg/doc/did"
vdrapi "github.com/hyperledger/aries-framework-go/pkg/framework/aries/api/vdr"
"github.com/hyperledger/aries-framework-go/pkg/storage"
)

Expand Down Expand Up @@ -36,6 +38,16 @@ func New(s storage.Provider) (*VDR, error) {
return &VDR{store: didDBStore}, nil
}

// Update did doc.
func (v *VDR) Update(didDoc *diddoc.Doc, opts ...vdrapi.DIDMethodOption) error {
return fmt.Errorf("not supported")
}

// Deactivate did doc.
func (v *VDR) Deactivate(did string, opts ...vdrapi.DIDMethodOption) error {
return fmt.Errorf("not supported")
}

// Accept did method.
func (v *VDR) Accept(method string) bool {
return method == DIDMethod
Expand Down
37 changes: 37 additions & 0 deletions pkg/vdr/peer/vdr_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
Copyright SecureKey Technologies Inc. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/

package peer

import (
"testing"

"github.com/stretchr/testify/require"

"github.com/hyperledger/aries-framework-go/pkg/mock/storage"
)

func TestUpdate(t *testing.T) {
t.Run("test update", func(t *testing.T) {
v, err := New(&storage.MockStoreProvider{})
require.NoError(t, err)

err = v.Update(nil)
require.Error(t, err)
require.Contains(t, err.Error(), "not supported")
})
}

func TestDeactivate(t *testing.T) {
t.Run("test deactivate", func(t *testing.T) {
v, err := New(&storage.MockStoreProvider{})
require.NoError(t, err)

err = v.Deactivate("")
require.Error(t, err)
require.Contains(t, err.Error(), "not supported")
})
}
32 changes: 32 additions & 0 deletions pkg/vdr/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,38 @@ func (r *Registry) Resolve(did string, opts ...vdrapi.ResolveOption) (*diddoc.Do
return didDocResolution, nil
}

// Update did document.
func (r *Registry) Update(didDoc *diddoc.Doc, opts ...vdrapi.DIDMethodOption) error {
didMethod, err := GetDidMethod(didDoc.ID)
if err != nil {
return err
}

// resolve did method
method, err := r.resolveVDR(didMethod)
if err != nil {
return err
}

return method.Update(didDoc, opts...)
}

// Deactivate did document.
func (r *Registry) Deactivate(did string, opts ...vdrapi.DIDMethodOption) error {
didMethod, err := GetDidMethod(did)
if err != nil {
return err
}

// resolve did method
method, err := r.resolveVDR(didMethod)
if err != nil {
return err
}

return method.Deactivate(did, opts...)
}

// Create a new DID Document and store it in this registry.
func (r *Registry) Create(didMethod string, did *diddoc.Doc,
opts ...vdrapi.DIDMethodOption) (*diddoc.DocResolution, error) {
Expand Down
Loading

0 comments on commit b71306f

Please sign in to comment.