/
msgs.go
236 lines (210 loc) · 7.58 KB
/
msgs.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
package types
import (
"fmt"
"strings"
time "time"
"gopkg.in/yaml.v2"
sdk "github.com/cosmos/cosmos-sdk/types"
)
// allRequestMsgs defines all the Msg*Request messages.
var allRequestMsgs = []sdk.Msg{
(*MsgAddAttributeRequest)(nil),
(*MsgUpdateAttributeRequest)(nil),
(*MsgUpdateAttributeExpirationRequest)(nil),
(*MsgDeleteAttributeRequest)(nil),
(*MsgDeleteDistinctAttributeRequest)(nil),
(*MsgSetAccountDataRequest)(nil),
}
// NewMsgAddAttributeRequest creates a new add attribute message
func NewMsgAddAttributeRequest(account string, owner sdk.AccAddress, name string, attributeType AttributeType, value []byte) *MsgAddAttributeRequest {
return &MsgAddAttributeRequest{
Account: account,
Name: strings.ToLower(strings.TrimSpace(name)),
Owner: owner.String(),
AttributeType: attributeType,
Value: value,
}
}
// ValidateBasic runs stateless validation checks on the message.
func (msg MsgAddAttributeRequest) ValidateBasic() error {
if len(msg.Owner) == 0 {
return fmt.Errorf("empty owner address")
}
if _, err := sdk.AccAddressFromBech32(msg.Owner); err != nil {
return err
}
a := NewAttribute(msg.Name, msg.Account, msg.AttributeType, msg.Value, msg.ExpirationDate)
return a.ValidateBasic()
}
// GetSigners indicates that the message must have been signed by the name owner.
func (msg MsgAddAttributeRequest) GetSigners() []sdk.AccAddress {
addr, err := sdk.AccAddressFromBech32(msg.Owner)
if err != nil {
panic(fmt.Errorf("invalid owner value on message: %w", err))
}
return []sdk.AccAddress{addr}
}
// String implements stringer interface
func (msg MsgAddAttributeRequest) String() string {
out, _ := yaml.Marshal(msg)
return string(out)
}
// NewMsgUpdateAttributeRequest creates a new add attribute message
func NewMsgUpdateAttributeRequest(account string, owner sdk.AccAddress, name string, originalValue []byte, updateValue []byte, origAttrType AttributeType, updatedAttrType AttributeType) *MsgUpdateAttributeRequest {
return &MsgUpdateAttributeRequest{
Account: account,
Name: strings.ToLower(strings.TrimSpace(name)),
Owner: owner.String(),
OriginalValue: originalValue,
UpdateValue: updateValue,
OriginalAttributeType: origAttrType,
UpdateAttributeType: updatedAttrType,
}
}
// ValidateBasic runs stateless validation checks on the message.
func (msg MsgUpdateAttributeRequest) ValidateBasic() error {
if len(msg.Owner) == 0 {
return fmt.Errorf("empty owner address")
}
if _, err := sdk.AccAddressFromBech32(msg.Owner); err != nil {
return err
}
a := NewAttribute(msg.Name, msg.Account, msg.UpdateAttributeType, msg.UpdateValue, nil)
return a.ValidateBasic()
}
// GetSigners indicates that the message must have been signed by the name owner.
func (msg MsgUpdateAttributeRequest) GetSigners() []sdk.AccAddress {
addr, err := sdk.AccAddressFromBech32(msg.Owner)
if err != nil {
panic(fmt.Errorf("invalid owner value on message: %w", err))
}
return []sdk.AccAddress{addr}
}
// String implements stringer interface
func (msg MsgUpdateAttributeRequest) String() string {
out, _ := yaml.Marshal(msg)
return string(out)
}
// NewMsgUpdateAttributeRequest creates a new add attribute message
func NewMsgUpdateAttributeExpirationRequest(account, name, value string, expirationDate *time.Time, owner sdk.AccAddress) *MsgUpdateAttributeExpirationRequest {
return &MsgUpdateAttributeExpirationRequest{
Account: account,
Name: strings.ToLower(strings.TrimSpace(name)),
Value: []byte(value),
ExpirationDate: expirationDate,
Owner: owner.String(),
}
}
// ValidateBasic runs stateless validation checks on the message.
func (msg MsgUpdateAttributeExpirationRequest) ValidateBasic() error {
if _, err := sdk.AccAddressFromBech32(msg.Owner); err != nil {
return err
}
if strings.TrimSpace(msg.Name) == "" {
return fmt.Errorf("invalid name: empty")
}
if msg.Value == nil {
return fmt.Errorf("invalid value: nil")
}
err := ValidateAttributeAddress(msg.Account)
if err != nil {
return fmt.Errorf("invalid attribute address: %w", err)
}
return nil
}
// GetSigners indicates that the message must have been signed by the name owner.
func (msg MsgUpdateAttributeExpirationRequest) GetSigners() []sdk.AccAddress {
addr := sdk.MustAccAddressFromBech32(msg.Owner)
return []sdk.AccAddress{addr}
}
// NewMsgDeleteAttributeRequest deletes all attributes with specific name
func NewMsgDeleteAttributeRequest(account string, owner sdk.AccAddress, name string) *MsgDeleteAttributeRequest {
return &MsgDeleteAttributeRequest{
Account: account,
Name: strings.ToLower(strings.TrimSpace(name)),
Owner: owner.String(),
}
}
// ValidateBasic runs stateless validation checks on the message.
func (msg MsgDeleteAttributeRequest) ValidateBasic() error {
if strings.TrimSpace(msg.Name) == "" {
return fmt.Errorf("empty name")
}
if err := ValidateAttributeAddress(msg.Account); err != nil {
return fmt.Errorf("invalid account address: %w", err)
}
if len(msg.Owner) == 0 {
return fmt.Errorf("empty owner address")
}
if _, err := sdk.AccAddressFromBech32(msg.Owner); err != nil {
return err
}
return nil
}
// String implements stringer interface
func (msg MsgDeleteAttributeRequest) String() string {
out, _ := yaml.Marshal(msg)
return string(out)
}
// GetSigners indicates that the message must have been signed by the name owner.
func (msg MsgDeleteAttributeRequest) GetSigners() []sdk.AccAddress {
addr, err := sdk.AccAddressFromBech32(msg.Owner)
if err != nil {
panic(fmt.Errorf("invalid owner value on message: %w", err))
}
return []sdk.AccAddress{addr}
}
// NewMsgDeleteDistinctAttributeRequest deletes a attribute with specific value and type
func NewMsgDeleteDistinctAttributeRequest(account string, owner sdk.AccAddress, name string, value []byte) *MsgDeleteDistinctAttributeRequest {
return &MsgDeleteDistinctAttributeRequest{
Account: account,
Name: strings.ToLower(strings.TrimSpace(name)),
Owner: owner.String(),
Value: value,
}
}
// ValidateBasic runs stateless validation checks on the message.
func (msg MsgDeleteDistinctAttributeRequest) ValidateBasic() error {
if strings.TrimSpace(msg.Name) == "" {
return fmt.Errorf("empty name")
}
if len(msg.Value) == 0 {
return fmt.Errorf("empty value")
}
if err := ValidateAttributeAddress(msg.Account); err != nil {
return fmt.Errorf("invalid account address: %w", err)
}
if len(msg.Owner) == 0 {
return fmt.Errorf("empty owner address")
}
if _, err := sdk.AccAddressFromBech32(msg.Owner); err != nil {
return err
}
return nil
}
// String implements stringer interface
func (msg MsgDeleteDistinctAttributeRequest) String() string {
out, _ := yaml.Marshal(msg)
return string(out)
}
// GetSigners indicates that the message must have been signed by the name owner.
func (msg MsgDeleteDistinctAttributeRequest) GetSigners() []sdk.AccAddress {
addr, err := sdk.AccAddressFromBech32(msg.Owner)
if err != nil {
panic(fmt.Errorf("invalid owner value on message: %w", err))
}
return []sdk.AccAddress{addr}
}
// ValidateBasic runs stateless validation checks on the message.
func (msg MsgSetAccountDataRequest) ValidateBasic() error {
// This message is only for regular account addresses. No need to allow for scopes or others.
if _, err := sdk.AccAddressFromBech32(msg.Account); err != nil {
return fmt.Errorf("invalid account: %w", err)
}
return nil
}
// GetSigners indicates that the message must have been signed by the account.
func (msg MsgSetAccountDataRequest) GetSigners() []sdk.AccAddress {
addr := sdk.MustAccAddressFromBech32(msg.Account)
return []sdk.AccAddress{addr}
}