-
Notifications
You must be signed in to change notification settings - Fork 29
/
msg_server.go
428 lines (375 loc) · 13.4 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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
package keeper
import (
"context"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/ixofoundation/ixo-blockchain/v3/x/iid/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) CreateIidDocument(goCtx context.Context, msg *types.MsgCreateIidDocument,
) (*types.MsgCreateIidDocumentResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)
// check that the did is not already taken
_, found := k.Keeper.GetDidDocument(ctx, []byte(msg.Id))
if found {
err := sdkerrors.Wrapf(types.ErrDidDocumentFound, "a document with did %s already exists", msg.Id)
return nil, err
}
// setup a new did document (performs input validation)
did, err := types.NewDidDocument(ctx, msg.Id,
types.WithServices(msg.Services...),
types.WithRights(msg.AccordedRight...),
types.WithResources(msg.LinkedResource...),
types.WithClaims(msg.LinkedClaim...),
types.WithEntities(msg.LinkedEntity...),
types.WithVerifications(msg.Verifications...),
types.WithControllers(msg.Controllers...),
types.WithContexts(msg.Context...),
types.WithAlsoKnownAs(msg.AlsoKnownAs),
)
if err != nil {
return nil, err
}
k.Keeper.SetDidDocument(ctx, []byte(msg.Id), did)
// emit the event
if err := ctx.EventManager().EmitTypedEvents(types.NewIidDocumentCreatedEvent(&did)); err != nil {
return nil, err
}
return &types.MsgCreateIidDocumentResponse{}, nil
}
// UpdateDidDocument update an existing DID document
func (k msgServer) UpdateIidDocument(goCtx context.Context, msg *types.MsgUpdateIidDocument,
) (*types.MsgUpdateIidDocumentResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)
if err := ExecuteOnDidWithRelationships(
ctx, &k.Keeper,
newConstraints(types.Authentication),
msg.Id, msg.Signer,
func(didDoc *types.IidDocument) error {
did, err := types.NewDidDocument(ctx, msg.Id,
types.WithServices(msg.Services...),
types.WithRights(msg.AccordedRight...),
types.WithResources(msg.LinkedResource...),
types.WithClaims(msg.LinkedClaim...),
types.WithEntities(msg.LinkedEntity...),
types.WithVerifications(msg.Verifications...),
types.WithControllers(msg.Controllers...),
types.WithContexts(msg.Context...),
types.WithAlsoKnownAs(msg.AlsoKnownAs),
)
if err != nil {
return err
}
// Keep old iid doc metadata
did.Metadata = didDoc.Metadata
return nil
}); err != nil {
return nil, err
}
return &types.MsgUpdateIidDocumentResponse{}, nil
}
func (k msgServer) AddVerification(goCtx context.Context, msg *types.MsgAddVerification,
) (*types.MsgAddVerificationResponse, error) {
if err := ExecuteOnDidWithRelationships(
sdk.UnwrapSDKContext(goCtx), &k.Keeper,
newConstraints(types.Authentication),
msg.Id, msg.Signer,
func(didDoc *types.IidDocument) error {
return didDoc.AddVerifications(msg.Verification)
}); err != nil {
return nil, err
}
return &types.MsgAddVerificationResponse{}, nil
}
func (k msgServer) AddService(goCtx context.Context, msg *types.MsgAddService,
) (*types.MsgAddServiceResponse, error) {
if err := ExecuteOnDidWithRelationships(
sdk.UnwrapSDKContext(goCtx), &k.Keeper,
newConstraints(types.Authentication),
msg.Id, msg.Signer,
func(didDoc *types.IidDocument) error {
return didDoc.AddServices(msg.ServiceData)
}); err != nil {
return nil, err
}
return &types.MsgAddServiceResponse{}, nil
}
func (k msgServer) AddLinkedResource(goCtx context.Context, msg *types.MsgAddLinkedResource,
) (*types.MsgAddLinkedResourceResponse, error) {
if err := ExecuteOnDidWithRelationships(
sdk.UnwrapSDKContext(goCtx), &k.Keeper,
newConstraints(types.Authentication),
msg.Id, msg.Signer,
func(didDoc *types.IidDocument) error {
return didDoc.AddLinkedResource(msg.LinkedResource)
}); err != nil {
return nil, err
}
return &types.MsgAddLinkedResourceResponse{}, nil
}
func (k msgServer) DeleteLinkedResource(goCtx context.Context, msg *types.MsgDeleteLinkedResource,
) (*types.MsgDeleteLinkedResourceResponse, error) {
if err := ExecuteOnDidWithRelationships(
sdk.UnwrapSDKContext(goCtx), &k.Keeper,
newConstraints(types.Authentication),
msg.Id, msg.Signer,
func(didDoc *types.IidDocument) error {
if len(didDoc.LinkedResource) == 0 {
return sdkerrors.Wrapf(types.ErrInvalidState, "the did document doesn't have resources associated")
}
didDoc.DeleteLinkedResource(msg.ResourceId)
return nil
}); err != nil {
return nil, err
}
return &types.MsgDeleteLinkedResourceResponse{}, nil
}
func (k msgServer) AddLinkedClaim(goCtx context.Context, msg *types.MsgAddLinkedClaim,
) (*types.MsgAddLinkedClaimResponse, error) {
if err := ExecuteOnDidWithRelationships(
sdk.UnwrapSDKContext(goCtx), &k.Keeper,
newConstraints(types.Authentication),
msg.Id, msg.Signer,
func(didDoc *types.IidDocument) error {
return didDoc.AddLinkedClaim(msg.LinkedClaim)
}); err != nil {
return nil, err
}
return &types.MsgAddLinkedClaimResponse{}, nil
}
func (k msgServer) DeleteLinkedClaim(goCtx context.Context, msg *types.MsgDeleteLinkedClaim,
) (*types.MsgDeleteLinkedClaimResponse, error) {
if err := ExecuteOnDidWithRelationships(
sdk.UnwrapSDKContext(goCtx), &k.Keeper,
newConstraints(types.Authentication),
msg.Id, msg.Signer,
func(didDoc *types.IidDocument) error {
if len(didDoc.LinkedClaim) == 0 {
return sdkerrors.Wrapf(types.ErrInvalidState, "the did document doesn't have Claims associated")
}
didDoc.DeleteLinkedClaim(msg.ClaimId)
return nil
}); err != nil {
return nil, err
}
return &types.MsgDeleteLinkedClaimResponse{}, nil
}
func (k msgServer) AddLinkedEntity(goCtx context.Context, msg *types.MsgAddLinkedEntity,
) (*types.MsgAddLinkedEntityResponse, error) {
if err := ExecuteOnDidWithRelationships(
sdk.UnwrapSDKContext(goCtx), &k.Keeper,
newConstraints(types.Authentication),
msg.Id, msg.Signer,
func(didDoc *types.IidDocument) error {
return didDoc.AddLinkedEntity(msg.LinkedEntity)
}); err != nil {
return nil, err
}
return &types.MsgAddLinkedEntityResponse{}, nil
}
func (k msgServer) DeleteLinkedEntity(goCtx context.Context, msg *types.MsgDeleteLinkedEntity,
) (*types.MsgDeleteLinkedEntityResponse, error) {
if err := ExecuteOnDidWithRelationships(
sdk.UnwrapSDKContext(goCtx), &k.Keeper,
newConstraints(types.Authentication),
msg.Id, msg.Signer,
func(didDoc *types.IidDocument) error {
if len(didDoc.LinkedEntity) == 0 {
return sdkerrors.Wrapf(types.ErrInvalidState, "the did document doesn't have entities associated")
}
didDoc.DeleteLinkedEntity(msg.EntityId)
return nil
}); err != nil {
return nil, err
}
return &types.MsgDeleteLinkedEntityResponse{}, nil
}
func (k msgServer) AddAccordedRight(goCtx context.Context, msg *types.MsgAddAccordedRight,
) (*types.MsgAddAccordedRightResponse, error) {
if err := ExecuteOnDidWithRelationships(
sdk.UnwrapSDKContext(goCtx), &k.Keeper,
newConstraints(types.Authentication),
msg.Id, msg.Signer,
func(didDoc *types.IidDocument) error {
return didDoc.AddAccordedRight(msg.AccordedRight)
}); err != nil {
return nil, err
}
return &types.MsgAddAccordedRightResponse{}, nil
}
func (k msgServer) DeleteAccordedRight(goCtx context.Context, msg *types.MsgDeleteAccordedRight,
) (*types.MsgDeleteAccordedRightResponse, error) {
if err := ExecuteOnDidWithRelationships(
sdk.UnwrapSDKContext(goCtx), &k.Keeper,
newConstraints(types.Authentication),
msg.Id, msg.Signer,
func(didDoc *types.IidDocument) error {
if len(didDoc.AccordedRight) == 0 {
return sdkerrors.Wrapf(types.ErrInvalidState, "the did document doesn't have rights associated")
}
didDoc.DeleteAccordedRight(msg.RightId)
return nil
}); err != nil {
return nil, err
}
return &types.MsgDeleteAccordedRightResponse{}, nil
}
func (k msgServer) AddIidContext(goCtx context.Context, msg *types.MsgAddIidContext,
) (*types.MsgAddIidContextResponse, error) {
if err := ExecuteOnDidWithRelationships(
sdk.UnwrapSDKContext(goCtx), &k.Keeper,
newConstraints(types.Authentication),
msg.Id, msg.Signer,
func(didDoc *types.IidDocument) error {
return didDoc.AddDidContext(msg.Context)
}); err != nil {
return nil, err
}
return &types.MsgAddIidContextResponse{}, nil
}
func (k msgServer) DeleteIidContext(goCtx context.Context, msg *types.MsgDeleteIidContext,
) (*types.MsgDeleteIidContextResponse, error) {
if err := ExecuteOnDidWithRelationships(
sdk.UnwrapSDKContext(goCtx), &k.Keeper,
newConstraints(types.Authentication),
msg.Id, msg.Signer,
func(didDoc *types.IidDocument) error {
if len(didDoc.Context) == 0 {
return sdkerrors.Wrapf(types.ErrInvalidState, "the did document doesn't have contexts associated")
}
didDoc.DeleteDidContext(msg.ContextKey)
return nil
}); err != nil {
return nil, err
}
return &types.MsgDeleteIidContextResponse{}, nil
}
func (k msgServer) RevokeVerification(goCtx context.Context, msg *types.MsgRevokeVerification,
) (*types.MsgRevokeVerificationResponse, error) {
if err := ExecuteOnDidWithRelationships(
sdk.UnwrapSDKContext(goCtx), &k.Keeper,
newConstraints(types.Authentication),
msg.Id, msg.Signer,
func(didDoc *types.IidDocument) error {
return didDoc.RevokeVerification(msg.MethodId)
}); err != nil {
return nil, err
}
return &types.MsgRevokeVerificationResponse{}, nil
}
func (k msgServer) DeleteService(goCtx context.Context, msg *types.MsgDeleteService,
) (*types.MsgDeleteServiceResponse, error) {
if err := ExecuteOnDidWithRelationships(
sdk.UnwrapSDKContext(goCtx), &k.Keeper,
newConstraints(types.Authentication),
msg.Id, msg.Signer,
func(didDoc *types.IidDocument) error {
if len(didDoc.Service) == 0 {
return sdkerrors.Wrapf(types.ErrInvalidState, "the did document doesn't have services associated")
}
didDoc.DeleteService(msg.ServiceId)
return nil
}); err != nil {
return nil, err
}
return &types.MsgDeleteServiceResponse{}, nil
}
func (k msgServer) SetVerificationRelationships(goCtx context.Context, msg *types.MsgSetVerificationRelationships,
) (*types.MsgSetVerificationRelationshipsResponse, error) {
if err := ExecuteOnDidWithRelationships(
sdk.UnwrapSDKContext(goCtx), &k.Keeper,
newConstraints(types.Authentication),
msg.Id, msg.Signer,
func(didDoc *types.IidDocument) error {
return didDoc.SetVerificationRelationships(msg.MethodId, msg.Relationships...)
}); err != nil {
return nil, err
}
return &types.MsgSetVerificationRelationshipsResponse{}, nil
}
func (k msgServer) AddController(goCtx context.Context, msg *types.MsgAddController,
) (*types.MsgAddControllerResponse, error) {
if err := ExecuteOnDidWithRelationships(
sdk.UnwrapSDKContext(goCtx), &k.Keeper,
newConstraints(types.Authentication),
msg.Id, msg.Signer,
func(didDoc *types.IidDocument) error {
return didDoc.AddControllers(msg.ControllerDid)
}); err != nil {
return nil, err
}
return &types.MsgAddControllerResponse{}, nil
}
func (k msgServer) DeleteController(goCtx context.Context, msg *types.MsgDeleteController,
) (*types.MsgDeleteControllerResponse, error) {
if err := ExecuteOnDidWithRelationships(
sdk.UnwrapSDKContext(goCtx), &k.Keeper,
newConstraints(types.Authentication),
msg.Id, msg.Signer,
func(didDoc *types.IidDocument) error {
return didDoc.DeleteControllers(msg.ControllerDid)
}); err != nil {
return nil, err
}
return &types.MsgDeleteControllerResponse{}, nil
}
func (k msgServer) DeactivateIID(goCtx context.Context, msg *types.MsgDeactivateIID,
) (*types.MsgDeactivateIIDResponse, error) {
if err := ExecuteOnDidWithRelationships(
sdk.UnwrapSDKContext(goCtx), &k.Keeper,
newConstraints(types.Authentication),
msg.Id, msg.Signer, func(didDoc *types.IidDocument) error {
return didDoc.Deactivate()
}); err != nil {
return nil, err
}
return &types.MsgDeactivateIIDResponse{}, nil
}
// VerificationRelationships for did document manipulation
type VerificationRelationships []string
func newConstraints(relationships ...string) VerificationRelationships {
return relationships
}
// Check the relations/controllers for did if have capabilities to modify did doc, do modifications and emit doc_updated event
func ExecuteOnDidWithRelationships(ctx sdk.Context, k *Keeper, constraints VerificationRelationships, did, signer string, update func(document *types.IidDocument) error) (err error) {
// get the did document
didDoc, found := k.GetDidDocument(ctx, []byte(did))
if !found {
err = sdkerrors.Wrapf(types.ErrDidDocumentNotFound, "did document at %s not found", did)
return
}
// Any verification method in the authentication relationship can update the DID document
if !didDoc.HasRelationship(types.NewBlockchainAccountID(signer), constraints...) {
// check also the controllers
if !didDoc.HasController(types.DID(signer)) {
// 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,
)
return
}
}
// apply the update
err = update(&didDoc)
if err != nil {
return
}
// update the Metadata
types.UpdateDidMetadata(didDoc.Metadata, ctx.TxBytes(), ctx.BlockTime())
// persist the did document
k.SetDidDocument(ctx, []byte(did), didDoc)
// fire the event
err = ctx.EventManager().EmitTypedEvent(types.NewIidDocumentUpdatedEvent(&didDoc))
return
}