-
Notifications
You must be signed in to change notification settings - Fork 44
/
msgs_chain_links.go
173 lines (143 loc) · 4.93 KB
/
msgs_chain_links.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
package types
import (
"fmt"
"strings"
"cosmossdk.io/errors"
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
)
//nolint:interfacer
func NewMsgLinkChainAccount(
chainAddress AddressData,
proof Proof,
chainConfig ChainConfig,
signer string,
) *MsgLinkChainAccount {
addressAny, err := codectypes.NewAnyWithValue(chainAddress)
if err != nil {
panic("failed to pack public key to any type")
}
return &MsgLinkChainAccount{
ChainAddress: addressAny,
Proof: proof,
ChainConfig: chainConfig,
Signer: signer,
}
}
// Route should return the name of the module
func (msg *MsgLinkChainAccount) Route() string { return RouterKey }
// Type should return the action
func (msg *MsgLinkChainAccount) Type() string {
return ActionLinkChainAccount
}
// ValidateBasic runs stateless checks on the message
func (msg *MsgLinkChainAccount) ValidateBasic() error {
if msg.ChainAddress == nil {
return fmt.Errorf("source address cannot be nil")
}
if err := msg.Proof.Validate(); err != nil {
return err
}
if err := msg.ChainConfig.Validate(); err != nil {
return err
}
if _, err := sdk.AccAddressFromBech32(msg.Signer); err != nil {
return errors.Wrapf(sdkerrors.ErrInvalidAddress, "invalid destination address: %s", msg.Signer)
}
return nil
}
// GetSignBytes encodes the message for signing
func (msg *MsgLinkChainAccount) GetSignBytes() []byte {
return sdk.MustSortJSON(AminoCdc.MustMarshalJSON(msg))
}
// UnpackInterfaces implements codectypes.UnpackInterfacesMessage
func (msg *MsgLinkChainAccount) UnpackInterfaces(unpacker codectypes.AnyUnpacker) error {
var address AddressData
if err := unpacker.UnpackAny(msg.ChainAddress, &address); err != nil {
return err
}
if err := msg.Proof.UnpackInterfaces(unpacker); err != nil {
return err
}
return nil
}
// GetSigners defines whose signature is required
func (msg *MsgLinkChainAccount) GetSigners() []sdk.AccAddress {
signer, _ := sdk.AccAddressFromBech32(msg.Signer)
return []sdk.AccAddress{signer}
}
// ___________________________________________________________________________________________________________________
func NewMsgUnlinkChainAccount(owner, chainName, target string) *MsgUnlinkChainAccount {
return &MsgUnlinkChainAccount{
Owner: owner,
ChainName: chainName,
Target: target,
}
}
// Route should return the name of the module
func (msg *MsgUnlinkChainAccount) Route() string { return RouterKey }
// Type should return the action
func (msg *MsgUnlinkChainAccount) Type() string {
return ActionUnlinkChainAccount
}
// ValidateBasic runs stateless checks on the message
func (msg *MsgUnlinkChainAccount) ValidateBasic() error {
_, err := sdk.AccAddressFromBech32(msg.Owner)
if err != nil {
return errors.Wrap(sdkerrors.ErrInvalidAddress, "invalid owner")
}
if strings.TrimSpace(msg.ChainName) == "" {
return errors.Wrap(sdkerrors.ErrInvalidRequest, "chain name cannot be empty or blank")
}
if strings.TrimSpace(msg.Target) == "" {
return errors.Wrap(sdkerrors.ErrInvalidAddress, "invalid target")
}
return nil
}
// GetSignBytes encodes the message for signing
func (msg *MsgUnlinkChainAccount) GetSignBytes() []byte {
return sdk.MustSortJSON(AminoCdc.MustMarshalJSON(msg))
}
// GetSigners defines whose signature is required
func (msg *MsgUnlinkChainAccount) GetSigners() []sdk.AccAddress {
signer, _ := sdk.AccAddressFromBech32(msg.Owner)
return []sdk.AccAddress{signer}
}
// ___________________________________________________________________________________________________________________
func NewMsgSetDefaultExternalAddress(chainName, target, signer string) *MsgSetDefaultExternalAddress {
return &MsgSetDefaultExternalAddress{
ChainName: chainName,
Target: target,
Signer: signer,
}
}
// Route should return the name of the module
func (msg *MsgSetDefaultExternalAddress) Route() string { return RouterKey }
// Type should return the action
func (msg *MsgSetDefaultExternalAddress) Type() string {
return ActionSetDefaultExternalAddress
}
// ValidateBasic runs stateless checks on the message
func (msg *MsgSetDefaultExternalAddress) ValidateBasic() error {
if strings.TrimSpace(msg.ChainName) == "" {
return errors.Wrap(sdkerrors.ErrInvalidRequest, "chain name cannot be empty or blank")
}
if strings.TrimSpace(msg.Target) == "" {
return errors.Wrap(sdkerrors.ErrInvalidAddress, "invalid external address")
}
_, err := sdk.AccAddressFromBech32(msg.Signer)
if err != nil {
return errors.Wrap(sdkerrors.ErrInvalidAddress, "invalid signer")
}
return nil
}
// GetSignBytes encodes the message for signing
func (msg *MsgSetDefaultExternalAddress) GetSignBytes() []byte {
return sdk.MustSortJSON(AminoCdc.MustMarshalJSON(msg))
}
// GetSigners defines whose signature is required
func (msg *MsgSetDefaultExternalAddress) GetSigners() []sdk.AccAddress {
signer, _ := sdk.AccAddressFromBech32(msg.Signer)
return []sdk.AccAddress{signer}
}