Skip to content

Commit 0ed1a9d

Browse files
committed
cmd: open channel command
1 parent ade098a commit 0ed1a9d

File tree

2 files changed

+363
-47
lines changed

2 files changed

+363
-47
lines changed

cmd/loop/openchannel.go

Lines changed: 350 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,350 @@
1+
package main
2+
3+
import (
4+
"context"
5+
"encoding/hex"
6+
"fmt"
7+
"strconv"
8+
9+
"github.com/lightninglabs/loop/looprpc"
10+
lndcommands "github.com/lightningnetwork/lnd/cmd/commands"
11+
"github.com/lightningnetwork/lnd/lnrpc"
12+
"github.com/urfave/cli/v3"
13+
)
14+
15+
const (
16+
defaultUtxoMinConf = 1
17+
)
18+
19+
var (
20+
channelTypeTweakless = "tweakless"
21+
channelTypeAnchors = "anchors"
22+
channelTypeSimpleTaproot = "taproot"
23+
)
24+
25+
var openChannelCommand = &cli.Command{
26+
Name: "openchannel",
27+
Usage: "Open a channel to a an existing peer.",
28+
Description: `
29+
Attempt to open a new channel to an existing peer with the key
30+
node-key.
31+
32+
The channel will be initialized with local-amt satoshis locally and
33+
push-amt satoshis for the remote node. Note that the push-amt is
34+
deducted from the specified local-amt which implies that the local-amt
35+
must be greater than the push-amt. Also note that specifying push-amt
36+
means you give that amount to the remote node as part of the channel
37+
opening. Once the channel is open, a channelPoint (txid:vout) of the
38+
funding output is returned.
39+
40+
If the remote peer supports the option upfront shutdown feature bit
41+
(query listpeers to see their supported feature bits), an address to
42+
enforce payout of funds on cooperative close can optionally be provided.
43+
Note that if you set this value, you will not be able to cooperatively
44+
close out to another address.
45+
46+
One can also specify a short string memo to record some useful
47+
information about the channel using the --memo argument. This is stored
48+
locally only, and is purely for reference. It has no bearing on the
49+
channel's operation. Max allowed length is 500 characters.`,
50+
Flags: []cli.Flag{
51+
&cli.StringFlag{
52+
Name: "node_key",
53+
Usage: "the identity public key of the target " +
54+
"node/peer serialized in compressed format",
55+
},
56+
&cli.IntFlag{
57+
Name: "local_amt",
58+
Usage: "the number of satoshis the wallet should " +
59+
"commit to the channel",
60+
},
61+
&cli.Uint64Flag{
62+
Name: "base_fee_msat",
63+
Usage: "the base fee in milli-satoshis that will " +
64+
"be charged for each forwarded HTLC, " +
65+
"regardless of payment size",
66+
},
67+
&cli.Uint64Flag{
68+
Name: "fee_rate_ppm",
69+
Usage: "the fee rate ppm (parts per million) that " +
70+
"will be charged proportionally based on the " +
71+
"value of each forwarded HTLC, the lowest " +
72+
"possible rate is 0 with a granularity of " +
73+
"0.000001 (millionths)",
74+
},
75+
&cli.IntFlag{
76+
Name: "push_amt",
77+
Usage: "the number of satoshis to give the remote " +
78+
"side as part of the initial commitment " +
79+
"state, this is equivalent to first opening " +
80+
"a channel and sending the remote party " +
81+
"funds, but done all in one step",
82+
},
83+
&cli.Int64Flag{
84+
Name: "sat_per_byte",
85+
Usage: "Deprecated, use sat_per_vbyte instead.",
86+
Hidden: true,
87+
},
88+
&cli.Int64Flag{
89+
Name: "sat_per_vbyte",
90+
Usage: "(optional) a manual fee expressed in " +
91+
"sat/vbyte that should be used when crafting " +
92+
"the transaction",
93+
},
94+
&cli.BoolFlag{
95+
Name: "private",
96+
Usage: "make the channel private, such that it won't " +
97+
"be announced to the greater network, and " +
98+
"nodes other than the two channel endpoints " +
99+
"must be explicitly told about it to be able " +
100+
"to route through it",
101+
},
102+
&cli.Int64Flag{
103+
Name: "min_htlc_msat",
104+
Usage: "(optional) the minimum value we will require " +
105+
"for incoming HTLCs on the channel",
106+
},
107+
&cli.Uint64Flag{
108+
Name: "remote_csv_delay",
109+
Usage: "(optional) the number of blocks we will " +
110+
"require our channel counterparty to wait " +
111+
"before accessing its funds in case of " +
112+
"unilateral close. If this is not set, we " +
113+
"will scale the value according to the " +
114+
"channel size",
115+
},
116+
&cli.Uint64Flag{
117+
Name: "max_local_csv",
118+
Usage: "(optional) the maximum number of blocks that " +
119+
"we will allow the remote peer to require we " +
120+
"wait before accessing our funds in the case " +
121+
"of a unilateral close.",
122+
},
123+
&cli.StringFlag{
124+
Name: "close_address",
125+
Usage: "(optional) an address to enforce payout of " +
126+
"our funds to on cooperative close. Note " +
127+
"that if this value is set on channel open, " +
128+
"you will *not* be able to cooperatively " +
129+
"close to a different address.",
130+
},
131+
&cli.Uint64Flag{
132+
Name: "remote_max_value_in_flight_msat",
133+
Usage: "(optional) the maximum value in msat that " +
134+
"can be pending within the channel at any " +
135+
"given time",
136+
},
137+
&cli.StringFlag{
138+
Name: "channel_type",
139+
Usage: fmt.Sprintf("(optional) the type of channel to "+
140+
"propose to the remote peer (%q, %q, %q)",
141+
channelTypeTweakless, channelTypeAnchors,
142+
channelTypeSimpleTaproot),
143+
},
144+
&cli.BoolFlag{
145+
Name: "zero_conf",
146+
Usage: "(optional) whether a zero-conf channel open " +
147+
"should be attempted.",
148+
},
149+
&cli.BoolFlag{
150+
Name: "scid_alias",
151+
Usage: "(optional) whether a scid-alias channel type" +
152+
" should be negotiated.",
153+
},
154+
&cli.Uint64Flag{
155+
Name: "remote_reserve_sats",
156+
Usage: "(optional) the minimum number of satoshis we " +
157+
"require the remote node to keep as a direct " +
158+
"payment. If not specified, a default of 1% " +
159+
"of the channel capacity will be used.",
160+
},
161+
&cli.StringFlag{
162+
Name: "memo",
163+
Usage: `(optional) a note-to-self containing some useful
164+
information about the channel. This is stored
165+
locally only, and is purely for reference. It
166+
has no bearing on the channel's operation. Max
167+
allowed length is 500 characters`,
168+
},
169+
&cli.BoolFlag{
170+
Name: "fundmax",
171+
Usage: "if set, the wallet will attempt to commit " +
172+
"the maximum possible local amount to the " +
173+
"channel. This must not be set at the same " +
174+
"time as local_amt",
175+
},
176+
&cli.StringSliceFlag{
177+
Name: "utxo",
178+
Usage: "a utxo specified as outpoint(tx:idx) which " +
179+
"will be used to fund a channel. This flag " +
180+
"can be repeatedly used to fund a channel " +
181+
"with a selection of utxos. The selected " +
182+
"funds can either be entirely spent by " +
183+
"specifying the fundmax flag or partially by " +
184+
"selecting a fraction of the sum of the " +
185+
"outpoints in local_amt",
186+
},
187+
},
188+
Action: openChannel,
189+
}
190+
191+
func openChannel(ctx context.Context, cmd *cli.Command) error {
192+
var (
193+
args = cmd.Args()
194+
remaining []string
195+
err error
196+
)
197+
198+
client, cleanup, err := getClient(ctx, cmd)
199+
if err != nil {
200+
return err
201+
}
202+
defer cleanup()
203+
204+
// Show command help if no arguments provided
205+
if cmd.NArg() == 0 && cmd.NumFlags() == 0 {
206+
_ = cli.ShowCommandHelp(ctx, cmd, "openchannel")
207+
return nil
208+
}
209+
210+
// Check that only the field sat_per_vbyte or the deprecated field
211+
// sat_per_byte is used.
212+
feeRateFlag, err := checkNotBothSet(
213+
cmd, "sat_per_vbyte", "sat_per_byte",
214+
)
215+
if err != nil {
216+
return err
217+
}
218+
219+
minConfs := defaultUtxoMinConf
220+
req := &lnrpc.OpenChannelRequest{
221+
SatPerVbyte: cmd.Uint64(feeRateFlag),
222+
FundMax: cmd.Bool("fundmax"),
223+
MinHtlcMsat: cmd.Int64("min_htlc_msat"),
224+
RemoteCsvDelay: uint32(cmd.Uint64("remote_csv_delay")),
225+
MinConfs: int32(minConfs),
226+
SpendUnconfirmed: minConfs == 0,
227+
CloseAddress: cmd.String("close_address"),
228+
RemoteMaxValueInFlightMsat: cmd.Uint64("remote_max_value_in_flight_msat"),
229+
MaxLocalCsv: uint32(cmd.Uint64("max_local_csv")),
230+
ZeroConf: cmd.Bool("zero_conf"),
231+
ScidAlias: cmd.Bool("scid_alias"),
232+
RemoteChanReserveSat: cmd.Uint64("remote_reserve_sats"),
233+
Memo: cmd.String("memo"),
234+
}
235+
236+
switch {
237+
case cmd.IsSet("node_key"):
238+
nodePubHex, err := hex.DecodeString(cmd.String("node_key"))
239+
if err != nil {
240+
return fmt.Errorf("unable to decode node "+
241+
"public key: %w", err)
242+
}
243+
req.NodePubkey = nodePubHex
244+
245+
case args.Present():
246+
nodePubHex, err := hex.DecodeString(args.First())
247+
if err != nil {
248+
return fmt.Errorf("unable to decode node "+
249+
"public key: %w", err)
250+
}
251+
remaining = args.Tail()
252+
req.NodePubkey = nodePubHex
253+
254+
default:
255+
return fmt.Errorf("node id argument missing")
256+
}
257+
258+
if cmd.IsSet("utxo") {
259+
utxos := cmd.StringSlice("utxo")
260+
261+
outpoints, err := lndcommands.UtxosToOutpoints(utxos)
262+
if err != nil {
263+
return fmt.Errorf("unable to decode utxos: %w", err)
264+
}
265+
266+
req.Outpoints = outpoints
267+
}
268+
269+
// The fundmax flag is NOT allowed to be combined with local_amt above.
270+
// It is allowed to be combined with push_amt, but only if explicitly
271+
// set.
272+
if cmd.Bool("fundmax") && cmd.IsSet("local_amt") {
273+
return fmt.Errorf("local_amt and fundmax are mutually " +
274+
"exclusive")
275+
}
276+
277+
switch {
278+
case cmd.IsSet("local_amt"):
279+
req.LocalFundingAmount = int64(cmd.Int("local_amt"))
280+
281+
case !cmd.Bool("fundmax"):
282+
return fmt.Errorf("either local_amt or fundmax must be " +
283+
"specified")
284+
}
285+
286+
if cmd.IsSet("push_amt") {
287+
req.PushSat = int64(cmd.Int("push_amt"))
288+
} else if len(remaining) > 0 {
289+
req.PushSat, err = strconv.ParseInt(remaining[0], 10, 64)
290+
if err != nil {
291+
return fmt.Errorf("unable to decode push amt: %w", err)
292+
}
293+
}
294+
295+
if cmd.IsSet("base_fee_msat") {
296+
req.BaseFee = cmd.Uint64("base_fee_msat")
297+
req.UseBaseFee = true
298+
}
299+
300+
if cmd.IsSet("fee_rate_ppm") {
301+
req.FeeRate = cmd.Uint64("fee_rate_ppm")
302+
req.UseFeeRate = true
303+
}
304+
305+
req.Private = cmd.Bool("private")
306+
307+
// Parse the channel type and map it to its RPC representation.
308+
channelType := cmd.String("channel_type")
309+
switch channelType {
310+
case "":
311+
break
312+
case channelTypeTweakless:
313+
req.CommitmentType = lnrpc.CommitmentType_STATIC_REMOTE_KEY
314+
315+
case channelTypeAnchors:
316+
req.CommitmentType = lnrpc.CommitmentType_ANCHORS
317+
318+
case channelTypeSimpleTaproot:
319+
req.CommitmentType = lnrpc.CommitmentType_SIMPLE_TAPROOT
320+
default:
321+
return fmt.Errorf("unsupported channel type %v", channelType)
322+
}
323+
324+
wrappedReq := &looprpc.StaticOpenChannelRequest{
325+
OpenChannelRequest: req,
326+
}
327+
328+
resp, err := client.StaticOpenChannel(ctx, wrappedReq)
329+
330+
printRespJSON(resp)
331+
332+
return err
333+
}
334+
335+
// checkNotBothSet accepts two flag names, a and b, and checks that only flag a
336+
// or flag b can be set, but not both. It returns the name of the flag or an
337+
// error.
338+
func checkNotBothSet(cmd *cli.Command, a, b string) (string, error) {
339+
if cmd.IsSet(a) && cmd.IsSet(b) {
340+
return "", fmt.Errorf(
341+
"either %s or %s should be set, but not both", a, b,
342+
)
343+
}
344+
345+
if cmd.IsSet(a) {
346+
return a, nil
347+
}
348+
349+
return b, nil
350+
}

0 commit comments

Comments
 (0)