github.com/cosmos/cosmos-sdk@v0.50.10/x/authz/msgs.go (about)

     1  package authz
     2  
     3  import (
     4  	"time"
     5  
     6  	"github.com/cosmos/gogoproto/proto"
     7  
     8  	cdctypes "github.com/cosmos/cosmos-sdk/codec/types"
     9  	sdk "github.com/cosmos/cosmos-sdk/types"
    10  	sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
    11  )
    12  
    13  var (
    14  	_ sdk.Msg = &MsgGrant{}
    15  	_ sdk.Msg = &MsgRevoke{}
    16  	_ sdk.Msg = &MsgExec{}
    17  
    18  	_ cdctypes.UnpackInterfacesMessage = &MsgGrant{}
    19  	_ cdctypes.UnpackInterfacesMessage = &MsgExec{}
    20  )
    21  
    22  // NewMsgGrant creates a new MsgGrant
    23  func NewMsgGrant(granter, grantee sdk.AccAddress, a Authorization, expiration *time.Time) (*MsgGrant, error) {
    24  	m := &MsgGrant{
    25  		Granter: granter.String(),
    26  		Grantee: grantee.String(),
    27  		Grant:   Grant{Expiration: expiration},
    28  	}
    29  	err := m.SetAuthorization(a)
    30  	if err != nil {
    31  		return nil, err
    32  	}
    33  	return m, nil
    34  }
    35  
    36  // GetAuthorization returns the cache value from the MsgGrant.Authorization if present.
    37  func (msg *MsgGrant) GetAuthorization() (Authorization, error) {
    38  	return msg.Grant.GetAuthorization()
    39  }
    40  
    41  // SetAuthorization converts Authorization to any and adds it to MsgGrant.Authorization.
    42  func (msg *MsgGrant) SetAuthorization(a Authorization) error {
    43  	m, ok := a.(proto.Message)
    44  	if !ok {
    45  		return sdkerrors.ErrPackAny.Wrapf("can't proto marshal %T", m)
    46  	}
    47  	any, err := cdctypes.NewAnyWithValue(m)
    48  	if err != nil {
    49  		return err
    50  	}
    51  	msg.Grant.Authorization = any
    52  	return nil
    53  }
    54  
    55  // UnpackInterfaces implements UnpackInterfacesMessage.UnpackInterfaces
    56  func (msg MsgExec) UnpackInterfaces(unpacker cdctypes.AnyUnpacker) error {
    57  	for _, x := range msg.Msgs {
    58  		var msgExecAuthorized sdk.Msg
    59  		err := unpacker.UnpackAny(x, &msgExecAuthorized)
    60  		if err != nil {
    61  			return err
    62  		}
    63  	}
    64  
    65  	return nil
    66  }
    67  
    68  // UnpackInterfaces implements UnpackInterfacesMessage.UnpackInterfaces
    69  func (msg MsgGrant) UnpackInterfaces(unpacker cdctypes.AnyUnpacker) error {
    70  	return msg.Grant.UnpackInterfaces(unpacker)
    71  }
    72  
    73  // NewMsgRevoke creates a new MsgRevoke
    74  func NewMsgRevoke(granter, grantee sdk.AccAddress, msgTypeURL string) MsgRevoke {
    75  	return MsgRevoke{
    76  		Granter:    granter.String(),
    77  		Grantee:    grantee.String(),
    78  		MsgTypeUrl: msgTypeURL,
    79  	}
    80  }
    81  
    82  // NewMsgExec creates a new MsgExecAuthorized
    83  func NewMsgExec(grantee sdk.AccAddress, msgs []sdk.Msg) MsgExec {
    84  	msgsAny := make([]*cdctypes.Any, len(msgs))
    85  	for i, msg := range msgs {
    86  		any, err := cdctypes.NewAnyWithValue(msg)
    87  		if err != nil {
    88  			panic(err)
    89  		}
    90  
    91  		msgsAny[i] = any
    92  	}
    93  
    94  	return MsgExec{
    95  		Grantee: grantee.String(),
    96  		Msgs:    msgsAny,
    97  	}
    98  }
    99  
   100  // GetMessages returns the cache values from the MsgExecAuthorized.Msgs if present.
   101  func (msg MsgExec) GetMessages() ([]sdk.Msg, error) {
   102  	msgs := make([]sdk.Msg, len(msg.Msgs))
   103  	for i, msgAny := range msg.Msgs {
   104  		msg, ok := msgAny.GetCachedValue().(sdk.Msg)
   105  		if !ok {
   106  			return nil, sdkerrors.ErrInvalidRequest.Wrapf("messages contains %T which is not a sdk.MsgRequest", msgAny)
   107  		}
   108  		msgs[i] = msg
   109  	}
   110  
   111  	return msgs, nil
   112  }