-
Notifications
You must be signed in to change notification settings - Fork 201
/
message_submit_tx.go
95 lines (75 loc) · 2.04 KB
/
message_submit_tx.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
package types
import (
fmt "fmt"
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
proto "github.com/gogo/protobuf/proto"
)
const TypeMsgSubmitTx = "submit_tx"
var _ sdk.Msg = &MsgSubmitTx{}
// NewMsgSubmitTx creates and returns a new MsgSubmitTx instance
func NewMsgSubmitTx(owner string, connectionID string, sdkMsg sdk.Msg) (*MsgSubmitTx, error) {
any, err := PackTxMsgAny(sdkMsg)
if err != nil {
return nil, err
}
return &MsgSubmitTx{
ConnectionId: connectionID,
Owner: owner,
Msg: any,
}, nil
}
func (msg *MsgSubmitTx) Route() string {
return RouterKey
}
func (msg *MsgSubmitTx) Type() string {
return TypeMsgSubmitTx
}
// PackTxMsgAny marshals the sdk.Msg payload to a protobuf Any type
func PackTxMsgAny(sdkMsg sdk.Msg) (*codectypes.Any, error) {
msg, ok := sdkMsg.(proto.Message)
if !ok {
return nil, fmt.Errorf("can't proto marshal %T", sdkMsg)
}
any, err := codectypes.NewAnyWithValue(msg)
if err != nil {
return nil, err
}
return any, nil
}
// UnpackInterfaces implements codectypes.UnpackInterfacesMessage
func (msg *MsgSubmitTx) UnpackInterfaces(unpacker codectypes.AnyUnpacker) error {
var (
sdkMsg sdk.Msg
)
return unpacker.UnpackAny(msg.Msg, &sdkMsg)
}
// GetTxMsg fetches the cached any message
func (msg *MsgSubmitTx) GetTxMsg() sdk.Msg {
sdkMsg, ok := msg.Msg.GetCachedValue().(sdk.Msg)
if !ok {
return nil
}
return sdkMsg
}
// GetSigners implements sdk.Msg
func (msg *MsgSubmitTx) GetSigners() []sdk.AccAddress {
accAddr, err := sdk.AccAddressFromBech32(msg.Owner)
if err != nil {
panic(err)
}
return []sdk.AccAddress{accAddr}
}
func (msg *MsgSubmitTx) GetSignBytes() []byte {
bz := ModuleCdc.MustMarshalJSON(msg)
return sdk.MustSortJSON(bz)
}
// ValidateBasic implements sdk.Msg
func (msg *MsgSubmitTx) ValidateBasic() error {
_, err := sdk.AccAddressFromBech32(msg.Owner)
if err != nil {
return sdkerrors.Wrap(sdkerrors.ErrInvalidAddress, "invalid owner address")
}
return nil
}