<commit_before>// +build invoicesrpc

package main

import (
	"context"
	"encoding/hex"
	"fmt"

	"github.com/lightningnetwork/lnd/lnrpc/invoicesrpc"
	"github.com/urfave/cli"
	"strconv"
)

// invoicesCommands will return nil for non-invoicesrpc builds.
func invoicesCommands() []cli.Command {
	return []cli.Command{
		cancelInvoiceCommand,
		addHoldInvoiceCommand,
	}
}

func getInvoicesClient(ctx *cli.Context) (invoicesrpc.InvoicesClient, func()) {
	conn := getClientConn(ctx, false)

	cleanUp := func() {
		conn.Close()
	}

	return invoicesrpc.NewInvoicesClient(conn), cleanUp
}

var cancelInvoiceCommand = cli.Command{
	Name:     "cancelinvoice",
	Category: "Payments",
	Usage:    "Cancels a (hold) invoice",
	Description: `
	Todo.`,
	ArgsUsage: "paymenthash",
	Flags: []cli.Flag{
		cli.StringFlag{
			Name: "paymenthash",
			Usage: "the hex-encoded payment hash (32 byte) for which the " +
				"corresponding invoice will be canceled.",
		},
	},
	Action: actionDecorator(cancelInvoice),
}

func cancelInvoice(ctx *cli.Context) error {
	var (
		paymentHash []byte
		err         error
	)<commit_msg>lncli: add settle invoice command<commit_after>// +build invoicesrpc

package main

import (
	"context"
	"encoding/hex"
	"fmt"

	"strconv"

	"github.com/lightningnetwork/lnd/lnrpc/invoicesrpc"
	"github.com/urfave/cli"
)

// invoicesCommands will return nil for non-invoicesrpc builds.
func invoicesCommands() []cli.Command {
	return []cli.Command{
		cancelInvoiceCommand,
		addHoldInvoiceCommand,
		settleInvoiceCommand,
	}
}

func getInvoicesClient(ctx *cli.Context) (invoicesrpc.InvoicesClient, func()) {
	conn := getClientConn(ctx, false)

	cleanUp := func() {
		conn.Close()
	}

	return invoicesrpc.NewInvoicesClient(conn), cleanUp
}

var settleInvoiceCommand = cli.Command{
	Name:     "settleinvoice",
	Category: "Payments",
	Usage:    "Reveal a preimage and use it to settle the corresponding invoice.",
	Description: `
	Todo.`,
	ArgsUsage: "preimage",
	Flags: []cli.Flag{
		cli.StringFlag{
			Name: "preimage",
			Usage: "the hex-encoded preimage (32 byte) which will " +
				"allow settling an incoming HTLC payable to this " +
				"preimage.",
		},
	},
	Action: actionDecorator(settleInvoice),
}

func settleInvoice(ctx *cli.Context) error {
	var (
		preimage []byte
		err      error
	)

	client, cleanUp := getInvoicesClient(ctx)
	defer cleanUp()

	args := ctx.Args()

	switch {
	case ctx.IsSet("preimage"):
		preimage, err = hex.DecodeString(ctx.String("preimage"))
	case args.Present():
		preimage, err = hex.DecodeString(args.First())
	}

	if err != nil {
		return fmt.Errorf("unable to parse preimage: %v", err)
	}

	invoice := &invoicesrpc.SettleInvoiceMsg{
		Preimage: preimage,
	}

	resp, err := client.SettleInvoice(context.Background(), invoice)
	if err != nil {
		return err
	}

	printJSON(resp)

	return nil
}

var cancelInvoiceCommand = cli.Command{
	Name:     "cancelinvoice",
	Category: "Payments",
	Usage:    "Cancels a (hold) invoice",
	Description: `
	Todo.`,
	ArgsUsage: "paymenthash",
	Flags: []cli.Flag{
		cli.StringFlag{
			Name: "paymenthash",
			Usage: "the hex-encoded payment hash (32 byte) for which the " +
				"corresponding invoice will be canceled.",
		},
	},
	Action: actionDecorator(cancelInvoice),
}

func cancelInvoice(ctx *cli.Context) error {
	var (
		paymentHash []byte
		err         error
	)
{'fraction_non_alphanumeric': 0.13023952095808383, 'fraction_numerical': 0.0017964071856287425, 'mean_word_length': 4.1321044546851, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6589391', 'n_tokens_mistral': 1267, 'n_tokens_neox': 1178, 'n_words': 279}