forked from seeleteam/go-seele
/
htlc.go
154 lines (130 loc) · 3.8 KB
/
htlc.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
/**
* @file
* @copyright defined in go-seele/LICENSE
*/
package cmd
import (
"crypto/rand"
"encoding/json"
"fmt"
"time"
"github.com/seeleteam/go-seele/common"
"github.com/seeleteam/go-seele/common/hexutil"
"github.com/seeleteam/go-seele/contract/system"
"github.com/seeleteam/go-seele/rpc"
"github.com/urfave/cli"
)
// createHTLC create HTLC
func createHTLC(client *rpc.Client) (interface{}, interface{}, error) {
hashLockBytes, err := hexutil.HexToBytes(hashValue)
if err != nil {
return nil, nil, fmt.Errorf("Failed to convert Hex to Hash %s", err)
}
var data system.HashTimeLock
data.HashLock = hashLockBytes
data.TimeLock = timeLockValue
toAddr, err := common.HexToAddress(toValue)
if err != nil {
return nil, nil, err
}
data.To = toAddr
dataBytes, err := json.Marshal(data)
if err != nil {
return nil, nil, err
}
tx, err := sendSystemContractTx(client, system.HashTimeLockContractAddress, system.CmdNewContract, dataBytes)
if err != nil {
return nil, nil, err
}
output := make(map[string]interface{})
output["Tx"] = *tx
output["HashLock"] = hashValue
output["TimeLock"] = timeLockValue
return output, tx, err
}
// withdraw obtain seele from transaction
func withdraw(client *rpc.Client) (interface{}, interface{}, error) {
amountValue = "0"
txHashBytes, err := common.HexToHash(hashValue)
if err != nil {
return nil, nil, fmt.Errorf("Failed to convert Hex to Hash %s", err)
}
preimageBytes, err := hexutil.HexToBytes(preimageValue)
if err != nil {
return nil, nil, fmt.Errorf("Failed to convert Hex to Bytes %s", err)
}
var data system.Withdrawing
data.Hash = txHashBytes
data.Preimage = preimageBytes
dataBytes, err := json.Marshal(data)
if err != nil {
return nil, nil, err
}
tx, err := sendSystemContractTx(client, system.HashTimeLockContractAddress, system.CmdWithdraw, dataBytes)
if err != nil {
return nil, nil, err
}
output := make(map[string]interface{})
output["Tx"] = *tx
output["hash"] = hashValue
output["preimage"] = preimageValue
return output, tx, err
}
// refund used to refund seele from HTLC
func refund(client *rpc.Client) (interface{}, interface{}, error) {
amountValue = "0"
txHashBytes, err := hexutil.HexToBytes(hashValue)
if err != nil {
return nil, nil, fmt.Errorf("Failed to convert Hex to Bytes %s", err)
}
tx, err := sendSystemContractTx(client, system.HashTimeLockContractAddress, system.CmdRefund, txHashBytes)
if err != nil {
return nil, nil, err
}
output := make(map[string]interface{})
output["Tx"] = *tx
output["hash"] = hashValue
return output, tx, err
}
// getHTLC used to get HTLC
func getHTLC(client *rpc.Client) (interface{}, interface{}, error) {
amountValue = "0"
priceValue = "1"
txHashBytes, err := hexutil.HexToBytes(hashValue)
if err != nil {
return nil, nil, fmt.Errorf("Failed to convert Hex to Bytes %s", err)
}
tx, err := sendSystemContractTx(client, system.HashTimeLockContractAddress, system.CmdGetContract, txHashBytes)
if err != nil {
return nil, nil, err
}
output := make(map[string]interface{})
output["Tx"] = *tx
output["hash"] = hashValue
return output, tx, err
}
// generateHTLCKey generate HTLC preimage and preimage hash
func generateHTLCKey(c *cli.Context) error {
secret := make([]byte, 32)
if _, err := rand.Read(secret[:]); err != nil {
return err
}
hash := system.Sha256Hash(secret)
fmt.Println("preimage:", hexutil.BytesToHex(secret[:]))
fmt.Println("hash:", hexutil.BytesToHex(hash[:]))
return nil
}
// generateHTLCTime generate HTLC time lock
func generateHTLCTime(c *cli.Context) error {
locktime := time.Now().Unix() + timeLockValue
fmt.Println("locktime:", locktime)
return nil
}
// decodeHTLC decode htlc information
func decodeHTLC(c *cli.Context) error {
result, err := system.DecodeHTLC(payloadValue)
if err != nil {
return err
}
return handleCallResult(nil, result)
}