Skip to content

Commit

Permalink
VDR: Introduce slimmed-down DIDResolver interface for implementing ot…
Browse files Browse the repository at this point in the history
…her DID methods (#2371)
  • Loading branch information
reinkrul authored Jul 25, 2023
1 parent 9638426 commit ff62235
Show file tree
Hide file tree
Showing 36 changed files with 268 additions and 308 deletions.
6 changes: 3 additions & 3 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,11 +198,11 @@ func CreateSystem(shutdownCallback context.CancelFunc) *core.System {
system.RegisterRoutes(&core.LandingPage{})
system.RegisterRoutes(&cryptoAPI.Wrapper{C: cryptoInstance, K: didservice.KeyResolver{Store: didStore}})
system.RegisterRoutes(&networkAPI.Wrapper{Service: networkInstance})
docResolver := didservice.Resolver{Store: didStore}
system.RegisterRoutes(&vdrAPI.Wrapper{VDR: vdrInstance, DocResolver: docResolver, DocManipulator: &didservice.Manipulator{
didResolver := didservice.Resolver{Store: didStore}
system.RegisterRoutes(&vdrAPI.Wrapper{VDR: vdrInstance, DIDResolver: didResolver, DocManipulator: &didservice.Manipulator{
KeyCreator: cryptoInstance,
Updater: vdrInstance,
Resolver: docResolver,
Resolver: didResolver,
}})
system.RegisterRoutes(&vcrAPI.Wrapper{VCR: credentialInstance, ContextManager: jsonld})
system.RegisterRoutes(&openid4vciAPI.Wrapper{
Expand Down
20 changes: 10 additions & 10 deletions didman/didman.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ func (e ErrReferencedServiceNotAnEndpoint) Is(other error) bool {

type didman struct {
jsonldManager jsonld.JSONLD
docResolver types.DocResolver
didResolver types.DIDResolver
serviceResolver types.ServiceResolver
store didstore.Store
vdr types.VDR
Expand All @@ -100,7 +100,7 @@ type didman struct {
// NewDidmanInstance creates a new didman instance with services set
func NewDidmanInstance(store didstore.Store, vdr types.VDR, vcr vcr.Finder, jsonldManager jsonld.JSONLD) Didman {
return &didman{
docResolver: didservice.Resolver{Store: store},
didResolver: didservice.Resolver{Store: store},
serviceResolver: didservice.ServiceResolver{Store: store},
store: store,
vdr: vdr,
Expand Down Expand Up @@ -158,7 +158,7 @@ func (d *didman) DeleteEndpointsByType(ctx context.Context, id did.DID, serviceT
unlockFn := d.callSerializer.Lock(id.String())
defer unlockFn()

doc, _, err := d.docResolver.Resolve(id, nil)
doc, _, err := d.didResolver.Resolve(id, nil)
if err != nil {
return err
}
Expand All @@ -179,7 +179,7 @@ func (d *didman) DeleteEndpointsByType(ctx context.Context, id did.DID, serviceT
}

func (d *didman) GetCompoundServices(id did.DID) ([]did.Service, error) {
doc, _, err := d.docResolver.Resolve(id, nil)
doc, _, err := d.didResolver.Resolve(id, nil)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -244,7 +244,7 @@ func (d *didman) UpdateCompoundService(ctx context.Context, id did.DID, serviceT
}

func (d *didman) GetCompoundServiceEndpoint(id did.DID, compoundServiceType string, endpointType string, resolveReferences bool) (string, error) {
document, _, err := d.docResolver.Resolve(id, nil)
document, _, err := d.didResolver.Resolve(id, nil)
if err != nil {
return "", err
}
Expand Down Expand Up @@ -309,7 +309,7 @@ func (d *didman) deleteService(ctx context.Context, serviceID ssi.URI) error {
return err
}

doc, _, err := d.docResolver.Resolve(id, nil)
doc, _, err := d.didResolver.Resolve(id, nil)
if err != nil {
return err
}
Expand Down Expand Up @@ -390,7 +390,7 @@ func (d *didman) UpdateContactInformation(ctx context.Context, id did.DID, infor
// GetContactInformation tries to find the ContactInformation for the indicated DID document.
// Returns nil, nil when no contactInformation for the DID was found.
func (d *didman) GetContactInformation(id did.DID) (*ContactInformation, error) {
doc, _, err := d.docResolver.Resolve(id, nil)
doc, _, err := d.didResolver.Resolve(id, nil)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -517,7 +517,7 @@ func (d *didman) resolveOrganizationDIDDocument(organization vc.VerifiableCreden
if err != nil {
return nil, did.DID{}, fmt.Errorf("unable to parse DID from organization credential: %w", err)
}
document, _, err := d.docResolver.Resolve(*organizationDID, nil)
document, _, err := d.didResolver.Resolve(*organizationDID, nil)
return document, *organizationDID, err
}

Expand Down Expand Up @@ -545,7 +545,7 @@ func filterServices(doc *did.Document, serviceType string) []did.Service {
}

func (d *didman) addService(ctx context.Context, id did.DID, serviceType string, serviceEndpoint interface{}, preprocessor func(*did.Document)) (*did.Service, error) {
doc, _, err := d.docResolver.Resolve(id, nil)
doc, _, err := d.didResolver.Resolve(id, nil)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -577,7 +577,7 @@ func (d *didman) addService(ctx context.Context, id did.DID, serviceType string,
}

func (d *didman) updateService(ctx context.Context, id did.DID, serviceType string, serviceEndpoint interface{}) (*did.Service, error) {
doc, _, err := d.docResolver.Resolve(id, nil)
doc, _, err := d.didResolver.Resolve(id, nil)
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion didman/didman_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -985,7 +985,7 @@ func newMockContext(t *testing.T) mockContext {
mockVDR := types.NewMockVDR(ctrl)
mockVCR := vcr.NewMockFinder(ctrl)
instance := NewDidmanInstance(store, mockVDR, mockVCR, jsonld.NewTestJSONLDManager(t))
instance.(*didman).docResolver = didservice.Resolver{Store: store}
instance.(*didman).didResolver = didservice.Resolver{Store: store}
instance.(*didman).serviceResolver = didservice.ServiceResolver{Store: store}

return mockContext{
Expand Down
18 changes: 9 additions & 9 deletions didman/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,47 +35,47 @@ type Didman interface {

// AddEndpoint adds a service to a DID Document. The serviceEndpoint is set to the given URL.
// It returns ErrDuplicateService if a service with the given type already exists.
// It can also return various errors from DocResolver.Resolve and VDR.Update
// It can also return various errors from DIDResolver.Resolve and VDR.Update
AddEndpoint(ctx context.Context, id did.DID, serviceType string, endpoint url.URL) (*did.Service, error)

// UpdateEndpoint updates the serviceEndpoint of a service in a DID Document. The serviceEndpoint is set to the given URL.
// It can return various errors from DocResolver.Resolve and VDR.Update.
// It can return various errors from DIDResolver.Resolve and VDR.Update.
UpdateEndpoint(ctx context.Context, id did.DID, serviceType string, endpoint url.URL) (*did.Service, error)

// DeleteEndpointsByType takes a did and type and removes all endpoint with the type from the DID Document.
// It returns ErrServiceNotFound if no services with the given type can't be found in the DID Document.
// It returns ErrServiceInUse if the service is referenced by other services.
// It can also return various errors from DocResolver.Resolve
// It can also return various errors from DIDResolver.Resolve
DeleteEndpointsByType(ctx context.Context, id did.DID, serviceType string) error

// DeleteService removes a service from a DID Document.
// It returns ErrServiceInUse if the service is referenced by other services.
// It returns ErrServiceNotFound if the service can't be found in the DID Document.
// It can also return various errors from DocResolver.Resolve and VDR.Update
// It can also return various errors from DIDResolver.Resolve and VDR.Update
DeleteService(ctx context.Context, id ssi.URI) error

// AddCompoundService adds a compound endpoint to a DID Document.
// It returns ErrDuplicateService if a service with the given type already exists.
// It returns didservice.DIDServiceQueryError if one of the service references is invalid.
// It returns ErrReferencedServiceNotAnEndpoint if one of the references does not resolve to a single endpoint URL.
// It can also return various errors from DocResolver.Resolve and VDR.Update
// It can also return various errors from DIDResolver.Resolve and VDR.Update
AddCompoundService(ctx context.Context, id did.DID, serviceType string, endpoints map[string]ssi.URI) (*did.Service, error)

// UpdateCompoundService updates a compound endpoint in a DID Document.
// It returns didservice.DIDServiceQueryError if one of the service references is invalid.
// It returns ErrReferencedServiceNotAnEndpoint if one of the references does not resolve to a single endpoint URL.
// It can also return various errors from DocResolver.Resolve and VDR.Update
// It can also return various errors from DIDResolver.Resolve and VDR.Update
UpdateCompoundService(ctx context.Context, id did.DID, serviceType string, endpoints map[string]ssi.URI) (*did.Service, error)

// UpdateContactInformation adds or updates the compoundService with type equal to node-contact-info with provided
// contact information to the DID Document.
// It returns the contact information when the update was successful.
// It can also return various errors from DocResolver.Resolve and VDR.Update
// It can also return various errors from DIDResolver.Resolve and VDR.Update
UpdateContactInformation(ctx context.Context, id did.DID, information ContactInformation) (*ContactInformation, error)

// GetContactInformation finds and returns the contact information from the provided DID Document.
// Returns nil, nil when no contactInformation for the DID was found.
// It can also return various errors from DocResolver.Resolve
// It can also return various errors from DIDResolver.Resolve
GetContactInformation(id did.DID) (*ContactInformation, error)

// SearchOrganizations searches VCR for organizations which's name matches the given query.
Expand All @@ -95,7 +95,7 @@ type CompoundServiceResolver interface {

// GetCompoundServices returns a list of all compoundServices defined on the given DID document.
// It does not include special compound services like ContactInformation
// It can also return various errors from DocResolver.Resolve
// It can also return various errors from DIDResolver.Resolve
GetCompoundServices(id did.DID) ([]did.Service, error)
}

Expand Down
8 changes: 4 additions & 4 deletions golden_hammer/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func New(documentOwner types.DocumentOwner, didmanAPI didman.Didman, didStore di
routines: &sync.WaitGroup{},
didmanAPI: didmanAPI,
documentOwner: documentOwner,
docResolver: didservice.Resolver{Store: didStore},
didResolver: didservice.Resolver{Store: didStore},
fixedDocumentDIDs: map[string]bool{},
}
}
Expand All @@ -63,7 +63,7 @@ type GoldenHammer struct {
ctx context.Context
cancelFunc context.CancelFunc
routines *sync.WaitGroup
docResolver types.DocResolver
didResolver types.DIDResolver
didmanAPI didman.Didman
documentOwner types.DocumentOwner
fixedDocumentDIDs map[string]bool
Expand Down Expand Up @@ -194,7 +194,7 @@ func (h *GoldenHammer) listDocumentToFix() ([]did.Document, error) {
// Already fixed
continue
}
document, _, err := h.docResolver.Resolve(id, nil)
document, _, err := h.didResolver.Resolve(id, nil)
if err != nil {
if !didservice.IsFunctionalResolveError(err) {
log.Logger().WithError(err).Infof("Can't resolve DID document, skipping fix (did=%s)", id)
Expand Down Expand Up @@ -239,7 +239,7 @@ func (h *GoldenHammer) tryResolveURL(id did.DID) (*url.URL, error) {

// resolveContainsService returns whether 1. given DID document can be resolved, and 2. it contains the specified service.
func (h *GoldenHammer) resolveContainsService(id did.DID, serviceType string) bool {
document, _, err := h.docResolver.Resolve(id, nil)
document, _, err := h.didResolver.Resolve(id, nil)
if didservice.IsFunctionalResolveError(err) {
// Unresolvable DID document, nothing to do
return false
Expand Down
62 changes: 31 additions & 31 deletions network/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,24 +72,24 @@ var defaultBBoltOptions = bbolt.DefaultOptions

// Network implements Transactions interface and Engine functions.
type Network struct {
config Config
certificate tls.Certificate
trustStore *core.TrustStore
strictMode bool
protocols []transport.Protocol
connectionManager transport.ConnectionManager
state dag.State
keyStore crypto.KeyStore
keyResolver types.KeyResolver
startTime atomic.Pointer[time.Time]
peerID transport.PeerID
didDocumentResolver types.DocResolver
nodeDID did.DID
didDocumentFinder types.DocFinder
serviceResolver types.ServiceResolver
eventPublisher events.Event
storeProvider storage.Provider
pkiValidator pki.Validator
config Config
certificate tls.Certificate
trustStore *core.TrustStore
strictMode bool
protocols []transport.Protocol
connectionManager transport.ConnectionManager
state dag.State
keyStore crypto.KeyStore
keyResolver types.KeyResolver
startTime atomic.Pointer[time.Time]
peerID transport.PeerID
didResolver types.DIDResolver
nodeDID did.DID
didDocumentFinder types.DocFinder
serviceResolver types.ServiceResolver
eventPublisher events.Event
storeProvider storage.Provider
pkiValidator pki.Validator
// assumeNewNode indicates the node hasn't initially sync'd with the network.
assumeNewNode bool
selfTestDialer tls.Dialer
Expand Down Expand Up @@ -148,15 +148,15 @@ func NewNetworkInstance(
pkiValidator pki.Validator,
) *Network {
return &Network{
config: config,
keyResolver: didservice.KeyResolver{Store: store},
keyStore: keyStore,
didDocumentResolver: didservice.Resolver{Store: store},
didDocumentFinder: didservice.Finder{Store: store},
serviceResolver: didservice.ServiceResolver{Store: store},
eventPublisher: eventPublisher,
storeProvider: storeProvider,
pkiValidator: pkiValidator,
config: config,
keyResolver: didservice.KeyResolver{Store: store},
keyStore: keyStore,
didResolver: didservice.Resolver{Store: store},
didDocumentFinder: didservice.Finder{Store: store},
serviceResolver: didservice.ServiceResolver{Store: store},
eventPublisher: eventPublisher,
storeProvider: storeProvider,
pkiValidator: pkiValidator,
selfTestDialer: tls.Dialer{
NetDialer: &net.Dialer{
Timeout: time.Second,
Expand All @@ -173,7 +173,7 @@ func (n *Network) Configure(config core.ServerConfig) error {
if err != nil {
return fmt.Errorf("unable to create database: %w", err)
}
nutsKeyResolver := didservice.NutsKeyResolver{Resolver: n.didDocumentResolver}
nutsKeyResolver := didservice.NutsKeyResolver{Resolver: n.didResolver}
if n.state, err = dag.NewState(dagStore, dag.NewPrevTransactionsVerifier(), dag.NewTransactionSignatureVerifier(nutsKeyResolver)); err != nil {
return fmt.Errorf("failed to configure state: %w", err)
}
Expand Down Expand Up @@ -218,7 +218,7 @@ func (n *Network) Configure(config core.ServerConfig) error {
var candidateProtocols []transport.Protocol
if n.protocols == nil {
candidateProtocols = []transport.Protocol{
v2.New(v2Cfg, n.nodeDID, n.state, n.didDocumentResolver, n.keyStore, n.collectDiagnosticsForPeers, dagStore),
v2.New(v2Cfg, n.nodeDID, n.state, n.didResolver, n.keyStore, n.collectDiagnosticsForPeers, dagStore),
}
} else {
// Only set protocols if not already set: improves testability
Expand Down Expand Up @@ -301,7 +301,7 @@ func (n *Network) DiscoverServices(updatedDID did.DID) {
if !n.config.EnableDiscovery {
return
}
document, _, err := n.didDocumentResolver.Resolve(updatedDID, nil)
document, _, err := n.didResolver.Resolve(updatedDID, nil)
if err != nil {
// VDR store is down. Any missed updates are resolved on node restart.
// This can happen when the VDR is receiving lots of DID updates, such as during the initial sync of the network.
Expand Down Expand Up @@ -444,7 +444,7 @@ inner:

func (n *Network) validateNodeDIDKeys(ctx context.Context, nodeDID did.DID) error {
// Check if DID document can be resolved
document, _, err := n.didDocumentResolver.Resolve(nodeDID, nil)
document, _, err := n.didResolver.Resolve(nodeDID, nil)
if err != nil {
return fmt.Errorf("DID document can't be resolved (did=%s): %w", nodeDID, err)
}
Expand Down
18 changes: 9 additions & 9 deletions network/network_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1107,15 +1107,15 @@ func startNode(t *testing.T, name string, testDirectory string, opts ...func(ser
}

instance := &Network{
config: config,
didDocumentResolver: didservice.Resolver{Store: didStore},
didDocumentFinder: didservice.Finder{Store: didStore},
keyStore: keyStore,
keyResolver: didservice.KeyResolver{Store: didStore},
serviceResolver: didservice.ServiceResolver{Store: didStore},
eventPublisher: eventPublisher,
storeProvider: &storeProvider,
pkiValidator: pkiValidator,
config: config,
didResolver: didservice.Resolver{Store: didStore},
didDocumentFinder: didservice.Finder{Store: didStore},
keyStore: keyStore,
keyResolver: didservice.KeyResolver{Store: didStore},
serviceResolver: didservice.ServiceResolver{Store: didStore},
eventPublisher: eventPublisher,
storeProvider: &storeProvider,
pkiValidator: pkiValidator,
}

if err := instance.Configure(*serverConfig); err != nil {
Expand Down
2 changes: 1 addition & 1 deletion network/network_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1331,7 +1331,7 @@ func createNetwork(t *testing.T, ctrl *gomock.Controller, cfgFn ...func(config *
pkiMock := pki.NewMockValidator(ctrl)
network := NewNetworkInstance(networkConfig, didStore, keyStore, eventPublisher, storageEngine.GetProvider(ModuleName), pkiMock)
network.keyResolver = keyResolver
network.didDocumentResolver = didservice.Resolver{Store: didStore}
network.didResolver = didservice.Resolver{Store: didStore}
network.serviceResolver = didservice.ServiceResolver{Store: didStore}
network.didDocumentFinder = docFinder
network.state = state
Expand Down
6 changes: 3 additions & 3 deletions network/transport/v2/handlers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ func TestProtocol_handleTransactionPayloadQuery(t *testing.T) {
t.Run("local node is not a participant in the TX", func(t *testing.T) {
p, mocks := newTestProtocol(t, nodeDID)
mocks.State.EXPECT().GetTransaction(gomock.Any(), tx.Ref()).Return(tx, nil)
mocks.DocResolver.EXPECT().Resolve(*nodeDID, nil).Return(&didDocument, nil, nil)
mocks.DIDResolver.EXPECT().Resolve(*nodeDID, nil).Return(&didDocument, nil, nil)
mocks.Decrypter.EXPECT().Decrypt(ctx, keyDID.String(), gomock.Any()).Return(nil, errors.New("will return nil for PAL decryption")).Times(2)
conns := grpc.NewStubConnectionList(authenticatedPeer)
p.connectionList = conns
Expand All @@ -198,7 +198,7 @@ func TestProtocol_handleTransactionPayloadQuery(t *testing.T) {
t.Run("peer is not in PAL", func(t *testing.T) {
p, mocks := newTestProtocol(t, nodeDID)
mocks.State.EXPECT().GetTransaction(gomock.Any(), tx.Ref()).Return(tx, nil)
mocks.DocResolver.EXPECT().Resolve(*nodeDID, nil).Return(&didDocument, nil, nil)
mocks.DIDResolver.EXPECT().Resolve(*nodeDID, nil).Return(&didDocument, nil, nil)
mocks.Decrypter.EXPECT().Decrypt(ctx, keyDID.String(), gomock.Any()).Return([]byte(nodeDID.String()), nil)
conns := grpc.NewStubConnectionList(authenticatedPeer)
p.connectionList = conns
Expand All @@ -211,7 +211,7 @@ func TestProtocol_handleTransactionPayloadQuery(t *testing.T) {
t.Run("ok", func(t *testing.T) {
p, mocks := newTestProtocol(t, nodeDID)
mocks.State.EXPECT().GetTransaction(gomock.Any(), tx.Ref()).Return(tx, nil)
mocks.DocResolver.EXPECT().Resolve(*nodeDID, nil).Return(&didDocument, nil, nil)
mocks.DIDResolver.EXPECT().Resolve(*nodeDID, nil).Return(&didDocument, nil, nil)
mocks.Decrypter.EXPECT().Decrypt(ctx, keyDID.String(), gomock.Any()).Return([]byte(peerDID.String()), nil)
mocks.State.EXPECT().ReadPayload(ctx, tx.PayloadHash()).Return([]byte{}, nil)
conns := grpc.NewStubConnectionList(authenticatedPeer)
Expand Down
Loading

0 comments on commit ff62235

Please sign in to comment.