-
Notifications
You must be signed in to change notification settings - Fork 41
/
msg_server_create_diddoc.go
73 lines (58 loc) · 2.02 KB
/
msg_server_create_diddoc.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
package keeper
import (
"context"
"github.com/cheqd/cheqd-node/x/did/types"
"github.com/cheqd/cheqd-node/x/did/utils"
sdk "github.com/cosmos/cosmos-sdk/types"
)
func (k MsgServer) CreateDidDoc(goCtx context.Context, msg *types.MsgCreateDidDoc) (*types.MsgCreateDidDocResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)
// Get sign bytes before modifying payload
signBytes := msg.Payload.GetSignBytes()
// Normalize UUID identifiers
msg.Normalize()
// Validate DID doesn't exist
if k.HasDidDoc(&ctx, msg.Payload.Id) {
return nil, types.ErrDidDocExists.Wrap(msg.Payload.Id)
}
// Validate namespaces
namespace := k.GetDidNamespace(&ctx)
err := msg.Validate([]string{namespace})
if err != nil {
return nil, types.ErrNamespaceValidation.Wrap(err.Error())
}
// Build metadata and stateValue
didDoc := msg.Payload.ToDidDoc()
metadata := types.NewMetadataFromContext(ctx, msg.Payload.VersionId)
didDocWithMetadata := types.NewDidDocWithMetadata(&didDoc, &metadata)
// Consider did that we are going to create during did resolutions
inMemoryDids := map[string]types.DidDocWithMetadata{didDoc.Id: didDocWithMetadata}
// Check controllers' existence
controllers := didDoc.AllControllerDids()
for _, controller := range controllers {
_, err := MustFindDidDoc(&k.Keeper, &ctx, inMemoryDids, controller)
if err != nil {
return nil, err
}
}
// Verify signatures
signers := GetSignerDIDsForDIDCreation(didDoc)
err = VerifyAllSignersHaveAllValidSignatures(&k.Keeper, &ctx, inMemoryDids, signBytes, signers, msg.Signatures)
if err != nil {
return nil, err
}
// Save first DIDDoc version
err = k.AddNewDidDocVersion(&ctx, &didDocWithMetadata)
if err != nil {
return nil, types.ErrInternal.Wrapf(err.Error())
}
// Build and return response
return &types.MsgCreateDidDocResponse{
Value: &didDocWithMetadata,
}, nil
}
func GetSignerDIDsForDIDCreation(did types.DidDoc) []string {
res := did.GetControllersOrSubject()
res = append(res, did.GetVerificationMethodControllers()...)
return utils.UniqueSorted(res)
}