-
Notifications
You must be signed in to change notification settings - Fork 1
/
msg_validation.go
175 lines (138 loc) · 4.33 KB
/
msg_validation.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
package did
import (
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
)
// --------------------------
// CREATE IDENTIFIER
// --------------------------
// ValidateBasic performs a basic check of the MsgCreateDidDocument fields.
func (msg MsgCreateDidDocument) ValidateBasic() error {
if !IsValidDID(msg.Id) {
return sdkerrors.Wrap(ErrInvalidDIDFormat, msg.Id)
}
if msg.Verifications == nil || len(msg.Verifications) == 0 {
return sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, "verifications are required")
}
for _, v := range msg.Verifications {
if err := ValidateVerification(v); err != nil {
return err
}
}
// services are optional
if msg.Services != nil {
for _, s := range msg.Services {
if err := ValidateService(s); err != nil {
return err
}
}
}
return nil
}
// --------------------------
// UPDATE IDENTIFIER
// --------------------------
// ValidateBasic performs a basic check of the MsgUpdateDidDocument fields.
func (msg MsgUpdateDidDocument) ValidateBasic() error {
if !IsValidDID(msg.Doc.Id) {
return sdkerrors.Wrap(ErrInvalidDIDFormat, msg.Doc.Id)
}
for _, c := range msg.Doc.Controller {
// if controller is set must be compliant
if !IsValidDID(c) {
return sdkerrors.Wrap(ErrInvalidDIDFormat, "controller validation error")
}
}
return nil
}
// --------------------------
// ADD VERIFICATION METHOD
// --------------------------
// ValidateBasic performs a basic check of the MsgAddVerification fields.
func (msg MsgAddVerification) ValidateBasic() error {
if !IsValidDID(msg.Id) {
return sdkerrors.Wrap(ErrInvalidDIDFormat, msg.Id)
}
return ValidateVerification(msg.Verification)
}
// --------------------------
// REVOKE VERIFICATION METHOD
// --------------------------
// ValidateBasic performs a basic check of the MsgRevokeVerification fields.
func (msg MsgRevokeVerification) ValidateBasic() error {
if !IsValidDID(msg.Id) {
return sdkerrors.Wrap(ErrInvalidDIDFormat, msg.Id)
}
if !IsValidDIDURL(msg.MethodId) {
return sdkerrors.Wrap(ErrInvalidDIDURLFormat, "verification method id validation error")
}
return nil
}
// --------------------------
// SET VERIFICATION RELATIONSHIPS
// --------------------------
// ValidateBasic performs a basic check of the MsgSetVerificationRelationships fields.
func (msg MsgSetVerificationRelationships) ValidateBasic() error {
if !IsValidDID(msg.Id) {
return sdkerrors.Wrap(ErrInvalidDIDFormat, msg.Id)
}
if !IsValidDIDURL(msg.MethodId) {
return sdkerrors.Wrap(ErrInvalidDIDURLFormat, "verification method id")
}
// there should be more then one relationship
if len(msg.Relationships) == 0 {
return sdkerrors.Wrap(ErrEmptyRelationships, "one ore more relationships is required")
}
return nil
}
// --------------------------
// ADD SERVICE
// --------------------------
// ValidateBasic performs a basic check of the MsgAddService fields.
func (msg MsgAddService) ValidateBasic() error {
if !IsValidDID(msg.Id) {
return sdkerrors.Wrap(ErrInvalidDIDFormat, msg.Id)
}
return ValidateService(msg.ServiceData)
}
// --------------------------
// DELETE SERVICE
// --------------------------
// ValidateBasic performs a basic check of the MsgDeleteService fields.
func (msg MsgDeleteService) ValidateBasic() error {
if !IsValidDID(msg.Id) {
return sdkerrors.Wrap(ErrInvalidDIDFormat, msg.Id)
}
if IsEmpty(msg.ServiceId) {
return sdkerrors.Wrap(ErrInvalidInput, "service id cannot be empty;")
}
if !IsValidRFC3986Uri(msg.ServiceId) {
return sdkerrors.Wrap(ErrInvalidRFC3986UriFormat, "service id validation error")
}
return nil
}
// --------------------------
// ADD CONTROLLERS
// --------------------------
// ValidateBasic performs a basic check of the MsgAddService fields.
func (msg MsgAddController) ValidateBasic() error {
if !IsValidDID(msg.Id) {
return sdkerrors.Wrap(ErrInvalidDIDFormat, msg.Id)
}
if !IsValidDIDKeyFormat(msg.ControllerDid) {
return sdkerrors.Wrap(ErrInvalidDIDFormat, msg.ControllerDid)
}
return nil
}
// --------------------------
// DELETE CONTROLLERS
// --------------------------
// ValidateBasic performs a basic check of the MsgDeleteService fields.
func (msg MsgDeleteController) ValidateBasic() error {
if !IsValidDID(msg.Id) {
return sdkerrors.Wrap(ErrInvalidDIDFormat, msg.Id)
}
if !IsValidDID(msg.ControllerDid) {
return sdkerrors.Wrap(ErrInvalidDIDFormat, msg.ControllerDid)
}
return nil
}