Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: simplify events for did documents #329

Merged
merged 2 commits into from
Jan 24, 2022
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
32 changes: 32 additions & 0 deletions proto/did/event.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
syntax = "proto3";
package allinbits.cosmoscash.did;

option go_package = "github.com/allinbits/cosmos-cash/v2/x/did/types";

import "gogoproto/gogo.proto";

// DidDocumentCreatedEvent is an event triggered on a DID document creation
message DidDocumentCreatedEvent {
option (gogoproto.equal) = true;
option (gogoproto.goproto_getters) = false;

// the did being created
string did = 1;

// the signer account creating the did
string signer = 2;

}

// DidDocumentUpdatedEvent is an event triggered on a DID document update
message DidDocumentUpdatedEvent {
option (gogoproto.equal) = true;
option (gogoproto.goproto_getters) = false;

// the did being updated
string did = 1;

// the signer account of the change
string signer = 2;

}
2 changes: 1 addition & 1 deletion proto/did/tx.proto
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ message MsgCreateDidDocument {
option (gogoproto.goproto_getters) = false;

string id = 1; // the did
string controller = 2; // the controller did
repeated string controllers = 2; // the list of controller DIDs
repeated Verification verifications = 3; // the list of verification methods and relationships
repeated Service services = 4; // the list of services
string signer = 5; // address of the account signing the message
Expand Down
34 changes: 22 additions & 12 deletions x/did/keeper/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ func (k msgServer) CreateDidDocument(
did, err := types.NewDidDocument(msg.Id,
types.WithServices(msg.Services...),
types.WithVerifications(msg.Verifications...),
types.WithControllers(msg.Controllers...),
)
if err != nil {
k.Logger(ctx).Error(err.Error())
Expand All @@ -57,9 +58,10 @@ func (k msgServer) CreateDidDocument(
k.Logger(ctx).Info("created did document", "did", msg.Id, "controller", msg.Signer)

// emit the event
ctx.EventManager().EmitEvent(
types.NewDidDocumentCreatedEvent(msg.Id),
)
if err := ctx.EventManager().EmitTypedEvents(types.NewDidDocumentCreatedEvent(msg.Id, msg.Signer)); err != nil {
k.Logger(ctx).Error("failed to emit DidDocumentCreatedEvent", "did", msg.Id, "signer", msg.Signer, "err", err)
}

return &types.MsgCreateDidDocumentResponse{}, nil
}

Expand Down Expand Up @@ -259,14 +261,20 @@ func executeOnDidWithRelationships(goCtx context.Context, k *Keeper, constraints

// Any verification method in the authentication relationship can update the DID document
if !didDoc.HasRelationship(types.NewBlockchainAccountID(ctx.ChainID(), signer), constraints.relationships...) {
err = sdkerrors.Wrapf(
types.ErrUnauthorized,
"signer account %s not authorized to update the target did document at %s",
signer, did,
)
k.Logger(ctx).Error(err.Error())
return
// check also the controllers
signerDID := types.NewKeyDID(signer)
if !didDoc.HasController(signerDID) {
// if also the controller was not set the error
err = sdkerrors.Wrapf(
types.ErrUnauthorized,
"signer account %s not authorized to update the target did document at %s",
signer, did,
)
k.Logger(ctx).Error(err.Error())
return
}
}

// apply the update
err = update(&didDoc)
if err != nil {
Expand All @@ -283,8 +291,10 @@ func executeOnDidWithRelationships(goCtx context.Context, k *Keeper, constraints
k.Logger(ctx).Error(err.Error(), "did", didDoc.Id)
return
}
// NOTE: events are expected to change during client development
ctx.EventManager().EmitEvent(types.NewDidDocumentUpdatedEvent(did))
// fire the event
if err := ctx.EventManager().EmitTypedEvent(types.NewDidDocumentUpdatedEvent(did, signer)); err != nil {
k.Logger(ctx).Error("failed to emit DidDocumentUpdatedEvent", "did", did, "signer", signer, "err", err)
}
k.Logger(ctx).Info("request to update did document success", "did", didDoc.Id)
return
}
11 changes: 11 additions & 0 deletions x/did/types/did.go
Original file line number Diff line number Diff line change
Expand Up @@ -706,6 +706,17 @@ func (didDoc DidDocument) HasPublicKey(pubkey cryptotypes.PubKey) bool {
return false
}

// HasController returns true if the DID document has the input DID as a controller, false otherwise
func (didDoc *DidDocument) HasController(controller DID) bool {
ctrl := controller.String()
for _, c := range didDoc.Controller {
if c == ctrl {
return true
}
}
return false
}

// AddServices add services to a did document
func (didDoc *DidDocument) AddServices(services ...*Service) (err error) {
if didDoc.Service == nil {
Expand Down
90 changes: 11 additions & 79 deletions x/did/types/event.go
Original file line number Diff line number Diff line change
@@ -1,85 +1,17 @@
package types

import (
sdk "github.com/cosmos/cosmos-sdk/types"
)

// did module event types
const (
AttributeValueCategory = ModuleName

EventTypeDidDocumentCreated = "did_document_created"
EventTypeDidDocumentUpdated = "did_document_updated"
EventTypeVerificationMethodAdded = "verification_method_added"
EventTypeVerificationRevoked = "verification_method_revoked"
EventTypeVerificationRelationshipsUpdated = "verification_relationships_updated"
EventTypeServiceAdded = "service_added"
EventTypeServiceDeleted = "service_deleted"

AttributeDID = "did"
AttributeKeyOwner = "owner"
AttributeKeyController = "verification_method_controller"
AttributeKeyServiceID = "service_id"
)

// NewDidDocumentCreatedEvent constructs a new did_created sdk.Event
func NewDidDocumentCreatedEvent(owner string) sdk.Event {
return sdk.NewEvent(
EventTypeDidDocumentCreated,
sdk.NewAttribute(AttributeKeyOwner, owner),
)
func NewDidDocumentCreatedEvent(did, owner string) *DidDocumentCreatedEvent {
return &DidDocumentCreatedEvent{
Did: did,
Signer: owner,
}
}

// NewDidDocumentUpdatedEvent constructs a new did_created sdk.Event
func NewDidDocumentUpdatedEvent(did string) sdk.Event {
e := sdk.NewEvent(
EventTypeDidDocumentUpdated,
sdk.NewAttribute(AttributeDID, did),
)
return e
}

// NewVerificationAddedEvent constructs a new authentication_added sdk.Event
func NewVerificationAddedEvent(owner string, controller string) sdk.Event {
return sdk.NewEvent(
EventTypeVerificationMethodAdded,
sdk.NewAttribute(AttributeKeyOwner, owner),
sdk.NewAttribute(AttributeKeyController, controller),
)
}

// NewServiceAddedEvent constructs a new service_added sdk.Event
func NewServiceAddedEvent(owner string, serviceID string) sdk.Event {
return sdk.NewEvent(
EventTypeServiceAdded,
sdk.NewAttribute(AttributeKeyOwner, owner),
sdk.NewAttribute(AttributeKeyServiceID, serviceID),
)
}

// NewVerificationRevokedEvent constructs a new authentication_deleted sdk.Event
func NewVerificationRevokedEvent(owner string, controller string) sdk.Event {
return sdk.NewEvent(
EventTypeVerificationRevoked,
sdk.NewAttribute(AttributeKeyOwner, owner),
sdk.NewAttribute(AttributeKeyController, controller),
)
}

// NewServiceDeletedEvent constructs a new service_deleted sdk.Event
func NewServiceDeletedEvent(owner string, serviceID string) sdk.Event {
return sdk.NewEvent(
EventTypeServiceDeleted,
sdk.NewAttribute(AttributeKeyOwner, owner),
sdk.NewAttribute(AttributeKeyServiceID, serviceID),
)
}

// NewVerificationRelationshipsUpdatedEvent constructs a new relationships updated sdk.Event
func NewVerificationRelationshipsUpdatedEvent(owner string, methodID string) sdk.Event {
return sdk.NewEvent(
EventTypeVerificationRelationshipsUpdated,
sdk.NewAttribute(AttributeKeyOwner, owner),
sdk.NewAttribute(AttributeKeyServiceID, methodID),
)
}
func NewDidDocumentUpdatedEvent(did, signer string) *DidDocumentUpdatedEvent {
return &DidDocumentUpdatedEvent{
Did: did,
Signer: signer,
}
}
Loading