-
Notifications
You must be signed in to change notification settings - Fork 12
/
msg_server.go
340 lines (291 loc) · 11.1 KB
/
msg_server.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
package keeper
import (
"context"
"fmt"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/allinbits/cosmos-cash/x/did/types"
vcstypes "github.com/allinbits/cosmos-cash/x/verifiable-credential/types"
)
type msgServer struct {
Keeper
}
// NewMsgServerImpl returns an implementation of the identity MsgServer interface
// for the provided Keeper.
func NewMsgServerImpl(keeper Keeper) types.MsgServer {
return &msgServer{Keeper: keeper}
}
var _ types.MsgServer = msgServer{}
// CreateDidDocument creates a new DID document
func (k msgServer) CreateDidDocument(
goCtx context.Context,
msg *types.MsgCreateDidDocument,
) (*types.MsgCreateDidDocumentResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)
// setup a new did document (performs input validation)
did, err := types.NewDidDocument(msg.Id,
types.WithServices(msg.Services...),
types.WithVerifications(msg.Verifications...),
)
if err != nil {
return nil, err
}
// check that the did is not already taken
_, found := k.Keeper.GetDidDocument(ctx, []byte(msg.Id))
if found {
return nil, sdkerrors.Wrapf(types.ErrDidDocumentFound, "a document with did %s already exists", msg.Id)
}
// persist the did document
k.Keeper.SetDidDocument(ctx, []byte(msg.Id), did)
// now create and persist the metadata
didM := types.NewDidMetadata(ctx.TxBytes(), ctx.BlockTime())
k.Keeper.SetDidMetadata(ctx, []byte(msg.Id), didM)
k.Logger(ctx).Info("Created a DidDocument for", "did", msg.Id, "controller", msg.Signer)
// emit the event
ctx.EventManager().EmitEvent(
types.NewDidDocumentCreatedEvent(msg.Id),
)
return &types.MsgCreateDidDocumentResponse{}, nil
}
// UpdateDidDocument update an existing DID document
func (k msgServer) UpdateDidDocument(
goCtx context.Context,
msg *types.MsgUpdateDidDocument,
) (*types.MsgUpdateDidDocumentResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)
// get the did document
didDoc, found := k.Keeper.GetDidDocument(ctx, []byte(msg.Id))
if !found {
return nil, sdkerrors.Wrapf(types.ErrDidDocumentNotFound, "did document at %s not found", msg.Id)
}
// Any verification method in the authentication relationship can update the DID document
if !didDoc.HasRelationship(msg.Signer, types.Authentication) {
return nil, sdkerrors.Wrapf(
types.ErrUnauthorized,
"signer %s not authorized to update the target did document at %s",
msg.Signer, msg.Id,
)
}
// set the controllers
err := didDoc.SetControllers(msg.Controller...)
if err != nil {
return nil, err
}
// write the did
k.Keeper.SetDidDocument(ctx, []byte(msg.Id), didDoc)
k.Logger(ctx).Info("Updated a DidDocument for", "did", msg.Id, "controller", msg.Signer)
// update the Metadata
if err := updateDidMetadata(&k.Keeper, ctx, didDoc.Id); err != nil {
k.Logger(ctx).Error(err.Error(), "did", didDoc.Id)
}
// NOTE: events are expected to change during client development
ctx.EventManager().EmitEvent(
types.NewDidDocumentUpdatedEvent(msg.Id, msg.Controller...),
)
return &types.MsgUpdateDidDocumentResponse{}, nil
}
// AddVerification adds a verification method and it's relationships to a DID Document
func (k msgServer) AddVerification(
goCtx context.Context,
msg *types.MsgAddVerification,
) (*types.MsgAddVerificationResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)
// get the did document
didDoc, found := k.Keeper.GetDidDocument(ctx, []byte(msg.Id))
if !found {
return nil, sdkerrors.Wrapf(types.ErrDidDocumentNotFound, "did document at %s not found", msg.Id)
}
// Any verification method in the authentication relationship can update the DID document
if !didDoc.HasRelationship(msg.Signer, types.Authentication) {
return nil, sdkerrors.Wrapf(
types.ErrUnauthorized,
"signer account %s not authorized to add verification methods to the target did document at %s",
msg.Signer, msg.Id,
)
}
// add verifications (perform additional checks)
if err := didDoc.AddVerifications(msg.Verification); err != nil {
return nil, err
}
// write the did
k.Keeper.SetDidDocument(ctx, []byte(msg.Id), didDoc)
k.Logger(ctx).Info("Added a new verification method for", "did", msg.Id, "controller", msg.Signer)
// update the Metadata
if err := updateDidMetadata(&k.Keeper, ctx, didDoc.Id); err != nil {
k.Logger(ctx).Error(err.Error(), "did", didDoc.Id)
}
// NOTE: events are expected to change during client development
ctx.EventManager().EmitEvent(
types.NewVerificationAddedEvent(msg.Id, msg.Verification.Method.Controller),
)
return &types.MsgAddVerificationResponse{}, nil
}
// AddService adds a service to an existing DID document
func (k msgServer) AddService(
goCtx context.Context,
msg *types.MsgAddService,
) (*types.MsgAddServiceResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)
// perform checks on the service
if err := types.ValidateService(msg.ServiceData); err != nil {
return nil, err
}
didDoc, found := k.Keeper.GetDidDocument(ctx, []byte(msg.Id))
if !found {
return nil, sdkerrors.Wrapf(types.ErrDidDocumentNotFound, "did document at %s not found", msg.Id)
}
// verify that the service type is of type credential
if !vcstypes.IsValidCredentialType(msg.ServiceData.Type) {
return nil, sdkerrors.Wrapf(
types.ErrInvalidInput,
"invalid service type %s", msg.ServiceData.Type,
)
}
// Any verification method in the authentication relationship can update the DID document
if !didDoc.HasRelationship(msg.Signer, types.Authentication) {
return nil, sdkerrors.Wrapf(
types.ErrUnauthorized,
"signer %s not authorized to add services to the target did document at %s",
msg.Signer, msg.Id,
)
}
// add the service to the document
err := didDoc.AddServices(msg.ServiceData)
if err != nil {
return nil, err
}
// write to storage
k.Keeper.SetDidDocument(ctx, []byte(msg.Id), didDoc)
k.Logger(ctx).Info("Added a new service for", "did", msg.Id, "controller", msg.Signer)
// update the Metadata
if err := updateDidMetadata(&k.Keeper, ctx, didDoc.Id); err != nil {
k.Logger(ctx).Error(err.Error(), "did", didDoc.Id)
}
// NOTE: events are expected to change during client development
ctx.EventManager().EmitEvent(
types.NewServiceAddedEvent(msg.Id, msg.ServiceData.Id),
)
return &types.MsgAddServiceResponse{}, nil
}
// RevokeVerification removes a public key and controller from an existing DID document
func (k msgServer) RevokeVerification(
goCtx context.Context,
msg *types.MsgRevokeVerification,
) (*types.MsgRevokeVerificationResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)
// retrieve the did document
didDoc, found := k.Keeper.GetDidDocument(ctx, []byte(msg.Id))
if !found {
return nil, sdkerrors.Wrapf(types.ErrDidDocumentNotFound, "did document at %s not found", msg.Id)
}
// any verification method in the authentication relationship can update the DID document
if !didDoc.HasRelationship(msg.Signer, types.Authentication) {
return nil, sdkerrors.Wrapf(
types.ErrUnauthorized,
"signer %s not authorized to revoke verification methods from the target did document at %s",
msg.Signer, msg.Id,
)
}
// revoke the verification method + relationships
if err := didDoc.RevokeVerification(msg.MethodId); err != nil {
return nil, err
}
// persist to storage
k.Keeper.SetDidDocument(ctx, []byte(msg.Id), didDoc)
k.Logger(ctx).Info("Revoked verification method from did document for", "did", msg.Id, "controller", msg.Signer)
// update the Metadata
if err := updateDidMetadata(&k.Keeper, ctx, didDoc.Id); err != nil {
k.Logger(ctx).Error(err.Error(), "did", didDoc.Id)
}
// emit event
ctx.EventManager().EmitEvent(
types.NewVerificationRevokedEvent(msg.Id, msg.Signer),
)
return &types.MsgRevokeVerificationResponse{}, nil
}
// DeleteService removes a service from an existing DID document
func (k msgServer) DeleteService(
goCtx context.Context,
msg *types.MsgDeleteService,
) (*types.MsgDeleteServiceResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)
// retrieve the did document
didDoc, found := k.Keeper.GetDidDocument(ctx, []byte(msg.Id))
if !found {
return nil, sdkerrors.Wrapf(types.ErrDidDocumentNotFound, "did document at %s not found", msg.Id)
}
// any verification method in the authentication relationship can update the DID document
if !didDoc.HasRelationship(msg.Signer, types.Authentication) {
return nil, sdkerrors.Wrapf(
types.ErrUnauthorized,
"signer %s not authorized to delete services from the target did document at %s",
msg.Signer, msg.Id,
)
}
// Only try to remove service if there are services
if len(didDoc.Service) == 0 {
return nil, sdkerrors.Wrapf(types.ErrInvalidState, "the did document doesn't have services associated")
}
// delete the service
didDoc.DeleteService(msg.ServiceId)
// persist the did document
k.Keeper.SetDidDocument(ctx, []byte(msg.Id), didDoc)
k.Logger(ctx).Info("Removed service from did document for", "did", msg.Id, "controller", msg.Signer)
// update the Metadata
if err := updateDidMetadata(&k.Keeper, ctx, didDoc.Id); err != nil {
k.Logger(ctx).Error(err.Error(), "did", didDoc.Id)
}
// NOTE: events are expected to change during client development
ctx.EventManager().EmitEvent(
types.NewServiceDeletedEvent(msg.Id, msg.ServiceId),
)
return &types.MsgDeleteServiceResponse{}, nil
}
// SetVerificationRelationships set the verification relationships for an existing DID document
func (k msgServer) SetVerificationRelationships(
goCtx context.Context,
msg *types.MsgSetVerificationRelationships,
) (*types.MsgSetVerificationRelationshipsResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)
// retrieve the did document
didDoc, found := k.Keeper.GetDidDocument(ctx, []byte(msg.Id))
if !found {
return nil, sdkerrors.Wrapf(types.ErrDidDocumentNotFound, "did document at %s not found", msg.Id)
}
// any verification method in the authentication relationship can update the DID document
if !didDoc.HasRelationship(msg.Signer, types.Authentication) {
return nil, sdkerrors.Wrapf(
types.ErrUnauthorized,
"signer %s not authorized to set verification relationships on the target did document at %s",
msg.Signer, msg.Id,
)
}
// set the verification relationships
err := didDoc.SetVerificationRelationships(msg.MethodId, msg.Relationships...)
if err != nil {
return nil, err
}
// persist the did document
k.Keeper.SetDidDocument(ctx, []byte(msg.Id), didDoc)
k.Logger(ctx).Info("Set verification relationship from did document for", "did", msg.Id, "controller", msg.Signer)
// update the Metadata
if err := updateDidMetadata(&k.Keeper, ctx, didDoc.Id); err != nil {
k.Logger(ctx).Error(err.Error(), "did", didDoc.Id)
}
// NOTE: events are expected to change during client development
ctx.EventManager().EmitEvent(
types.NewVerificationRelationshipsUpdatedEvent(msg.Id, msg.MethodId),
)
return &types.MsgSetVerificationRelationshipsResponse{}, nil
}
// helper function to update the did metadata
func updateDidMetadata(keeper *Keeper, ctx sdk.Context, did string) (err error) {
didMeta, found := keeper.GetDidMetadata(ctx, []byte(did))
if found {
types.UpdateDidMetadata(&didMeta, ctx.TxBytes(), ctx.BlockTime())
keeper.SetDidMetadata(ctx, []byte(did), didMeta)
} else {
err = fmt.Errorf("(warning) did metadata not found")
}
return
}