forked from breez/breez
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
292 lines (268 loc) · 7.45 KB
/
main.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
package main
import (
"encoding/hex"
"fmt"
"os"
"strconv"
"github.com/breez/breez/bindings"
"github.com/breez/breez/data"
cli "github.com/deadsy/go-cli"
"github.com/golang/protobuf/proto"
)
var cmdHelp = cli.Leaf{
Descr: "general help",
F: func(c *cli.CLI, args []string) {
c.GeneralHelp()
},
}
var cmdHistory = cli.Leaf{
Descr: "command history",
F: func(c *cli.CLI, args []string) {
c.SetLine(c.DisplayHistory(args))
},
}
var cmdExit = cli.Leaf{
Descr: "exit application",
F: func(c *cli.CLI, args []string) {
c.Exit()
},
}
var cmdNewReverseSwap = cli.Leaf{
Descr: "create a new reverse swap",
F: func(c *cli.CLI, args []string) {
if len(args) < 2 {
fmt.Println("Error: Need an amount and an address!")
return
}
amt, err := strconv.Atoi(args[0])
if err != nil {
fmt.Println(fmt.Errorf("strconv.Atoi(%v): %w", args[0], err))
return
}
req, err := proto.Marshal(&data.ReverseSwapRequest{Amount: int64(amt), Address: args[1]})
if err != nil {
fmt.Println(fmt.Errorf("proto.Marshal(%#v): %w", data.ReverseSwapRequest{Amount: int64(amt), Address: args[1]}, err))
return
}
h, err := bindings.NewReverseSwap(req)
if err != nil {
fmt.Println(fmt.Errorf("bindings.NewReverseSwap(%#v): %w", data.ReverseSwapRequest{Amount: int64(amt), Address: args[1]}, err))
return
}
fmt.Printf("%s\n", h)
},
}
var cmdFetchReverseSwap = cli.Leaf{
Descr: "fetch reverse swap info",
F: func(c *cli.CLI, args []string) {
if len(args) < 1 {
fmt.Println("need the hash of a payment preimage!")
return
}
b, err := bindings.FetchReverseSwap(args[0])
if err != nil {
fmt.Println(fmt.Errorf("bindings.FetchReverseSwap(%v): %w", args[0], err))
return
}
var rs data.ReverseSwap
err = proto.Unmarshal(b, &rs)
if err != nil {
fmt.Println(fmt.Errorf("proto.Unmarshal(%x): %w", b, err))
return
}
fmt.Printf("%#v\n", rs)
},
}
var cmdReverseSwapClaimFeeEstimates = cli.Leaf{
Descr: "get blocks/fee pairs for the claim transaction of a reverse swap",
F: func(c *cli.CLI, args []string) {
if len(args) < 1 {
fmt.Println("need a claim address!")
return
}
b, err := bindings.ReverseSwapClaimFeeEstimates(args[0])
if err != nil {
fmt.Println(fmt.Errorf("bindings.ReverseSwapClaimFeeEstimates(%v): %w", args[0], err))
return
}
var fees data.ClaimFeeEstimates
err = proto.Unmarshal(b, &fees)
if err != nil {
fmt.Println(fmt.Errorf("proto.Unmarshal(%x): %w", b, err))
return
}
fmt.Printf("%#v\n", fees.Fees)
},
}
var cmdSetReverseSwapClaimFee = cli.Leaf{
Descr: "set the fee for the claim transaction of a reverse swap",
F: func(c *cli.CLI, args []string) {
if len(args) < 2 {
fmt.Println("Error: Need a hash and a fee!")
return
}
fee, err := strconv.Atoi(args[1])
if err != nil {
fmt.Println(fmt.Errorf("strconv.Atoi(%v): %w", args[1], err))
return
}
req, err := proto.Marshal(&data.ReverseSwapClaimFee{Hash: args[0], Fee: int64(fee)})
if err != nil {
fmt.Println(fmt.Errorf("proto.Marshal(%#v): %w", data.ReverseSwapClaimFee{Hash: args[0], Fee: int64(fee)}, err))
return
}
err = bindings.SetReverseSwapClaimFee(req)
if err != nil {
fmt.Println(fmt.Errorf("bindings.SetReverseSwapClaimFee(%#v): %w", data.ReverseSwapClaimFee{Hash: args[0], Fee: int64(fee)}, err))
return
}
fmt.Printf("done.\n")
},
}
var cmdPayReverseSwap = cli.Leaf{
Descr: "pay reverse swap ln invoice",
F: func(c *cli.CLI, args []string) {
if len(args) < 1 {
fmt.Println("need the hash of a payment preimage!")
return
}
pr := data.ReverseSwapPaymentRequest{
Hash: args[0],
PushNotificationDetails: &data.PushNotificationDetails{
DeviceId: "<NA>", Title: "", Body: ""},
}
req, err := proto.Marshal(&pr)
if err != nil {
fmt.Println(fmt.Errorf("proto.Marshal(%#v): %w", pr, err))
return
}
err = bindings.PayReverseSwap(req)
if err != nil {
fmt.Println(fmt.Errorf("bindings.PayReverseSwap(%v): %w", args[0], err))
return
}
},
}
var cmdReverseSwapPaymentStatuses = cli.Leaf{
Descr: "get the status of the in-flight reverse swap payments",
F: func(c *cli.CLI, args []string) {
b, err := bindings.ReverseSwapPayments()
if err != nil {
fmt.Println(fmt.Errorf("bindings.ReverseSwapPayments(): %w", err))
return
}
var s data.ReverseSwapPaymentStatuses
err = proto.Unmarshal(b, &s)
if err != nil {
fmt.Println(fmt.Errorf("proto.Unmarshal(%x): %w", b, err))
return
}
for _, ps := range s.PaymentsStatus {
fmt.Printf("hash:%v,eta:%v\n", ps.Hash, ps.Eta)
}
},
}
var cmdUnconfirmedReverseSwapClaimTransaction = cli.Leaf{
Descr: "get the txid of the unconfirmed reverse swap claim transaction, if any",
F: func(c *cli.CLI, args []string) {
h, err := bindings.UnconfirmedReverseSwapClaimTransaction()
fmt.Printf("txid: %v, err: %v\n", h, err)
},
}
var cmdSweepAllCoinsTransactions = cli.Leaf{
Descr: "sweep all coins transactions",
F: func(c *cli.CLI, args []string) {
if len(args) < 1 {
fmt.Println("need an address")
return
}
b, err := bindings.SweepAllCoinsTransactions(args[0])
if err != nil {
fmt.Println(fmt.Errorf("bindings.SweepAllCoinsTransactions(): %w", err))
return
}
var transactions data.SweepAllCoinsTransactions
err = proto.Unmarshal(b, &transactions)
if err != nil {
fmt.Println(fmt.Errorf("proto.Unmarshal(%x): %w", b, err))
return
}
fmt.Printf("Amount: %v\n", transactions.Amt)
for confTarget, t := range transactions.Transactions {
fmt.Printf("\ntarget: %v\n fees: %v\n txHash: %v\n tx: %x\n",
confTarget, t.Fees, t.TxHash, t.Tx)
}
},
}
var cmdPublishTransaction = cli.Leaf{
Descr: "publish a transaction",
F: func(c *cli.CLI, args []string) {
if len(args) < 1 {
fmt.Println("need an transaction")
return
}
tx, err := hex.DecodeString(args[0])
if err != nil {
fmt.Println(fmt.Errorf("hex.DecodeString(%v): %w", args[0], err))
}
err = bindings.PublishTransaction(tx)
if err != nil {
fmt.Println(fmt.Errorf("bindings.PublishTransaction(%x): %w", tx, err))
}
fmt.Println("done.")
},
}
var menuRoot = cli.Menu{
{"exit", cmdExit},
{"help", cmdHelp},
{"history", cmdHistory, cli.HistoryHelp},
{"newreverseswap", cmdNewReverseSwap},
{"fetchreverseswap", cmdFetchReverseSwap},
{"claimfeeestimates", cmdReverseSwapClaimFeeEstimates},
{"setreverseswapclaimfee", cmdSetReverseSwapClaimFee},
{"payreverseswap", cmdPayReverseSwap},
{"reverseswapstatus", cmdReverseSwapPaymentStatuses},
{"unconfirmedreverseswapclaimtransaction", cmdUnconfirmedReverseSwapClaimTransaction},
{"sendallcoinstransactions", cmdSweepAllCoinsTransactions},
{"publishtransaction", cmdPublishTransaction},
}
type breezApp struct{}
func newBreezApp() *breezApp {
return &breezApp{}
}
func (app *breezApp) Put(s string) {
fmt.Printf("%s", s)
}
type ServicesImpl struct {
}
func (a *ServicesImpl) Notify(notificationEvent []byte) {}
func (a *ServicesImpl) BackupProviderSignIn() (string, error) {
return "", nil
}
func (a *ServicesImpl) BackupProviderName() string {
return "gdrive"
}
func main() {
workingDir := os.Getenv("LND_DIR")
err := bindings.Init(os.TempDir(), workingDir, &ServicesImpl{})
if err != nil {
fmt.Println("Error in binding.Init", err)
os.Exit(1)
}
err = bindings.Start()
if err != nil {
fmt.Println("Error in binding.Start", err)
os.Exit(1)
}
//hPath := filepath.Join(workingDir, "breez_history.txt")
hPath := "history.txt"
app := newBreezApp()
c := cli.NewCLI(app)
//c.SetPrompt("")
c.HistoryLoad(hPath)
c.SetRoot(menuRoot)
for c.Running() {
c.Run()
}
c.HistorySave(hPath)
}