-
Notifications
You must be signed in to change notification settings - Fork 42
/
msg_server.go
144 lines (114 loc) · 4.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
package keeper
import (
"github.com/cheqd/cheqd-node/x/did/types"
"github.com/cheqd/cheqd-node/x/did/utils"
sdk "github.com/cosmos/cosmos-sdk/types"
)
type MsgServer struct {
Keeper
}
// NewMsgServer returns an implementation of the MsgServer interface for the provided Keeper.
func NewMsgServer(keeper Keeper) types.MsgServer {
return &MsgServer{Keeper: keeper}
}
var _ types.MsgServer = MsgServer{}
func FindDidDoc(k *Keeper, ctx *sdk.Context, inMemoryDIDs map[string]types.DidDocWithMetadata, did string) (res types.DidDocWithMetadata, found bool, err error) {
// Look in inMemory dict
value, found := inMemoryDIDs[did]
if found {
return value, true, nil
}
// Look in state
if k.HasDidDoc(ctx, did) {
value, err := k.GetLatestDidDoc(ctx, did)
if err != nil {
return types.DidDocWithMetadata{}, false, err
}
return value, true, nil
}
return types.DidDocWithMetadata{}, false, nil
}
func MustFindDidDoc(k *Keeper, ctx *sdk.Context, inMemoryDIDDocs map[string]types.DidDocWithMetadata, did string) (res types.DidDocWithMetadata, err error) {
res, found, err := FindDidDoc(k, ctx, inMemoryDIDDocs, did)
if err != nil {
return types.DidDocWithMetadata{}, err
}
if !found {
return types.DidDocWithMetadata{}, types.ErrDidDocNotFound.Wrap(did)
}
return res, nil
}
func FindVerificationMethod(k *Keeper, ctx *sdk.Context, inMemoryDIDs map[string]types.DidDocWithMetadata, didURL string) (res types.VerificationMethod, found bool, err error) {
did, _, _, _ := utils.MustSplitDIDUrl(didURL)
didDoc, found, err := FindDidDoc(k, ctx, inMemoryDIDs, did)
if err != nil || !found {
return types.VerificationMethod{}, found, err
}
for _, vm := range didDoc.DidDoc.VerificationMethod {
if vm.Id == didURL {
return *vm, true, nil
}
}
return types.VerificationMethod{}, false, nil
}
func MustFindVerificationMethod(k *Keeper, ctx *sdk.Context, inMemoryDIDs map[string]types.DidDocWithMetadata, didURL string) (res types.VerificationMethod, err error) {
res, found, err := FindVerificationMethod(k, ctx, inMemoryDIDs, didURL)
if err != nil {
return types.VerificationMethod{}, err
}
if !found {
return types.VerificationMethod{}, types.ErrVerificationMethodNotFound.Wrap(didURL)
}
return res, nil
}
func VerifySignature(k *Keeper, ctx *sdk.Context, inMemoryDIDs map[string]types.DidDocWithMetadata, message []byte, signature types.SignInfo) error {
verificationMethod, err := MustFindVerificationMethod(k, ctx, inMemoryDIDs, signature.VerificationMethodId)
if err != nil {
return err
}
err = types.VerifySignature(verificationMethod, message, signature.Signature)
if err != nil {
return types.ErrInvalidSignature.Wrapf("method id: %s", signature.VerificationMethodId)
}
return nil
}
func VerifyAllSignersHaveAllValidSignatures(k *Keeper, ctx *sdk.Context, inMemoryDIDs map[string]types.DidDocWithMetadata, message []byte, signers []string, signatures []*types.SignInfo) error {
for _, signer := range signers {
signatures := types.FindSignInfosBySigner(signatures, signer)
if len(signatures) == 0 {
return types.ErrSignatureNotFound.Wrapf("signer: %s", signer)
}
for _, signature := range signatures {
err := VerifySignature(k, ctx, inMemoryDIDs, message, signature)
if err != nil {
return err
}
}
}
return nil
}
// VerifyAllSignersHaveAtLeastOneValidSignature verifies that all signers have at least one valid signature.
// Omit didToBeUpdated and updatedDID if not updating a DID. Otherwise those values will be used to better format error messages.
func VerifyAllSignersHaveAtLeastOneValidSignature(k *Keeper, ctx *sdk.Context, inMemoryDIDs map[string]types.DidDocWithMetadata,
message []byte, signers []string, signatures []*types.SignInfo, didToBeUpdated string, updatedDID string,
) error {
for _, signer := range signers {
signaturesBySigner := types.FindSignInfosBySigner(signatures, signer)
signerForErrorMessage := GetSignerIDForErrorMessage(signer, didToBeUpdated, updatedDID)
if len(signaturesBySigner) == 0 {
return types.ErrSignatureNotFound.Wrapf("there should be at least one signature by %s", signerForErrorMessage)
}
found := false
for _, signature := range signaturesBySigner {
err := VerifySignature(k, ctx, inMemoryDIDs, message, signature)
if err == nil {
found = true
break
}
}
if !found {
return types.ErrInvalidSignature.Wrapf("there should be at least one valid signature by %s", signerForErrorMessage)
}
}
return nil
}