forked from mit-dci/lit
/
chancmds.go
442 lines (367 loc) · 11.9 KB
/
chancmds.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
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
package main
import (
"encoding/hex"
"fmt"
"strconv"
"github.com/fatih/color"
"github.com/mit-dci/lit/litrpc"
"github.com/mit-dci/lit/lnutil"
)
var fundCommand = &Command{
Format: fmt.Sprintf("%s%s%s\n", lnutil.White("fund"),
lnutil.ReqColor("peer", "coinType", "capacity", "initialSend"), lnutil.OptColor("data")),
Description: fmt.Sprintf("%s\n%s\n%s\n%s\n",
"Establish and fund a new lightning channel with the given peer.",
"The capacity is the amount of satoshi we insert into the channel,",
"and initialSend is the amount we initially hand over to the other party.",
"data is an optional field that can contain 32 bytes of hex to send as part of the channel fund",
),
ShortDescription: "Establish and fund a new lightning channel with the given peer.\n",
}
var dualFundCommand = &Command{
Format: fmt.Sprintf("%s%s%s\n", lnutil.White("dualfund"),
lnutil.ReqColor("subcommand"), lnutil.OptColor("parameters...")),
Description: fmt.Sprintf("%s\n%s\n",
"Commands for establishing and mutually funding a new lightning channel with the given peer.",
"Subcommands: start, accept, decline"),
ShortDescription: "Commands for dual funding\n",
}
var dualFundStartCommand = &Command{
Format: fmt.Sprintf("%s%s\n", lnutil.White("dualfund start"),
lnutil.ReqColor("peer", "coinType", "ourAmount", "theirAmount")),
Description: fmt.Sprintf("%s\n%s\n%s\n",
"Establish and mutually fund a new lightning channel with the given peer.",
"The capacity is the sum of the amounts both peers insert into the channel,",
"each party will end up with their funded amount in the channel."),
ShortDescription: "Establish and mutually fund a new lightning channel with the given peer.\n",
}
var dualFundDeclineCommand = &Command{
Format: fmt.Sprintf("%s\n", lnutil.White("dualfund decline")),
Description: "Declines the pending dual funding request received from another peer (if any)\n",
ShortDescription: "Declines the pending dual funding request received from another peer (if any)\n",
}
var dualFundAcceptCommand = &Command{
Format: fmt.Sprintf("%s\n", lnutil.White("dualfund accept")),
Description: "Accepts the pending dual funding request received from another peer (if any)\n",
ShortDescription: "Accepts the pending dual funding request received from another peer (if any)\n",
}
var watchCommand = &Command{
Format: fmt.Sprintf("%s%s\n", lnutil.White("watch"),
lnutil.ReqColor("channel idx", "watchPeerIdx")),
Description: fmt.Sprintf("%s\n%s\n",
"Send channel data to a watcher",
"The watcher can defend your channel while you're offline."),
ShortDescription: "Send channel watch data to watcher.\n",
}
var pushCommand = &Command{
Format: fmt.Sprintf("%s%s%s%s\n", lnutil.White("push"), lnutil.ReqColor("channel idx", "amount"), lnutil.OptColor("times"), lnutil.OptColor("data")),
Description: fmt.Sprintf("%s\n%s\n%s\n",
"Push the given amount (in satoshis) to the other party on the given channel.",
"Optionally, the push operation can be associated with a 32 byte value hex encoded.",
"Optionally, the push operation can be repeated <times> number of times."),
ShortDescription: "Push the given amount (in satoshis) to the other party on the given channel.\n",
}
var closeCommand = &Command{
Format: fmt.Sprintf("%s%s\n", lnutil.White("close"), lnutil.ReqColor("channel idx")),
Description: fmt.Sprintf("%s\n%s\n%s%s\n",
"Cooperatively close the channel with the given index by asking",
"the other party to finalize the channel pay-out.",
"See also: ", lnutil.White("break")),
ShortDescription: "Cooperatively close the channel with the given index by asking\n",
}
var breakCommand = &Command{
Format: fmt.Sprintf("%s%s\n", lnutil.White("break"), lnutil.ReqColor("channel idx")),
Description: fmt.Sprintf("%s\n%s\n%s%s\n",
"Forcibly break the given channel. Note that you need to wait",
"a set number of blocks before you can use the money.",
"See also: ", lnutil.White("stop")),
ShortDescription: "Forcibly break the given channel.\n",
}
var historyCommand = &Command{
Format: lnutil.White("history"),
Description: "Show all the metadata for justice txs",
ShortDescription: "Show all the metadata for justice txs.\n",
}
func (lc *litAfClient) History(textArgs []string) error {
if len(textArgs) > 0 && textArgs[0] == "-h" {
fmt.Fprintf(color.Output, historyCommand.Format)
fmt.Fprintf(color.Output, historyCommand.Description)
return nil
}
args := new(litrpc.StateDumpArgs)
reply := new(litrpc.StateDumpReply)
err := lc.Call("LitRPC.StateDump", args, reply)
if err != nil {
return err
}
for _, tx := range reply.Txs {
fmt.Fprintf(color.Output, "Pkh: %x, Idx: %d, Sig: %x, Txid: %x, Data: %x, Amt: %d\n", tx.Pkh, tx.Idx, tx.Sig, tx.Txid, tx.Data, tx.Amt)
}
return nil
}
// CheckHelpCommand checks whether the user wants help regarding the command
// or passed invalid arguments. Also checks for expected length of command
// and returns and error if the expected length is different.
func CheckHelpCommand(command *Command, textArgs []string, expectedLength int) (bool, error) {
if len(textArgs) > 0 && textArgs[0] == "-h" {
fmt.Fprintf(color.Output, command.Format)
fmt.Fprintf(color.Output, command.Description)
return true, nil // stop Execution if the guy just wants help
}
if len(textArgs) < expectedLength {
// if number of args are less than expected, return
return true, fmt.Errorf(command.Format) // stop execution in case of err
}
return false, nil
}
func (lc *litAfClient) FundChannel(textArgs []string) error {
stopEx, err := CheckHelpCommand(fundCommand, textArgs, 4)
if err != nil || stopEx {
return err
}
args := new(litrpc.FundArgs)
reply := new(litrpc.StatusReply)
peer, err := strconv.Atoi(textArgs[0])
if err != nil {
return err
}
coinType, err := strconv.Atoi(textArgs[1])
if err != nil {
return err
}
cCap, err := strconv.Atoi(textArgs[2])
if err != nil {
return err
}
iSend, err := strconv.Atoi(textArgs[3])
if err != nil {
return err
}
if len(textArgs) > 4 {
data, err := hex.DecodeString(textArgs[4])
if err != nil {
// Wasn't valid hex, copy directly and truncate
copy(args.Data[:], textArgs[3])
} else {
copy(args.Data[:], data[:])
}
}
args.Peer = uint32(peer)
args.CoinType = uint32(coinType)
args.Capacity = int64(cCap)
args.InitialSend = int64(iSend)
err = lc.Call("LitRPC.FundChannel", args, reply)
if err != nil {
return err
}
fmt.Fprintf(color.Output, "%s\n", reply.Status)
return nil
}
func (lc *litAfClient) DualFund(textArgs []string) error {
stopEx, err := CheckHelpCommand(dualFundCommand, textArgs, 2)
if err != nil || stopEx {
return err
}
return nil
}
// Mutually fund a channel
func (lc *litAfClient) DualFundChannel(textArgs []string) error {
stopEx, err := CheckHelpCommand(dualFundStartCommand, textArgs, 4)
if err != nil || stopEx {
return err
}
args := new(litrpc.DualFundArgs)
reply := new(litrpc.StatusReply)
peer, err := strconv.Atoi(textArgs[0])
if err != nil {
return err
}
coinType, err := strconv.Atoi(textArgs[1])
if err != nil {
return err
}
ourAmt, err := strconv.Atoi(textArgs[2])
if err != nil {
return err
}
theirAmt, err := strconv.Atoi(textArgs[3])
if err != nil {
return err
}
args.Peer = uint32(peer)
args.CoinType = uint32(coinType)
args.OurAmount = int64(ourAmt)
args.TheirAmount = int64(theirAmt)
err = lc.rpccon.Call("LitRPC.DualFundChannel", args, reply)
if err != nil {
return err
}
fmt.Fprintf(color.Output, "%s\n", reply.Status)
return nil
}
// Decline mutual funding of a channel
func (lc *litAfClient) DualFundDecline(textArgs []string) error {
stopEx, err := CheckHelpCommand(dualFundDeclineCommand, textArgs, 0)
if err != nil || stopEx {
return err
}
reply := new(litrpc.StatusReply)
err = lc.rpccon.Call("LitRPC.DualFundDecline", nil, reply)
if err != nil {
return err
}
fmt.Fprintf(color.Output, "%s\n", reply.Status)
return nil
}
// Accept mutual funding of a channel
func (lc *litAfClient) DualFundAccept(textArgs []string) error {
stopEx, err := CheckHelpCommand(dualFundAcceptCommand, textArgs, 0)
if err != nil || stopEx {
return err
}
reply := new(litrpc.StatusReply)
err = lc.rpccon.Call("LitRPC.DualFundAccept", nil, reply)
if err != nil {
return err
}
fmt.Fprintf(color.Output, "%s\n", reply.Status)
return nil
}
// Request close of a channel. Need to pass in peer, channel index
func (lc *litAfClient) CloseChannel(textArgs []string) error {
stopEx, err := CheckHelpCommand(closeCommand, textArgs, 1)
if err != nil || stopEx {
return err
}
args := new(litrpc.ChanArgs)
reply := new(litrpc.StatusReply)
cIdx, err := strconv.Atoi(textArgs[0])
if err != nil {
return err
}
args.ChanIdx = uint32(cIdx)
err = lc.Call("LitRPC.CloseChannel", args, reply)
if err != nil {
return err
}
fmt.Fprintf(color.Output, "%s\n", reply.Status)
return nil
}
// Almost exactly the same as CloseChannel. Maybe make "break" a bool...?
func (lc *litAfClient) BreakChannel(textArgs []string) error {
stopEx, err := CheckHelpCommand(breakCommand, textArgs, 1)
if err != nil || stopEx {
return err
}
args := new(litrpc.ChanArgs)
reply := new(litrpc.StatusReply)
cIdx, err := strconv.Atoi(textArgs[0])
if err != nil {
return err
}
args.ChanIdx = uint32(cIdx)
err = lc.Call("LitRPC.BreakChannel", args, reply)
if err != nil {
return err
}
fmt.Fprintf(color.Output, "%s\n", reply.Status)
return nil
}
// Push is the shell command which calls PushChannel
func (lc *litAfClient) Push(textArgs []string) error {
stopEx, err := CheckHelpCommand(pushCommand, textArgs, 2)
if err != nil || stopEx {
return err
}
args := new(litrpc.PushArgs)
reply := new(litrpc.PushReply)
// this stuff is all the same as in cclose, should put into a function...
cIdx, err := strconv.Atoi(textArgs[0])
if err != nil {
return err
}
amt, err := strconv.Atoi(textArgs[1])
if err != nil {
return err
}
times := int(1)
if len(textArgs) > 2 {
times, err = strconv.Atoi(textArgs[2])
if err != nil {
return err
}
}
if len(textArgs) > 3 {
data, err := hex.DecodeString(textArgs[3])
if err != nil {
// Wasn't valid hex, copy directly and truncate
copy(args.Data[:], textArgs[3])
} else {
copy(args.Data[:], data[:])
}
}
args.ChanIdx = uint32(cIdx)
args.Amt = int64(amt)
for times > 0 {
err := lc.Call("LitRPC.Push", args, reply)
if err != nil {
return err
}
fmt.Fprintf(color.Output, "Pushed %s at state %s\n", lnutil.SatoshiColor(int64(amt)), lnutil.White(reply.StateIndex))
times--
}
return nil
}
func (lc *litAfClient) Dump(textArgs []string) error {
pReply := new(litrpc.DumpReply)
pArgs := new(litrpc.NoArgs)
err := lc.Call("LitRPC.DumpPrivs", pArgs, pReply)
if err != nil {
return err
}
fmt.Fprintf(color.Output, "Private keys for all channels and utxos:\n")
// Display DumpPriv info
for i, t := range pReply.Privs {
fmt.Fprintf(color.Output, "%d %s h:%d amt:%s %s ",
i, lnutil.OutPoint(t.OutPoint), t.Height,
lnutil.SatoshiColor(t.Amt), t.CoinType)
if t.Delay != 0 {
fmt.Fprintf(color.Output, " delay: %d", t.Delay)
}
if !t.Witty {
fmt.Fprintf(color.Output, " non-witness")
}
if len(t.PairKey) > 1 {
fmt.Fprintf(
color.Output, "\nPair Pubkey: %s", lnutil.Green(t.PairKey))
}
fmt.Fprintf(color.Output, "\n\tprivkey: %s", lnutil.Red(t.WIF))
fmt.Fprintf(color.Output, "\n")
}
return nil
}
func (lc *litAfClient) Watch(textArgs []string) error {
stopEx, err := CheckHelpCommand(watchCommand, textArgs, 2)
if err != nil || stopEx {
return err
}
args := new(litrpc.WatchArgs)
reply := new(litrpc.WatchReply)
cIdx, err := strconv.Atoi(textArgs[0])
if err != nil {
return err
}
peer, err := strconv.Atoi(textArgs[1])
if err != nil {
return err
}
args.ChanIdx = uint32(cIdx)
args.SendToPeer = uint32(peer)
err = lc.Call("LitRPC.Watch", args, reply)
if err != nil {
return err
}
fmt.Fprintf(color.Output, "Send channel %d data to peer %d\n",
args.ChanIdx, args.SendToPeer)
return nil
}