github.com/Finschia/finschia-sdk@v0.49.1/x/feegrant/msgs.go (about) 1 package feegrant 2 3 import ( 4 "github.com/gogo/protobuf/proto" 5 6 "github.com/Finschia/finschia-sdk/codec/types" 7 sdk "github.com/Finschia/finschia-sdk/types" 8 sdkerrors "github.com/Finschia/finschia-sdk/types/errors" 9 "github.com/Finschia/finschia-sdk/x/auth/legacy/legacytx" 10 ) 11 12 var ( 13 _, _ sdk.Msg = &MsgGrantAllowance{}, &MsgRevokeAllowance{} 14 _, _ legacytx.LegacyMsg = &MsgGrantAllowance{}, &MsgRevokeAllowance{} // For amino support. 15 16 _ types.UnpackInterfacesMessage = &MsgGrantAllowance{} 17 ) 18 19 // NewMsgGrantAllowance creates a new MsgGrantAllowance. 20 // 21 //nolint:interfacer 22 func NewMsgGrantAllowance(feeAllowance FeeAllowanceI, granter, grantee sdk.AccAddress) (*MsgGrantAllowance, error) { 23 msg, ok := feeAllowance.(proto.Message) 24 if !ok { 25 return nil, sdkerrors.Wrapf(sdkerrors.ErrPackAny, "cannot proto marshal %T", msg) 26 } 27 any, err := types.NewAnyWithValue(msg) 28 if err != nil { 29 return nil, err 30 } 31 32 return &MsgGrantAllowance{ 33 Granter: granter.String(), 34 Grantee: grantee.String(), 35 Allowance: any, 36 }, nil 37 } 38 39 // ValidateBasic implements the sdk.Msg interface. 40 func (msg MsgGrantAllowance) ValidateBasic() error { 41 if msg.Granter == "" { 42 return sdkerrors.Wrap(sdkerrors.ErrInvalidAddress, "missing granter address") 43 } 44 if msg.Grantee == "" { 45 return sdkerrors.Wrap(sdkerrors.ErrInvalidAddress, "missing grantee address") 46 } 47 if msg.Grantee == msg.Granter { 48 return sdkerrors.Wrap(sdkerrors.ErrInvalidAddress, "cannot self-grant fee authorization") 49 } 50 51 allowance, err := msg.GetFeeAllowanceI() 52 if err != nil { 53 return err 54 } 55 56 return allowance.ValidateBasic() 57 } 58 59 // GetSigners gets the granter account associated with an allowance 60 func (msg MsgGrantAllowance) GetSigners() []sdk.AccAddress { 61 granter, err := sdk.AccAddressFromBech32(msg.Granter) 62 if err != nil { 63 panic(err) 64 } 65 return []sdk.AccAddress{granter} 66 } 67 68 // Type implements the LegacyMsg.Type method. 69 func (msg MsgGrantAllowance) Type() string { 70 return sdk.MsgTypeURL(&msg) 71 } 72 73 // Route implements the LegacyMsg.Route method. 74 func (msg MsgGrantAllowance) Route() string { 75 return sdk.MsgTypeURL(&msg) 76 } 77 78 // GetSignBytes implements the LegacyMsg.GetSignBytes method. 79 func (msg MsgGrantAllowance) GetSignBytes() []byte { 80 return sdk.MustSortJSON(ModuleCdc.MustMarshalJSON(&msg)) 81 } 82 83 // GetFeeAllowanceI returns unpacked FeeAllowance 84 func (msg MsgGrantAllowance) GetFeeAllowanceI() (FeeAllowanceI, error) { 85 allowance, ok := msg.Allowance.GetCachedValue().(FeeAllowanceI) 86 if !ok { 87 return nil, sdkerrors.Wrap(ErrNoAllowance, "failed to get allowance") 88 } 89 90 return allowance, nil 91 } 92 93 // UnpackInterfaces implements UnpackInterfacesMessage.UnpackInterfaces 94 func (msg MsgGrantAllowance) UnpackInterfaces(unpacker types.AnyUnpacker) error { 95 var allowance FeeAllowanceI 96 return unpacker.UnpackAny(msg.Allowance, &allowance) 97 } 98 99 // NewMsgRevokeAllowance returns a message to revoke a fee allowance for a given 100 // granter and grantee 101 // 102 //nolint:interfacer 103 func NewMsgRevokeAllowance(granter, grantee sdk.AccAddress) MsgRevokeAllowance { 104 return MsgRevokeAllowance{Granter: granter.String(), Grantee: grantee.String()} 105 } 106 107 // ValidateBasic implements the sdk.Msg interface. 108 func (msg MsgRevokeAllowance) ValidateBasic() error { 109 if msg.Granter == "" { 110 return sdkerrors.Wrap(sdkerrors.ErrInvalidAddress, "missing granter address") 111 } 112 if msg.Grantee == "" { 113 return sdkerrors.Wrap(sdkerrors.ErrInvalidAddress, "missing grantee address") 114 } 115 if msg.Grantee == msg.Granter { 116 return sdkerrors.Wrap(sdkerrors.ErrInvalidAddress, "addresses must be different") 117 } 118 119 return nil 120 } 121 122 // GetSigners gets the granter address associated with an Allowance 123 // to revoke. 124 func (msg MsgRevokeAllowance) GetSigners() []sdk.AccAddress { 125 granter, err := sdk.AccAddressFromBech32(msg.Granter) 126 if err != nil { 127 panic(err) 128 } 129 return []sdk.AccAddress{granter} 130 } 131 132 // Type implements the LegacyMsg.Type method. 133 func (msg MsgRevokeAllowance) Type() string { 134 return sdk.MsgTypeURL(&msg) 135 } 136 137 // Route implements the LegacyMsg.Route method. 138 func (msg MsgRevokeAllowance) Route() string { 139 return sdk.MsgTypeURL(&msg) 140 } 141 142 // GetSignBytes implements the LegacyMsg.GetSignBytes method. 143 func (msg MsgRevokeAllowance) GetSignBytes() []byte { 144 return sdk.MustSortJSON(ModuleCdc.MustMarshalJSON(&msg)) 145 }