-
Notifications
You must be signed in to change notification settings - Fork 3.6k
/
msgs.go
137 lines (115 loc) · 4.3 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
package feegrant
import (
"github.com/gogo/protobuf/proto"
"github.com/cosmos/cosmos-sdk/codec/legacy"
"github.com/cosmos/cosmos-sdk/codec/types"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/cosmos/cosmos-sdk/x/auth/migrations/legacytx"
)
var (
_, _ sdk.Msg = &MsgGrantAllowance{}, &MsgRevokeAllowance{}
_, _ legacytx.LegacyMsg = &MsgGrantAllowance{}, &MsgRevokeAllowance{} // For amino support.
_ types.UnpackInterfacesMessage = &MsgGrantAllowance{}
)
// NewMsgGrantAllowance creates a new MsgGrantAllowance.
//nolint:interfacer
func NewMsgGrantAllowance(feeAllowance FeeAllowanceI, granter, grantee sdk.AccAddress) (*MsgGrantAllowance, error) {
msg, ok := feeAllowance.(proto.Message)
if !ok {
return nil, sdkerrors.Wrapf(sdkerrors.ErrPackAny, "cannot proto marshal %T", msg)
}
any, err := types.NewAnyWithValue(msg)
if err != nil {
return nil, err
}
return &MsgGrantAllowance{
Granter: granter.String(),
Grantee: grantee.String(),
Allowance: any,
}, nil
}
// ValidateBasic implements the sdk.Msg interface.
func (msg MsgGrantAllowance) ValidateBasic() error {
if _, err := sdk.AccAddressFromBech32(msg.Granter); err != nil {
return sdkerrors.ErrInvalidAddress.Wrapf("invalid granter address: %s", err)
}
if _, err := sdk.AccAddressFromBech32(msg.Grantee); err != nil {
return sdkerrors.ErrInvalidAddress.Wrapf("invalid grantee address: %s", err)
}
if msg.Grantee == msg.Granter {
return sdkerrors.Wrap(sdkerrors.ErrInvalidAddress, "cannot self-grant fee authorization")
}
allowance, err := msg.GetFeeAllowanceI()
if err != nil {
return err
}
return allowance.ValidateBasic()
}
// GetSigners gets the granter account associated with an allowance
func (msg MsgGrantAllowance) GetSigners() []sdk.AccAddress {
granter, _ := sdk.AccAddressFromBech32(msg.Granter)
return []sdk.AccAddress{granter}
}
// Type implements the LegacyMsg.Type method.
func (msg MsgGrantAllowance) Type() string {
return sdk.MsgTypeURL(&msg)
}
// Route implements the LegacyMsg.Route method.
func (msg MsgGrantAllowance) Route() string {
return sdk.MsgTypeURL(&msg)
}
// GetSignBytes implements the LegacyMsg.GetSignBytes method.
func (msg MsgGrantAllowance) GetSignBytes() []byte {
return sdk.MustSortJSON(legacy.Cdc.MustMarshalJSON(&msg))
}
// GetFeeAllowanceI returns unpacked FeeAllowance
func (msg MsgGrantAllowance) GetFeeAllowanceI() (FeeAllowanceI, error) {
allowance, ok := msg.Allowance.GetCachedValue().(FeeAllowanceI)
if !ok {
return nil, sdkerrors.Wrap(ErrNoAllowance, "failed to get allowance")
}
return allowance, nil
}
// UnpackInterfaces implements UnpackInterfacesMessage.UnpackInterfaces
func (msg MsgGrantAllowance) UnpackInterfaces(unpacker types.AnyUnpacker) error {
var allowance FeeAllowanceI
return unpacker.UnpackAny(msg.Allowance, &allowance)
}
// NewMsgRevokeAllowance returns a message to revoke a fee allowance for a given
// granter and grantee
//nolint:interfacer
func NewMsgRevokeAllowance(granter sdk.AccAddress, grantee sdk.AccAddress) MsgRevokeAllowance {
return MsgRevokeAllowance{Granter: granter.String(), Grantee: grantee.String()}
}
// ValidateBasic implements the sdk.Msg interface.
func (msg MsgRevokeAllowance) ValidateBasic() error {
if _, err := sdk.AccAddressFromBech32(msg.Granter); err != nil {
return sdkerrors.ErrInvalidAddress.Wrapf("invalid granter address: %s", err)
}
if _, err := sdk.AccAddressFromBech32(msg.Grantee); err != nil {
return sdkerrors.ErrInvalidAddress.Wrapf("invalid grantee address: %s", err)
}
if msg.Grantee == msg.Granter {
return sdkerrors.Wrap(sdkerrors.ErrInvalidAddress, "addresses must be different")
}
return nil
}
// GetSigners gets the granter address associated with an Allowance
// to revoke.
func (msg MsgRevokeAllowance) GetSigners() []sdk.AccAddress {
granter, _ := sdk.AccAddressFromBech32(msg.Granter)
return []sdk.AccAddress{granter}
}
// Type implements the LegacyMsg.Type method.
func (msg MsgRevokeAllowance) Type() string {
return sdk.MsgTypeURL(&msg)
}
// Route implements the LegacyMsg.Route method.
func (msg MsgRevokeAllowance) Route() string {
return sdk.MsgTypeURL(&msg)
}
// GetSignBytes implements the LegacyMsg.GetSignBytes method.
func (msg MsgRevokeAllowance) GetSignBytes() []byte {
return sdk.MustSortJSON(legacy.Cdc.MustMarshalJSON(&msg))
}