Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify coin selection for sendcoins #8516

Closed
wants to merge 10 commits into from
20 changes: 1 addition & 19 deletions cmd/lncli/cmd_open_channel.go
Original file line number Diff line number Diff line change
Expand Up @@ -406,7 +406,7 @@ func openChannel(ctx *cli.Context) error {
if ctx.IsSet("utxo") {
utxos := ctx.StringSlice("utxo")

outpoints, err := utxosToOutpoints(utxos)
outpoints, err := UtxosToOutpoints(utxos)
if err != nil {
return fmt.Errorf("unable to decode utxos: %w", err)
}
Expand Down Expand Up @@ -1141,21 +1141,3 @@ func decodePsbt(psbt string) ([]byte, error) {
return nil, fmt.Errorf("not a PSBT")
}
}

// parseUtxos parses a comma separated list of utxos into outpoints that are
// passed to the server.
func utxosToOutpoints(utxos []string) ([]*lnrpc.OutPoint, error) {
var outpoints []*lnrpc.OutPoint
if len(utxos) == 0 {
return nil, fmt.Errorf("no utxos specified")
}
for _, utxo := range utxos {
outpoint, err := NewProtoOutPoint(utxo)
if err != nil {
return nil, err
}
outpoints = append(outpoints, outpoint)
}

return outpoints, nil
}
37 changes: 30 additions & 7 deletions cmd/lncli/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -288,10 +288,11 @@ var sendCoinsCommand = cli.Command{
},
cli.BoolFlag{
Name: "sweepall",
Usage: "if set, then the amount field will be ignored, " +
"and the wallet will attempt to sweep all " +
"outputs within the wallet to the target " +
"address",
Usage: "if set, then the amount field should be " +
"unset. This indicates that the wallet will " +
"attempt to sweep all outputs within the " +
"wallet or all funds in select utxos (when " +
"supplied) to the target address",
},
cli.Int64Flag{
Name: "amt",
Expand Down Expand Up @@ -330,16 +331,28 @@ var sendCoinsCommand = cli.Command{
"scripts",
},
coinSelectionStrategyFlag,
cli.StringSliceFlag{
Name: "utxo",
hieblmi marked this conversation as resolved.
Show resolved Hide resolved
Chinwendu20 marked this conversation as resolved.
Show resolved Hide resolved
Usage: "a utxo specified as outpoint(tx:idx) which " +
"will be used as input for the transaction. " +
"This flag can be repeatedly used to specify " +
"multiple utxos as inputs. The selected " +
"utxos can either be entirely spent by " +
"specifying the sweepall flag or a specified " +
"amount can be spent in the utxos through " +
"the amt flag",
},
txLabelFlag,
},
Action: actionDecorator(sendCoins),
}

func sendCoins(ctx *cli.Context) error {
var (
addr string
amt int64
err error
addr string
amt int64
err error
outpoints []*lnrpc.OutPoint
)
ctxc := getContext()
args := ctx.Args()
Expand Down Expand Up @@ -417,6 +430,15 @@ func sendCoins(ctx *cli.Context) error {
displayAmt = balanceResponse.GetConfirmedBalance()
}

if ctx.IsSet("utxo") {
hieblmi marked this conversation as resolved.
Show resolved Hide resolved
utxos := ctx.StringSlice("utxo")

outpoints, err = UtxosToOutpoints(utxos)
if err != nil {
return fmt.Errorf("unable to decode utxos: %w", err)
}
}

// Ask for confirmation if we're on an actual terminal and the output is
// not being redirected to another command. This prevents existing shell
// scripts from breaking.
Expand All @@ -440,6 +462,7 @@ func sendCoins(ctx *cli.Context) error {
MinConfs: minConfs,
SpendUnconfirmed: minConfs == 0,
CoinSelectionStrategy: coinSelectionStrategy,
Outpoints: outpoints,
}
txid, err := client.SendCoins(ctxc, req)
if err != nil {
Expand Down
19 changes: 19 additions & 0 deletions cmd/lncli/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,3 +85,22 @@ func NewFailedUpdateFromProto(update *lnrpc.FailedUpdate) *FailedUpdate {
UpdateError: update.UpdateError,
}
}

// UtxosToOutpoints converts a slice of UTXO strings into a slice of OutPoint
// protobuf objects. It returns an error if no UTXOs are specified or if any
// UTXO string cannot be parsed into an OutPoint.
func UtxosToOutpoints(utxos []string) ([]*lnrpc.OutPoint, error) {
var outpoints []*lnrpc.OutPoint
if len(utxos) == 0 {
return nil, fmt.Errorf("no utxos specified")
}
for _, utxo := range utxos {
outpoint, err := NewProtoOutPoint(utxo)
if err != nil {
return nil, err
}
outpoints = append(outpoints, outpoint)
}

return outpoints, nil
}
7 changes: 7 additions & 0 deletions docs/release-notes/release-notes-0.18.1.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@
* [`ChanInfoRequest`](https://github.com/lightningnetwork/lnd/pull/8813)
adds support for channel points.

* [Added `outpoints` to `SendCoinsRequest`](
https://github.com/lightningnetwork/lnd/pull/8516).

## lncli Updates

* [`importmc`](https://github.com/lightningnetwork/lnd/pull/8779) now accepts
Expand All @@ -53,6 +56,10 @@
* [Fixed](https://github.com/lightningnetwork/lnd/pull/8823) how we parse the
`--amp` flag when sending a payment specifying the payment request.

* [`sendcoins` now takes an optional utxo flag](
https://github.com/lightningnetwork/lnd/pull/8516). This allows users to
specify the coins that they want to use as inputs for the transaction.

## Code Health
## Breaking Changes
## Performance Improvements
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ require (
github.com/btcsuite/btcwallet v0.16.10-0.20240404104514-b2f31f9045fb
github.com/btcsuite/btcwallet/wallet/txauthor v1.3.4
github.com/btcsuite/btcwallet/wallet/txrules v1.2.1
github.com/btcsuite/btcwallet/wallet/txsizes v1.2.4
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmmm how did you create this commit? Like what was the command used?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

make tidy-module

github.com/btcsuite/btcwallet/walletdb v1.4.2
github.com/btcsuite/btcwallet/wtxmgr v1.5.3
github.com/coreos/go-systemd v0.0.0-20190719114852-fd7a80b32e1f
Expand Down Expand Up @@ -72,7 +73,6 @@ require (
github.com/aead/chacha20 v0.0.0-20180709150244-8b13a72661da // indirect
github.com/aead/siphash v1.0.1 // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/btcsuite/btcwallet/wallet/txsizes v1.2.4 // indirect
github.com/btcsuite/go-socks v0.0.0-20170105172521-4720035b7bfd // indirect
github.com/btcsuite/websocket v0.0.0-20150119174127-31079b680792 // indirect
github.com/btcsuite/winsvc v1.0.0 // indirect
Expand Down
4 changes: 4 additions & 0 deletions itest/list_on_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -630,4 +630,8 @@ var allTestCases = []*lntest.TestCase{
Name: "coop close with external delivery",
TestFunc: testCoopCloseWithExternalDelivery,
},
{
Name: "send coins with select utxos",
TestFunc: testSendCoinSelectUtxo,
},
}
Loading
Loading