-
Notifications
You must be signed in to change notification settings - Fork 0
/
atomic_swap.go
377 lines (307 loc) · 10.5 KB
/
atomic_swap.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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
package commands
import (
"crypto/rand"
"encoding/hex"
"fmt"
"time"
sdk "github.com/aximchain/axc-cosmos-sdk/types"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"github.com/aximchain/flash-node/common"
"github.com/aximchain/flash-node/common/client"
"github.com/aximchain/flash-node/plugins/tokens/swap"
)
const (
flagCreatorAddr = "creator-addr"
flagRecipientAddr = "recipient-addr"
flagExpectedIncome = "expected-income"
flagRecipientOtherChain = "recipient-other-chain"
flagSenderOtherChain = "sender-other-chain"
flagRandomNumberHash = "random-number-hash"
flagRandomNumber = "random-number"
flagSwapID = "swap-id"
flagTimestamp = "timestamp"
flagHeightSpan = "height-span"
flagCrossChain = "cross-chain"
flagLimit = "limit"
flagOffset = "offset"
)
func initiateHTLTCmd(cmdr Commander) *cobra.Command {
cmd := &cobra.Command{
Use: "HTLT",
Short: "Create a hash timer lock transfer",
RunE: cmdr.initiateHTLT,
}
cmd.Flags().String(flagRecipientAddr, "", "The recipient address of BEP2 token, bech32 encoding")
cmd.Flags().String(flagAmount, "", "The swapped out amount BEP2 tokens, example: \"100:AXC\" or \"100:AXC,10000:BTCB-1DE\"")
cmd.Flags().String(flagExpectedIncome, "", "Expected income from swap counter party, example: \"100:AXC\" or \"100:AXC,10000:BTCB-1DE\"")
cmd.Flags().String(flagRecipientOtherChain, "", "The recipient address on other chain, like Ethereum, leave it empty for single chain swap")
cmd.Flags().String(flagSenderOtherChain, "", "The sender address on other chain, like Ethereum, leave it empty for single chain swap")
cmd.Flags().String(flagRandomNumberHash, "", "RandomNumberHash of random number and timestamp, based on SHA256, 32 bytes, hex encoding. If left out, a random value will be generated")
cmd.Flags().Int64(flagTimestamp, 0, "The time of sending transaction, counted by second. In the response to a swap request from other chains, it should be identical to the one in the swap request. If left out, current timestamp will be used")
cmd.Flags().Int64(flagHeightSpan, 0, "The number of blocks to wait before the asset may be returned to swap creator if not claimed via random number")
cmd.Flags().Bool(flagCrossChain, false, "Create cross chain hash timer lock transfer")
return cmd
}
func (c Commander) initiateHTLT(cmd *cobra.Command, args []string) error {
cliCtx, txBldr := client.PrepareCtx(c.Cdc)
from, err := cliCtx.GetFromAddress()
if err != nil {
return err
}
to, err := sdk.AccAddressFromBech32(viper.GetString(flagRecipientAddr))
if err != nil {
return err
}
amount, err := sdk.ParseCoins(viper.GetString(flagAmount))
if err != nil {
return err
}
expectedIncome := viper.GetString(flagExpectedIncome)
recipientOtherChain := viper.GetString(flagRecipientOtherChain)
senderOtherChain := viper.GetString(flagSenderOtherChain)
var timestamp int64
var randomNumberHash []byte
timestamp = viper.GetInt64(flagTimestamp)
if timestamp == 0 {
timestamp = time.Now().Unix()
}
randomNumberHashStr := viper.GetString(flagRandomNumberHash)
if len(randomNumberHashStr) == 0 {
randomNumber := make([]byte, swap.RandomNumberLength)
length, err := rand.Read(randomNumber)
if err != nil || length != swap.RandomNumberLength {
return fmt.Errorf("failed to generate random number")
}
randomNumberHash = swap.CalculateRandomHash(randomNumber, timestamp)
fmt.Printf("Random number: %s\n", hex.EncodeToString(randomNumber))
} else {
randomNumberHash, err = hex.DecodeString(randomNumberHashStr)
if err != nil {
return err
}
}
fmt.Printf("Timestamp: %d\nRandom number hash: %s\n", timestamp, hex.EncodeToString(randomNumberHash))
heightSpan := viper.GetInt64(flagHeightSpan)
crossChain := viper.GetBool(flagCrossChain)
// build message
msg := swap.NewHTLTMsg(from, to, recipientOtherChain, senderOtherChain, randomNumberHash, timestamp, amount, expectedIncome, heightSpan, crossChain)
sdkErr := msg.ValidateBasic()
if sdkErr != nil {
return fmt.Errorf("%v", sdkErr.Data())
}
return client.SendOrPrintTx(cliCtx, txBldr, msg)
}
func depositHTLTCmd(cmdr Commander) *cobra.Command {
cmd := &cobra.Command{
Use: "deposit",
Short: "deposit a hash timer lock transfer",
RunE: cmdr.depositHTLT,
}
cmd.Flags().String(flagAmount, "", "The swapped out amount BEP2 tokens, example: \"100:AXC\" or \"100:AXC,10000:BTCB-1DE\"")
cmd.Flags().String(flagSwapID, "", "ID of previously created swap, hex encoding")
return cmd
}
func (c Commander) depositHTLT(cmd *cobra.Command, args []string) error {
cliCtx, txBldr := client.PrepareCtx(c.Cdc)
from, err := cliCtx.GetFromAddress()
if err != nil {
return err
}
amount, err := sdk.ParseCoins(viper.GetString(flagAmount))
if err != nil {
return err
}
swapID, err := hex.DecodeString(viper.GetString(flagSwapID))
if err != nil {
return err
}
// build message
msg := swap.NewDepositHTLTMsg(from, amount, swapID)
sdkErr := msg.ValidateBasic()
if sdkErr != nil {
return fmt.Errorf("%v", sdkErr.Data())
}
return client.SendOrPrintTx(cliCtx, txBldr, msg)
}
func claimHTLTCmd(cmdr Commander) *cobra.Command {
cmd := &cobra.Command{
Use: "claim",
Short: "claim a hash timer lock transfer",
RunE: cmdr.claimHTLT,
}
cmd.Flags().String(flagSwapID, "", "ID of previously created swap, hex encoding")
cmd.Flags().String(flagRandomNumber, "", "The random number to unlock the locked hash, 32 bytes, hex encoding")
return cmd
}
func (c Commander) claimHTLT(cmd *cobra.Command, args []string) error {
cliCtx, txBldr := client.PrepareCtx(c.Cdc)
from, err := cliCtx.GetFromAddress()
if err != nil {
return err
}
swapID, err := hex.DecodeString(viper.GetString(flagSwapID))
if err != nil {
return err
}
randomNumber, err := hex.DecodeString(viper.GetString(flagRandomNumber))
if err != nil {
return err
}
// build message
msg := swap.NewClaimHTLTMsg(from, swapID, randomNumber)
sdkErr := msg.ValidateBasic()
if sdkErr != nil {
return fmt.Errorf("%v", sdkErr.Data())
}
return client.SendOrPrintTx(cliCtx, txBldr, msg)
}
func refundHTLTCmd(cmdr Commander) *cobra.Command {
cmd := &cobra.Command{
Use: "refund",
Short: "refund a hash timer lock transfer",
RunE: cmdr.refundHTLT,
}
cmd.Flags().String(flagSwapID, "", "ID of previously created swap, hex encoding")
return cmd
}
func (c Commander) refundHTLT(cmd *cobra.Command, args []string) error {
cliCtx, txBldr := client.PrepareCtx(c.Cdc)
from, err := cliCtx.GetFromAddress()
if err != nil {
return err
}
swapID, err := hex.DecodeString(viper.GetString(flagSwapID))
if err != nil {
return err
}
// build message
msg := swap.NewRefundHTLTMsg(from, swapID)
sdkErr := msg.ValidateBasic()
if sdkErr != nil {
return fmt.Errorf("%v", sdkErr.Data())
}
return client.SendOrPrintTx(cliCtx, txBldr, msg)
}
func querySwapCmd(cmdr Commander) *cobra.Command {
cmd := &cobra.Command{
Use: "query-swap",
Short: "Query an atomic swap by swapID",
RunE: cmdr.querySwap,
}
cmd.Flags().String(flagSwapID, "", "ID of previously created swap, hex encoding")
return cmd
}
func (c Commander) querySwap(cmd *cobra.Command, args []string) error {
cliCtx, _ := client.PrepareCtx(c.Cdc)
swapID, err := hex.DecodeString(viper.GetString(flagSwapID))
if err != nil {
return err
}
if len(swapID) != swap.SwapIDLength {
return fmt.Errorf("expected swapID length is %d, actually it is %d", swap.SwapIDLength, len(swapID))
}
hashKey := swap.BuildHashKey(swapID)
res, err := cliCtx.QueryStore(hashKey, common.AtomicSwapStoreName)
if err != nil {
return err
}
if res == nil {
return fmt.Errorf("no matched swap record")
}
var atomicSwap swap.AtomicSwap
c.Cdc.MustUnmarshalBinaryBare(res, &atomicSwap)
var output []byte
if cliCtx.Indent {
output, err = c.Cdc.MarshalJSONIndent(atomicSwap, "", " ")
} else {
output, err = c.Cdc.MarshalJSON(atomicSwap)
}
if err != nil {
return err
}
fmt.Println(string(output))
return nil
}
func querySwapsByCreatorCmd(cmdr Commander) *cobra.Command {
cmd := &cobra.Command{
Use: "query-swapIDs-by-creator",
Short: "Query swapID list by the creator address",
RunE: cmdr.querySwapsByCreator,
}
cmd.Flags().String(flagCreatorAddr, "", "Swap creator address, bech32 encoding")
cmd.Flags().Int64(flagLimit, 100, "The maximum quantity of swapIDs you want to get")
cmd.Flags().Int64(flagOffset, 0, "The number of swapIDs you want to skip")
return cmd
}
func (c Commander) querySwapsByCreator(cmd *cobra.Command, args []string) error {
cliCtx, _ := client.PrepareCtx(c.Cdc)
creator, err := sdk.AccAddressFromBech32(viper.GetString(flagCreatorAddr))
if err != nil {
return err
}
limit := viper.GetInt64(flagLimit)
offset := viper.GetInt64(flagOffset)
if limit <= 0 || limit > 100 {
return fmt.Errorf("limit should be (1, 100]")
}
if offset < 0 {
return fmt.Errorf("offset must be positive")
}
params := swap.QuerySwapByCreatorParams{
Creator: creator,
Limit: limit,
Offset: offset,
}
bz, err := c.Cdc.MarshalJSON(params)
if err != nil {
return err
}
res, err := cliCtx.QueryWithData(fmt.Sprintf("custom/%s/%s", swap.AtomicSwapRoute, swap.QuerySwapCreator), bz)
if err != nil {
return err
}
fmt.Println(string(res))
return nil
}
func querySwapsByRecipientCmd(cmdr Commander) *cobra.Command {
cmd := &cobra.Command{
Use: "query-swapIDs-by-recipient",
Short: "Query swapID list by the recipient address",
RunE: cmdr.querySwapsByRecipient,
}
cmd.Flags().String(flagRecipientAddr, "", "Swap recipient address, bech32 encoding")
cmd.Flags().Int64(flagLimit, 100, "The maximum quantity of swapIDs you want to get")
cmd.Flags().Int64(flagOffset, 0, "The number of swapIDs you want to skip")
return cmd
}
func (c Commander) querySwapsByRecipient(cmd *cobra.Command, args []string) error {
cliCtx, _ := client.PrepareCtx(c.Cdc)
recipient, err := sdk.AccAddressFromBech32(viper.GetString(flagRecipientAddr))
if err != nil {
return err
}
limit := viper.GetInt64(flagLimit)
offset := viper.GetInt64(flagOffset)
if limit <= 0 || limit > 100 {
return fmt.Errorf("limit should be (1, 100]")
}
if offset < 0 {
return fmt.Errorf("offset must be positive")
}
params := swap.QuerySwapByRecipientParams{
Recipient: recipient,
Limit: limit,
Offset: offset,
}
bz, err := c.Cdc.MarshalJSON(params)
if err != nil {
return err
}
res, err := cliCtx.QueryWithData(fmt.Sprintf("custom/%s/%s", swap.AtomicSwapRoute, swap.QuerySwapRecipient), bz)
if err != nil {
return err
}
fmt.Println(string(res))
return nil
}