-
Notifications
You must be signed in to change notification settings - Fork 28
/
msgs.go
172 lines (143 loc) · 4.29 KB
/
msgs.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
package types
import (
errorsmod "cosmossdk.io/errors"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
)
const (
// TypeMsgCreateHTLC is the type for MsgCreateHTLC
TypeMsgCreateHTLC = "create_htlc"
// TypeMsgClaimHTLC is the type for MsgClaimHTLC
TypeMsgClaimHTLC = "claim_htlc"
// TypeMsgRefundHTLC is the type for MsgRefundHTLC
TypeMsgRefundHTLC = "refund_htlc"
)
var (
_ sdk.Msg = &MsgCreateHTLC{}
_ sdk.Msg = &MsgClaimHTLC{}
_ sdk.Msg = &MsgUpdateParams{}
)
// NewMsgCreateHTLC creates a new MsgCreateHTLC instance
func NewMsgCreateHTLC(
sender string,
to string,
receiverOnOtherChain string,
senderOnOtherChain string,
amount sdk.Coins,
hashLock string,
timestamp uint64,
timeLock uint64,
transfer bool,
) MsgCreateHTLC {
return MsgCreateHTLC{
Sender: sender,
To: to,
ReceiverOnOtherChain: receiverOnOtherChain,
SenderOnOtherChain: senderOnOtherChain,
Amount: amount,
HashLock: hashLock,
Timestamp: timestamp,
TimeLock: timeLock,
Transfer: transfer,
}
}
// Route implements Msg
func (msg MsgCreateHTLC) Route() string { return RouterKey }
// Type implements Msg
func (msg MsgCreateHTLC) Type() string { return TypeMsgCreateHTLC }
// ValidateBasic implements Msg
func (msg MsgCreateHTLC) ValidateBasic() error {
if _, err := sdk.AccAddressFromBech32(msg.Sender); err != nil {
return errorsmod.Wrapf(sdkerrors.ErrInvalidAddress, "invalid sender address (%s)", err)
}
if _, err := sdk.AccAddressFromBech32(msg.To); err != nil {
return errorsmod.Wrapf(sdkerrors.ErrInvalidAddress, "invalid recipient address (%s)", err)
}
if err := ValidateReceiverOnOtherChain(msg.ReceiverOnOtherChain); err != nil {
return err
}
if err := ValidateSenderOnOtherChain(msg.SenderOnOtherChain); err != nil {
return err
}
if err := ValidateAmount(msg.Transfer, msg.Amount); err != nil {
return err
}
if err := ValidateID(msg.HashLock); err != nil {
return err
}
return ValidateTimeLock(msg.TimeLock)
}
// GetSignBytes implements Msg
func (msg MsgCreateHTLC) GetSignBytes() []byte {
bz := ModuleCdc.MustMarshalJSON(&msg)
return sdk.MustSortJSON(bz)
}
// GetSigners implements Msg
func (msg MsgCreateHTLC) GetSigners() []sdk.AccAddress {
from, err := sdk.AccAddressFromBech32(msg.Sender)
if err != nil {
panic(err)
}
return []sdk.AccAddress{from}
}
// -----------------------------------------------------------------------------
// NewMsgClaimHTLC constructs a new MsgClaimHTLC instance
func NewMsgClaimHTLC(
sender string,
id string,
secret string,
) MsgClaimHTLC {
return MsgClaimHTLC{
Sender: sender,
Id: id,
Secret: secret,
}
}
// Route implements Msg
func (msg MsgClaimHTLC) Route() string { return RouterKey }
// Type implements Msg
func (msg MsgClaimHTLC) Type() string { return TypeMsgClaimHTLC }
// ValidateBasic implements Msg.
func (msg MsgClaimHTLC) ValidateBasic() error {
if _, err := sdk.AccAddressFromBech32(msg.Sender); err != nil {
return errorsmod.Wrapf(sdkerrors.ErrInvalidAddress, "invalid sender address (%s)", err)
}
if err := ValidateID(msg.Id); err != nil {
return err
}
if err := ValidateSecret(msg.Secret); err != nil {
return err
}
return nil
}
// GetSignBytes implements Msg
func (msg MsgClaimHTLC) GetSignBytes() []byte {
bz := ModuleCdc.MustMarshalJSON(&msg)
return sdk.MustSortJSON(bz)
}
// GetSigners implements Msg
func (msg MsgClaimHTLC) GetSigners() []sdk.AccAddress {
from, err := sdk.AccAddressFromBech32(msg.Sender)
if err != nil {
panic(err)
}
return []sdk.AccAddress{from}
}
// GetSignBytes returns the raw bytes for a MsgUpdateParams message that
// the expected signer needs to sign.
func (m *MsgUpdateParams) GetSignBytes() []byte {
bz := ModuleCdc.MustMarshalJSON(m)
return sdk.MustSortJSON(bz)
}
// ValidateBasic executes sanity validation on the provided data
func (m *MsgUpdateParams) ValidateBasic() error {
if _, err := sdk.AccAddressFromBech32(m.Authority); err != nil {
return errorsmod.Wrap(err, "invalid authority address")
}
return m.Params.Validate()
}
// GetSigners returns the expected signers for a MsgUpdateParams message
func (m *MsgUpdateParams) GetSigners() []sdk.AccAddress {
addr, _ := sdk.AccAddressFromBech32(m.Authority)
return []sdk.AccAddress{addr}
}