-
Notifications
You must be signed in to change notification settings - Fork 40
/
msg.go
139 lines (124 loc) · 5.2 KB
/
msg.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
package types
import (
"net/url"
sdkerrors "cosmossdk.io/errors"
sdk "github.com/cosmos/cosmos-sdk/types"
hubtypes "github.com/sentinel-official/hub/v12/types"
)
// The `types` package contains custom message types for the Cosmos SDK.
// The following variables implement the sdk.Msg interface for MsgRegisterRequest and MsgUpdateRequest.
// These variables ensure that the corresponding types can be used as messages in the Cosmos SDK.
var (
_ sdk.Msg = (*MsgRegisterRequest)(nil)
_ sdk.Msg = (*MsgUpdateRequest)(nil)
)
// NewMsgRegisterRequest creates a new MsgRegisterRequest instance with the given parameters.
func NewMsgRegisterRequest(from sdk.AccAddress, name, identity, website, description string) *MsgRegisterRequest {
return &MsgRegisterRequest{
From: from.String(),
Name: name,
Identity: identity,
Website: website,
Description: description,
}
}
// ValidateBasic performs basic validation checks on the MsgRegisterRequest fields.
// It checks if the 'From' field is not empty and represents a valid account address,
// if the 'Name' field is not empty and its length is not greater than 64 characters,
// if the 'Identity' field's length is not greater than 64 characters,
// if the 'Website' field's length is not greater than 64 characters (if not empty),
// if the 'Website' field represents a valid URL (if not empty),
// and if the 'Description' field's length is not greater than 256 characters.
func (m *MsgRegisterRequest) ValidateBasic() error {
if m.From == "" {
return sdkerrors.Wrap(ErrorInvalidMessage, "from cannot be empty")
}
if _, err := sdk.AccAddressFromBech32(m.From); err != nil {
return sdkerrors.Wrap(ErrorInvalidMessage, err.Error())
}
if m.Name == "" {
return sdkerrors.Wrap(ErrorInvalidMessage, "name cannot be empty")
}
if len(m.Name) > 64 {
return sdkerrors.Wrapf(ErrorInvalidMessage, "name length cannot be greater than %d chars", 64)
}
if len(m.Identity) > 64 {
return sdkerrors.Wrapf(ErrorInvalidMessage, "identity length cannot be greater than %d chars", 64)
}
if len(m.Website) > 64 {
return sdkerrors.Wrapf(ErrorInvalidMessage, "website length cannot be greater than %d chars", 64)
}
if m.Website != "" {
if _, err := url.ParseRequestURI(m.Website); err != nil {
return sdkerrors.Wrap(ErrorInvalidMessage, err.Error())
}
}
if len(m.Description) > 256 {
return sdkerrors.Wrapf(ErrorInvalidMessage, "description length cannot be greater than %d chars", 256)
}
return nil
}
// GetSigners returns an array containing the signer's account address extracted from the 'From' field of the MsgRegisterRequest.
func (m *MsgRegisterRequest) GetSigners() []sdk.AccAddress {
from, err := sdk.AccAddressFromBech32(m.From)
if err != nil {
panic(err)
}
return []sdk.AccAddress{from}
}
// NewMsgUpdateRequest creates a new MsgUpdateRequest instance with the given parameters.
func NewMsgUpdateRequest(from hubtypes.ProvAddress, name, identity, website, description string, status hubtypes.Status) *MsgUpdateRequest {
return &MsgUpdateRequest{
From: from.String(),
Name: name,
Identity: identity,
Website: website,
Description: description,
Status: status,
}
}
// ValidateBasic performs basic validation checks on the MsgUpdateRequest fields.
// It checks if the 'From' field is not empty and represents a valid provider address,
// if the 'Name' field's length is not greater than 64 characters,
// if the 'Identity' field's length is not greater than 64 characters,
// if the 'Website' field's length is not greater than 64 characters (if not empty),
// if the 'Website' field represents a valid URL (if not empty),
// if the 'Description' field's length is not greater than 256 characters,
// and if the 'Status' field is one of the allowed values [unspecified, active, inactive].
func (m *MsgUpdateRequest) ValidateBasic() error {
if m.From == "" {
return sdkerrors.Wrap(ErrorInvalidMessage, "from cannot be empty")
}
if _, err := hubtypes.ProvAddressFromBech32(m.From); err != nil {
return sdkerrors.Wrap(ErrorInvalidMessage, err.Error())
}
if len(m.Name) > 64 {
return sdkerrors.Wrapf(ErrorInvalidMessage, "name length cannot be greater than %d chars", 64)
}
if len(m.Identity) > 64 {
return sdkerrors.Wrapf(ErrorInvalidMessage, "identity length cannot be greater than %d chars", 64)
}
if len(m.Website) > 64 {
return sdkerrors.Wrapf(ErrorInvalidMessage, "website length cannot be greater than %d chars", 64)
}
if m.Website != "" {
if _, err := url.ParseRequestURI(m.Website); err != nil {
return sdkerrors.Wrap(ErrorInvalidMessage, err.Error())
}
}
if len(m.Description) > 256 {
return sdkerrors.Wrapf(ErrorInvalidMessage, "description length cannot be greater than %d chars", 256)
}
if !m.Status.IsOneOf(hubtypes.StatusUnspecified, hubtypes.StatusActive, hubtypes.StatusInactive) {
return sdkerrors.Wrap(ErrorInvalidMessage, "status must be one of [unspecified, active, inactive]")
}
return nil
}
// GetSigners returns an array containing the signer's account address extracted from the 'From' field of the MsgUpdateRequest.
func (m *MsgUpdateRequest) GetSigners() []sdk.AccAddress {
from, err := hubtypes.ProvAddressFromBech32(m.From)
if err != nil {
panic(err)
}
return []sdk.AccAddress{from.Bytes()}
}