/
msg.go
258 lines (228 loc) · 8.36 KB
/
msg.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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
package types
import (
"encoding/hex"
"errors"
"fmt"
"strings"
"github.com/tendermint/tendermint/crypto"
tmbytes "github.com/tendermint/tendermint/libs/bytes"
errorsmod "cosmossdk.io/errors"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
)
const (
CreateAtomicSwap = "createAtomicSwap"
ClaimAtomicSwap = "claimAtomicSwap"
RefundAtomicSwap = "refundAtomicSwap"
CalcSwapID = "calcSwapID"
Int64Size = 8
RandomNumberHashLength = 32
RandomNumberLength = 32
MaxOtherChainAddrLength = 64
SwapIDLength = 32
MaxExpectedIncomeLength = 64
)
// ensure Msg interface compliance at compile time
var (
_ sdk.Msg = &MsgCreateAtomicSwap{}
_ sdk.Msg = &MsgClaimAtomicSwap{}
_ sdk.Msg = &MsgRefundAtomicSwap{}
AtomicSwapCoinsAccAddr = sdk.AccAddress(crypto.AddressHash([]byte("FuryAtomicSwapCoins")))
)
// NewMsgCreateAtomicSwap initializes a new MsgCreateAtomicSwap
func NewMsgCreateAtomicSwap(from, to string, recipientOtherChain,
senderOtherChain string, randomNumberHash tmbytes.HexBytes, timestamp int64,
amount sdk.Coins, heightSpan uint64,
) MsgCreateAtomicSwap {
return MsgCreateAtomicSwap{
From: from,
To: to,
RecipientOtherChain: recipientOtherChain,
SenderOtherChain: senderOtherChain,
RandomNumberHash: randomNumberHash.String(),
Timestamp: timestamp,
Amount: amount,
HeightSpan: heightSpan,
}
}
// Route establishes the route for the MsgCreateAtomicSwap
func (msg MsgCreateAtomicSwap) Route() string { return RouterKey }
// Type is the name of MsgCreateAtomicSwap
func (msg MsgCreateAtomicSwap) Type() string { return CreateAtomicSwap }
// String prints the MsgCreateAtomicSwap
func (msg MsgCreateAtomicSwap) String() string {
return fmt.Sprintf("AtomicSwap{%v#%v#%v#%v#%v#%v#%v#%v}",
msg.From, msg.To, msg.RecipientOtherChain, msg.SenderOtherChain,
msg.RandomNumberHash, msg.Timestamp, msg.Amount, msg.HeightSpan)
}
// GetInvolvedAddresses gets the addresses involved in a MsgCreateAtomicSwap
func (msg MsgCreateAtomicSwap) GetInvolvedAddresses() []sdk.AccAddress {
return append(msg.GetSigners(), AtomicSwapCoinsAccAddr)
}
// GetSigners gets the signers of a MsgCreateAtomicSwap
func (msg MsgCreateAtomicSwap) GetSigners() []sdk.AccAddress {
from, err := sdk.AccAddressFromBech32(msg.From)
if err != nil {
panic(err)
}
return []sdk.AccAddress{from}
}
// ValidateBasic validates the MsgCreateAtomicSwap
func (msg MsgCreateAtomicSwap) ValidateBasic() error {
from, err := sdk.AccAddressFromBech32(msg.From)
if err != nil {
return errorsmod.Wrap(sdkerrors.ErrInvalidAddress, err.Error())
}
to, err := sdk.AccAddressFromBech32(msg.To)
if err != nil {
return errorsmod.Wrap(sdkerrors.ErrInvalidAddress, err.Error())
}
if from.Empty() {
return errorsmod.Wrap(sdkerrors.ErrInvalidAddress, "sender address cannot be empty")
}
if to.Empty() {
return errorsmod.Wrap(sdkerrors.ErrInvalidAddress, "recipient address cannot be empty")
}
if strings.TrimSpace(msg.RecipientOtherChain) == "" {
return errors.New("missing recipient address on other chain")
}
if len(msg.RecipientOtherChain) > MaxOtherChainAddrLength {
return fmt.Errorf("the length of recipient address on other chain should be less than %d", MaxOtherChainAddrLength)
}
if len(msg.SenderOtherChain) > MaxOtherChainAddrLength {
return fmt.Errorf("the length of sender address on other chain should be less than %d", MaxOtherChainAddrLength)
}
randomNumberHash, err := hex.DecodeString(msg.RandomNumberHash)
if err != nil {
return fmt.Errorf("random number hash should be valid hex: %v", err)
}
if len(randomNumberHash) != RandomNumberHashLength {
return fmt.Errorf("the length of random number hash should be %d", RandomNumberHashLength)
}
if msg.Timestamp <= 0 {
return errors.New("timestamp must be positive")
}
if len(msg.Amount) == 0 {
return errorsmod.Wrap(sdkerrors.ErrInvalidCoins, "amount cannot be empty")
}
if !msg.Amount.IsValid() {
return errorsmod.Wrap(sdkerrors.ErrInvalidCoins, msg.Amount.String())
}
if msg.HeightSpan <= 0 {
return errors.New("height span must be positive")
}
return nil
}
// GetSignBytes gets the sign bytes of a MsgCreateAtomicSwap
func (msg MsgCreateAtomicSwap) GetSignBytes() []byte {
bz := ModuleCdc.MustMarshalJSON(&msg)
return sdk.MustSortJSON(bz)
}
// NewMsgClaimAtomicSwap initializes a new MsgClaimAtomicSwap
func NewMsgClaimAtomicSwap(from string, swapID, randomNumber tmbytes.HexBytes) MsgClaimAtomicSwap {
return MsgClaimAtomicSwap{
From: from,
SwapID: swapID.String(),
RandomNumber: randomNumber.String(),
}
}
// Route establishes the route for the MsgClaimAtomicSwap
func (msg MsgClaimAtomicSwap) Route() string { return RouterKey }
// Type is the name of MsgClaimAtomicSwap
func (msg MsgClaimAtomicSwap) Type() string { return ClaimAtomicSwap }
// String prints the MsgClaimAtomicSwap
func (msg MsgClaimAtomicSwap) String() string {
return fmt.Sprintf("claimAtomicSwap{%v#%v#%v}", msg.From, msg.SwapID, msg.RandomNumber)
}
// GetInvolvedAddresses gets the addresses involved in a MsgClaimAtomicSwap
func (msg MsgClaimAtomicSwap) GetInvolvedAddresses() []sdk.AccAddress {
return append(msg.GetSigners(), AtomicSwapCoinsAccAddr)
}
// GetSigners gets the signers of a MsgClaimAtomicSwap
func (msg MsgClaimAtomicSwap) GetSigners() []sdk.AccAddress {
from, err := sdk.AccAddressFromBech32(msg.From)
if err != nil {
panic(err)
}
return []sdk.AccAddress{from}
}
// ValidateBasic validates the MsgClaimAtomicSwap
func (msg MsgClaimAtomicSwap) ValidateBasic() error {
from, err := sdk.AccAddressFromBech32(msg.From)
if err != nil {
return errorsmod.Wrap(sdkerrors.ErrInvalidAddress, err.Error())
}
if from.Empty() {
return errorsmod.Wrap(sdkerrors.ErrInvalidAddress, "sender address cannot be empty")
}
swapID, err := hex.DecodeString(msg.SwapID)
if err != nil {
return fmt.Errorf("swap id should be valid hex: %v", err)
}
if len(swapID) != SwapIDLength {
return fmt.Errorf("the length of swapID should be %d", SwapIDLength)
}
randomNumber, err := hex.DecodeString(msg.RandomNumber)
if err != nil {
return fmt.Errorf("random number should be valid hex: %v", err)
}
if len(randomNumber) != RandomNumberLength {
return fmt.Errorf("the length of random number should be %d", RandomNumberLength)
}
return nil
}
// GetSignBytes gets the sign bytes of a MsgClaimAtomicSwap
func (msg MsgClaimAtomicSwap) GetSignBytes() []byte {
bz := ModuleCdc.MustMarshalJSON(&msg)
return sdk.MustSortJSON(bz)
}
// NewMsgRefundAtomicSwap initializes a new MsgRefundAtomicSwap
func NewMsgRefundAtomicSwap(from string, swapID tmbytes.HexBytes) MsgRefundAtomicSwap {
return MsgRefundAtomicSwap{
From: from,
SwapID: swapID.String(),
}
}
// Route establishes the route for the MsgRefundAtomicSwap
func (msg MsgRefundAtomicSwap) Route() string { return RouterKey }
// Type is the name of MsgRefundAtomicSwap
func (msg MsgRefundAtomicSwap) Type() string { return RefundAtomicSwap }
// String prints the MsgRefundAtomicSwap
func (msg MsgRefundAtomicSwap) String() string {
return fmt.Sprintf("refundAtomicSwap{%v#%v}", msg.From, msg.SwapID)
}
// GetInvolvedAddresses gets the addresses involved in a MsgRefundAtomicSwap
func (msg MsgRefundAtomicSwap) GetInvolvedAddresses() []sdk.AccAddress {
return append(msg.GetSigners(), AtomicSwapCoinsAccAddr)
}
// GetSigners gets the signers of a MsgRefundAtomicSwap
func (msg MsgRefundAtomicSwap) GetSigners() []sdk.AccAddress {
from, err := sdk.AccAddressFromBech32(msg.From)
if err != nil {
panic(err)
}
return []sdk.AccAddress{from}
}
// ValidateBasic validates the MsgRefundAtomicSwap
func (msg MsgRefundAtomicSwap) ValidateBasic() error {
from, err := sdk.AccAddressFromBech32(msg.From)
if err != nil {
return errorsmod.Wrap(sdkerrors.ErrInvalidAddress, err.Error())
}
if from.Empty() {
return errorsmod.Wrap(sdkerrors.ErrInvalidAddress, "sender address cannot be empty")
}
swapID, err := hex.DecodeString(msg.SwapID)
if err != nil {
return fmt.Errorf("swap id should be valid hex: %v", err)
}
if len(swapID) != SwapIDLength {
return fmt.Errorf("the length of swapID should be %d", SwapIDLength)
}
return nil
}
// GetSignBytes gets the sign bytes of a MsgRefundAtomicSwap
func (msg MsgRefundAtomicSwap) GetSignBytes() []byte {
bz := ModuleCdc.MustMarshalJSON(&msg)
return sdk.MustSortJSON(bz)
}