forked from noble-assets/paramauthority
/
msgs.go
124 lines (97 loc) · 3.41 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
package types
import (
codecTypes "github.com/cosmos/cosmos-sdk/codec/types"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/errors"
"github.com/cosmos/cosmos-sdk/types/tx"
"github.com/cosmos/cosmos-sdk/x/auth/migrations/legacytx"
"github.com/cosmos/cosmos-sdk/x/gov/types/v1beta1"
)
var (
_, _ legacytx.LegacyMsg = &MsgExecute{}, &MsgUpdateAuthority{}
_, _, _ sdk.Msg = &MsgExecute{}, &MsgExecuteLegacyContent{}, &MsgUpdateAuthority{}
_, _ codecTypes.UnpackInterfacesMessage = &MsgExecute{}, &MsgExecuteLegacyContent{}
)
// MsgExecute
func (m *MsgExecute) GetMsgs() ([]sdk.Msg, error) {
msgs, err := tx.GetMsgs(m.Messages, "")
if err != nil {
return nil, err
}
for idx, msg := range msgs {
signers := msg.GetSigners()
if len(signers) != 1 {
return nil, errors.ErrUnauthorized.Wrapf("msg %d (%s) must only have one signer", idx, sdk.MsgTypeURL(msg))
}
if !signers[0].Equals(ModuleAddress) {
return nil, errors.ErrUnauthorized.Wrapf("msg %d (%s) signer must be the authority address (%s)", idx, sdk.MsgTypeURL(msg), ModuleAddress)
}
if err := msg.ValidateBasic(); err != nil {
return nil, errors.ErrLogic.Wrapf("msg %d (%s) failed validation: %s", idx, sdk.MsgTypeURL(msg), err)
}
}
return msgs, nil
}
func (m *MsgExecute) GetSignBytes() []byte {
bz := ModuleCdc.MustMarshal(m)
return sdk.MustSortJSON(bz)
}
func (m *MsgExecute) GetSigners() []sdk.AccAddress {
authority, _ := sdk.AccAddressFromBech32(m.Authority)
return []sdk.AccAddress{authority}
}
func (m *MsgExecute) Route() string {
return ModuleName
}
func (m *MsgExecute) Type() string {
return "noble/x/authority/MsgExecute"
}
func (m *MsgExecute) UnpackInterfaces(unpacker codecTypes.AnyUnpacker) error {
return tx.UnpackInterfaces(unpacker, m.Messages)
}
func (m *MsgExecute) ValidateBasic() error {
if _, err := sdk.AccAddressFromBech32(m.Authority); err != nil {
return errors.ErrInvalidAddress.Wrapf("invalid authority address: %s", err)
}
_, err := m.GetMsgs()
return err
}
// MsgExecuteLegacyContent
func (m *MsgExecuteLegacyContent) GetSigners() []sdk.AccAddress {
authority, _ := sdk.AccAddressFromBech32(m.Authority)
return []sdk.AccAddress{authority}
}
func (m *MsgExecuteLegacyContent) UnpackInterfaces(unpacker codecTypes.AnyUnpacker) error {
var content v1beta1.Content
return unpacker.UnpackAny(m.Content, &content)
}
func (m *MsgExecuteLegacyContent) ValidateBasic() error {
if _, err := sdk.AccAddressFromBech32(m.Authority); err != nil {
return errors.ErrInvalidAddress.Wrapf("invalid authority address: %s", err)
}
return nil
}
// MsgUpdateAuthority
func (m *MsgUpdateAuthority) GetSignBytes() []byte {
bz := ModuleCdc.MustMarshal(m)
return sdk.MustSortJSON(bz)
}
func (m *MsgUpdateAuthority) GetSigners() []sdk.AccAddress {
authority, _ := sdk.AccAddressFromBech32(m.Authority)
return []sdk.AccAddress{authority}
}
func (m *MsgUpdateAuthority) Route() string {
return ModuleName
}
func (m *MsgUpdateAuthority) Type() string {
return "noble/x/authority/MsgUpdateAuthority"
}
func (m *MsgUpdateAuthority) ValidateBasic() error {
if _, err := sdk.AccAddressFromBech32(m.Authority); err != nil {
return errors.ErrInvalidAddress.Wrapf("invalid authority address: %s", err)
}
if _, err := sdk.AccAddressFromBech32(m.NewAuthority); err != nil {
return errors.ErrInvalidAddress.Wrapf("invalid new authority address: %s", err)
}
return nil
}