/
msg-HTLT.go
251 lines (226 loc) · 8.25 KB
/
msg-HTLT.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
package msg
import (
"encoding/json"
"fmt"
"github.com/Robbin-Liu/go-binance-sdk/common/types"
"github.com/tendermint/tendermint/crypto"
)
const (
AtomicSwapRoute = "atomicSwap"
HTLT = "HTLT"
DepositHTLT = "depositHTLT"
ClaimHTLT = "claimHTLT"
RefundHTLT = "refundHTLT"
Int64Size = 8
RandomNumberHashLength = 32
RandomNumberLength = 32
MaxOtherChainAddrLength = 64
SwapIDLength = 32
MaxExpectedIncomeLength = 64
MinimumHeightSpan = 360
MaximumHeightSpan = 518400
)
var (
// bnb prefix address: bnb1wxeplyw7x8aahy93w96yhwm7xcq3ke4f8ge93u
// tbnb prefix address: tbnb1wxeplyw7x8aahy93w96yhwm7xcq3ke4ffasp3d
AtomicSwapCoinsAccAddr = types.AccAddress(crypto.AddressHash([]byte("BinanceChainAtomicSwapCoins")))
)
type HTLTMsg struct {
From types.AccAddress `json:"from"`
To types.AccAddress `json:"to"`
RecipientOtherChain string `json:"recipient_other_chain"`
SenderOtherChain string `json:"sender_other_chain"`
RandomNumberHash types.SwapBytes `json:"random_number_hash"`
Timestamp int64 `json:"timestamp"`
Amount types.Coins `json:"amount"`
ExpectedIncome string `json:"expected_income"`
HeightSpan int64 `json:"height_span"`
CrossChain bool `json:"cross_chain"`
}
func NewHTLTMsg(from, to types.AccAddress, recipientOtherChain, senderOtherChain string, randomNumberHash types.SwapBytes, timestamp int64,
amount types.Coins, expectedIncome string, heightSpan int64, crossChain bool) HTLTMsg {
return HTLTMsg{
From: from,
To: to,
RecipientOtherChain: recipientOtherChain,
SenderOtherChain: senderOtherChain,
RandomNumberHash: randomNumberHash,
Timestamp: timestamp,
Amount: amount,
ExpectedIncome: expectedIncome,
HeightSpan: heightSpan,
CrossChain: crossChain,
}
}
func (msg HTLTMsg) Route() string { return AtomicSwapRoute }
func (msg HTLTMsg) Type() string { return HTLT }
func (msg HTLTMsg) String() string {
return fmt.Sprintf("HTLT{%v#%v#%v#%v#%v#%v#%v#%v#%v#%v}", msg.From, msg.To, msg.RecipientOtherChain, msg.SenderOtherChain, msg.RandomNumberHash,
msg.Timestamp, msg.Amount, msg.ExpectedIncome, msg.HeightSpan, msg.CrossChain)
}
func (msg HTLTMsg) GetInvolvedAddresses() []types.AccAddress {
return append(msg.GetSigners(), AtomicSwapCoinsAccAddr)
}
func (msg HTLTMsg) GetSigners() []types.AccAddress {
return []types.AccAddress{msg.From}
}
func (msg HTLTMsg) ValidateBasic() error {
if len(msg.From) != types.AddrLen {
return fmt.Errorf("the expected address length is %d, actual length is %d", types.AddrLen, len(msg.From))
}
if len(msg.To) != types.AddrLen {
return fmt.Errorf("the expected address length is %d, actual length is %d", types.AddrLen, len(msg.To))
}
if !msg.CrossChain && len(msg.RecipientOtherChain) != 0 {
return fmt.Errorf("must leave recipient address on other chain to empty for single chain swap")
}
if !msg.CrossChain && len(msg.SenderOtherChain) != 0 {
return fmt.Errorf("must leave sender address on other chain to empty for single chain swap")
}
if msg.CrossChain && len(msg.RecipientOtherChain) == 0 {
return fmt.Errorf("missing recipient address on other chain for cross chain swap")
}
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)
}
if len(msg.ExpectedIncome) > MaxExpectedIncomeLength {
return fmt.Errorf("the length of expected income should be less than %d", MaxExpectedIncomeLength)
}
if len(msg.RandomNumberHash) != RandomNumberHashLength {
return fmt.Errorf("the length of random number hash should be %d", RandomNumberHashLength)
}
if !msg.Amount.IsPositive() {
return fmt.Errorf("the swapped out coin must be positive")
}
if msg.HeightSpan < MinimumHeightSpan || msg.HeightSpan > MaximumHeightSpan {
return fmt.Errorf("the height span should be no less than 360 and no greater than 518400")
}
return nil
}
func (msg HTLTMsg) GetSignBytes() []byte {
b, err := json.Marshal(msg)
if err != nil {
panic(err)
}
return b
}
type DepositHTLTMsg struct {
From types.AccAddress `json:"from"`
Amount types.Coins `json:"amount"`
SwapID types.SwapBytes `json:"swap_id"`
}
func NewDepositHTLTMsg(from types.AccAddress, swapID []byte, amount types.Coins) DepositHTLTMsg {
return DepositHTLTMsg{
From: from,
SwapID: swapID,
Amount: amount,
}
}
func (msg DepositHTLTMsg) Route() string { return AtomicSwapRoute }
func (msg DepositHTLTMsg) Type() string { return DepositHTLT }
func (msg DepositHTLTMsg) String() string {
return fmt.Sprintf("depositHTLT{%v#%v#%v}", msg.From, msg.Amount, msg.SwapID)
}
func (msg DepositHTLTMsg) GetInvolvedAddresses() []types.AccAddress {
return append(msg.GetSigners(), AtomicSwapCoinsAccAddr)
}
func (msg DepositHTLTMsg) GetSigners() []types.AccAddress {
return []types.AccAddress{msg.From}
}
func (msg DepositHTLTMsg) ValidateBasic() error {
if len(msg.From) != types.AddrLen {
return fmt.Errorf("the expected address length is %d, actual length is %d", types.AddrLen, len(msg.From))
}
if len(msg.SwapID) != SwapIDLength {
return fmt.Errorf("the length of swapID should be %d", SwapIDLength)
}
if !msg.Amount.IsPositive() {
return fmt.Errorf("the swapped out coin must be positive")
}
return nil
}
func (msg DepositHTLTMsg) GetSignBytes() []byte {
b, err := json.Marshal(msg)
if err != nil {
panic(err)
}
return b
}
type ClaimHTLTMsg struct {
From types.AccAddress `json:"from"`
SwapID types.SwapBytes `json:"swap_id"`
RandomNumber types.SwapBytes `json:"random_number"`
}
func NewClaimHTLTMsg(from types.AccAddress, swapID, randomNumber []byte) ClaimHTLTMsg {
return ClaimHTLTMsg{
From: from,
SwapID: swapID,
RandomNumber: randomNumber,
}
}
func (msg ClaimHTLTMsg) Route() string { return AtomicSwapRoute }
func (msg ClaimHTLTMsg) Type() string { return ClaimHTLT }
func (msg ClaimHTLTMsg) String() string {
return fmt.Sprintf("claimHTLT{%v#%v#%v}", msg.From, msg.SwapID, msg.RandomNumber)
}
func (msg ClaimHTLTMsg) GetInvolvedAddresses() []types.AccAddress {
return append(msg.GetSigners(), AtomicSwapCoinsAccAddr)
}
func (msg ClaimHTLTMsg) GetSigners() []types.AccAddress { return []types.AccAddress{msg.From} }
func (msg ClaimHTLTMsg) ValidateBasic() error {
if len(msg.From) != types.AddrLen {
return fmt.Errorf("the expected address length is %d, actual length is %d", types.AddrLen, len(msg.From))
}
if len(msg.SwapID) != SwapIDLength {
return fmt.Errorf("the length of swapID should be %d", SwapIDLength)
}
if len(msg.RandomNumber) != RandomNumberLength {
return fmt.Errorf("the length of random number should be %d", RandomNumberLength)
}
return nil
}
func (msg ClaimHTLTMsg) GetSignBytes() []byte {
b, err := json.Marshal(msg)
if err != nil {
panic(err)
}
return b
}
type RefundHTLTMsg struct {
From types.AccAddress `json:"from"`
SwapID types.SwapBytes `json:"swap_id"`
}
func NewRefundHTLTMsg(from types.AccAddress, swapID []byte) RefundHTLTMsg {
return RefundHTLTMsg{
From: from,
SwapID: swapID,
}
}
func (msg RefundHTLTMsg) Route() string { return AtomicSwapRoute }
func (msg RefundHTLTMsg) Type() string { return RefundHTLT }
func (msg RefundHTLTMsg) String() string {
return fmt.Sprintf("refundHTLT{%v#%v}", msg.From, msg.SwapID)
}
func (msg RefundHTLTMsg) GetInvolvedAddresses() []types.AccAddress {
return append(msg.GetSigners(), AtomicSwapCoinsAccAddr)
}
func (msg RefundHTLTMsg) GetSigners() []types.AccAddress { return []types.AccAddress{msg.From} }
func (msg RefundHTLTMsg) ValidateBasic() error {
if len(msg.From) != types.AddrLen {
return fmt.Errorf("the expected address length is %d, actual length is %d", types.AddrLen, len(msg.From))
}
if len(msg.SwapID) != SwapIDLength {
return fmt.Errorf("the length of swapID should be %d", SwapIDLength)
}
return nil
}
func (msg RefundHTLTMsg) GetSignBytes() []byte {
b, err := json.Marshal(msg)
if err != nil {
panic(err)
}
return b
}