github.com/fibonacci-chain/fbc@v0.0.0-20231124064014-c7636198c1e9/x/icamauth/types/msgs.go (about)

     1  package types
     2  
     3  import (
     4  	fmt "fmt"
     5  	"strings"
     6  
     7  	"github.com/fibonacci-chain/fbc/libs/ibc-go/modules/apps/common"
     8  
     9  	sdk "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/types"
    10  
    11  	codectypes "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/codec/types"
    12  	sdkerrors "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/types/errors"
    13  	proto "github.com/gogo/protobuf/proto"
    14  )
    15  
    16  var (
    17  	_ sdk.Msg             = &MsgRegisterAccount{}
    18  	_ sdk.HeightSensitive = MsgRegisterAccount{}
    19  	_ sdk.Msg             = &MsgSubmitTx{}
    20  	_ sdk.HeightSensitive = MsgSubmitTx{}
    21  
    22  	_ codectypes.UnpackInterfacesMessage = MsgSubmitTx{}
    23  )
    24  
    25  // NewMsgRegisterAccount creates a new MsgRegisterAccount instance
    26  func NewMsgRegisterAccount(owner, connectionID, version string) *MsgRegisterAccount {
    27  	return &MsgRegisterAccount{
    28  		Owner:        owner,
    29  		ConnectionId: connectionID,
    30  		Version:      version,
    31  	}
    32  }
    33  
    34  // ValidateBasic implements sdk.Msg
    35  func (msg MsgRegisterAccount) ValidateBasic() error {
    36  	if strings.TrimSpace(msg.Owner) == "" {
    37  		return sdkerrors.Wrap(sdkerrors.ErrInvalidAddress, "missing sender address")
    38  	}
    39  
    40  	if _, err := sdk.AccAddressFromBech32(msg.Owner); err != nil {
    41  		return sdkerrors.Wrapf(sdkerrors.ErrInvalidAddress, "failed to parse address: %s", msg.Owner)
    42  	}
    43  
    44  	return nil
    45  }
    46  
    47  // GetSigners implements sdk.Msg
    48  func (msg MsgRegisterAccount) GetSigners() []sdk.AccAddress {
    49  	accAddr, err := sdk.AccAddressFromBech32(msg.Owner)
    50  	if err != nil {
    51  		panic(err)
    52  	}
    53  
    54  	return []sdk.AccAddress{accAddr}
    55  }
    56  
    57  func (m MsgRegisterAccount) Route() string {
    58  	return RouterKey
    59  }
    60  
    61  func (m MsgRegisterAccount) Type() string {
    62  	return sdk.MsgTypeURL(&m)
    63  }
    64  
    65  func (m MsgRegisterAccount) GetSignBytes() []byte {
    66  	return sdk.MustSortJSON(ModuleCdc.MustMarshalJSON(&m))
    67  }
    68  
    69  // NewMsgSubmitTx creates and returns a new MsgSubmitTx instance
    70  func NewMsgSubmitTx(sdkMsg sdk.Msg, connectionID, owner string) (*MsgSubmitTx, error) {
    71  	any, err := PackTxMsgAny(sdkMsg)
    72  	if err != nil {
    73  		return nil, err
    74  	}
    75  
    76  	return &MsgSubmitTx{
    77  		ConnectionId: connectionID,
    78  		Owner:        owner,
    79  		Msg:          any,
    80  	}, nil
    81  }
    82  
    83  // PackTxMsgAny marshals the sdk.Msg payload to a protobuf Any type
    84  func PackTxMsgAny(sdkMsg sdk.Msg) (*codectypes.Any, error) {
    85  	msg, ok := sdkMsg.(proto.Message)
    86  	if !ok {
    87  		return nil, fmt.Errorf("can't proto marshal %T", sdkMsg)
    88  	}
    89  
    90  	any, err := codectypes.NewAnyWithValue(msg)
    91  	if err != nil {
    92  		return nil, err
    93  	}
    94  
    95  	return any, nil
    96  }
    97  
    98  // UnpackInterfaces implements codectypes.UnpackInterfacesMessage
    99  func (msg MsgSubmitTx) UnpackInterfaces(unpacker codectypes.AnyUnpacker) error {
   100  	var sdkMsg sdk.Msg
   101  
   102  	return unpacker.UnpackAny(msg.Msg, &sdkMsg)
   103  }
   104  
   105  // GetTxMsg fetches the cached any message
   106  func (msg *MsgSubmitTx) GetTxMsg() sdk.MsgAdapter {
   107  	sdkMsg, ok := msg.Msg.GetCachedValue().(sdk.MsgAdapter)
   108  	if !ok {
   109  		return nil
   110  	}
   111  
   112  	return sdkMsg
   113  }
   114  
   115  // GetSigners implements sdk.Msg
   116  func (msg MsgSubmitTx) GetSigners() []sdk.AccAddress {
   117  	accAddr, err := sdk.AccAddressFromBech32(msg.Owner)
   118  	if err != nil {
   119  		panic(err)
   120  	}
   121  
   122  	return []sdk.AccAddress{accAddr}
   123  }
   124  
   125  // ValidateBasic implements sdk.Msg
   126  func (msg MsgSubmitTx) ValidateBasic() error {
   127  	_, err := sdk.AccAddressFromBech32(msg.Owner)
   128  	if err != nil {
   129  		return sdkerrors.Wrap(sdkerrors.ErrInvalidAddress, "invalid owner address")
   130  	}
   131  
   132  	return nil
   133  }
   134  
   135  func (m MsgSubmitTx) Route() string {
   136  	return RouterKey
   137  }
   138  
   139  func (m MsgSubmitTx) Type() string {
   140  	return sdk.MsgTypeURL(&m)
   141  }
   142  
   143  func (m *MsgSubmitTx) GetSignBytes() []byte {
   144  	panic("MsgSubmitTx messages do not support amino")
   145  }
   146  
   147  // ////////
   148  func (msg MsgRegisterAccount) ValidWithHeight(h int64) error {
   149  	return common.MsgNotSupportBeforeHeight(&msg, h)
   150  }
   151  
   152  func (msg MsgSubmitTx) ValidWithHeight(h int64) error {
   153  	return common.MsgNotSupportBeforeHeight(&msg, h)
   154  }