-
Notifications
You must be signed in to change notification settings - Fork 30
/
msgs.go
190 lines (152 loc) · 5.2 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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
package types
import (
tmbytes "github.com/tendermint/tendermint/libs/bytes"
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"
SecretLength = 32 // length for the secret in bytes
HashLockLength = 32 // length for the hash lock in bytes
MaxLengthForAddressOnOtherChain = 128 // maximum length for the address on other chains
MinTimeLock = 50 // minimum time span for HTLC
MaxTimeLock = 25480 // maximum time span for HTLC
)
var (
_ sdk.Msg = &MsgCreateHTLC{}
_ sdk.Msg = &MsgClaimHTLC{}
_ sdk.Msg = &MsgRefundHTLC{}
)
// NewMsgCreateHTLC creates a new MsgCreateHTLC instance
func NewMsgCreateHTLC(
sender sdk.AccAddress,
to sdk.AccAddress,
receiverOnOtherChain string,
amount sdk.Coins,
hashLock tmbytes.HexBytes,
timestamp uint64,
timeLock uint64,
) MsgCreateHTLC {
return MsgCreateHTLC{
Sender: sender,
To: to,
ReceiverOnOtherChain: receiverOnOtherChain,
Amount: amount,
HashLock: hashLock,
Timestamp: timestamp,
TimeLock: timeLock,
}
}
// 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 msg.Sender.Empty() {
return sdkerrors.Wrap(sdkerrors.ErrInvalidAddress, "sender missing")
}
if len(msg.To) == 0 {
return sdkerrors.Wrap(sdkerrors.ErrInvalidAddress, "recipient missing")
}
if len(msg.ReceiverOnOtherChain) > MaxLengthForAddressOnOtherChain {
return sdkerrors.Wrapf(sdkerrors.ErrInvalidAddress, "length of the receiver on other chain must be between [0,%d]", MaxLengthForAddressOnOtherChain)
}
if !msg.Amount.IsValid() || !msg.Amount.IsAllPositive() {
return sdkerrors.Wrap(sdkerrors.ErrInvalidCoins, "the transferred amount must be valid")
}
if len(msg.HashLock) != HashLockLength {
return sdkerrors.Wrapf(ErrInvalidHashLock, "length of the hash lock must be %d in bytes", HashLockLength)
}
if msg.TimeLock < MinTimeLock || msg.TimeLock > MaxTimeLock {
return sdkerrors.Wrapf(ErrInvalidTimeLock, "the time lock must be between [%d,%d]", MinTimeLock, MaxTimeLock)
}
return nil
}
// 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 {
return []sdk.AccAddress{msg.Sender}
}
// -----------------------------------------------------------------------------
// NewMsgClaimHTLC constructs a new MsgClaimHTLC instance
func NewMsgClaimHTLC(
sender sdk.AccAddress,
hashLock tmbytes.HexBytes,
secret tmbytes.HexBytes,
) MsgClaimHTLC {
return MsgClaimHTLC{
Sender: sender,
HashLock: hashLock,
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 msg.Sender.Empty() {
return sdkerrors.Wrap(sdkerrors.ErrInvalidAddress, "sender missing")
}
if len(msg.HashLock) != HashLockLength {
return sdkerrors.Wrapf(ErrInvalidHashLock, "length of the hash lock must be %d in bytes", HashLockLength)
}
if len(msg.Secret) != SecretLength {
return sdkerrors.Wrapf(ErrInvalidSecret, "length of the secret must be %d in bytes", SecretLength)
}
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 {
return []sdk.AccAddress{msg.Sender}
}
// -----------------------------------------------------------------------------
// NewMsgRefundHTLC constructs a new MsgRefundHTLC instance
func NewMsgRefundHTLC(
sender sdk.AccAddress,
hashLock tmbytes.HexBytes,
) MsgRefundHTLC {
return MsgRefundHTLC{
Sender: sender,
HashLock: hashLock,
}
}
// Route implements Msg
func (msg MsgRefundHTLC) Route() string { return RouterKey }
// Type implements Msg
func (msg MsgRefundHTLC) Type() string { return TypeMsgRefundHTLC }
// ValidateBasic implements Msg
func (msg MsgRefundHTLC) ValidateBasic() error {
if msg.Sender.Empty() {
return sdkerrors.Wrap(sdkerrors.ErrInvalidAddress, "sender missing")
}
if len(msg.HashLock) != HashLockLength {
return sdkerrors.Wrapf(ErrInvalidHashLock, "length of the hash lock must be %d in bytes", HashLockLength)
}
return nil
}
// GetSignBytes implements Msg
func (msg MsgRefundHTLC) GetSignBytes() []byte {
bz := ModuleCdc.MustMarshalJSON(&msg)
return sdk.MustSortJSON(bz)
}
// GetSigners implements Msg
func (msg MsgRefundHTLC) GetSigners() []sdk.AccAddress {
return []sdk.AccAddress{msg.Sender}
}