2015-12-30 05:31:03 +03:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2018-02-02 07:52:40 +03:00
|
|
|
"bufio"
|
2015-12-30 05:58:58 +03:00
|
|
|
"bytes"
|
2019-09-29 01:44:45 +03:00
|
|
|
"context"
|
2019-12-05 14:27:17 +03:00
|
|
|
"crypto/rand"
|
2016-07-13 03:47:24 +03:00
|
|
|
"encoding/hex"
|
2018-01-24 05:34:29 +03:00
|
|
|
"errors"
|
2016-04-25 06:27:19 +03:00
|
|
|
"fmt"
|
2016-07-08 01:35:06 +03:00
|
|
|
"io"
|
2017-01-24 07:32:17 +03:00
|
|
|
"io/ioutil"
|
2017-01-25 05:07:15 +03:00
|
|
|
"math"
|
2015-12-30 05:58:58 +03:00
|
|
|
"os"
|
2017-01-24 07:32:17 +03:00
|
|
|
"strconv"
|
2016-06-21 22:35:07 +03:00
|
|
|
"strings"
|
2018-01-24 05:34:29 +03:00
|
|
|
"sync"
|
2017-12-19 01:04:04 +03:00
|
|
|
"syscall"
|
2019-10-04 01:12:39 +03:00
|
|
|
"time"
|
2016-12-27 08:52:15 +03:00
|
|
|
|
2018-07-31 10:17:17 +03:00
|
|
|
"github.com/btcsuite/btcd/chaincfg/chainhash"
|
2018-12-10 07:16:11 +03:00
|
|
|
"github.com/btcsuite/btcd/wire"
|
2019-12-20 12:05:08 +03:00
|
|
|
"github.com/lightninglabs/protobuf-hex-display/json"
|
|
|
|
"github.com/lightninglabs/protobuf-hex-display/jsonpb"
|
|
|
|
"github.com/lightninglabs/protobuf-hex-display/proto"
|
2016-01-16 21:45:54 +03:00
|
|
|
"github.com/lightningnetwork/lnd/lnrpc"
|
2019-08-29 19:09:37 +03:00
|
|
|
"github.com/lightningnetwork/lnd/lnrpc/routerrpc"
|
2019-12-05 14:27:17 +03:00
|
|
|
"github.com/lightningnetwork/lnd/lntypes"
|
|
|
|
"github.com/lightningnetwork/lnd/record"
|
2019-11-18 14:08:42 +03:00
|
|
|
"github.com/lightningnetwork/lnd/routing/route"
|
2019-02-21 17:25:42 +03:00
|
|
|
"github.com/lightningnetwork/lnd/walletunlocker"
|
2016-07-26 20:42:35 +03:00
|
|
|
"github.com/urfave/cli"
|
2017-10-12 12:42:06 +03:00
|
|
|
"golang.org/x/crypto/ssh/terminal"
|
2017-11-07 01:34:49 +03:00
|
|
|
"google.golang.org/grpc/codes"
|
|
|
|
"google.golang.org/grpc/status"
|
2015-12-30 05:31:03 +03:00
|
|
|
)
|
|
|
|
|
2016-06-21 22:35:07 +03:00
|
|
|
// TODO(roasbeef): cli logic for supporting both positional and unix style
|
|
|
|
// arguments.
|
|
|
|
|
2017-11-23 22:40:14 +03:00
|
|
|
// TODO(roasbeef): expose all fee conf targets
|
|
|
|
|
2019-02-23 05:08:01 +03:00
|
|
|
const defaultRecoveryWindow int32 = 2500
|
2018-03-27 00:15:04 +03:00
|
|
|
|
2017-02-23 22:56:47 +03:00
|
|
|
func printJSON(resp interface{}) {
|
2015-12-30 05:58:58 +03:00
|
|
|
b, err := json.Marshal(resp)
|
|
|
|
if err != nil {
|
|
|
|
fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var out bytes.Buffer
|
|
|
|
json.Indent(&out, b, "", "\t")
|
2017-03-16 22:06:12 +03:00
|
|
|
out.WriteString("\n")
|
2015-12-30 05:58:58 +03:00
|
|
|
out.WriteTo(os.Stdout)
|
|
|
|
}
|
|
|
|
|
2017-02-23 22:56:47 +03:00
|
|
|
func printRespJSON(resp proto.Message) {
|
2017-01-30 01:56:31 +03:00
|
|
|
jsonMarshaler := &jsonpb.Marshaler{
|
|
|
|
EmitDefaults: true,
|
|
|
|
Indent: " ",
|
|
|
|
}
|
|
|
|
|
|
|
|
jsonStr, err := jsonMarshaler.MarshalToString(resp)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println("unable to decode response: ", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Println(jsonStr)
|
|
|
|
}
|
|
|
|
|
2017-11-07 01:34:49 +03:00
|
|
|
// actionDecorator is used to add additional information and error handling
|
|
|
|
// to command actions.
|
|
|
|
func actionDecorator(f func(*cli.Context) error) func(*cli.Context) error {
|
|
|
|
return func(c *cli.Context) error {
|
|
|
|
if err := f(c); err != nil {
|
2018-05-06 23:50:07 +03:00
|
|
|
s, ok := status.FromError(err)
|
|
|
|
|
|
|
|
// If it's a command for the UnlockerService (like
|
|
|
|
// 'create' or 'unlock') but the wallet is already
|
|
|
|
// unlocked, then these methods aren't recognized any
|
|
|
|
// more because this service is shut down after
|
|
|
|
// successful unlock. That's why the code
|
|
|
|
// 'Unimplemented' means something different for these
|
|
|
|
// two commands.
|
|
|
|
if s.Code() == codes.Unimplemented &&
|
|
|
|
(c.Command.Name == "create" ||
|
|
|
|
c.Command.Name == "unlock") {
|
|
|
|
return fmt.Errorf("Wallet is already unlocked")
|
|
|
|
}
|
|
|
|
|
2017-11-07 01:34:49 +03:00
|
|
|
// lnd might be active, but not possible to contact
|
|
|
|
// using RPC if the wallet is encrypted. If we get
|
|
|
|
// error code Unimplemented, it means that lnd is
|
|
|
|
// running, but the RPC server is not active yet (only
|
|
|
|
// WalletUnlocker server active) and most likely this
|
|
|
|
// is because of an encrypted wallet.
|
|
|
|
if ok && s.Code() == codes.Unimplemented {
|
|
|
|
return fmt.Errorf("Wallet is encrypted. " +
|
|
|
|
"Please unlock using 'lncli unlock', " +
|
|
|
|
"or set password using 'lncli create'" +
|
|
|
|
" if this is the first time starting " +
|
|
|
|
"lnd.")
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-24 16:32:33 +03:00
|
|
|
var newAddressCommand = cli.Command{
|
2017-03-03 01:23:16 +03:00
|
|
|
Name: "newaddress",
|
2018-05-01 14:28:30 +03:00
|
|
|
Category: "Wallet",
|
2018-02-06 02:05:04 +03:00
|
|
|
Usage: "Generates a new address.",
|
2017-03-03 01:23:16 +03:00
|
|
|
ArgsUsage: "address-type",
|
2017-11-23 22:40:14 +03:00
|
|
|
Description: `
|
|
|
|
Generate a wallet new address. Address-types has to be one of:
|
2018-02-18 02:41:34 +03:00
|
|
|
- p2wkh: Pay to witness key hash
|
|
|
|
- np2wkh: Pay to nested witness key hash`,
|
2017-11-07 01:34:49 +03:00
|
|
|
Action: actionDecorator(newAddress),
|
2015-12-30 05:31:03 +03:00
|
|
|
}
|
|
|
|
|
2016-06-29 23:01:08 +03:00
|
|
|
func newAddress(ctx *cli.Context) error {
|
2017-01-30 01:51:30 +03:00
|
|
|
client, cleanUp := getClient(ctx)
|
|
|
|
defer cleanUp()
|
2015-12-30 05:31:03 +03:00
|
|
|
|
2017-03-03 01:23:16 +03:00
|
|
|
stringAddrType := ctx.Args().First()
|
2016-04-25 06:27:19 +03:00
|
|
|
|
|
|
|
// Map the string encoded address type, to the concrete typed address
|
|
|
|
// type enum. An unrecognized address type will result in an error.
|
2018-09-27 16:49:44 +03:00
|
|
|
var addrType lnrpc.AddressType
|
2016-04-25 06:27:19 +03:00
|
|
|
switch stringAddrType { // TODO(roasbeef): make them ints on the cli?
|
|
|
|
case "p2wkh":
|
2018-09-27 16:49:44 +03:00
|
|
|
addrType = lnrpc.AddressType_WITNESS_PUBKEY_HASH
|
2016-04-25 06:27:19 +03:00
|
|
|
case "np2wkh":
|
2018-09-27 16:49:44 +03:00
|
|
|
addrType = lnrpc.AddressType_NESTED_PUBKEY_HASH
|
2016-04-25 06:27:19 +03:00
|
|
|
default:
|
2016-06-29 23:01:08 +03:00
|
|
|
return fmt.Errorf("invalid address type %v, support address type "+
|
2018-02-18 02:41:34 +03:00
|
|
|
"are: p2wkh and np2wkh", stringAddrType)
|
2016-04-25 06:27:19 +03:00
|
|
|
}
|
|
|
|
|
2015-12-30 05:31:03 +03:00
|
|
|
ctxb := context.Background()
|
2016-04-25 06:27:19 +03:00
|
|
|
addr, err := client.NewAddress(ctxb, &lnrpc.NewAddressRequest{
|
|
|
|
Type: addrType,
|
|
|
|
})
|
2015-12-30 05:31:03 +03:00
|
|
|
if err != nil {
|
2016-06-29 23:01:08 +03:00
|
|
|
return err
|
2015-12-30 05:31:03 +03:00
|
|
|
}
|
|
|
|
|
2017-02-23 22:56:47 +03:00
|
|
|
printRespJSON(addr)
|
2016-06-29 23:01:08 +03:00
|
|
|
return nil
|
2015-12-30 05:31:03 +03:00
|
|
|
}
|
|
|
|
|
2019-03-05 16:22:30 +03:00
|
|
|
var estimateFeeCommand = cli.Command{
|
|
|
|
Name: "estimatefee",
|
|
|
|
Category: "On-chain",
|
|
|
|
Usage: "Get fee estimates for sending bitcoin on-chain to multiple addresses.",
|
|
|
|
ArgsUsage: "send-json-string [--conf_target=N]",
|
|
|
|
Description: `
|
|
|
|
Get fee estimates for sending a transaction paying the specified amount(s) to the passed address(es).
|
|
|
|
|
|
|
|
The send-json-string' param decodes addresses and the amount to send respectively in the following format:
|
|
|
|
|
|
|
|
'{"ExampleAddr": NumCoinsInSatoshis, "SecondAddr": NumCoins}'
|
|
|
|
`,
|
|
|
|
Flags: []cli.Flag{
|
|
|
|
cli.Int64Flag{
|
|
|
|
Name: "conf_target",
|
|
|
|
Usage: "(optional) the number of blocks that the transaction *should* " +
|
|
|
|
"confirm in",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Action: actionDecorator(estimateFees),
|
|
|
|
}
|
|
|
|
|
|
|
|
func estimateFees(ctx *cli.Context) error {
|
|
|
|
var amountToAddr map[string]int64
|
|
|
|
|
|
|
|
jsonMap := ctx.Args().First()
|
|
|
|
if err := json.Unmarshal([]byte(jsonMap), &amountToAddr); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
ctxb := context.Background()
|
|
|
|
client, cleanUp := getClient(ctx)
|
|
|
|
defer cleanUp()
|
|
|
|
|
|
|
|
resp, err := client.EstimateFee(ctxb, &lnrpc.EstimateFeeRequest{
|
|
|
|
AddrToAmount: amountToAddr,
|
|
|
|
TargetConf: int32(ctx.Int64("conf_target")),
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
printRespJSON(resp)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-02-24 16:32:33 +03:00
|
|
|
var sendCoinsCommand = cli.Command{
|
2017-03-03 01:23:16 +03:00
|
|
|
Name: "sendcoins",
|
2018-05-01 14:28:30 +03:00
|
|
|
Category: "On-chain",
|
2018-05-03 11:49:09 +03:00
|
|
|
Usage: "Send bitcoin on-chain to an address.",
|
2017-03-03 01:23:16 +03:00
|
|
|
ArgsUsage: "addr amt",
|
2017-11-23 22:40:14 +03:00
|
|
|
Description: `
|
2020-01-15 16:01:42 +03:00
|
|
|
Send amt coins in satoshis to the base58 or bech32 encoded bitcoin address addr.
|
2017-11-23 22:40:14 +03:00
|
|
|
|
2018-09-10 01:16:00 +03:00
|
|
|
Fees used when sending the transaction can be specified via the --conf_target, or
|
2017-11-23 22:40:14 +03:00
|
|
|
--sat_per_byte optional flags.
|
2018-09-10 01:16:00 +03:00
|
|
|
|
2017-11-23 22:40:14 +03:00
|
|
|
Positional arguments and flags can be used interchangeably but not at the same time!
|
|
|
|
`,
|
2016-06-29 21:29:21 +03:00
|
|
|
Flags: []cli.Flag{
|
|
|
|
cli.StringFlag{
|
2020-01-15 16:01:42 +03:00
|
|
|
Name: "addr",
|
|
|
|
Usage: "the base58 or bech32 encoded bitcoin address to send coins " +
|
|
|
|
"to on-chain",
|
2016-06-29 21:29:21 +03:00
|
|
|
},
|
2018-11-18 08:11:47 +03:00
|
|
|
cli.BoolFlag{
|
|
|
|
Name: "sweepall",
|
|
|
|
Usage: "if set, then the amount field will be ignored, " +
|
|
|
|
"and all the wallet will attempt to sweep all " +
|
|
|
|
"outputs within the wallet to the target " +
|
|
|
|
"address",
|
|
|
|
},
|
2017-03-03 01:23:16 +03:00
|
|
|
cli.Int64Flag{
|
2016-06-29 21:29:21 +03:00
|
|
|
Name: "amt",
|
|
|
|
Usage: "the number of bitcoin denominated in satoshis to send",
|
|
|
|
},
|
2017-11-23 22:40:14 +03:00
|
|
|
cli.Int64Flag{
|
|
|
|
Name: "conf_target",
|
|
|
|
Usage: "(optional) the number of blocks that the " +
|
|
|
|
"transaction *should* confirm in, will be " +
|
|
|
|
"used for fee estimation",
|
|
|
|
},
|
|
|
|
cli.Int64Flag{
|
|
|
|
Name: "sat_per_byte",
|
|
|
|
Usage: "(optional) a manual fee expressed in " +
|
|
|
|
"sat/byte that should be used when crafting " +
|
|
|
|
"the transaction",
|
|
|
|
},
|
2016-06-29 21:29:21 +03:00
|
|
|
},
|
2017-11-07 01:34:49 +03:00
|
|
|
Action: actionDecorator(sendCoins),
|
2016-06-29 21:29:21 +03:00
|
|
|
}
|
|
|
|
|
2016-06-29 23:01:08 +03:00
|
|
|
func sendCoins(ctx *cli.Context) error {
|
2017-03-03 01:23:16 +03:00
|
|
|
var (
|
|
|
|
addr string
|
|
|
|
amt int64
|
|
|
|
err error
|
|
|
|
)
|
|
|
|
args := ctx.Args()
|
|
|
|
|
|
|
|
if ctx.NArg() == 0 && ctx.NumFlags() == 0 {
|
|
|
|
cli.ShowCommandHelp(ctx, "sendcoins")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-11-23 22:40:14 +03:00
|
|
|
if ctx.IsSet("conf_target") && ctx.IsSet("sat_per_byte") {
|
|
|
|
return fmt.Errorf("either conf_target or sat_per_byte should be " +
|
|
|
|
"set, but not both")
|
|
|
|
}
|
|
|
|
|
2017-03-03 01:23:16 +03:00
|
|
|
switch {
|
|
|
|
case ctx.IsSet("addr"):
|
|
|
|
addr = ctx.String("addr")
|
|
|
|
case args.Present():
|
|
|
|
addr = args.First()
|
|
|
|
args = args.Tail()
|
|
|
|
default:
|
|
|
|
return fmt.Errorf("Address argument missing")
|
|
|
|
}
|
|
|
|
|
|
|
|
switch {
|
|
|
|
case ctx.IsSet("amt"):
|
|
|
|
amt = ctx.Int64("amt")
|
|
|
|
case args.Present():
|
|
|
|
amt, err = strconv.ParseInt(args.First(), 10, 64)
|
2018-11-18 08:11:47 +03:00
|
|
|
case !ctx.Bool("sweepall"):
|
2017-03-03 01:23:16 +03:00
|
|
|
return fmt.Errorf("Amount argument missing")
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("unable to decode amount: %v", err)
|
|
|
|
}
|
|
|
|
|
2018-11-18 08:11:47 +03:00
|
|
|
if amt != 0 && ctx.Bool("sweepall") {
|
|
|
|
return fmt.Errorf("amount cannot be set if attempting to " +
|
|
|
|
"sweep all coins out of the wallet")
|
|
|
|
}
|
|
|
|
|
2016-06-29 21:29:21 +03:00
|
|
|
ctxb := context.Background()
|
2017-01-30 01:51:30 +03:00
|
|
|
client, cleanUp := getClient(ctx)
|
|
|
|
defer cleanUp()
|
2016-06-29 21:29:21 +03:00
|
|
|
|
|
|
|
req := &lnrpc.SendCoinsRequest{
|
2017-11-23 22:40:14 +03:00
|
|
|
Addr: addr,
|
|
|
|
Amount: amt,
|
|
|
|
TargetConf: int32(ctx.Int64("conf_target")),
|
|
|
|
SatPerByte: ctx.Int64("sat_per_byte"),
|
2018-11-18 08:11:47 +03:00
|
|
|
SendAll: ctx.Bool("sweepall"),
|
2016-06-29 21:29:21 +03:00
|
|
|
}
|
|
|
|
txid, err := client.SendCoins(ctxb, req)
|
|
|
|
if err != nil {
|
2016-06-29 23:01:08 +03:00
|
|
|
return err
|
2016-06-29 21:29:21 +03:00
|
|
|
}
|
|
|
|
|
2017-02-23 22:56:47 +03:00
|
|
|
printRespJSON(txid)
|
2016-06-29 23:01:08 +03:00
|
|
|
return nil
|
2016-06-29 21:29:21 +03:00
|
|
|
}
|
|
|
|
|
2018-09-27 16:49:44 +03:00
|
|
|
var listUnspentCommand = cli.Command{
|
|
|
|
Name: "listunspent",
|
|
|
|
Category: "On-chain",
|
|
|
|
Usage: "List utxos available for spending.",
|
2019-02-12 00:02:35 +03:00
|
|
|
ArgsUsage: "[min-confs [max-confs]] [--unconfirmed_only]",
|
2018-09-27 16:49:44 +03:00
|
|
|
Description: `
|
|
|
|
For each spendable utxo currently in the wallet, with at least min_confs
|
2019-02-12 00:02:35 +03:00
|
|
|
confirmations, and at most max_confs confirmations, lists the txid,
|
|
|
|
index, amount, address, address type, scriptPubkey and number of
|
|
|
|
confirmations. Use --min_confs=0 to include unconfirmed coins. To list
|
|
|
|
all coins with at least min_confs confirmations, omit the second
|
|
|
|
argument or flag '--max_confs'. To list all confirmed and unconfirmed
|
2019-02-21 17:25:42 +03:00
|
|
|
coins, no arguments are required. To see only unconfirmed coins, use
|
2019-02-12 00:02:35 +03:00
|
|
|
'--unconfirmed_only' with '--min_confs' and '--max_confs' set to zero or
|
|
|
|
not present.
|
2018-09-27 16:49:44 +03:00
|
|
|
`,
|
|
|
|
Flags: []cli.Flag{
|
|
|
|
cli.Int64Flag{
|
|
|
|
Name: "min_confs",
|
|
|
|
Usage: "the minimum number of confirmations for a utxo",
|
|
|
|
},
|
|
|
|
cli.Int64Flag{
|
|
|
|
Name: "max_confs",
|
|
|
|
Usage: "the maximum number of confirmations for a utxo",
|
|
|
|
},
|
2019-02-12 00:02:35 +03:00
|
|
|
cli.BoolFlag{
|
|
|
|
Name: "unconfirmed_only",
|
|
|
|
Usage: "when min_confs and max_confs are zero, " +
|
|
|
|
"setting false implicitly overrides max_confs " +
|
|
|
|
"to be MaxInt32, otherwise max_confs remains " +
|
|
|
|
"zero. An error is returned if the value is " +
|
|
|
|
"true and both min_confs and max_confs are " +
|
2019-05-05 01:35:37 +03:00
|
|
|
"non-zero. (default: false)",
|
2019-02-12 00:02:35 +03:00
|
|
|
},
|
2018-09-27 16:49:44 +03:00
|
|
|
},
|
|
|
|
Action: actionDecorator(listUnspent),
|
|
|
|
}
|
|
|
|
|
|
|
|
func listUnspent(ctx *cli.Context) error {
|
|
|
|
var (
|
|
|
|
minConfirms int64
|
|
|
|
maxConfirms int64
|
|
|
|
err error
|
|
|
|
)
|
|
|
|
args := ctx.Args()
|
|
|
|
|
|
|
|
if ctx.IsSet("max_confs") && !ctx.IsSet("min_confs") {
|
|
|
|
return fmt.Errorf("max_confs cannot be set without " +
|
|
|
|
"min_confs being set")
|
|
|
|
}
|
|
|
|
|
|
|
|
switch {
|
|
|
|
case ctx.IsSet("min_confs"):
|
|
|
|
minConfirms = ctx.Int64("min_confs")
|
|
|
|
case args.Present():
|
|
|
|
minConfirms, err = strconv.ParseInt(args.First(), 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
cli.ShowCommandHelp(ctx, "listunspent")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
args = args.Tail()
|
|
|
|
}
|
|
|
|
|
|
|
|
switch {
|
|
|
|
case ctx.IsSet("max_confs"):
|
|
|
|
maxConfirms = ctx.Int64("max_confs")
|
|
|
|
case args.Present():
|
|
|
|
maxConfirms, err = strconv.ParseInt(args.First(), 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
cli.ShowCommandHelp(ctx, "listunspent")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
args = args.Tail()
|
|
|
|
}
|
|
|
|
|
2019-02-12 00:02:35 +03:00
|
|
|
unconfirmedOnly := ctx.Bool("unconfirmed_only")
|
|
|
|
|
|
|
|
// Force minConfirms and maxConfirms to be zero if unconfirmedOnly is
|
|
|
|
// true.
|
|
|
|
if unconfirmedOnly && (minConfirms != 0 || maxConfirms != 0) {
|
|
|
|
cli.ShowCommandHelp(ctx, "listunspent")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// When unconfirmedOnly is inactive, we will override maxConfirms to be
|
|
|
|
// a MaxInt32 to return all confirmed and unconfirmed utxos.
|
|
|
|
if maxConfirms == 0 && !unconfirmedOnly {
|
|
|
|
maxConfirms = math.MaxInt32
|
2018-09-27 16:49:44 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
ctxb := context.Background()
|
|
|
|
client, cleanUp := getClient(ctx)
|
|
|
|
defer cleanUp()
|
|
|
|
|
|
|
|
req := &lnrpc.ListUnspentRequest{
|
|
|
|
MinConfs: int32(minConfirms),
|
|
|
|
MaxConfs: int32(maxConfirms),
|
|
|
|
}
|
2019-02-02 05:02:02 +03:00
|
|
|
resp, err := client.ListUnspent(ctxb, req)
|
2018-09-27 16:49:44 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-02-02 05:02:02 +03:00
|
|
|
|
|
|
|
// Parse the response into the final json object that will be printed
|
|
|
|
// to stdout. At the moment, this filters out the raw txid bytes from
|
|
|
|
// each utxo's outpoint and only prints the txid string.
|
|
|
|
var listUnspentResp = struct {
|
|
|
|
Utxos []*Utxo `json:"utxos"`
|
|
|
|
}{
|
|
|
|
Utxos: make([]*Utxo, 0, len(resp.Utxos)),
|
|
|
|
}
|
|
|
|
for _, protoUtxo := range resp.Utxos {
|
|
|
|
utxo := NewUtxoFromProto(protoUtxo)
|
|
|
|
listUnspentResp.Utxos = append(listUnspentResp.Utxos, utxo)
|
|
|
|
}
|
|
|
|
|
|
|
|
printJSON(listUnspentResp)
|
|
|
|
|
2018-09-27 16:49:44 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-02-24 16:32:33 +03:00
|
|
|
var sendManyCommand = cli.Command{
|
2017-03-03 01:23:16 +03:00
|
|
|
Name: "sendmany",
|
2018-05-01 14:28:30 +03:00
|
|
|
Category: "On-chain",
|
2018-02-06 02:05:04 +03:00
|
|
|
Usage: "Send bitcoin on-chain to multiple addresses.",
|
2017-11-23 22:40:14 +03:00
|
|
|
ArgsUsage: "send-json-string [--conf_target=N] [--sat_per_byte=P]",
|
|
|
|
Description: `
|
|
|
|
Create and broadcast a transaction paying the specified amount(s) to the passed address(es).
|
|
|
|
|
2018-09-10 01:16:00 +03:00
|
|
|
The send-json-string' param decodes addresses and the amount to send
|
2017-11-23 22:40:14 +03:00
|
|
|
respectively in the following format:
|
|
|
|
|
|
|
|
'{"ExampleAddr": NumCoinsInSatoshis, "SecondAddr": NumCoins}'
|
|
|
|
`,
|
|
|
|
Flags: []cli.Flag{
|
|
|
|
cli.Int64Flag{
|
|
|
|
Name: "conf_target",
|
|
|
|
Usage: "(optional) the number of blocks that the transaction *should* " +
|
|
|
|
"confirm in, will be used for fee estimation",
|
|
|
|
},
|
|
|
|
cli.Int64Flag{
|
|
|
|
Name: "sat_per_byte",
|
|
|
|
Usage: "(optional) a manual fee expressed in sat/byte that should be " +
|
|
|
|
"used when crafting the transaction",
|
|
|
|
},
|
|
|
|
},
|
2017-11-07 01:34:49 +03:00
|
|
|
Action: actionDecorator(sendMany),
|
2015-12-30 05:31:03 +03:00
|
|
|
}
|
|
|
|
|
2016-06-29 23:01:08 +03:00
|
|
|
func sendMany(ctx *cli.Context) error {
|
2015-12-30 05:31:03 +03:00
|
|
|
var amountToAddr map[string]int64
|
|
|
|
|
2017-03-03 01:23:16 +03:00
|
|
|
jsonMap := ctx.Args().First()
|
2015-12-30 05:31:03 +03:00
|
|
|
if err := json.Unmarshal([]byte(jsonMap), &amountToAddr); err != nil {
|
2016-06-29 23:01:08 +03:00
|
|
|
return err
|
2015-12-30 05:31:03 +03:00
|
|
|
}
|
|
|
|
|
2017-11-23 22:40:14 +03:00
|
|
|
if ctx.IsSet("conf_target") && ctx.IsSet("sat_per_byte") {
|
|
|
|
return fmt.Errorf("either conf_target or sat_per_byte should be " +
|
|
|
|
"set, but not both")
|
|
|
|
}
|
|
|
|
|
2015-12-30 05:31:03 +03:00
|
|
|
ctxb := context.Background()
|
2017-01-30 01:51:30 +03:00
|
|
|
client, cleanUp := getClient(ctx)
|
|
|
|
defer cleanUp()
|
2015-12-30 05:31:03 +03:00
|
|
|
|
2017-02-23 22:09:34 +03:00
|
|
|
txid, err := client.SendMany(ctxb, &lnrpc.SendManyRequest{
|
|
|
|
AddrToAmount: amountToAddr,
|
2017-11-23 22:40:14 +03:00
|
|
|
TargetConf: int32(ctx.Int64("conf_target")),
|
|
|
|
SatPerByte: ctx.Int64("sat_per_byte"),
|
2017-02-23 22:09:34 +03:00
|
|
|
})
|
2015-12-30 05:31:03 +03:00
|
|
|
if err != nil {
|
2016-06-29 23:01:08 +03:00
|
|
|
return err
|
2015-12-30 05:31:03 +03:00
|
|
|
}
|
|
|
|
|
2017-02-23 22:56:47 +03:00
|
|
|
printRespJSON(txid)
|
2016-06-29 23:01:08 +03:00
|
|
|
return nil
|
2015-12-30 05:31:03 +03:00
|
|
|
}
|
2016-01-17 06:10:29 +03:00
|
|
|
|
2017-02-24 16:32:33 +03:00
|
|
|
var connectCommand = cli.Command{
|
2017-03-03 01:23:16 +03:00
|
|
|
Name: "connect",
|
2018-05-01 14:28:30 +03:00
|
|
|
Category: "Peers",
|
2018-05-03 11:49:09 +03:00
|
|
|
Usage: "Connect to a remote lnd peer.",
|
2017-03-03 01:23:16 +03:00
|
|
|
ArgsUsage: "<pubkey>@host",
|
2017-01-10 06:09:45 +03:00
|
|
|
Flags: []cli.Flag{
|
|
|
|
cli.BoolFlag{
|
|
|
|
Name: "perm",
|
2017-03-03 01:23:16 +03:00
|
|
|
Usage: "If set, the daemon will attempt to persistently " +
|
|
|
|
"connect to the target peer.\n" +
|
|
|
|
" If not, the call will be synchronous.",
|
2017-01-10 06:09:45 +03:00
|
|
|
},
|
|
|
|
},
|
2017-11-07 01:34:49 +03:00
|
|
|
Action: actionDecorator(connectPeer),
|
2016-01-17 06:10:29 +03:00
|
|
|
}
|
|
|
|
|
2016-06-29 23:01:08 +03:00
|
|
|
func connectPeer(ctx *cli.Context) error {
|
2016-01-17 06:10:29 +03:00
|
|
|
ctxb := context.Background()
|
2017-01-30 01:51:30 +03:00
|
|
|
client, cleanUp := getClient(ctx)
|
|
|
|
defer cleanUp()
|
2016-01-17 06:10:29 +03:00
|
|
|
|
2017-03-03 01:23:16 +03:00
|
|
|
targetAddress := ctx.Args().First()
|
2016-06-21 22:35:07 +03:00
|
|
|
splitAddr := strings.Split(targetAddress, "@")
|
2016-07-17 03:43:27 +03:00
|
|
|
if len(splitAddr) != 2 {
|
2016-10-28 05:42:47 +03:00
|
|
|
return fmt.Errorf("target address expected in format: " +
|
|
|
|
"pubkey@host:port")
|
2016-07-17 03:43:27 +03:00
|
|
|
}
|
|
|
|
|
2016-06-21 22:35:07 +03:00
|
|
|
addr := &lnrpc.LightningAddress{
|
2016-10-28 05:42:47 +03:00
|
|
|
Pubkey: splitAddr[0],
|
|
|
|
Host: splitAddr[1],
|
2016-06-21 22:35:07 +03:00
|
|
|
}
|
2017-01-10 06:09:45 +03:00
|
|
|
req := &lnrpc.ConnectPeerRequest{
|
|
|
|
Addr: addr,
|
|
|
|
Perm: ctx.Bool("perm"),
|
|
|
|
}
|
2016-01-17 06:10:29 +03:00
|
|
|
|
|
|
|
lnid, err := client.ConnectPeer(ctxb, req)
|
2017-05-05 15:05:30 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
printRespJSON(lnid)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var disconnectCommand = cli.Command{
|
|
|
|
Name: "disconnect",
|
2018-05-01 14:28:30 +03:00
|
|
|
Category: "Peers",
|
2018-05-03 11:49:09 +03:00
|
|
|
Usage: "Disconnect a remote lnd peer identified by public key.",
|
2017-05-05 15:05:30 +03:00
|
|
|
ArgsUsage: "<pubkey>",
|
2017-05-06 01:54:25 +03:00
|
|
|
Flags: []cli.Flag{
|
|
|
|
cli.StringFlag{
|
|
|
|
Name: "node_key",
|
|
|
|
Usage: "The hex-encoded compressed public key of the peer " +
|
|
|
|
"to disconnect from",
|
|
|
|
},
|
|
|
|
},
|
2017-11-07 01:34:49 +03:00
|
|
|
Action: actionDecorator(disconnectPeer),
|
2017-05-05 15:05:30 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func disconnectPeer(ctx *cli.Context) error {
|
|
|
|
ctxb := context.Background()
|
|
|
|
client, cleanUp := getClient(ctx)
|
|
|
|
defer cleanUp()
|
|
|
|
|
2017-05-06 01:54:25 +03:00
|
|
|
var pubKey string
|
|
|
|
switch {
|
|
|
|
case ctx.IsSet("node_key"):
|
|
|
|
pubKey = ctx.String("node_key")
|
|
|
|
case ctx.Args().Present():
|
|
|
|
pubKey = ctx.Args().First()
|
|
|
|
default:
|
|
|
|
return fmt.Errorf("must specify target public key")
|
2017-05-05 15:05:30 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
req := &lnrpc.DisconnectPeerRequest{
|
|
|
|
PubKey: pubKey,
|
|
|
|
}
|
|
|
|
|
|
|
|
lnid, err := client.DisconnectPeer(ctxb, req)
|
2016-01-17 06:10:29 +03:00
|
|
|
if err != nil {
|
2016-06-29 23:01:08 +03:00
|
|
|
return err
|
2016-01-17 06:10:29 +03:00
|
|
|
}
|
|
|
|
|
2017-02-23 22:56:47 +03:00
|
|
|
printRespJSON(lnid)
|
2016-06-29 23:01:08 +03:00
|
|
|
return nil
|
2016-01-17 06:10:29 +03:00
|
|
|
}
|
2016-06-21 22:35:07 +03:00
|
|
|
|
2017-03-03 01:23:16 +03:00
|
|
|
// TODO(roasbeef): change default number of confirmations
|
2017-02-24 16:32:33 +03:00
|
|
|
var openChannelCommand = cli.Command{
|
2018-05-01 14:28:30 +03:00
|
|
|
Name: "openchannel",
|
|
|
|
Category: "Channels",
|
|
|
|
Usage: "Open a channel to a node or an existing peer.",
|
2017-11-23 22:40:14 +03:00
|
|
|
Description: `
|
|
|
|
Attempt to open a new channel to an existing peer with the key node-key
|
|
|
|
optionally blocking until the channel is 'open'.
|
|
|
|
|
2018-01-10 10:27:49 +03:00
|
|
|
One can also connect to a node before opening a new channel to it by
|
|
|
|
setting its host:port via the --connect argument. For this to work,
|
|
|
|
the node_key must be provided, rather than the peer_id. This is optional.
|
|
|
|
|
2017-11-23 22:40:14 +03:00
|
|
|
The channel will be initialized with local-amt satoshis local and push-amt
|
2018-05-31 01:13:26 +03:00
|
|
|
satoshis for the remote node. Note that specifying push-amt means you give that
|
|
|
|
amount to the remote node as part of the channel opening. Once the channel is open,
|
|
|
|
a channelPoint (txid:vout) of the funding output is returned.
|
2017-11-23 22:40:14 +03:00
|
|
|
|
2019-12-17 22:58:28 +03:00
|
|
|
If the remote peer supports the option upfront shutdown feature bit (query
|
|
|
|
listpeers to see their supported feature bits), an address to enforce
|
|
|
|
payout of funds on cooperative close can optionally be provided. Note that
|
|
|
|
if you set this value, you will not be able to cooperatively close out to
|
|
|
|
another address.
|
|
|
|
|
2017-11-23 22:40:14 +03:00
|
|
|
One can manually set the fee to be used for the funding transaction via either
|
2018-02-14 08:23:01 +03:00
|
|
|
the --conf_target or --sat_per_byte arguments. This is optional.`,
|
2017-07-31 00:23:29 +03:00
|
|
|
ArgsUsage: "node-key local-amt push-amt",
|
2016-06-21 22:35:07 +03:00
|
|
|
Flags: []cli.Flag{
|
2016-09-14 01:36:27 +03:00
|
|
|
cli.StringFlag{
|
2016-10-28 05:42:47 +03:00
|
|
|
Name: "node_key",
|
2018-01-10 10:27:49 +03:00
|
|
|
Usage: "the identity public key of the target node/peer " +
|
2016-10-28 05:42:47 +03:00
|
|
|
"serialized in compressed format",
|
2016-06-21 22:35:07 +03:00
|
|
|
},
|
2018-01-10 10:27:49 +03:00
|
|
|
cli.StringFlag{
|
|
|
|
Name: "connect",
|
|
|
|
Usage: "(optional) the host:port of the target node",
|
|
|
|
},
|
2016-06-21 22:35:07 +03:00
|
|
|
cli.IntFlag{
|
|
|
|
Name: "local_amt",
|
|
|
|
Usage: "the number of satoshis the wallet should commit to the channel",
|
|
|
|
},
|
|
|
|
cli.IntFlag{
|
2017-01-10 06:06:07 +03:00
|
|
|
Name: "push_amt",
|
2018-07-09 22:40:47 +03:00
|
|
|
Usage: "the number of satoshis to give the remote side " +
|
|
|
|
"as part of the initial commitment state, " +
|
|
|
|
"this is equivalent to first opening a " +
|
|
|
|
"channel and sending the remote party funds, " +
|
|
|
|
"but done all in one step",
|
2016-06-21 22:35:07 +03:00
|
|
|
},
|
2016-07-08 01:35:06 +03:00
|
|
|
cli.BoolFlag{
|
|
|
|
Name: "block",
|
|
|
|
Usage: "block and wait until the channel is fully open",
|
|
|
|
},
|
2017-11-23 22:40:14 +03:00
|
|
|
cli.Int64Flag{
|
|
|
|
Name: "conf_target",
|
|
|
|
Usage: "(optional) the number of blocks that the " +
|
|
|
|
"transaction *should* confirm in, will be " +
|
|
|
|
"used for fee estimation",
|
|
|
|
},
|
|
|
|
cli.Int64Flag{
|
|
|
|
Name: "sat_per_byte",
|
|
|
|
Usage: "(optional) a manual fee expressed in " +
|
|
|
|
"sat/byte that should be used when crafting " +
|
|
|
|
"the transaction",
|
|
|
|
},
|
2017-11-14 04:08:22 +03:00
|
|
|
cli.BoolFlag{
|
|
|
|
Name: "private",
|
|
|
|
Usage: "make the channel private, such that it won't " +
|
|
|
|
"be announced to the greater network, and " +
|
|
|
|
"nodes other than the two channel endpoints " +
|
|
|
|
"must be explicitly told about it to be able " +
|
|
|
|
"to route through it",
|
|
|
|
},
|
2017-12-17 01:57:31 +03:00
|
|
|
cli.Int64Flag{
|
|
|
|
Name: "min_htlc_msat",
|
|
|
|
Usage: "(optional) the minimum value we will require " +
|
|
|
|
"for incoming HTLCs on the channel",
|
|
|
|
},
|
2018-03-14 16:28:54 +03:00
|
|
|
cli.Uint64Flag{
|
|
|
|
Name: "remote_csv_delay",
|
|
|
|
Usage: "(optional) the number of blocks we will require " +
|
|
|
|
"our channel counterparty to wait before accessing " +
|
|
|
|
"its funds in case of unilateral close. If this is " +
|
|
|
|
"not set, we will scale the value according to the " +
|
|
|
|
"channel size",
|
|
|
|
},
|
2018-08-10 05:40:11 +03:00
|
|
|
cli.Uint64Flag{
|
|
|
|
Name: "min_confs",
|
|
|
|
Usage: "(optional) the minimum number of confirmations " +
|
|
|
|
"each one of your outputs used for the funding " +
|
|
|
|
"transaction must satisfy",
|
2018-09-09 00:40:55 +03:00
|
|
|
Value: 1,
|
2018-08-10 05:40:11 +03:00
|
|
|
},
|
2019-12-17 22:58:28 +03:00
|
|
|
cli.StringFlag{
|
|
|
|
Name: "close_address",
|
|
|
|
Usage: "(optional) an address to enforce payout of our " +
|
|
|
|
"funds to on cooperative close. Note that if this " +
|
|
|
|
"value is set on channel open, you will *not* be " +
|
|
|
|
"able to cooperatively close to a different address.",
|
|
|
|
},
|
2016-06-21 22:35:07 +03:00
|
|
|
},
|
2017-11-07 01:34:49 +03:00
|
|
|
Action: actionDecorator(openChannel),
|
2016-06-21 22:35:07 +03:00
|
|
|
}
|
|
|
|
|
2016-06-29 23:01:08 +03:00
|
|
|
func openChannel(ctx *cli.Context) error {
|
2016-06-21 22:35:07 +03:00
|
|
|
// TODO(roasbeef): add deadline to context
|
|
|
|
ctxb := context.Background()
|
2017-01-30 01:51:30 +03:00
|
|
|
client, cleanUp := getClient(ctx)
|
|
|
|
defer cleanUp()
|
2017-03-03 22:33:16 +03:00
|
|
|
|
2017-03-03 01:23:16 +03:00
|
|
|
args := ctx.Args()
|
|
|
|
var err error
|
|
|
|
|
|
|
|
// Show command help if no arguments provided
|
|
|
|
if ctx.NArg() == 0 && ctx.NumFlags() == 0 {
|
|
|
|
cli.ShowCommandHelp(ctx, "openchannel")
|
|
|
|
return nil
|
|
|
|
}
|
2016-06-21 22:35:07 +03:00
|
|
|
|
2019-05-17 23:09:26 +03:00
|
|
|
minConfs := int32(ctx.Uint64("min_confs"))
|
2017-11-23 22:40:14 +03:00
|
|
|
req := &lnrpc.OpenChannelRequest{
|
2019-05-17 23:09:26 +03:00
|
|
|
TargetConf: int32(ctx.Int64("conf_target")),
|
|
|
|
SatPerByte: ctx.Int64("sat_per_byte"),
|
|
|
|
MinHtlcMsat: ctx.Int64("min_htlc_msat"),
|
|
|
|
RemoteCsvDelay: uint32(ctx.Uint64("remote_csv_delay")),
|
|
|
|
MinConfs: minConfs,
|
|
|
|
SpendUnconfirmed: minConfs == 0,
|
2019-12-17 22:58:28 +03:00
|
|
|
CloseAddress: ctx.String("close_address"),
|
2017-11-23 22:40:14 +03:00
|
|
|
}
|
2016-06-21 22:35:07 +03:00
|
|
|
|
2017-03-03 01:23:16 +03:00
|
|
|
switch {
|
|
|
|
case ctx.IsSet("node_key"):
|
2016-10-28 05:42:47 +03:00
|
|
|
nodePubHex, err := hex.DecodeString(ctx.String("node_key"))
|
2016-09-14 01:36:27 +03:00
|
|
|
if err != nil {
|
2017-03-03 01:23:16 +03:00
|
|
|
return fmt.Errorf("unable to decode node public key: %v", err)
|
2016-09-14 01:36:27 +03:00
|
|
|
}
|
2016-10-28 05:42:47 +03:00
|
|
|
req.NodePubkey = nodePubHex
|
2018-01-10 10:27:49 +03:00
|
|
|
|
2017-03-03 01:23:16 +03:00
|
|
|
case args.Present():
|
|
|
|
nodePubHex, err := hex.DecodeString(args.First())
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("unable to decode node public key: %v", err)
|
|
|
|
}
|
|
|
|
args = args.Tail()
|
|
|
|
req.NodePubkey = nodePubHex
|
|
|
|
default:
|
2017-03-03 22:33:16 +03:00
|
|
|
return fmt.Errorf("node id argument missing")
|
2017-03-03 01:23:16 +03:00
|
|
|
}
|
|
|
|
|
2018-01-10 10:27:49 +03:00
|
|
|
// As soon as we can confirm that the node's node_key was set, rather
|
|
|
|
// than the peer_id, we can check if the host:port was also set to
|
|
|
|
// connect to it before opening the channel.
|
|
|
|
if req.NodePubkey != nil && ctx.IsSet("connect") {
|
|
|
|
addr := &lnrpc.LightningAddress{
|
|
|
|
Pubkey: hex.EncodeToString(req.NodePubkey),
|
|
|
|
Host: ctx.String("connect"),
|
|
|
|
}
|
|
|
|
|
|
|
|
req := &lnrpc.ConnectPeerRequest{
|
|
|
|
Addr: addr,
|
|
|
|
Perm: false,
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if connecting to the node was successful.
|
|
|
|
// We discard the peer id returned as it is not needed.
|
|
|
|
_, err := client.ConnectPeer(ctxb, req)
|
|
|
|
if err != nil &&
|
|
|
|
!strings.Contains(err.Error(), "already connected") {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-03 01:23:16 +03:00
|
|
|
switch {
|
|
|
|
case ctx.IsSet("local_amt"):
|
|
|
|
req.LocalFundingAmount = int64(ctx.Int("local_amt"))
|
|
|
|
case args.Present():
|
|
|
|
req.LocalFundingAmount, err = strconv.ParseInt(args.First(), 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("unable to decode local amt: %v", err)
|
|
|
|
}
|
|
|
|
args = args.Tail()
|
|
|
|
default:
|
|
|
|
return fmt.Errorf("local amt argument missing")
|
|
|
|
}
|
|
|
|
|
2017-03-03 22:33:16 +03:00
|
|
|
if ctx.IsSet("push_amt") {
|
2017-03-03 01:23:16 +03:00
|
|
|
req.PushSat = int64(ctx.Int("push_amt"))
|
2017-03-03 22:33:16 +03:00
|
|
|
} else if args.Present() {
|
2017-03-03 01:23:16 +03:00
|
|
|
req.PushSat, err = strconv.ParseInt(args.First(), 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("unable to decode push amt: %v", err)
|
|
|
|
}
|
2016-09-14 01:36:27 +03:00
|
|
|
}
|
|
|
|
|
2017-11-14 04:08:22 +03:00
|
|
|
req.Private = ctx.Bool("private")
|
|
|
|
|
2016-07-08 01:35:06 +03:00
|
|
|
stream, err := client.OpenChannel(ctxb, req)
|
2016-06-21 22:35:07 +03:00
|
|
|
if err != nil {
|
2016-06-29 23:01:08 +03:00
|
|
|
return err
|
2016-06-21 22:35:07 +03:00
|
|
|
}
|
|
|
|
|
2016-07-08 01:35:06 +03:00
|
|
|
for {
|
|
|
|
resp, err := stream.Recv()
|
|
|
|
if err == io.EOF {
|
|
|
|
return nil
|
|
|
|
} else if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-08-31 02:54:49 +03:00
|
|
|
switch update := resp.Update.(type) {
|
2017-02-08 06:36:15 +03:00
|
|
|
case *lnrpc.OpenStatusUpdate_ChanPending:
|
|
|
|
txid, err := chainhash.NewHash(update.ChanPending.Txid)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-02-23 22:56:47 +03:00
|
|
|
printJSON(struct {
|
2017-02-08 06:36:15 +03:00
|
|
|
FundingTxid string `json:"funding_txid"`
|
|
|
|
}{
|
|
|
|
FundingTxid: txid.String(),
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
if !ctx.Bool("block") {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-08-31 02:54:49 +03:00
|
|
|
case *lnrpc.OpenStatusUpdate_ChanOpen:
|
|
|
|
channelPoint := update.ChanOpen.ChannelPoint
|
2018-01-11 07:59:30 +03:00
|
|
|
|
|
|
|
// A channel point's funding txid can be get/set as a
|
|
|
|
// byte slice or a string. In the case it is a string,
|
|
|
|
// decode it.
|
|
|
|
var txidHash []byte
|
|
|
|
switch channelPoint.GetFundingTxid().(type) {
|
|
|
|
case *lnrpc.ChannelPoint_FundingTxidBytes:
|
|
|
|
txidHash = channelPoint.GetFundingTxidBytes()
|
|
|
|
case *lnrpc.ChannelPoint_FundingTxidStr:
|
|
|
|
s := channelPoint.GetFundingTxidStr()
|
|
|
|
h, err := chainhash.NewHashFromStr(s)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
txidHash = h[:]
|
|
|
|
}
|
|
|
|
|
|
|
|
txid, err := chainhash.NewHash(txidHash)
|
2016-08-31 02:54:49 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
index := channelPoint.OutputIndex
|
2017-02-23 22:56:47 +03:00
|
|
|
printJSON(struct {
|
2016-08-31 02:54:49 +03:00
|
|
|
ChannelPoint string `json:"channel_point"`
|
|
|
|
}{
|
|
|
|
ChannelPoint: fmt.Sprintf("%v:%v", txid, index),
|
|
|
|
},
|
|
|
|
)
|
2016-07-08 01:35:06 +03:00
|
|
|
}
|
2016-06-21 22:35:07 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO(roasbeef): also allow short relative channel ID.
|
2017-02-23 22:56:47 +03:00
|
|
|
|
2017-02-24 16:32:33 +03:00
|
|
|
var closeChannelCommand = cli.Command{
|
2018-05-01 14:28:30 +03:00
|
|
|
Name: "closechannel",
|
|
|
|
Category: "Channels",
|
|
|
|
Usage: "Close an existing channel.",
|
2017-11-23 22:40:14 +03:00
|
|
|
Description: `
|
2018-01-24 05:34:29 +03:00
|
|
|
Close an existing channel. The channel can be closed either cooperatively,
|
2017-11-23 22:40:14 +03:00
|
|
|
or unilaterally (--force).
|
2018-01-24 05:34:29 +03:00
|
|
|
|
2017-11-23 22:40:14 +03:00
|
|
|
A unilateral channel closure means that the latest commitment
|
|
|
|
transaction will be broadcast to the network. As a result, any settled
|
2018-01-24 05:34:29 +03:00
|
|
|
funds will be time locked for a few blocks before they can be spent.
|
2017-11-23 22:40:14 +03:00
|
|
|
|
2019-12-09 16:43:59 +03:00
|
|
|
In the case of a cooperative closure, one can manually set the fee to
|
2017-11-23 22:40:14 +03:00
|
|
|
be used for the closing transaction via either the --conf_target or
|
|
|
|
--sat_per_byte arguments. This will be the starting value used during
|
2018-04-16 04:28:37 +03:00
|
|
|
fee negotiation. This is optional.
|
|
|
|
|
2019-12-09 16:43:59 +03:00
|
|
|
In the case of a cooperative closure, one can manually set the address
|
|
|
|
to deliver funds to upon closure. This is optional, and may only be used
|
|
|
|
if an upfront shutdown address has not already been set. If neither are
|
|
|
|
set the funds will be delivered to a new wallet address.
|
|
|
|
|
2018-04-16 04:28:37 +03:00
|
|
|
To view which funding_txids/output_indexes can be used for a channel close,
|
|
|
|
see the channel_point values within the listchannels command output.
|
|
|
|
The format for a channel_point is 'funding_txid:output_index'.`,
|
2019-04-14 20:09:52 +03:00
|
|
|
ArgsUsage: "funding_txid [output_index]",
|
2016-06-21 22:35:07 +03:00
|
|
|
Flags: []cli.Flag{
|
|
|
|
cli.StringFlag{
|
|
|
|
Name: "funding_txid",
|
|
|
|
Usage: "the txid of the channel's funding transaction",
|
|
|
|
},
|
|
|
|
cli.IntFlag{
|
|
|
|
Name: "output_index",
|
|
|
|
Usage: "the output index for the funding output of the funding " +
|
|
|
|
"transaction",
|
|
|
|
},
|
|
|
|
cli.BoolFlag{
|
2019-04-14 20:09:52 +03:00
|
|
|
Name: "force",
|
|
|
|
Usage: "attempt an uncooperative closure",
|
2016-06-21 22:35:07 +03:00
|
|
|
},
|
2016-07-08 01:35:06 +03:00
|
|
|
cli.BoolFlag{
|
|
|
|
Name: "block",
|
|
|
|
Usage: "block until the channel is closed",
|
|
|
|
},
|
2017-11-23 22:40:14 +03:00
|
|
|
cli.Int64Flag{
|
|
|
|
Name: "conf_target",
|
|
|
|
Usage: "(optional) the number of blocks that the " +
|
|
|
|
"transaction *should* confirm in, will be " +
|
|
|
|
"used for fee estimation",
|
|
|
|
},
|
|
|
|
cli.Int64Flag{
|
|
|
|
Name: "sat_per_byte",
|
|
|
|
Usage: "(optional) a manual fee expressed in " +
|
|
|
|
"sat/byte that should be used when crafting " +
|
|
|
|
"the transaction",
|
|
|
|
},
|
2019-12-09 16:43:59 +03:00
|
|
|
cli.StringFlag{
|
|
|
|
Name: "delivery_addr",
|
|
|
|
Usage: "(optional) an address to deliver funds " +
|
|
|
|
"upon cooperative channel closing, may only " +
|
|
|
|
"be used if an upfront shutdown addresss is not" +
|
|
|
|
"already set",
|
|
|
|
},
|
2016-06-21 22:35:07 +03:00
|
|
|
},
|
2017-11-07 01:34:49 +03:00
|
|
|
Action: actionDecorator(closeChannel),
|
2016-06-21 22:35:07 +03:00
|
|
|
}
|
|
|
|
|
2016-06-29 23:01:08 +03:00
|
|
|
func closeChannel(ctx *cli.Context) error {
|
2017-01-30 01:51:30 +03:00
|
|
|
client, cleanUp := getClient(ctx)
|
|
|
|
defer cleanUp()
|
2016-06-21 22:35:07 +03:00
|
|
|
|
2018-01-24 05:34:29 +03:00
|
|
|
// Show command help if no arguments and flags were provided.
|
2017-03-03 01:23:16 +03:00
|
|
|
if ctx.NArg() == 0 && ctx.NumFlags() == 0 {
|
2017-12-26 04:06:21 +03:00
|
|
|
cli.ShowCommandHelp(ctx, "closechannel")
|
2017-03-03 01:23:16 +03:00
|
|
|
return nil
|
2016-06-21 22:35:07 +03:00
|
|
|
}
|
|
|
|
|
2018-05-29 12:26:47 +03:00
|
|
|
channelPoint, err := parseChannelPoint(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-09-12 22:28:28 +03:00
|
|
|
// TODO(roasbeef): implement time deadline within server
|
2016-06-21 22:35:07 +03:00
|
|
|
req := &lnrpc.CloseChannelRequest{
|
2019-12-09 16:43:59 +03:00
|
|
|
ChannelPoint: channelPoint,
|
|
|
|
Force: ctx.Bool("force"),
|
|
|
|
TargetConf: int32(ctx.Int64("conf_target")),
|
|
|
|
SatPerByte: ctx.Int64("sat_per_byte"),
|
|
|
|
DeliveryAddress: ctx.String("delivery_addr"),
|
2017-03-03 01:23:16 +03:00
|
|
|
}
|
|
|
|
|
2018-01-24 05:34:29 +03:00
|
|
|
// After parsing the request, we'll spin up a goroutine that will
|
|
|
|
// retrieve the closing transaction ID when attempting to close the
|
|
|
|
// channel. We do this to because `executeChannelClose` can block, so we
|
|
|
|
// would like to present the closing transaction ID to the user as soon
|
|
|
|
// as it is broadcasted.
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
txidChan := make(chan string, 1)
|
|
|
|
|
|
|
|
wg.Add(1)
|
|
|
|
go func() {
|
|
|
|
defer wg.Done()
|
|
|
|
|
|
|
|
printJSON(struct {
|
|
|
|
ClosingTxid string `json:"closing_txid"`
|
|
|
|
}{
|
|
|
|
ClosingTxid: <-txidChan,
|
|
|
|
})
|
|
|
|
}()
|
|
|
|
|
2018-05-29 12:26:47 +03:00
|
|
|
err = executeChannelClose(client, req, txidChan, ctx.Bool("block"))
|
2018-01-24 05:34:29 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// In the case that the user did not provide the `block` flag, then we
|
|
|
|
// need to wait for the goroutine to be done to prevent it from being
|
|
|
|
// destroyed when exiting before printing the closing transaction ID.
|
|
|
|
wg.Wait()
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// executeChannelClose attempts to close the channel from a request. The closing
|
|
|
|
// transaction ID is sent through `txidChan` as soon as it is broadcasted to the
|
|
|
|
// network. The block boolean is used to determine if we should block until the
|
|
|
|
// closing transaction receives all of its required confirmations.
|
|
|
|
func executeChannelClose(client lnrpc.LightningClient, req *lnrpc.CloseChannelRequest,
|
|
|
|
txidChan chan<- string, block bool) error {
|
|
|
|
|
|
|
|
stream, err := client.CloseChannel(context.Background(), req)
|
2016-06-21 22:35:07 +03:00
|
|
|
if err != nil {
|
2016-06-29 23:01:08 +03:00
|
|
|
return err
|
2016-06-21 22:35:07 +03:00
|
|
|
}
|
|
|
|
|
2016-07-08 01:35:06 +03:00
|
|
|
for {
|
|
|
|
resp, err := stream.Recv()
|
|
|
|
if err == io.EOF {
|
|
|
|
return nil
|
|
|
|
} else if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-08-31 02:54:49 +03:00
|
|
|
|
|
|
|
switch update := resp.Update.(type) {
|
2017-02-08 06:36:15 +03:00
|
|
|
case *lnrpc.CloseStatusUpdate_ClosePending:
|
|
|
|
closingHash := update.ClosePending.Txid
|
|
|
|
txid, err := chainhash.NewHash(closingHash)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-01-24 05:34:29 +03:00
|
|
|
txidChan <- txid.String()
|
2017-02-08 06:36:15 +03:00
|
|
|
|
2018-01-24 05:34:29 +03:00
|
|
|
if !block {
|
2017-02-08 06:36:15 +03:00
|
|
|
return nil
|
|
|
|
}
|
2016-08-31 02:54:49 +03:00
|
|
|
case *lnrpc.CloseStatusUpdate_ChanClose:
|
2018-01-24 05:34:29 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var closeAllChannelsCommand = cli.Command{
|
2018-05-01 14:28:30 +03:00
|
|
|
Name: "closeallchannels",
|
|
|
|
Category: "Channels",
|
|
|
|
Usage: "Close all existing channels.",
|
2018-01-24 05:34:29 +03:00
|
|
|
Description: `
|
|
|
|
Close all existing channels.
|
|
|
|
|
|
|
|
Channels will be closed either cooperatively or unilaterally, depending
|
|
|
|
on whether the channel is active or not. If the channel is inactive, any
|
|
|
|
settled funds within it will be time locked for a few blocks before they
|
|
|
|
can be spent.
|
|
|
|
|
|
|
|
One can request to close inactive channels only by using the
|
|
|
|
--inactive_only flag.
|
|
|
|
|
|
|
|
By default, one is prompted for confirmation every time an inactive
|
|
|
|
channel is requested to be closed. To avoid this, one can set the
|
|
|
|
--force flag, which will only prompt for confirmation once for all
|
2019-05-10 16:03:22 +03:00
|
|
|
inactive channels and proceed to close them.
|
|
|
|
|
|
|
|
In the case of cooperative closures, one can manually set the fee to
|
|
|
|
be used for the closing transactions via either the --conf_target or
|
|
|
|
--sat_per_byte arguments. This will be the starting value used during
|
|
|
|
fee negotiation. This is optional.`,
|
2018-01-24 05:34:29 +03:00
|
|
|
Flags: []cli.Flag{
|
|
|
|
cli.BoolFlag{
|
|
|
|
Name: "inactive_only",
|
|
|
|
Usage: "close inactive channels only",
|
|
|
|
},
|
|
|
|
cli.BoolFlag{
|
|
|
|
Name: "force",
|
|
|
|
Usage: "ask for confirmation once before attempting " +
|
|
|
|
"to close existing channels",
|
|
|
|
},
|
2019-05-10 16:03:22 +03:00
|
|
|
cli.Int64Flag{
|
|
|
|
Name: "conf_target",
|
|
|
|
Usage: "(optional) the number of blocks that the " +
|
|
|
|
"closing transactions *should* confirm in, will be " +
|
|
|
|
"used for fee estimation",
|
|
|
|
},
|
|
|
|
cli.Int64Flag{
|
|
|
|
Name: "sat_per_byte",
|
|
|
|
Usage: "(optional) a manual fee expressed in " +
|
|
|
|
"sat/byte that should be used when crafting " +
|
|
|
|
"the closing transactions",
|
|
|
|
},
|
2018-01-24 05:34:29 +03:00
|
|
|
},
|
|
|
|
Action: actionDecorator(closeAllChannels),
|
|
|
|
}
|
|
|
|
|
|
|
|
func closeAllChannels(ctx *cli.Context) error {
|
|
|
|
client, cleanUp := getClient(ctx)
|
|
|
|
defer cleanUp()
|
|
|
|
|
|
|
|
listReq := &lnrpc.ListChannelsRequest{}
|
|
|
|
openChannels, err := client.ListChannels(context.Background(), listReq)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("unable to fetch open channels: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(openChannels.Channels) == 0 {
|
|
|
|
return errors.New("no open channels to close")
|
|
|
|
}
|
|
|
|
|
2018-03-13 22:11:30 +03:00
|
|
|
var channelsToClose []*lnrpc.Channel
|
2018-01-24 05:34:29 +03:00
|
|
|
|
|
|
|
switch {
|
|
|
|
case ctx.Bool("force") && ctx.Bool("inactive_only"):
|
|
|
|
msg := "Unilaterally close all inactive channels? The funds " +
|
|
|
|
"within these channels will be locked for some blocks " +
|
|
|
|
"(CSV delay) before they can be spent. (yes/no): "
|
|
|
|
|
|
|
|
confirmed := promptForConfirmation(msg)
|
|
|
|
|
|
|
|
// We can safely exit if the user did not confirm.
|
|
|
|
if !confirmed {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Go through the list of open channels and only add inactive
|
|
|
|
// channels to the closing list.
|
|
|
|
for _, channel := range openChannels.Channels {
|
|
|
|
if !channel.GetActive() {
|
|
|
|
channelsToClose = append(
|
|
|
|
channelsToClose, channel,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
case ctx.Bool("force"):
|
|
|
|
msg := "Close all active and inactive channels? Inactive " +
|
|
|
|
"channels will be closed unilaterally, so funds " +
|
|
|
|
"within them will be locked for a few blocks (CSV " +
|
|
|
|
"delay) before they can be spent. (yes/no): "
|
|
|
|
|
|
|
|
confirmed := promptForConfirmation(msg)
|
|
|
|
|
|
|
|
// We can safely exit if the user did not confirm.
|
|
|
|
if !confirmed {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
channelsToClose = openChannels.Channels
|
|
|
|
default:
|
|
|
|
// Go through the list of open channels and determine which
|
|
|
|
// should be added to the closing list.
|
|
|
|
for _, channel := range openChannels.Channels {
|
|
|
|
// If the channel is inactive, we'll attempt to
|
|
|
|
// unilaterally close the channel, so we should prompt
|
|
|
|
// the user for confirmation beforehand.
|
|
|
|
if !channel.GetActive() {
|
|
|
|
msg := fmt.Sprintf("Unilaterally close channel "+
|
|
|
|
"with node %s and channel point %s? "+
|
|
|
|
"The closing transaction will need %d "+
|
|
|
|
"confirmations before the funds can be "+
|
|
|
|
"spent. (yes/no): ", channel.RemotePubkey,
|
|
|
|
channel.ChannelPoint, channel.CsvDelay)
|
|
|
|
|
|
|
|
confirmed := promptForConfirmation(msg)
|
|
|
|
|
|
|
|
if confirmed {
|
|
|
|
channelsToClose = append(
|
|
|
|
channelsToClose, channel,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
} else if !ctx.Bool("inactive_only") {
|
|
|
|
// Otherwise, we'll only add active channels if
|
|
|
|
// we were not requested to close inactive
|
|
|
|
// channels only.
|
|
|
|
channelsToClose = append(
|
|
|
|
channelsToClose, channel,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// result defines the result of closing a channel. The closing
|
|
|
|
// transaction ID is populated if a channel is successfully closed.
|
|
|
|
// Otherwise, the error that prevented closing the channel is populated.
|
|
|
|
type result struct {
|
|
|
|
RemotePubKey string `json:"remote_pub_key"`
|
|
|
|
ChannelPoint string `json:"channel_point"`
|
|
|
|
ClosingTxid string `json:"closing_txid"`
|
|
|
|
FailErr string `json:"error"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// Launch each channel closure in a goroutine in order to execute them
|
|
|
|
// in parallel. Once they're all executed, we will print the results as
|
|
|
|
// they come.
|
|
|
|
resultChan := make(chan result, len(channelsToClose))
|
|
|
|
for _, channel := range channelsToClose {
|
2018-03-13 22:11:30 +03:00
|
|
|
go func(channel *lnrpc.Channel) {
|
2018-01-24 05:34:29 +03:00
|
|
|
res := result{}
|
|
|
|
res.RemotePubKey = channel.RemotePubkey
|
|
|
|
res.ChannelPoint = channel.ChannelPoint
|
|
|
|
defer func() {
|
|
|
|
resultChan <- res
|
|
|
|
}()
|
|
|
|
|
|
|
|
// Parse the channel point in order to create the close
|
|
|
|
// channel request.
|
|
|
|
s := strings.Split(res.ChannelPoint, ":")
|
|
|
|
if len(s) != 2 {
|
|
|
|
res.FailErr = "expected channel point with " +
|
|
|
|
"format txid:index"
|
|
|
|
return
|
|
|
|
}
|
|
|
|
index, err := strconv.ParseUint(s[1], 10, 32)
|
2016-08-31 02:54:49 +03:00
|
|
|
if err != nil {
|
2018-01-24 05:34:29 +03:00
|
|
|
res.FailErr = fmt.Sprintf("unable to parse "+
|
|
|
|
"channel point output index: %v", err)
|
|
|
|
return
|
2016-08-31 02:54:49 +03:00
|
|
|
}
|
|
|
|
|
2018-01-24 05:34:29 +03:00
|
|
|
req := &lnrpc.CloseChannelRequest{
|
|
|
|
ChannelPoint: &lnrpc.ChannelPoint{
|
|
|
|
FundingTxid: &lnrpc.ChannelPoint_FundingTxidStr{
|
|
|
|
FundingTxidStr: s[0],
|
|
|
|
},
|
|
|
|
OutputIndex: uint32(index),
|
|
|
|
},
|
2019-05-10 16:03:22 +03:00
|
|
|
Force: !channel.GetActive(),
|
|
|
|
TargetConf: int32(ctx.Int64("conf_target")),
|
|
|
|
SatPerByte: ctx.Int64("sat_per_byte"),
|
2018-01-24 05:34:29 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
txidChan := make(chan string, 1)
|
|
|
|
err = executeChannelClose(client, req, txidChan, false)
|
|
|
|
if err != nil {
|
|
|
|
res.FailErr = fmt.Sprintf("unable to close "+
|
|
|
|
"channel: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
res.ClosingTxid = <-txidChan
|
|
|
|
}(channel)
|
|
|
|
}
|
|
|
|
|
|
|
|
for range channelsToClose {
|
|
|
|
res := <-resultChan
|
|
|
|
printJSON(res)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// promptForConfirmation continuously prompts the user for the message until
|
|
|
|
// receiving a response of "yes" or "no" and returns their answer as a bool.
|
|
|
|
func promptForConfirmation(msg string) bool {
|
|
|
|
reader := bufio.NewReader(os.Stdin)
|
|
|
|
|
|
|
|
for {
|
|
|
|
fmt.Print(msg)
|
|
|
|
|
|
|
|
answer, err := reader.ReadString('\n')
|
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
answer = strings.ToLower(strings.TrimSpace(answer))
|
|
|
|
|
|
|
|
switch {
|
|
|
|
case answer == "yes":
|
|
|
|
return true
|
|
|
|
case answer == "no":
|
|
|
|
return false
|
|
|
|
default:
|
|
|
|
continue
|
2016-08-31 02:54:49 +03:00
|
|
|
}
|
2016-07-08 01:35:06 +03:00
|
|
|
}
|
2016-06-21 22:35:07 +03:00
|
|
|
}
|
|
|
|
|
2018-05-29 12:26:47 +03:00
|
|
|
var abandonChannelCommand = cli.Command{
|
|
|
|
Name: "abandonchannel",
|
|
|
|
Category: "Channels",
|
|
|
|
Usage: "Abandons an existing channel.",
|
|
|
|
Description: `
|
2018-11-12 18:03:00 +03:00
|
|
|
Removes all channel state from the database except for a close
|
2018-05-29 12:26:47 +03:00
|
|
|
summary. This method can be used to get rid of permanently unusable
|
2018-11-12 18:03:00 +03:00
|
|
|
channels due to bugs fixed in newer versions of lnd.
|
|
|
|
|
2018-05-29 12:26:47 +03:00
|
|
|
Only available when lnd is built in debug mode.
|
|
|
|
|
|
|
|
To view which funding_txids/output_indexes can be used for this command,
|
|
|
|
see the channel_point values within the listchannels command output.
|
|
|
|
The format for a channel_point is 'funding_txid:output_index'.`,
|
|
|
|
ArgsUsage: "funding_txid [output_index]",
|
|
|
|
Flags: []cli.Flag{
|
|
|
|
cli.StringFlag{
|
|
|
|
Name: "funding_txid",
|
|
|
|
Usage: "the txid of the channel's funding transaction",
|
|
|
|
},
|
|
|
|
cli.IntFlag{
|
|
|
|
Name: "output_index",
|
|
|
|
Usage: "the output index for the funding output of the funding " +
|
|
|
|
"transaction",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Action: actionDecorator(abandonChannel),
|
|
|
|
}
|
|
|
|
|
|
|
|
func abandonChannel(ctx *cli.Context) error {
|
|
|
|
ctxb := context.Background()
|
|
|
|
|
|
|
|
client, cleanUp := getClient(ctx)
|
|
|
|
defer cleanUp()
|
|
|
|
|
|
|
|
// Show command help if no arguments and flags were provided.
|
|
|
|
if ctx.NArg() == 0 && ctx.NumFlags() == 0 {
|
|
|
|
cli.ShowCommandHelp(ctx, "abandonchannel")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
channelPoint, err := parseChannelPoint(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
req := &lnrpc.AbandonChannelRequest{
|
|
|
|
ChannelPoint: channelPoint,
|
|
|
|
}
|
|
|
|
|
|
|
|
resp, err := client.AbandonChannel(ctxb, req)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
printRespJSON(resp)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// parseChannelPoint parses a funding txid and output index from the command
|
|
|
|
// line. Both named options as well as unnamed parameters are supported.
|
|
|
|
func parseChannelPoint(ctx *cli.Context) (*lnrpc.ChannelPoint, error) {
|
|
|
|
channelPoint := &lnrpc.ChannelPoint{}
|
|
|
|
|
|
|
|
args := ctx.Args()
|
|
|
|
|
|
|
|
switch {
|
|
|
|
case ctx.IsSet("funding_txid"):
|
|
|
|
channelPoint.FundingTxid = &lnrpc.ChannelPoint_FundingTxidStr{
|
|
|
|
FundingTxidStr: ctx.String("funding_txid"),
|
|
|
|
}
|
|
|
|
case args.Present():
|
|
|
|
channelPoint.FundingTxid = &lnrpc.ChannelPoint_FundingTxidStr{
|
|
|
|
FundingTxidStr: args.First(),
|
|
|
|
}
|
|
|
|
args = args.Tail()
|
|
|
|
default:
|
|
|
|
return nil, fmt.Errorf("funding txid argument missing")
|
|
|
|
}
|
|
|
|
|
|
|
|
switch {
|
|
|
|
case ctx.IsSet("output_index"):
|
|
|
|
channelPoint.OutputIndex = uint32(ctx.Int("output_index"))
|
|
|
|
case args.Present():
|
|
|
|
index, err := strconv.ParseUint(args.First(), 10, 32)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("unable to decode output index: %v", err)
|
|
|
|
}
|
|
|
|
channelPoint.OutputIndex = uint32(index)
|
|
|
|
default:
|
|
|
|
channelPoint.OutputIndex = 0
|
|
|
|
}
|
|
|
|
|
|
|
|
return channelPoint, nil
|
|
|
|
}
|
|
|
|
|
2017-02-24 16:32:33 +03:00
|
|
|
var listPeersCommand = cli.Command{
|
2018-05-01 14:28:30 +03:00
|
|
|
Name: "listpeers",
|
|
|
|
Category: "Peers",
|
|
|
|
Usage: "List all active, currently connected peers.",
|
|
|
|
Action: actionDecorator(listPeers),
|
2016-06-21 22:35:07 +03:00
|
|
|
}
|
|
|
|
|
2016-06-29 23:01:08 +03:00
|
|
|
func listPeers(ctx *cli.Context) error {
|
2016-06-21 22:35:07 +03:00
|
|
|
ctxb := context.Background()
|
2017-01-30 01:51:30 +03:00
|
|
|
client, cleanUp := getClient(ctx)
|
|
|
|
defer cleanUp()
|
2016-06-21 22:35:07 +03:00
|
|
|
|
|
|
|
req := &lnrpc.ListPeersRequest{}
|
|
|
|
resp, err := client.ListPeers(ctxb, req)
|
|
|
|
if err != nil {
|
2016-06-29 23:01:08 +03:00
|
|
|
return err
|
2016-06-21 22:35:07 +03:00
|
|
|
}
|
|
|
|
|
2017-02-23 22:56:47 +03:00
|
|
|
printRespJSON(resp)
|
2016-06-29 23:01:08 +03:00
|
|
|
return nil
|
2016-06-21 22:35:07 +03:00
|
|
|
}
|
|
|
|
|
2017-10-12 12:42:06 +03:00
|
|
|
var createCommand = cli.Command{
|
2018-05-01 14:28:30 +03:00
|
|
|
Name: "create",
|
2018-05-03 11:49:09 +03:00
|
|
|
Category: "Startup",
|
|
|
|
Usage: "Initialize a wallet when starting lnd for the first time.",
|
2018-02-02 07:52:40 +03:00
|
|
|
Description: `
|
|
|
|
The create command is used to initialize an lnd wallet from scratch for
|
|
|
|
the very first time. This is interactive command with one required
|
|
|
|
argument (the password), and one optional argument (the mnemonic
|
2018-09-10 01:16:00 +03:00
|
|
|
passphrase).
|
2018-02-02 07:52:40 +03:00
|
|
|
|
|
|
|
The first argument (the password) is required and MUST be greater than
|
|
|
|
8 characters. This will be used to encrypt the wallet within lnd. This
|
|
|
|
MUST be remembered as it will be required to fully start up the daemon.
|
|
|
|
|
|
|
|
The second argument is an optional 24-word mnemonic derived from BIP
|
|
|
|
39. If provided, then the internal wallet will use the seed derived
|
|
|
|
from this mnemonic to generate all keys.
|
|
|
|
|
|
|
|
This command returns a 24-word seed in the scenario that NO mnemonic
|
|
|
|
was provided by the user. This should be written down as it can be used
|
|
|
|
to potentially recover all on-chain funds, and most off-chain funds as
|
|
|
|
well.
|
2018-12-10 07:16:11 +03:00
|
|
|
|
|
|
|
Finally, it's also possible to use this command and a set of static
|
|
|
|
channel backups to trigger a recover attempt for the provided Static
|
|
|
|
Channel Backups. Only one of the three parameters will be accepted. See
|
|
|
|
the restorechanbackup command for further details w.r.t the format
|
|
|
|
accepted.
|
2018-02-02 07:52:40 +03:00
|
|
|
`,
|
2018-12-10 07:16:11 +03:00
|
|
|
Flags: []cli.Flag{
|
|
|
|
cli.StringFlag{
|
|
|
|
Name: "single_backup",
|
|
|
|
Usage: "a hex encoded single channel backup obtained " +
|
|
|
|
"from exportchanbackup",
|
|
|
|
},
|
|
|
|
cli.StringFlag{
|
|
|
|
Name: "multi_backup",
|
|
|
|
Usage: "a hex encoded multi-channel backup obtained " +
|
|
|
|
"from exportchanbackup",
|
|
|
|
},
|
|
|
|
cli.StringFlag{
|
|
|
|
Name: "multi_file",
|
|
|
|
Usage: "the path to a multi-channel back up file",
|
|
|
|
},
|
|
|
|
},
|
2017-11-07 01:34:49 +03:00
|
|
|
Action: actionDecorator(create),
|
2017-10-12 12:42:06 +03:00
|
|
|
}
|
|
|
|
|
2018-02-02 07:52:40 +03:00
|
|
|
// monowidthColumns takes a set of words, and the number of desired columns,
|
|
|
|
// and returns a new set of words that have had white space appended to the
|
|
|
|
// word in order to create a mono-width column.
|
|
|
|
func monowidthColumns(words []string, ncols int) []string {
|
|
|
|
// Determine max size of words in each column.
|
|
|
|
colWidths := make([]int, ncols)
|
|
|
|
for i, word := range words {
|
|
|
|
col := i % ncols
|
|
|
|
curWidth := colWidths[col]
|
|
|
|
if len(word) > curWidth {
|
|
|
|
colWidths[col] = len(word)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Append whitespace to each word to make columns mono-width.
|
|
|
|
finalWords := make([]string, len(words))
|
|
|
|
for i, word := range words {
|
|
|
|
col := i % ncols
|
|
|
|
width := colWidths[col]
|
|
|
|
|
|
|
|
diff := width - len(word)
|
|
|
|
finalWords[i] = word + strings.Repeat(" ", diff)
|
|
|
|
}
|
|
|
|
|
|
|
|
return finalWords
|
|
|
|
}
|
|
|
|
|
2017-10-12 12:42:06 +03:00
|
|
|
func create(ctx *cli.Context) error {
|
|
|
|
ctxb := context.Background()
|
|
|
|
client, cleanUp := getWalletUnlockerClient(ctx)
|
|
|
|
defer cleanUp()
|
|
|
|
|
2019-11-09 19:37:03 +03:00
|
|
|
var (
|
|
|
|
chanBackups *lnrpc.ChanBackupSnapshot
|
|
|
|
|
|
|
|
// We use var restoreSCB to track if we will be including an SCB
|
|
|
|
// recovery in the init wallet request.
|
|
|
|
restoreSCB = false
|
|
|
|
)
|
|
|
|
|
|
|
|
backups, err := parseChanBackups(ctx)
|
|
|
|
|
|
|
|
// We'll check to see if the user provided any static channel backups (SCB),
|
|
|
|
// if so, we will warn the user that SCB recovery closes all open channels
|
|
|
|
// and ask them to confirm their intention.
|
|
|
|
// If the user agrees, we'll add the SCB recovery onto the final init wallet
|
|
|
|
// request.
|
|
|
|
switch {
|
|
|
|
// parseChanBackups returns an errMissingBackup error (which we ignore) if
|
|
|
|
// the user did not request a SCB recovery.
|
|
|
|
case err == errMissingChanBackup:
|
|
|
|
|
|
|
|
// Passed an invalid channel backup file.
|
|
|
|
case err != nil:
|
|
|
|
return fmt.Errorf("unable to parse chan backups: %v", err)
|
|
|
|
|
|
|
|
// We have an SCB recovery option with a valid backup file.
|
|
|
|
default:
|
|
|
|
|
|
|
|
warningLoop:
|
|
|
|
for {
|
|
|
|
|
|
|
|
fmt.Println()
|
|
|
|
fmt.Printf("WARNING: You are attempting to restore from a " +
|
|
|
|
"static channel backup (SCB) file.\nThis action will CLOSE " +
|
|
|
|
"all currently open channels, and you will pay on-chain fees." +
|
|
|
|
"\n\nAre you sure you want to recover funds from a" +
|
|
|
|
" static channel backup? (Enter y/n): ")
|
|
|
|
|
|
|
|
reader := bufio.NewReader(os.Stdin)
|
|
|
|
answer, err := reader.ReadString('\n')
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
answer = strings.TrimSpace(answer)
|
|
|
|
answer = strings.ToLower(answer)
|
|
|
|
|
|
|
|
switch answer {
|
|
|
|
case "y":
|
|
|
|
restoreSCB = true
|
|
|
|
break warningLoop
|
|
|
|
case "n":
|
2019-12-05 06:24:35 +03:00
|
|
|
fmt.Println("Aborting SCB recovery")
|
|
|
|
return nil
|
2019-11-09 19:37:03 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Proceed with SCB recovery.
|
|
|
|
if restoreSCB {
|
|
|
|
fmt.Println("Static Channel Backup (SCB) recovery selected!")
|
|
|
|
if backups != nil {
|
|
|
|
switch {
|
|
|
|
case backups.GetChanBackups() != nil:
|
|
|
|
singleBackup := backups.GetChanBackups()
|
|
|
|
chanBackups = &lnrpc.ChanBackupSnapshot{
|
|
|
|
SingleChanBackups: singleBackup,
|
|
|
|
}
|
|
|
|
|
|
|
|
case backups.GetMultiChanBackup() != nil:
|
|
|
|
multiBackup := backups.GetMultiChanBackup()
|
|
|
|
chanBackups = &lnrpc.ChanBackupSnapshot{
|
|
|
|
MultiChanBackup: &lnrpc.MultiChanBackup{
|
|
|
|
MultiChanBackup: multiBackup,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-07-15 17:27:32 +03:00
|
|
|
walletPassword, err := capturePassword(
|
|
|
|
"Input wallet password: ", false, walletunlocker.ValidatePassword,
|
|
|
|
)
|
2017-10-12 12:42:06 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-02-21 17:25:42 +03:00
|
|
|
|
2018-02-02 07:52:40 +03:00
|
|
|
// Next, we'll see if the user has 24-word mnemonic they want to use to
|
|
|
|
// derive a seed within the wallet.
|
|
|
|
var (
|
|
|
|
hasMnemonic bool
|
|
|
|
)
|
|
|
|
|
|
|
|
mnemonicCheck:
|
|
|
|
for {
|
|
|
|
fmt.Println()
|
|
|
|
fmt.Printf("Do you have an existing cipher seed " +
|
|
|
|
"mnemonic you want to use? (Enter y/n): ")
|
|
|
|
|
|
|
|
reader := bufio.NewReader(os.Stdin)
|
|
|
|
answer, err := reader.ReadString('\n')
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Println()
|
|
|
|
|
|
|
|
answer = strings.TrimSpace(answer)
|
|
|
|
answer = strings.ToLower(answer)
|
|
|
|
|
|
|
|
switch answer {
|
|
|
|
case "y":
|
|
|
|
hasMnemonic = true
|
|
|
|
break mnemonicCheck
|
|
|
|
case "n":
|
|
|
|
hasMnemonic = false
|
|
|
|
break mnemonicCheck
|
|
|
|
}
|
2017-10-12 12:42:06 +03:00
|
|
|
}
|
2018-02-02 07:52:40 +03:00
|
|
|
|
|
|
|
// If the user *does* have an existing seed they want to use, then
|
|
|
|
// we'll read that in directly from the terminal.
|
|
|
|
var (
|
|
|
|
cipherSeedMnemonic []string
|
|
|
|
aezeedPass []byte
|
2018-03-27 00:15:04 +03:00
|
|
|
recoveryWindow int32
|
2018-02-02 07:52:40 +03:00
|
|
|
)
|
|
|
|
if hasMnemonic {
|
|
|
|
// We'll now prompt the user to enter in their 24-word
|
|
|
|
// mnemonic.
|
|
|
|
fmt.Printf("Input your 24-word mnemonic separated by spaces: ")
|
|
|
|
reader := bufio.NewReader(os.Stdin)
|
|
|
|
mnemonic, err := reader.ReadString('\n')
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// We'll trim off extra spaces, and ensure the mnemonic is all
|
|
|
|
// lower case, then populate our request.
|
|
|
|
mnemonic = strings.TrimSpace(mnemonic)
|
|
|
|
mnemonic = strings.ToLower(mnemonic)
|
|
|
|
|
|
|
|
cipherSeedMnemonic = strings.Split(mnemonic, " ")
|
|
|
|
|
|
|
|
fmt.Println()
|
|
|
|
|
2018-03-13 22:42:44 +03:00
|
|
|
if len(cipherSeedMnemonic) != 24 {
|
|
|
|
return fmt.Errorf("wrong cipher seed mnemonic "+
|
|
|
|
"length: got %v words, expecting %v words",
|
2018-03-13 23:01:11 +03:00
|
|
|
len(cipherSeedMnemonic), 24)
|
2018-03-13 22:42:44 +03:00
|
|
|
}
|
|
|
|
|
2018-02-02 07:52:40 +03:00
|
|
|
// Additionally, the user may have a passphrase, that will also
|
|
|
|
// need to be provided so the daemon can properly decipher the
|
|
|
|
// cipher seed.
|
|
|
|
fmt.Printf("Input your cipher seed passphrase (press enter if " +
|
|
|
|
"your seed doesn't have a passphrase): ")
|
|
|
|
passphrase, err := terminal.ReadPassword(int(syscall.Stdin))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
aezeedPass = []byte(passphrase)
|
|
|
|
|
2018-03-27 00:15:04 +03:00
|
|
|
for {
|
|
|
|
fmt.Println()
|
|
|
|
fmt.Printf("Input an optional address look-ahead "+
|
|
|
|
"used to scan for used keys (default %d): ",
|
|
|
|
defaultRecoveryWindow)
|
|
|
|
|
|
|
|
reader := bufio.NewReader(os.Stdin)
|
|
|
|
answer, err := reader.ReadString('\n')
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Println()
|
|
|
|
|
|
|
|
answer = strings.TrimSpace(answer)
|
|
|
|
|
|
|
|
if len(answer) == 0 {
|
|
|
|
recoveryWindow = defaultRecoveryWindow
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
lookAhead, err := strconv.Atoi(answer)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println("Unable to parse recovery "+
|
|
|
|
"window: %v", err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
recoveryWindow = int32(lookAhead)
|
|
|
|
break
|
|
|
|
}
|
2018-02-02 07:52:40 +03:00
|
|
|
} else {
|
|
|
|
// Otherwise, if the user doesn't have a mnemonic that they
|
|
|
|
// want to use, we'll generate a fresh one with the GenSeed
|
|
|
|
// command.
|
|
|
|
fmt.Println("Your cipher seed can optionally be encrypted.")
|
2019-07-15 17:27:32 +03:00
|
|
|
|
|
|
|
instruction := "Input your passphrase if you wish to encrypt it " +
|
2018-02-02 07:52:40 +03:00
|
|
|
"(or press enter to proceed without a cipher seed " +
|
2019-07-15 17:27:32 +03:00
|
|
|
"passphrase): "
|
|
|
|
aezeedPass, err = capturePassword(
|
|
|
|
instruction, true, func(_ []byte) error { return nil },
|
|
|
|
)
|
2018-02-02 07:52:40 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Println()
|
|
|
|
fmt.Println("Generating fresh cipher seed...")
|
|
|
|
fmt.Println()
|
|
|
|
|
|
|
|
genSeedReq := &lnrpc.GenSeedRequest{
|
2019-07-15 17:27:32 +03:00
|
|
|
AezeedPassphrase: aezeedPass,
|
2018-02-02 07:52:40 +03:00
|
|
|
}
|
|
|
|
seedResp, err := client.GenSeed(ctxb, genSeedReq)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("unable to generate seed: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
cipherSeedMnemonic = seedResp.CipherSeedMnemonic
|
|
|
|
}
|
|
|
|
|
|
|
|
// Before we initialize the wallet, we'll display the cipher seed to
|
|
|
|
// the user so they can write it down.
|
|
|
|
mnemonicWords := cipherSeedMnemonic
|
|
|
|
|
|
|
|
fmt.Println("!!!YOU MUST WRITE DOWN THIS SEED TO BE ABLE TO " +
|
|
|
|
"RESTORE THE WALLET!!!\n")
|
|
|
|
|
|
|
|
fmt.Println("---------------BEGIN LND CIPHER SEED---------------")
|
|
|
|
|
|
|
|
numCols := 4
|
|
|
|
colWords := monowidthColumns(mnemonicWords, numCols)
|
|
|
|
for i := 0; i < len(colWords); i += numCols {
|
|
|
|
fmt.Printf("%2d. %3s %2d. %3s %2d. %3s %2d. %3s\n",
|
|
|
|
i+1, colWords[i], i+2, colWords[i+1], i+3,
|
|
|
|
colWords[i+2], i+4, colWords[i+3])
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Println("---------------END LND CIPHER SEED-----------------")
|
|
|
|
|
|
|
|
fmt.Println("\n!!!YOU MUST WRITE DOWN THIS SEED TO BE ABLE TO " +
|
|
|
|
"RESTORE THE WALLET!!!")
|
|
|
|
|
|
|
|
// With either the user's prior cipher seed, or a newly generated one,
|
|
|
|
// we'll go ahead and initialize the wallet.
|
|
|
|
req := &lnrpc.InitWalletRequest{
|
2019-07-15 17:27:32 +03:00
|
|
|
WalletPassword: walletPassword,
|
2018-02-02 07:52:40 +03:00
|
|
|
CipherSeedMnemonic: cipherSeedMnemonic,
|
|
|
|
AezeedPassphrase: aezeedPass,
|
2018-03-27 00:15:04 +03:00
|
|
|
RecoveryWindow: recoveryWindow,
|
2018-12-10 07:16:11 +03:00
|
|
|
ChannelBackups: chanBackups,
|
2018-02-02 07:52:40 +03:00
|
|
|
}
|
|
|
|
if _, err := client.InitWallet(ctxb, req); err != nil {
|
2017-10-12 12:42:06 +03:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-02-02 07:52:40 +03:00
|
|
|
fmt.Println("\nlnd successfully initialized!")
|
|
|
|
|
2017-10-12 12:42:06 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-07-15 17:27:32 +03:00
|
|
|
// capturePassword returns a password value that has been entered twice by the
|
|
|
|
// user, to ensure that the user knows what password they have entered. The user
|
|
|
|
// will be prompted to retry until the passwords match. If the optional param is
|
|
|
|
// true, the function may return an empty byte array if the user opts against
|
|
|
|
// using a password.
|
|
|
|
func capturePassword(instruction string, optional bool,
|
|
|
|
validate func([]byte) error) ([]byte, error) {
|
|
|
|
|
|
|
|
for {
|
|
|
|
fmt.Printf(instruction)
|
|
|
|
password, err := terminal.ReadPassword(int(syscall.Stdin))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
fmt.Println()
|
|
|
|
|
|
|
|
// Do not require users to repeat password if
|
|
|
|
// it is optional and they are not using one.
|
|
|
|
if len(password) == 0 && optional {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the password provided is not valid, restart
|
|
|
|
// password capture process from the beginning.
|
|
|
|
if err := validate(password); err != nil {
|
|
|
|
fmt.Println(err.Error())
|
|
|
|
fmt.Println()
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Println("Confirm password:")
|
|
|
|
passwordConfirmed, err := terminal.ReadPassword(
|
|
|
|
int(syscall.Stdin),
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
fmt.Println()
|
|
|
|
|
|
|
|
if bytes.Equal(password, passwordConfirmed) {
|
|
|
|
return password, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Println("Passwords don't match, " +
|
|
|
|
"please try again")
|
|
|
|
fmt.Println()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-12 12:42:06 +03:00
|
|
|
var unlockCommand = cli.Command{
|
2018-05-01 14:28:30 +03:00
|
|
|
Name: "unlock",
|
2018-05-03 11:49:09 +03:00
|
|
|
Category: "Startup",
|
|
|
|
Usage: "Unlock an encrypted wallet at startup.",
|
2018-02-02 07:52:40 +03:00
|
|
|
Description: `
|
|
|
|
The unlock command is used to decrypt lnd's wallet state in order to
|
|
|
|
start up. This command MUST be run after booting up lnd before it's
|
|
|
|
able to carry out its duties. An exception is if a user is running with
|
2018-09-05 04:53:37 +03:00
|
|
|
--noseedbackup, then a default passphrase will be used.
|
2018-02-02 07:52:40 +03:00
|
|
|
`,
|
2018-03-27 00:15:04 +03:00
|
|
|
Flags: []cli.Flag{
|
|
|
|
cli.IntFlag{
|
|
|
|
Name: "recovery_window",
|
|
|
|
Usage: "address lookahead to resume recovery rescan, " +
|
|
|
|
"value should be non-zero -- To recover all " +
|
|
|
|
"funds, this should be greater than the " +
|
|
|
|
"maximum number of consecutive, unused " +
|
|
|
|
"addresses ever generated by the wallet.",
|
|
|
|
},
|
|
|
|
},
|
2017-11-07 01:34:49 +03:00
|
|
|
Action: actionDecorator(unlock),
|
2017-10-12 12:42:06 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func unlock(ctx *cli.Context) error {
|
|
|
|
ctxb := context.Background()
|
|
|
|
client, cleanUp := getWalletUnlockerClient(ctx)
|
|
|
|
defer cleanUp()
|
|
|
|
|
|
|
|
fmt.Printf("Input wallet password: ")
|
2017-12-19 01:04:04 +03:00
|
|
|
pw, err := terminal.ReadPassword(int(syscall.Stdin))
|
2017-10-12 12:42:06 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
fmt.Println()
|
|
|
|
|
2018-03-27 00:15:04 +03:00
|
|
|
args := ctx.Args()
|
|
|
|
|
|
|
|
// Parse the optional recovery window if it is specified. By default,
|
|
|
|
// the recovery window will be 0, indicating no lookahead should be
|
|
|
|
// used.
|
|
|
|
var recoveryWindow int32
|
|
|
|
switch {
|
|
|
|
case ctx.IsSet("recovery_window"):
|
|
|
|
recoveryWindow = int32(ctx.Int64("recovery_window"))
|
|
|
|
case args.Present():
|
|
|
|
window, err := strconv.ParseInt(args.First(), 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
recoveryWindow = int32(window)
|
|
|
|
}
|
|
|
|
|
2017-10-12 12:42:06 +03:00
|
|
|
req := &lnrpc.UnlockWalletRequest{
|
2018-02-02 07:52:40 +03:00
|
|
|
WalletPassword: pw,
|
2018-03-27 00:15:04 +03:00
|
|
|
RecoveryWindow: recoveryWindow,
|
2017-10-12 12:42:06 +03:00
|
|
|
}
|
|
|
|
_, err = client.UnlockWallet(ctxb, req)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-02-02 07:52:40 +03:00
|
|
|
fmt.Println("\nlnd successfully unlocked!")
|
|
|
|
|
2018-12-10 07:16:11 +03:00
|
|
|
// TODO(roasbeef): add ability to accept hex single and multi backups
|
|
|
|
|
2017-10-12 12:42:06 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-04-20 10:14:51 +03:00
|
|
|
var changePasswordCommand = cli.Command{
|
|
|
|
Name: "changepassword",
|
|
|
|
Category: "Startup",
|
|
|
|
Usage: "Change an encrypted wallet's password at startup.",
|
|
|
|
Description: `
|
|
|
|
The changepassword command is used to Change lnd's encrypted wallet's
|
|
|
|
password. It will automatically unlock the daemon if the password change
|
|
|
|
is successful.
|
|
|
|
|
|
|
|
If one did not specify a password for their wallet (running lnd with
|
2018-09-05 04:53:37 +03:00
|
|
|
--noseedbackup), one must restart their daemon without
|
|
|
|
--noseedbackup and use this command. The "current password" field
|
2018-04-20 10:14:51 +03:00
|
|
|
should be left empty.
|
|
|
|
`,
|
|
|
|
Action: actionDecorator(changePassword),
|
|
|
|
}
|
|
|
|
|
|
|
|
func changePassword(ctx *cli.Context) error {
|
|
|
|
ctxb := context.Background()
|
|
|
|
client, cleanUp := getWalletUnlockerClient(ctx)
|
|
|
|
defer cleanUp()
|
|
|
|
|
|
|
|
fmt.Printf("Input current wallet password: ")
|
|
|
|
currentPw, err := terminal.ReadPassword(int(syscall.Stdin))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
fmt.Println()
|
|
|
|
|
|
|
|
fmt.Printf("Input new wallet password: ")
|
|
|
|
newPw, err := terminal.ReadPassword(int(syscall.Stdin))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
fmt.Println()
|
|
|
|
|
|
|
|
fmt.Printf("Confirm new wallet password: ")
|
|
|
|
confirmPw, err := terminal.ReadPassword(int(syscall.Stdin))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
fmt.Println()
|
|
|
|
|
|
|
|
if !bytes.Equal(newPw, confirmPw) {
|
|
|
|
return fmt.Errorf("passwords don't match")
|
|
|
|
}
|
|
|
|
|
|
|
|
req := &lnrpc.ChangePasswordRequest{
|
|
|
|
CurrentPassword: currentPw,
|
|
|
|
NewPassword: newPw,
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = client.ChangePassword(ctxb, req)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-02-24 16:32:33 +03:00
|
|
|
var walletBalanceCommand = cli.Command{
|
2018-05-01 14:28:30 +03:00
|
|
|
Name: "walletbalance",
|
|
|
|
Category: "Wallet",
|
2018-05-03 11:49:09 +03:00
|
|
|
Usage: "Compute and display the wallet's current balance.",
|
2018-05-01 14:28:30 +03:00
|
|
|
Action: actionDecorator(walletBalance),
|
2016-06-21 22:35:07 +03:00
|
|
|
}
|
|
|
|
|
2016-06-29 23:01:08 +03:00
|
|
|
func walletBalance(ctx *cli.Context) error {
|
2016-06-21 22:35:07 +03:00
|
|
|
ctxb := context.Background()
|
2017-01-30 01:51:30 +03:00
|
|
|
client, cleanUp := getClient(ctx)
|
|
|
|
defer cleanUp()
|
2016-06-21 22:35:07 +03:00
|
|
|
|
2018-02-18 02:42:17 +03:00
|
|
|
req := &lnrpc.WalletBalanceRequest{}
|
2016-06-21 22:35:07 +03:00
|
|
|
resp, err := client.WalletBalance(ctxb, req)
|
|
|
|
if err != nil {
|
2016-06-29 23:01:08 +03:00
|
|
|
return err
|
2016-06-21 22:35:07 +03:00
|
|
|
}
|
|
|
|
|
2017-02-23 22:56:47 +03:00
|
|
|
printRespJSON(resp)
|
2016-06-29 23:01:08 +03:00
|
|
|
return nil
|
2016-06-21 22:35:07 +03:00
|
|
|
}
|
2016-07-06 04:58:41 +03:00
|
|
|
|
2017-02-24 16:32:33 +03:00
|
|
|
var channelBalanceCommand = cli.Command{
|
2018-05-01 14:28:30 +03:00
|
|
|
Name: "channelbalance",
|
|
|
|
Category: "Channels",
|
2018-04-20 10:14:51 +03:00
|
|
|
Usage: "Returns the sum of the total available channel balance across " +
|
2018-05-03 11:49:09 +03:00
|
|
|
"all open channels.",
|
2018-04-20 10:14:51 +03:00
|
|
|
Action: actionDecorator(channelBalance),
|
2016-09-15 21:59:51 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func channelBalance(ctx *cli.Context) error {
|
|
|
|
ctxb := context.Background()
|
2017-01-30 01:51:30 +03:00
|
|
|
client, cleanUp := getClient(ctx)
|
|
|
|
defer cleanUp()
|
2016-09-15 21:59:51 +03:00
|
|
|
|
|
|
|
req := &lnrpc.ChannelBalanceRequest{}
|
|
|
|
resp, err := client.ChannelBalance(ctxb, req)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-02-23 22:56:47 +03:00
|
|
|
printRespJSON(resp)
|
2016-09-15 21:59:51 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-02-24 16:32:33 +03:00
|
|
|
var getInfoCommand = cli.Command{
|
2017-03-03 01:23:16 +03:00
|
|
|
Name: "getinfo",
|
2018-05-03 11:49:09 +03:00
|
|
|
Usage: "Returns basic information related to the active daemon.",
|
2017-11-07 01:34:49 +03:00
|
|
|
Action: actionDecorator(getInfo),
|
2016-07-06 04:58:41 +03:00
|
|
|
}
|
|
|
|
|
2019-01-02 18:10:12 +03:00
|
|
|
type chain struct {
|
|
|
|
Chain string `json:"chain"`
|
|
|
|
Network string `json:"network"`
|
|
|
|
}
|
|
|
|
|
2016-07-06 04:58:41 +03:00
|
|
|
func getInfo(ctx *cli.Context) error {
|
|
|
|
ctxb := context.Background()
|
2017-01-30 01:51:30 +03:00
|
|
|
client, cleanUp := getClient(ctx)
|
|
|
|
defer cleanUp()
|
2016-07-06 04:58:41 +03:00
|
|
|
|
|
|
|
req := &lnrpc.GetInfoRequest{}
|
|
|
|
resp, err := client.GetInfo(ctxb, req)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-12-20 12:05:27 +03:00
|
|
|
printRespJSON(resp)
|
2016-07-06 04:58:41 +03:00
|
|
|
return nil
|
|
|
|
}
|
2016-07-08 01:35:58 +03:00
|
|
|
|
2017-02-24 16:32:33 +03:00
|
|
|
var pendingChannelsCommand = cli.Command{
|
2018-05-01 14:28:30 +03:00
|
|
|
Name: "pendingchannels",
|
|
|
|
Category: "Channels",
|
2018-05-03 11:49:09 +03:00
|
|
|
Usage: "Display information pertaining to pending channels.",
|
2018-05-01 14:28:30 +03:00
|
|
|
Action: actionDecorator(pendingChannels),
|
2016-07-08 01:35:58 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func pendingChannels(ctx *cli.Context) error {
|
|
|
|
ctxb := context.Background()
|
2017-01-30 01:51:30 +03:00
|
|
|
client, cleanUp := getClient(ctx)
|
|
|
|
defer cleanUp()
|
2016-07-08 01:35:58 +03:00
|
|
|
|
2018-01-04 23:20:25 +03:00
|
|
|
req := &lnrpc.PendingChannelsRequest{}
|
2016-07-08 01:35:58 +03:00
|
|
|
resp, err := client.PendingChannels(ctxb, req)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-02-23 22:56:47 +03:00
|
|
|
printRespJSON(resp)
|
2016-07-08 01:35:58 +03:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2016-07-13 03:47:24 +03:00
|
|
|
|
2017-02-24 16:32:33 +03:00
|
|
|
var listChannelsCommand = cli.Command{
|
2018-05-01 14:28:30 +03:00
|
|
|
Name: "listchannels",
|
|
|
|
Category: "Channels",
|
2018-05-03 11:49:09 +03:00
|
|
|
Usage: "List all open channels.",
|
2016-09-26 06:04:58 +03:00
|
|
|
Flags: []cli.Flag{
|
|
|
|
cli.BoolFlag{
|
2018-03-13 22:11:25 +03:00
|
|
|
Name: "active_only",
|
2016-09-26 06:04:58 +03:00
|
|
|
Usage: "only list channels which are currently active",
|
|
|
|
},
|
2018-03-13 22:11:25 +03:00
|
|
|
cli.BoolFlag{
|
|
|
|
Name: "inactive_only",
|
|
|
|
Usage: "only list channels which are currently inactive",
|
|
|
|
},
|
|
|
|
cli.BoolFlag{
|
|
|
|
Name: "public_only",
|
|
|
|
Usage: "only list channels which are currently public",
|
|
|
|
},
|
|
|
|
cli.BoolFlag{
|
|
|
|
Name: "private_only",
|
|
|
|
Usage: "only list channels which are currently private",
|
|
|
|
},
|
2016-09-26 06:04:58 +03:00
|
|
|
},
|
2017-11-07 01:34:49 +03:00
|
|
|
Action: actionDecorator(listChannels),
|
2016-09-26 06:04:58 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func listChannels(ctx *cli.Context) error {
|
|
|
|
ctxb := context.Background()
|
2017-01-30 01:51:30 +03:00
|
|
|
client, cleanUp := getClient(ctx)
|
|
|
|
defer cleanUp()
|
2016-09-26 06:04:58 +03:00
|
|
|
|
2018-03-13 22:11:25 +03:00
|
|
|
req := &lnrpc.ListChannelsRequest{
|
|
|
|
ActiveOnly: ctx.Bool("active_only"),
|
|
|
|
InactiveOnly: ctx.Bool("inactive_only"),
|
|
|
|
PublicOnly: ctx.Bool("public_only"),
|
|
|
|
PrivateOnly: ctx.Bool("private_only"),
|
|
|
|
}
|
|
|
|
|
2016-09-26 06:04:58 +03:00
|
|
|
resp, err := client.ListChannels(ctxb, req)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-01-18 00:39:30 +03:00
|
|
|
// TODO(roasbeef): defer close the client for the all
|
|
|
|
|
2017-02-23 22:56:47 +03:00
|
|
|
printRespJSON(resp)
|
2016-09-26 06:04:58 +03:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-05-24 12:35:53 +03:00
|
|
|
var closedChannelsCommand = cli.Command{
|
|
|
|
Name: "closedchannels",
|
|
|
|
Category: "Channels",
|
|
|
|
Usage: "List all closed channels.",
|
2018-07-09 22:40:47 +03:00
|
|
|
Flags: []cli.Flag{
|
2018-05-24 12:35:53 +03:00
|
|
|
cli.BoolFlag{
|
|
|
|
Name: "cooperative",
|
|
|
|
Usage: "list channels that were closed cooperatively",
|
|
|
|
},
|
|
|
|
cli.BoolFlag{
|
2018-07-09 22:40:47 +03:00
|
|
|
Name: "local_force",
|
2018-05-24 12:35:53 +03:00
|
|
|
Usage: "list channels that were force-closed " +
|
2018-07-09 22:40:47 +03:00
|
|
|
"by the local node",
|
2018-05-24 12:35:53 +03:00
|
|
|
},
|
|
|
|
cli.BoolFlag{
|
2018-07-09 22:40:47 +03:00
|
|
|
Name: "remote_force",
|
2018-05-24 12:35:53 +03:00
|
|
|
Usage: "list channels that were force-closed " +
|
2018-07-09 22:40:47 +03:00
|
|
|
"by the remote node",
|
2018-05-24 12:35:53 +03:00
|
|
|
},
|
|
|
|
cli.BoolFlag{
|
2018-07-09 22:40:47 +03:00
|
|
|
Name: "breach",
|
2018-05-24 12:35:53 +03:00
|
|
|
Usage: "list channels for which the remote node " +
|
2018-07-09 22:40:47 +03:00
|
|
|
"attempted to broadcast a prior " +
|
|
|
|
"revoked channel state",
|
2018-05-24 12:35:53 +03:00
|
|
|
},
|
|
|
|
cli.BoolFlag{
|
|
|
|
Name: "funding_canceled",
|
|
|
|
Usage: "list channels that were never fully opened",
|
|
|
|
},
|
2018-05-29 12:26:47 +03:00
|
|
|
cli.BoolFlag{
|
|
|
|
Name: "abandoned",
|
|
|
|
Usage: "list channels that were abandoned by " +
|
|
|
|
"the local node",
|
|
|
|
},
|
2018-05-24 12:35:53 +03:00
|
|
|
},
|
2018-07-09 22:40:47 +03:00
|
|
|
Action: actionDecorator(closedChannels),
|
2018-05-24 12:35:53 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func closedChannels(ctx *cli.Context) error {
|
|
|
|
ctxb := context.Background()
|
|
|
|
client, cleanUp := getClient(ctx)
|
|
|
|
defer cleanUp()
|
|
|
|
|
|
|
|
req := &lnrpc.ClosedChannelsRequest{
|
|
|
|
Cooperative: ctx.Bool("cooperative"),
|
|
|
|
LocalForce: ctx.Bool("local_force"),
|
|
|
|
RemoteForce: ctx.Bool("remote_force"),
|
|
|
|
Breach: ctx.Bool("breach"),
|
2019-10-03 18:22:43 +03:00
|
|
|
FundingCanceled: ctx.Bool("funding_canceled"),
|
2018-05-29 12:26:47 +03:00
|
|
|
Abandoned: ctx.Bool("abandoned"),
|
2018-05-24 12:35:53 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
resp, err := client.ClosedChannels(ctxb, req)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
printRespJSON(resp)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-11-18 14:08:42 +03:00
|
|
|
var (
|
|
|
|
cltvLimitFlag = cli.UintFlag{
|
|
|
|
Name: "cltv_limit",
|
|
|
|
Usage: "the maximum time lock that may be used for " +
|
|
|
|
"this payment",
|
|
|
|
}
|
|
|
|
|
|
|
|
lastHopFlag = cli.StringFlag{
|
2020-01-10 04:57:52 +03:00
|
|
|
Name: "last_hop",
|
|
|
|
Usage: "pubkey of the last hop (penultimate node in the path) " +
|
|
|
|
"to route through for this payment",
|
2019-11-18 14:08:42 +03:00
|
|
|
}
|
2020-01-14 11:33:35 +03:00
|
|
|
|
|
|
|
dataFlag = cli.StringFlag{
|
|
|
|
Name: "data",
|
|
|
|
Usage: "attach custom data to the payment. The required " +
|
|
|
|
"format is: <record_id>=<hex_value>,<record_id>=" +
|
|
|
|
"<hex_value>,.. For example: --data 3438382=0a21ff. " +
|
|
|
|
"Custom record ids start from 65536.",
|
|
|
|
}
|
2019-11-18 14:08:42 +03:00
|
|
|
)
|
2019-02-13 14:26:29 +03:00
|
|
|
|
2019-04-18 16:27:54 +03:00
|
|
|
// paymentFlags returns common flags for sendpayment and payinvoice.
|
|
|
|
func paymentFlags() []cli.Flag {
|
|
|
|
return []cli.Flag{
|
|
|
|
cli.StringFlag{
|
|
|
|
Name: "pay_req",
|
|
|
|
Usage: "a zpay32 encoded payment request to fulfill",
|
|
|
|
},
|
|
|
|
cli.Int64Flag{
|
|
|
|
Name: "fee_limit",
|
|
|
|
Usage: "maximum fee allowed in satoshis when " +
|
|
|
|
"sending the payment",
|
|
|
|
},
|
|
|
|
cli.Int64Flag{
|
|
|
|
Name: "fee_limit_percent",
|
|
|
|
Usage: "percentage of the payment's amount used as " +
|
|
|
|
"the maximum fee allowed when sending the " +
|
|
|
|
"payment",
|
|
|
|
},
|
|
|
|
cltvLimitFlag,
|
2019-11-18 14:08:42 +03:00
|
|
|
lastHopFlag,
|
2019-04-18 16:27:54 +03:00
|
|
|
cli.Uint64Flag{
|
|
|
|
Name: "outgoing_chan_id",
|
|
|
|
Usage: "short channel id of the outgoing channel to " +
|
|
|
|
"use for the first hop of the payment",
|
|
|
|
Value: 0,
|
|
|
|
},
|
|
|
|
cli.BoolFlag{
|
|
|
|
Name: "force, f",
|
|
|
|
Usage: "will skip payment request confirmation",
|
|
|
|
},
|
2019-11-25 16:13:21 +03:00
|
|
|
cli.BoolFlag{
|
|
|
|
Name: "allow_self_payment",
|
|
|
|
Usage: "allow sending a circular payment to self",
|
|
|
|
},
|
2020-01-14 11:33:35 +03:00
|
|
|
dataFlag,
|
2019-04-18 16:27:54 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-24 16:32:33 +03:00
|
|
|
var sendPaymentCommand = cli.Command{
|
2018-05-01 14:28:30 +03:00
|
|
|
Name: "sendpayment",
|
|
|
|
Category: "Payments",
|
2018-05-03 11:49:09 +03:00
|
|
|
Usage: "Send a payment over lightning.",
|
2018-01-20 04:33:03 +03:00
|
|
|
Description: `
|
|
|
|
Send a payment over Lightning. One can either specify the full
|
|
|
|
parameters of the payment, or just use a payment request which encodes
|
|
|
|
all the payment details.
|
|
|
|
|
|
|
|
If payment isn't manually specified, then only a payment request needs
|
|
|
|
to be passed using the --pay_req argument.
|
|
|
|
|
|
|
|
If the payment *is* manually specified, then all four alternative
|
|
|
|
arguments need to be specified in order to complete the payment:
|
|
|
|
* --dest=N
|
|
|
|
* --amt=A
|
2018-01-28 03:23:42 +03:00
|
|
|
* --final_cltv_delta=T
|
2018-01-20 04:33:03 +03:00
|
|
|
* --payment_hash=H
|
|
|
|
`,
|
|
|
|
ArgsUsage: "dest amt payment_hash final_cltv_delta | --pay_req=[payment request]",
|
2019-04-18 16:27:54 +03:00
|
|
|
Flags: append(paymentFlags(),
|
2016-07-13 03:47:24 +03:00
|
|
|
cli.StringFlag{
|
2016-09-21 02:19:15 +03:00
|
|
|
Name: "dest, d",
|
|
|
|
Usage: "the compressed identity pubkey of the " +
|
|
|
|
"payment recipient",
|
2016-07-13 03:47:24 +03:00
|
|
|
},
|
2017-03-03 01:23:16 +03:00
|
|
|
cli.Int64Flag{
|
2016-07-13 03:47:24 +03:00
|
|
|
Name: "amt, a",
|
|
|
|
Usage: "number of satoshis to send",
|
|
|
|
},
|
|
|
|
cli.StringFlag{
|
|
|
|
Name: "payment_hash, r",
|
|
|
|
Usage: "the hash to use within the payment's HTLC",
|
|
|
|
},
|
2018-01-20 04:32:32 +03:00
|
|
|
cli.Int64Flag{
|
|
|
|
Name: "final_cltv_delta",
|
|
|
|
Usage: "the number of blocks the last hop has to reveal the preimage",
|
|
|
|
},
|
2019-12-05 14:27:17 +03:00
|
|
|
cli.BoolFlag{
|
2020-01-16 15:13:59 +03:00
|
|
|
Name: "keysend",
|
2019-12-05 14:27:17 +03:00
|
|
|
Usage: "will generate a pre-image and encode it in the sphinx packet, a dest must be set [experimental]",
|
|
|
|
},
|
2019-04-18 16:27:54 +03:00
|
|
|
),
|
2017-02-24 16:32:33 +03:00
|
|
|
Action: sendPayment,
|
2016-07-13 03:47:24 +03:00
|
|
|
}
|
|
|
|
|
2018-04-19 17:28:13 +03:00
|
|
|
// retrieveFeeLimit retrieves the fee limit based on the different fee limit
|
|
|
|
// flags passed.
|
|
|
|
func retrieveFeeLimit(ctx *cli.Context) (*lnrpc.FeeLimit, error) {
|
|
|
|
switch {
|
|
|
|
case ctx.IsSet("fee_limit") && ctx.IsSet("fee_limit_percent"):
|
|
|
|
return nil, fmt.Errorf("either fee_limit or fee_limit_percent " +
|
|
|
|
"can be set, but not both")
|
|
|
|
case ctx.IsSet("fee_limit"):
|
|
|
|
return &lnrpc.FeeLimit{
|
|
|
|
Limit: &lnrpc.FeeLimit_Fixed{
|
|
|
|
Fixed: ctx.Int64("fee_limit"),
|
|
|
|
},
|
|
|
|
}, nil
|
|
|
|
case ctx.IsSet("fee_limit_percent"):
|
|
|
|
return &lnrpc.FeeLimit{
|
|
|
|
Limit: &lnrpc.FeeLimit_Percent{
|
|
|
|
Percent: ctx.Int64("fee_limit_percent"),
|
|
|
|
},
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Since the fee limit flags aren't required, we don't return an error
|
|
|
|
// if they're not set.
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2019-04-18 16:27:54 +03:00
|
|
|
func confirmPayReq(resp *lnrpc.PayReq, amt int64) error {
|
2018-06-28 08:02:01 +03:00
|
|
|
fmt.Printf("Description: %v\n", resp.GetDescription())
|
2018-09-12 01:27:54 +03:00
|
|
|
fmt.Printf("Amount (in satoshis): %v\n", amt)
|
2018-06-28 08:02:01 +03:00
|
|
|
fmt.Printf("Destination: %v\n", resp.GetDestination())
|
|
|
|
|
|
|
|
confirm := promptForConfirmation("Confirm payment (yes/no): ")
|
|
|
|
if !confirm {
|
|
|
|
return fmt.Errorf("payment not confirmed")
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-02-24 16:32:33 +03:00
|
|
|
func sendPayment(ctx *cli.Context) error {
|
2018-02-07 06:11:11 +03:00
|
|
|
// Show command help if no arguments provided
|
2017-03-03 01:23:16 +03:00
|
|
|
if ctx.NArg() == 0 && ctx.NumFlags() == 0 {
|
|
|
|
cli.ShowCommandHelp(ctx, "sendpayment")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-04-19 17:28:13 +03:00
|
|
|
// If a payment request was provided, we can exit early since all of the
|
|
|
|
// details of the payment are encoded within the request.
|
2017-03-03 01:23:16 +03:00
|
|
|
if ctx.IsSet("pay_req") {
|
2018-04-19 17:28:13 +03:00
|
|
|
req := &lnrpc.SendRequest{
|
2017-01-03 02:38:00 +03:00
|
|
|
PaymentRequest: ctx.String("pay_req"),
|
2018-01-22 23:28:50 +03:00
|
|
|
Amt: ctx.Int64("amt"),
|
2017-01-03 02:38:00 +03:00
|
|
|
}
|
2017-03-03 01:23:16 +03:00
|
|
|
|
2019-04-18 16:27:54 +03:00
|
|
|
return sendPaymentRequest(ctx, req)
|
2018-04-19 17:28:13 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
destNode []byte
|
|
|
|
amount int64
|
2019-04-18 16:27:54 +03:00
|
|
|
err error
|
2018-04-19 17:28:13 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
args := ctx.Args()
|
|
|
|
|
|
|
|
switch {
|
|
|
|
case ctx.IsSet("dest"):
|
|
|
|
destNode, err = hex.DecodeString(ctx.String("dest"))
|
|
|
|
case args.Present():
|
|
|
|
destNode, err = hex.DecodeString(args.First())
|
|
|
|
args = args.Tail()
|
|
|
|
default:
|
|
|
|
return fmt.Errorf("destination txid argument missing")
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(destNode) != 33 {
|
|
|
|
return fmt.Errorf("dest node pubkey must be exactly 33 bytes, is "+
|
|
|
|
"instead: %v", len(destNode))
|
|
|
|
}
|
|
|
|
|
|
|
|
if ctx.IsSet("amt") {
|
|
|
|
amount = ctx.Int64("amt")
|
|
|
|
} else if args.Present() {
|
|
|
|
amount, err = strconv.ParseInt(args.First(), 10, 64)
|
|
|
|
args = args.Tail()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("unable to decode payment amount: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
req := &lnrpc.SendRequest{
|
2020-01-14 11:33:35 +03:00
|
|
|
Dest: destNode,
|
|
|
|
Amt: amount,
|
|
|
|
DestCustomRecords: make(map[uint64][]byte),
|
2018-04-19 17:28:13 +03:00
|
|
|
}
|
|
|
|
|
2019-12-05 14:21:10 +03:00
|
|
|
var rHash []byte
|
2017-03-03 01:23:16 +03:00
|
|
|
|
2020-01-16 15:13:59 +03:00
|
|
|
if ctx.Bool("keysend") {
|
2019-12-05 14:27:17 +03:00
|
|
|
if ctx.IsSet("payment_hash") {
|
|
|
|
return errors.New("cannot set payment hash when using " +
|
2020-01-16 15:13:59 +03:00
|
|
|
"keysend")
|
2019-12-05 14:27:17 +03:00
|
|
|
}
|
|
|
|
var preimage lntypes.Preimage
|
|
|
|
if _, err := rand.Read(preimage[:]); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-01-14 11:33:35 +03:00
|
|
|
// Set the preimage. If the user supplied a preimage with the
|
|
|
|
// data flag, the preimage that is set here will be overwritten
|
|
|
|
// later.
|
|
|
|
req.DestCustomRecords[record.KeySendType] = preimage[:]
|
2019-12-05 14:27:17 +03:00
|
|
|
|
|
|
|
hash := preimage.Hash()
|
|
|
|
rHash = hash[:]
|
|
|
|
} else {
|
|
|
|
switch {
|
|
|
|
case ctx.IsSet("payment_hash"):
|
|
|
|
rHash, err = hex.DecodeString(ctx.String("payment_hash"))
|
|
|
|
case args.Present():
|
|
|
|
rHash, err = hex.DecodeString(args.First())
|
|
|
|
args = args.Tail()
|
|
|
|
default:
|
|
|
|
return fmt.Errorf("payment hash argument missing")
|
|
|
|
}
|
2019-12-05 14:21:10 +03:00
|
|
|
}
|
2018-04-19 17:28:13 +03:00
|
|
|
|
2019-12-05 14:21:10 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if len(rHash) != 32 {
|
|
|
|
return fmt.Errorf("payment hash must be exactly 32 "+
|
|
|
|
"bytes, is instead %v", len(rHash))
|
|
|
|
}
|
|
|
|
req.PaymentHash = rHash
|
|
|
|
|
|
|
|
switch {
|
|
|
|
case ctx.IsSet("final_cltv_delta"):
|
|
|
|
req.FinalCltvDelta = int32(ctx.Int64("final_cltv_delta"))
|
|
|
|
case args.Present():
|
|
|
|
delta, err := strconv.ParseInt(args.First(), 10, 64)
|
2016-09-21 02:14:45 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-12-05 14:21:10 +03:00
|
|
|
req.FinalCltvDelta = int32(delta)
|
2016-07-13 03:47:24 +03:00
|
|
|
}
|
|
|
|
|
2019-04-18 16:27:54 +03:00
|
|
|
return sendPaymentRequest(ctx, req)
|
2017-10-28 01:39:54 +03:00
|
|
|
}
|
|
|
|
|
2019-04-18 16:27:54 +03:00
|
|
|
func sendPaymentRequest(ctx *cli.Context, req *lnrpc.SendRequest) error {
|
|
|
|
client, cleanUp := getClient(ctx)
|
|
|
|
defer cleanUp()
|
|
|
|
|
|
|
|
// First, we'll retrieve the fee limit value passed since it can apply
|
|
|
|
// to both ways of sending payments (with the payment request or
|
|
|
|
// providing the details manually).
|
|
|
|
feeLimit, err := retrieveFeeLimit(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
req.FeeLimit = feeLimit
|
|
|
|
|
|
|
|
req.OutgoingChanId = ctx.Uint64("outgoing_chan_id")
|
2019-11-18 14:08:42 +03:00
|
|
|
if ctx.IsSet(lastHopFlag.Name) {
|
|
|
|
lastHop, err := route.NewVertexFromStr(
|
|
|
|
ctx.String(lastHopFlag.Name),
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
req.LastHopPubkey = lastHop[:]
|
|
|
|
}
|
|
|
|
|
2019-04-18 16:27:54 +03:00
|
|
|
req.CltvLimit = uint32(ctx.Int(cltvLimitFlag.Name))
|
|
|
|
|
2019-11-25 16:13:21 +03:00
|
|
|
req.AllowSelfPayment = ctx.Bool("allow_self_payment")
|
|
|
|
|
2020-01-14 11:33:35 +03:00
|
|
|
// Parse custom data records.
|
|
|
|
data := ctx.String(dataFlag.Name)
|
|
|
|
if data != "" {
|
|
|
|
records := strings.Split(data, ",")
|
|
|
|
for _, r := range records {
|
|
|
|
kv := strings.Split(r, "=")
|
|
|
|
if len(kv) != 2 {
|
|
|
|
return errors.New("invalid data format: " +
|
|
|
|
"multiple equal signs in record")
|
|
|
|
}
|
|
|
|
|
|
|
|
recordID, err := strconv.ParseUint(kv[0], 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("invalid data format: %v",
|
|
|
|
err)
|
|
|
|
}
|
|
|
|
|
|
|
|
hexValue, err := hex.DecodeString(kv[1])
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("invalid data format: %v",
|
|
|
|
err)
|
|
|
|
}
|
|
|
|
|
|
|
|
req.DestCustomRecords[recordID] = hexValue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-18 16:27:54 +03:00
|
|
|
amt := req.Amt
|
|
|
|
|
|
|
|
if req.PaymentRequest != "" {
|
|
|
|
req := &lnrpc.PayReqString{PayReq: req.PaymentRequest}
|
|
|
|
resp, err := client.DecodePayReq(context.Background(), req)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
invoiceAmt := resp.GetNumSatoshis()
|
|
|
|
if invoiceAmt != 0 {
|
|
|
|
amt = invoiceAmt
|
|
|
|
}
|
|
|
|
|
|
|
|
if !ctx.Bool("force") {
|
|
|
|
err := confirmPayReq(resp, amt)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-13 03:47:24 +03:00
|
|
|
paymentStream, err := client.SendPayment(context.Background())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := paymentStream.Send(req); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
resp, err := paymentStream.Recv()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-07-22 02:18:42 +03:00
|
|
|
paymentStream.CloseSend()
|
|
|
|
|
2019-12-20 12:05:08 +03:00
|
|
|
printRespJSON(resp)
|
2016-07-13 03:47:24 +03:00
|
|
|
|
2019-02-17 23:09:15 +03:00
|
|
|
// If we get a payment error back, we pass an error
|
|
|
|
// up to main which eventually calls fatal() and returns
|
|
|
|
// with a non-zero exit code.
|
|
|
|
if resp.PaymentError != "" {
|
|
|
|
return errors.New(resp.PaymentError)
|
|
|
|
}
|
|
|
|
|
2016-07-13 03:47:24 +03:00
|
|
|
return nil
|
|
|
|
}
|
2016-07-15 14:02:59 +03:00
|
|
|
|
2017-10-28 01:39:54 +03:00
|
|
|
var payInvoiceCommand = cli.Command{
|
|
|
|
Name: "payinvoice",
|
2018-05-01 14:28:30 +03:00
|
|
|
Category: "Payments",
|
2018-05-03 11:49:09 +03:00
|
|
|
Usage: "Pay an invoice over lightning.",
|
2017-10-28 01:39:54 +03:00
|
|
|
ArgsUsage: "pay_req",
|
2019-04-18 16:27:54 +03:00
|
|
|
Flags: append(paymentFlags(),
|
2018-01-22 23:28:50 +03:00
|
|
|
cli.Int64Flag{
|
|
|
|
Name: "amt",
|
|
|
|
Usage: "(optional) number of satoshis to fulfill the " +
|
|
|
|
"invoice",
|
|
|
|
},
|
2019-04-18 16:27:54 +03:00
|
|
|
),
|
2017-11-07 01:34:49 +03:00
|
|
|
Action: actionDecorator(payInvoice),
|
2017-10-28 01:39:54 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func payInvoice(ctx *cli.Context) error {
|
|
|
|
args := ctx.Args()
|
|
|
|
|
|
|
|
var payReq string
|
|
|
|
switch {
|
|
|
|
case ctx.IsSet("pay_req"):
|
|
|
|
payReq = ctx.String("pay_req")
|
|
|
|
case args.Present():
|
|
|
|
payReq = args.First()
|
|
|
|
default:
|
|
|
|
return fmt.Errorf("pay_req argument missing")
|
|
|
|
}
|
|
|
|
|
|
|
|
req := &lnrpc.SendRequest{
|
2020-01-14 11:33:35 +03:00
|
|
|
PaymentRequest: payReq,
|
|
|
|
Amt: ctx.Int64("amt"),
|
|
|
|
DestCustomRecords: make(map[uint64][]byte),
|
2017-10-28 01:39:54 +03:00
|
|
|
}
|
2019-02-13 14:26:29 +03:00
|
|
|
|
2019-04-18 16:27:54 +03:00
|
|
|
return sendPaymentRequest(ctx, req)
|
2017-10-28 01:39:54 +03:00
|
|
|
}
|
|
|
|
|
2018-01-25 07:19:40 +03:00
|
|
|
var sendToRouteCommand = cli.Command{
|
2019-06-18 10:37:44 +03:00
|
|
|
Name: "sendtoroute",
|
|
|
|
Category: "Payments",
|
|
|
|
Usage: "Send a payment over a predefined route.",
|
2018-01-25 07:19:40 +03:00
|
|
|
Description: `
|
|
|
|
Send a payment over Lightning using a specific route. One must specify
|
2019-08-29 19:09:37 +03:00
|
|
|
the route to attempt and the payment hash. This command can even
|
|
|
|
be chained with the response to queryroutes or buildroute. This command
|
2019-11-09 19:37:03 +03:00
|
|
|
can be used to implement channel rebalancing by crafting a self-route,
|
2019-08-29 19:09:37 +03:00
|
|
|
or even atomic swaps using a self-route that crosses multiple chains.
|
2018-06-07 06:44:41 +03:00
|
|
|
|
2019-08-29 19:09:37 +03:00
|
|
|
There are three ways to specify a route:
|
2018-06-07 06:44:41 +03:00
|
|
|
* using the --routes parameter to manually specify a JSON encoded
|
2019-08-29 19:09:37 +03:00
|
|
|
route in the format of the return value of queryroutes or
|
|
|
|
buildroute:
|
2018-06-07 06:44:41 +03:00
|
|
|
(lncli sendtoroute --payment_hash=<pay_hash> --routes=<route>)
|
|
|
|
|
2019-08-29 19:09:37 +03:00
|
|
|
* passing the route as a positional argument:
|
2018-06-07 06:44:41 +03:00
|
|
|
(lncli sendtoroute --payment_hash=pay_hash <route>)
|
|
|
|
|
2019-08-29 19:09:37 +03:00
|
|
|
* or reading in the route from stdin, which can allow chaining the
|
|
|
|
response from queryroutes or buildroute, or even read in a file
|
|
|
|
with a pre-computed route:
|
2018-06-07 06:44:41 +03:00
|
|
|
(lncli queryroutes --args.. | lncli sendtoroute --payment_hash= -
|
|
|
|
|
|
|
|
notice the '-' at the end, which signals that lncli should read
|
|
|
|
the route in from stdin
|
2018-01-25 07:19:40 +03:00
|
|
|
`,
|
|
|
|
Flags: []cli.Flag{
|
|
|
|
cli.StringFlag{
|
2018-06-07 06:44:41 +03:00
|
|
|
Name: "payment_hash, pay_hash",
|
2018-01-25 07:19:40 +03:00
|
|
|
Usage: "the hash to use within the payment's HTLC",
|
|
|
|
},
|
2018-06-07 06:44:41 +03:00
|
|
|
cli.StringFlag{
|
|
|
|
Name: "routes, r",
|
|
|
|
Usage: "a json array string in the format of the response " +
|
|
|
|
"of queryroutes that denotes which routes to use",
|
2018-01-25 07:19:40 +03:00
|
|
|
},
|
|
|
|
},
|
|
|
|
Action: sendToRoute,
|
|
|
|
}
|
|
|
|
|
|
|
|
func sendToRoute(ctx *cli.Context) error {
|
2018-06-07 06:44:41 +03:00
|
|
|
// Show command help if no arguments provided.
|
2018-01-25 07:19:40 +03:00
|
|
|
if ctx.NArg() == 0 && ctx.NumFlags() == 0 {
|
|
|
|
cli.ShowCommandHelp(ctx, "sendtoroute")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
args := ctx.Args()
|
|
|
|
|
2018-06-07 06:44:41 +03:00
|
|
|
var (
|
|
|
|
rHash []byte
|
|
|
|
err error
|
|
|
|
)
|
|
|
|
switch {
|
|
|
|
case ctx.IsSet("payment_hash"):
|
|
|
|
rHash, err = hex.DecodeString(ctx.String("payment_hash"))
|
|
|
|
case args.Present():
|
|
|
|
rHash, err = hex.DecodeString(args.First())
|
|
|
|
|
|
|
|
args = args.Tail()
|
|
|
|
default:
|
|
|
|
return fmt.Errorf("payment hash argument missing")
|
2018-01-25 07:19:40 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
2018-06-07 06:44:41 +03:00
|
|
|
return err
|
2018-01-25 07:19:40 +03:00
|
|
|
}
|
|
|
|
|
2018-06-07 06:44:41 +03:00
|
|
|
if len(rHash) != 32 {
|
|
|
|
return fmt.Errorf("payment hash must be exactly 32 "+
|
|
|
|
"bytes, is instead %d", len(rHash))
|
|
|
|
}
|
2018-01-25 07:19:40 +03:00
|
|
|
|
2018-06-07 06:44:41 +03:00
|
|
|
var jsonRoutes string
|
|
|
|
switch {
|
|
|
|
// The user is specifying the routes explicitly via the key word
|
|
|
|
// argument.
|
|
|
|
case ctx.IsSet("routes"):
|
|
|
|
jsonRoutes = ctx.String("routes")
|
|
|
|
|
|
|
|
// The user is specifying the routes as a positional argument.
|
|
|
|
case args.Present() && args.First() != "-":
|
|
|
|
jsonRoutes = args.First()
|
|
|
|
|
|
|
|
// The user is signalling that we should read stdin in order to parse
|
|
|
|
// the set of target routes.
|
|
|
|
case args.Present() && args.First() == "-":
|
|
|
|
b, err := ioutil.ReadAll(os.Stdin)
|
2018-01-25 07:19:40 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-06-07 06:44:41 +03:00
|
|
|
if len(b) == 0 {
|
|
|
|
return fmt.Errorf("queryroutes output is empty")
|
2018-01-25 07:19:40 +03:00
|
|
|
}
|
2018-06-07 06:44:41 +03:00
|
|
|
|
|
|
|
jsonRoutes = string(b)
|
|
|
|
}
|
|
|
|
|
2019-08-29 19:09:37 +03:00
|
|
|
// Try to parse the provided json both in the legacy QueryRoutes format
|
|
|
|
// that contains a list of routes and the single route BuildRoute
|
|
|
|
// format.
|
|
|
|
var route *lnrpc.Route
|
2018-06-07 06:44:41 +03:00
|
|
|
routes := &lnrpc.QueryRoutesResponse{}
|
|
|
|
err = jsonpb.UnmarshalString(jsonRoutes, routes)
|
2019-08-29 19:09:37 +03:00
|
|
|
if err == nil {
|
|
|
|
if len(routes.Routes) == 0 {
|
|
|
|
return fmt.Errorf("no routes provided")
|
|
|
|
}
|
2018-01-25 07:19:40 +03:00
|
|
|
|
2019-08-29 19:09:37 +03:00
|
|
|
if len(routes.Routes) != 1 {
|
|
|
|
return fmt.Errorf("expected a single route, but got %v",
|
|
|
|
len(routes.Routes))
|
|
|
|
}
|
|
|
|
|
|
|
|
route = routes.Routes[0]
|
|
|
|
} else {
|
|
|
|
routes := &routerrpc.BuildRouteResponse{}
|
|
|
|
err = jsonpb.UnmarshalString(jsonRoutes, routes)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("unable to unmarshal json string "+
|
|
|
|
"from incoming array of routes: %v", err)
|
|
|
|
}
|
2018-08-08 12:09:30 +03:00
|
|
|
|
2019-08-29 19:09:37 +03:00
|
|
|
route = routes.Route
|
2018-08-08 12:09:30 +03:00
|
|
|
}
|
|
|
|
|
2018-01-25 07:19:40 +03:00
|
|
|
req := &lnrpc.SendToRouteRequest{
|
|
|
|
PaymentHash: rHash,
|
2019-08-29 19:09:37 +03:00
|
|
|
Route: route,
|
2018-01-25 07:19:40 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return sendToRouteRequest(ctx, req)
|
|
|
|
}
|
|
|
|
|
|
|
|
func sendToRouteRequest(ctx *cli.Context, req *lnrpc.SendToRouteRequest) error {
|
|
|
|
client, cleanUp := getClient(ctx)
|
|
|
|
defer cleanUp()
|
|
|
|
|
|
|
|
paymentStream, err := client.SendToRoute(context.Background())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := paymentStream.Send(req); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
resp, err := paymentStream.Recv()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-12-20 12:05:08 +03:00
|
|
|
printRespJSON(resp)
|
2018-01-25 07:19:40 +03:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-02-24 16:32:33 +03:00
|
|
|
var addInvoiceCommand = cli.Command{
|
2018-05-01 14:28:30 +03:00
|
|
|
Name: "addinvoice",
|
|
|
|
Category: "Payments",
|
|
|
|
Usage: "Add a new invoice.",
|
2017-11-23 22:40:14 +03:00
|
|
|
Description: `
|
|
|
|
Add a new invoice, expressing intent for a future payment.
|
2018-01-22 22:51:09 +03:00
|
|
|
|
|
|
|
Invoices without an amount can be created by not supplying any
|
|
|
|
parameters or providing an amount of 0. These invoices allow the payee
|
|
|
|
to specify the amount of satoshis they wish to send.`,
|
2017-03-03 01:23:16 +03:00
|
|
|
ArgsUsage: "value preimage",
|
2016-09-19 22:05:54 +03:00
|
|
|
Flags: []cli.Flag{
|
|
|
|
cli.StringFlag{
|
2017-09-05 19:11:04 +03:00
|
|
|
Name: "memo",
|
|
|
|
Usage: "a description of the payment to attach along " +
|
|
|
|
"with the invoice (default=\"\")",
|
2016-09-19 22:05:54 +03:00
|
|
|
},
|
|
|
|
cli.StringFlag{
|
2017-09-05 19:11:04 +03:00
|
|
|
Name: "preimage",
|
|
|
|
Usage: "the hex-encoded preimage (32 byte) which will " +
|
|
|
|
"allow settling an incoming HTLC payable to this " +
|
|
|
|
"preimage. If not set, a random preimage will be " +
|
|
|
|
"created.",
|
2016-09-19 22:05:54 +03:00
|
|
|
},
|
2017-03-03 01:23:16 +03:00
|
|
|
cli.Int64Flag{
|
2018-01-18 21:32:24 +03:00
|
|
|
Name: "amt",
|
|
|
|
Usage: "the amt of satoshis in this invoice",
|
2016-09-19 22:05:54 +03:00
|
|
|
},
|
2017-09-05 19:11:04 +03:00
|
|
|
cli.StringFlag{
|
|
|
|
Name: "description_hash",
|
|
|
|
Usage: "SHA-256 hash of the description of the payment. " +
|
|
|
|
"Used if the purpose of payment cannot naturally " +
|
|
|
|
"fit within the memo. If provided this will be " +
|
|
|
|
"used instead of the description(memo) field in " +
|
|
|
|
"the encoded invoice.",
|
|
|
|
},
|
|
|
|
cli.StringFlag{
|
|
|
|
Name: "fallback_addr",
|
|
|
|
Usage: "fallback on-chain address that can be used in " +
|
|
|
|
"case the lightning payment fails",
|
|
|
|
},
|
|
|
|
cli.Int64Flag{
|
|
|
|
Name: "expiry",
|
|
|
|
Usage: "the invoice's expiry time in seconds. If not " +
|
|
|
|
"specified an expiry of 3600 seconds (1 hour) " +
|
|
|
|
"is implied.",
|
|
|
|
},
|
2018-03-28 07:50:03 +03:00
|
|
|
cli.BoolTFlag{
|
|
|
|
Name: "private",
|
|
|
|
Usage: "encode routing hints in the invoice with " +
|
|
|
|
"private channels in order to assist the " +
|
|
|
|
"payer in reaching you",
|
|
|
|
},
|
2016-09-19 22:05:54 +03:00
|
|
|
},
|
2017-11-07 01:34:49 +03:00
|
|
|
Action: actionDecorator(addInvoice),
|
2016-09-19 22:05:54 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func addInvoice(ctx *cli.Context) error {
|
2017-03-03 01:23:16 +03:00
|
|
|
var (
|
|
|
|
preimage []byte
|
2017-09-05 19:11:04 +03:00
|
|
|
descHash []byte
|
2018-01-28 02:05:32 +03:00
|
|
|
amt int64
|
|
|
|
err error
|
2017-03-03 01:23:16 +03:00
|
|
|
)
|
|
|
|
|
2017-01-30 01:51:30 +03:00
|
|
|
client, cleanUp := getClient(ctx)
|
|
|
|
defer cleanUp()
|
2016-09-19 22:05:54 +03:00
|
|
|
|
2017-03-03 01:23:16 +03:00
|
|
|
args := ctx.Args()
|
|
|
|
|
|
|
|
switch {
|
2018-01-18 21:32:24 +03:00
|
|
|
case ctx.IsSet("amt"):
|
|
|
|
amt = ctx.Int64("amt")
|
2017-03-03 01:23:16 +03:00
|
|
|
case args.Present():
|
2018-01-18 21:32:24 +03:00
|
|
|
amt, err = strconv.ParseInt(args.First(), 10, 64)
|
2017-03-03 01:23:16 +03:00
|
|
|
args = args.Tail()
|
|
|
|
if err != nil {
|
2018-01-18 21:32:24 +03:00
|
|
|
return fmt.Errorf("unable to decode amt argument: %v", err)
|
2017-03-03 01:23:16 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
switch {
|
|
|
|
case ctx.IsSet("preimage"):
|
|
|
|
preimage, err = hex.DecodeString(ctx.String("preimage"))
|
|
|
|
case args.Present():
|
|
|
|
preimage, err = hex.DecodeString(args.First())
|
|
|
|
}
|
|
|
|
|
2016-09-19 22:05:54 +03:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("unable to parse preimage: %v", err)
|
|
|
|
}
|
|
|
|
|
2017-09-05 19:11:04 +03:00
|
|
|
descHash, err = hex.DecodeString(ctx.String("description_hash"))
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("unable to parse description_hash: %v", err)
|
|
|
|
}
|
|
|
|
|
2016-09-19 22:05:54 +03:00
|
|
|
invoice := &lnrpc.Invoice{
|
2017-09-05 19:11:04 +03:00
|
|
|
Memo: ctx.String("memo"),
|
|
|
|
RPreimage: preimage,
|
2018-01-18 21:32:24 +03:00
|
|
|
Value: amt,
|
2017-09-05 19:11:04 +03:00
|
|
|
DescriptionHash: descHash,
|
|
|
|
FallbackAddr: ctx.String("fallback_addr"),
|
|
|
|
Expiry: ctx.Int64("expiry"),
|
2018-03-28 07:50:03 +03:00
|
|
|
Private: ctx.Bool("private"),
|
2016-09-19 22:05:54 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
resp, err := client.AddInvoice(context.Background(), invoice)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-12-20 12:05:08 +03:00
|
|
|
printRespJSON(resp)
|
2016-09-19 22:05:54 +03:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-02-24 16:32:33 +03:00
|
|
|
var lookupInvoiceCommand = cli.Command{
|
2017-03-03 01:23:16 +03:00
|
|
|
Name: "lookupinvoice",
|
2018-05-01 14:28:30 +03:00
|
|
|
Category: "Payments",
|
2017-03-03 01:23:16 +03:00
|
|
|
Usage: "Lookup an existing invoice by its payment hash.",
|
|
|
|
ArgsUsage: "rhash",
|
2016-09-19 22:05:54 +03:00
|
|
|
Flags: []cli.Flag{
|
|
|
|
cli.StringFlag{
|
|
|
|
Name: "rhash",
|
2017-03-03 01:23:16 +03:00
|
|
|
Usage: "the 32 byte payment hash of the invoice to query for, the hash " +
|
2016-09-19 22:05:54 +03:00
|
|
|
"should be a hex-encoded string",
|
|
|
|
},
|
|
|
|
},
|
2017-11-07 01:34:49 +03:00
|
|
|
Action: actionDecorator(lookupInvoice),
|
2016-09-19 22:05:54 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func lookupInvoice(ctx *cli.Context) error {
|
2017-01-30 01:51:30 +03:00
|
|
|
client, cleanUp := getClient(ctx)
|
|
|
|
defer cleanUp()
|
2016-09-19 22:05:54 +03:00
|
|
|
|
2017-03-03 01:23:16 +03:00
|
|
|
var (
|
|
|
|
rHash []byte
|
|
|
|
err error
|
|
|
|
)
|
|
|
|
|
|
|
|
switch {
|
|
|
|
case ctx.IsSet("rhash"):
|
|
|
|
rHash, err = hex.DecodeString(ctx.String("rhash"))
|
|
|
|
case ctx.Args().Present():
|
|
|
|
rHash, err = hex.DecodeString(ctx.Args().First())
|
|
|
|
default:
|
|
|
|
return fmt.Errorf("rhash argument missing")
|
|
|
|
}
|
|
|
|
|
2016-09-19 22:05:54 +03:00
|
|
|
if err != nil {
|
2017-03-03 01:23:16 +03:00
|
|
|
return fmt.Errorf("unable to decode rhash argument: %v", err)
|
2016-09-19 22:05:54 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
req := &lnrpc.PaymentHash{
|
|
|
|
RHash: rHash,
|
|
|
|
}
|
|
|
|
|
|
|
|
invoice, err := client.LookupInvoice(context.Background(), req)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-02-23 22:56:47 +03:00
|
|
|
printRespJSON(invoice)
|
2016-09-19 22:05:54 +03:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-02-24 16:32:33 +03:00
|
|
|
var listInvoicesCommand = cli.Command{
|
2018-05-01 14:28:30 +03:00
|
|
|
Name: "listinvoices",
|
|
|
|
Category: "Payments",
|
2018-09-14 00:49:47 +03:00
|
|
|
Usage: "List all invoices currently stored within the database. Any " +
|
2018-12-10 18:20:20 +03:00
|
|
|
"active debug invoices are ignored.",
|
2018-09-11 04:23:22 +03:00
|
|
|
Description: `
|
|
|
|
This command enables the retrieval of all invoices currently stored
|
2018-09-14 00:49:47 +03:00
|
|
|
within the database. It has full support for paginationed responses,
|
2018-09-11 04:23:22 +03:00
|
|
|
allowing users to query for specific invoices through their add_index.
|
|
|
|
This can be done by using either the first_index_offset or
|
2018-09-14 00:49:47 +03:00
|
|
|
last_index_offset fields included in the response as the index_offset of
|
|
|
|
the next request. The reversed flag is set by default in order to
|
|
|
|
paginate backwards. If you wish to paginate forwards, you must
|
|
|
|
explicitly set the flag to false. If none of the parameters are
|
|
|
|
specified, then the last 100 invoices will be returned.
|
|
|
|
|
|
|
|
For example: if you have 200 invoices, "lncli listinvoices" will return
|
|
|
|
the last 100 created. If you wish to retrieve the previous 100, the
|
|
|
|
first_offset_index of the response can be used as the index_offset of
|
|
|
|
the next listinvoices request.`,
|
2016-09-19 22:05:54 +03:00
|
|
|
Flags: []cli.Flag{
|
|
|
|
cli.BoolFlag{
|
|
|
|
Name: "pending_only",
|
2018-08-11 05:38:01 +03:00
|
|
|
Usage: "toggles if all invoices should be returned, " +
|
|
|
|
"or only those that are currently unsettled",
|
|
|
|
},
|
|
|
|
cli.Uint64Flag{
|
2018-09-14 00:49:47 +03:00
|
|
|
Name: "index_offset",
|
|
|
|
Usage: "the index of an invoice that will be used as " +
|
|
|
|
"either the start or end of a query to " +
|
|
|
|
"determine which invoices should be returned " +
|
|
|
|
"in the response",
|
2018-08-11 05:38:01 +03:00
|
|
|
},
|
|
|
|
cli.Uint64Flag{
|
|
|
|
Name: "max_invoices",
|
|
|
|
Usage: "the max number of invoices to return",
|
2016-09-19 22:05:54 +03:00
|
|
|
},
|
2018-09-11 04:23:22 +03:00
|
|
|
cli.BoolTFlag{
|
|
|
|
Name: "reversed",
|
|
|
|
Usage: "if set, the invoices returned precede the " +
|
|
|
|
"given index_offset, allowing backwards " +
|
|
|
|
"pagination",
|
|
|
|
},
|
2016-09-19 22:05:54 +03:00
|
|
|
},
|
2017-11-07 01:34:49 +03:00
|
|
|
Action: actionDecorator(listInvoices),
|
2016-09-19 22:05:54 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func listInvoices(ctx *cli.Context) error {
|
2017-01-30 01:51:30 +03:00
|
|
|
client, cleanUp := getClient(ctx)
|
|
|
|
defer cleanUp()
|
2016-09-19 22:05:54 +03:00
|
|
|
|
|
|
|
req := &lnrpc.ListInvoiceRequest{
|
2018-08-11 05:38:01 +03:00
|
|
|
PendingOnly: ctx.Bool("pending_only"),
|
2018-09-11 04:23:22 +03:00
|
|
|
IndexOffset: ctx.Uint64("index_offset"),
|
|
|
|
NumMaxInvoices: ctx.Uint64("max_invoices"),
|
|
|
|
Reversed: ctx.Bool("reversed"),
|
2016-09-19 22:05:54 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
invoices, err := client.ListInvoices(context.Background(), req)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-02-23 22:56:47 +03:00
|
|
|
printRespJSON(invoices)
|
2016-09-19 22:05:54 +03:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-02-24 16:32:33 +03:00
|
|
|
var describeGraphCommand = cli.Command{
|
2018-05-01 14:28:30 +03:00
|
|
|
Name: "describegraph",
|
|
|
|
Category: "Peers",
|
2018-02-06 02:05:04 +03:00
|
|
|
Description: "Prints a human readable version of the known channel " +
|
2016-12-27 08:52:15 +03:00
|
|
|
"graph from the PoV of the node",
|
2018-09-27 09:45:04 +03:00
|
|
|
Usage: "Describe the network graph.",
|
|
|
|
Flags: []cli.Flag{
|
|
|
|
cli.BoolFlag{
|
|
|
|
Name: "include_unannounced",
|
|
|
|
Usage: "If set, unannounced channels will be included in the " +
|
|
|
|
"graph. Unannounced channels are both private channels, and " +
|
|
|
|
"public channels that are not yet announced to the network.",
|
|
|
|
},
|
|
|
|
},
|
2017-11-07 01:34:49 +03:00
|
|
|
Action: actionDecorator(describeGraph),
|
2016-08-31 02:54:49 +03:00
|
|
|
}
|
2016-08-20 23:49:35 +03:00
|
|
|
|
2016-12-27 08:52:15 +03:00
|
|
|
func describeGraph(ctx *cli.Context) error {
|
2017-01-30 01:51:30 +03:00
|
|
|
client, cleanUp := getClient(ctx)
|
|
|
|
defer cleanUp()
|
2016-08-21 17:46:54 +03:00
|
|
|
|
2018-09-27 09:45:04 +03:00
|
|
|
req := &lnrpc.ChannelGraphRequest{
|
|
|
|
IncludeUnannounced: ctx.Bool("include_unannounced"),
|
|
|
|
}
|
2016-12-28 02:25:43 +03:00
|
|
|
|
2016-12-27 08:52:15 +03:00
|
|
|
graph, err := client.DescribeGraph(context.Background(), req)
|
2016-08-21 17:46:54 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-02-23 22:56:47 +03:00
|
|
|
printRespJSON(graph)
|
2016-08-21 17:46:54 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-02-24 16:32:33 +03:00
|
|
|
var listPaymentsCommand = cli.Command{
|
2018-05-01 14:28:30 +03:00
|
|
|
Name: "listpayments",
|
|
|
|
Category: "Payments",
|
2018-05-03 11:49:09 +03:00
|
|
|
Usage: "List all outgoing payments.",
|
2019-06-14 02:05:52 +03:00
|
|
|
Flags: []cli.Flag{
|
|
|
|
cli.BoolFlag{
|
|
|
|
Name: "include_incomplete",
|
|
|
|
Usage: "if set to true, payments still in flight (or failed) will be returned as well",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Action: actionDecorator(listPayments),
|
2016-12-05 14:59:36 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func listPayments(ctx *cli.Context) error {
|
2017-01-30 01:51:30 +03:00
|
|
|
client, cleanUp := getClient(ctx)
|
|
|
|
defer cleanUp()
|
2016-12-05 14:59:36 +03:00
|
|
|
|
2019-06-14 02:05:52 +03:00
|
|
|
req := &lnrpc.ListPaymentsRequest{
|
|
|
|
IncludeIncomplete: ctx.Bool("include_incomplete"),
|
|
|
|
}
|
2016-12-05 14:59:36 +03:00
|
|
|
|
|
|
|
payments, err := client.ListPayments(context.Background(), req)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-02-23 22:56:47 +03:00
|
|
|
printRespJSON(payments)
|
2016-12-28 02:45:10 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-02-24 16:32:33 +03:00
|
|
|
var getChanInfoCommand = cli.Command{
|
2018-05-01 14:28:30 +03:00
|
|
|
Name: "getchaninfo",
|
|
|
|
Category: "Channels",
|
2018-05-03 11:49:09 +03:00
|
|
|
Usage: "Get the state of a channel.",
|
2018-02-06 02:05:04 +03:00
|
|
|
Description: "Prints out the latest authenticated state for a " +
|
2016-12-28 02:45:10 +03:00
|
|
|
"particular channel",
|
2017-03-03 01:23:16 +03:00
|
|
|
ArgsUsage: "chan_id",
|
2016-12-28 02:45:10 +03:00
|
|
|
Flags: []cli.Flag{
|
2017-03-03 01:23:16 +03:00
|
|
|
cli.Int64Flag{
|
2016-12-28 02:45:10 +03:00
|
|
|
Name: "chan_id",
|
|
|
|
Usage: "the 8-byte compact channel ID to query for",
|
|
|
|
},
|
|
|
|
},
|
2017-11-07 01:34:49 +03:00
|
|
|
Action: actionDecorator(getChanInfo),
|
2016-12-28 02:45:10 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func getChanInfo(ctx *cli.Context) error {
|
|
|
|
ctxb := context.Background()
|
2017-01-30 01:51:30 +03:00
|
|
|
client, cleanUp := getClient(ctx)
|
|
|
|
defer cleanUp()
|
2016-12-28 02:45:10 +03:00
|
|
|
|
2017-03-03 01:23:16 +03:00
|
|
|
var (
|
2017-03-09 07:44:32 +03:00
|
|
|
chanID int64
|
|
|
|
err error
|
2017-03-03 01:23:16 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
switch {
|
|
|
|
case ctx.IsSet("chan_id"):
|
2017-03-09 07:44:32 +03:00
|
|
|
chanID = ctx.Int64("chan_id")
|
2017-03-03 01:23:16 +03:00
|
|
|
case ctx.Args().Present():
|
2017-03-09 07:44:32 +03:00
|
|
|
chanID, err = strconv.ParseInt(ctx.Args().First(), 10, 64)
|
2019-08-30 03:47:06 +03:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("error parsing chan_id: %s", err)
|
|
|
|
}
|
2017-03-03 01:23:16 +03:00
|
|
|
default:
|
|
|
|
return fmt.Errorf("chan_id argument missing")
|
|
|
|
}
|
|
|
|
|
2016-12-28 02:45:10 +03:00
|
|
|
req := &lnrpc.ChanInfoRequest{
|
2017-03-09 07:44:32 +03:00
|
|
|
ChanId: uint64(chanID),
|
2016-12-28 02:45:10 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
chanInfo, err := client.GetChanInfo(ctxb, req)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-02-23 22:56:47 +03:00
|
|
|
printRespJSON(chanInfo)
|
2016-12-28 02:45:10 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-02-24 16:32:33 +03:00
|
|
|
var getNodeInfoCommand = cli.Command{
|
2018-05-01 14:28:30 +03:00
|
|
|
Name: "getnodeinfo",
|
|
|
|
Category: "Peers",
|
|
|
|
Usage: "Get information on a specific node.",
|
2018-02-06 02:05:04 +03:00
|
|
|
Description: "Prints out the latest authenticated node state for an " +
|
2016-12-28 02:45:10 +03:00
|
|
|
"advertised node",
|
|
|
|
Flags: []cli.Flag{
|
|
|
|
cli.StringFlag{
|
|
|
|
Name: "pub_key",
|
|
|
|
Usage: "the 33-byte hex-encoded compressed public of the target " +
|
|
|
|
"node",
|
|
|
|
},
|
2019-06-17 21:30:38 +03:00
|
|
|
cli.BoolFlag{
|
|
|
|
Name: "include_channels",
|
|
|
|
Usage: "if true, will return all known channels " +
|
|
|
|
"associated with the node",
|
|
|
|
},
|
2016-12-28 02:45:10 +03:00
|
|
|
},
|
2017-11-07 01:34:49 +03:00
|
|
|
Action: actionDecorator(getNodeInfo),
|
2016-12-28 02:45:10 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func getNodeInfo(ctx *cli.Context) error {
|
|
|
|
ctxb := context.Background()
|
2017-01-30 01:51:30 +03:00
|
|
|
client, cleanUp := getClient(ctx)
|
|
|
|
defer cleanUp()
|
2016-12-28 02:45:10 +03:00
|
|
|
|
2017-04-13 21:55:17 +03:00
|
|
|
args := ctx.Args()
|
|
|
|
|
|
|
|
var pubKey string
|
|
|
|
switch {
|
|
|
|
case ctx.IsSet("pub_key"):
|
|
|
|
pubKey = ctx.String("pub_key")
|
|
|
|
case args.Present():
|
|
|
|
pubKey = args.First()
|
|
|
|
default:
|
2017-03-03 01:23:16 +03:00
|
|
|
return fmt.Errorf("pub_key argument missing")
|
|
|
|
}
|
|
|
|
|
2016-12-28 02:45:10 +03:00
|
|
|
req := &lnrpc.NodeInfoRequest{
|
2019-06-17 21:30:38 +03:00
|
|
|
PubKey: pubKey,
|
|
|
|
IncludeChannels: ctx.Bool("include_channels"),
|
2016-12-28 02:45:10 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
nodeInfo, err := client.GetNodeInfo(ctxb, req)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-02-23 22:56:47 +03:00
|
|
|
printRespJSON(nodeInfo)
|
2016-12-28 02:45:10 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-03-21 05:01:57 +03:00
|
|
|
var queryRoutesCommand = cli.Command{
|
|
|
|
Name: "queryroutes",
|
2018-05-01 14:28:30 +03:00
|
|
|
Category: "Payments",
|
2017-03-03 01:23:16 +03:00
|
|
|
Usage: "Query a route to a destination.",
|
|
|
|
Description: "Queries the channel router for a potential path to the destination that has sufficient flow for the amount including fees",
|
|
|
|
ArgsUsage: "dest amt",
|
2016-12-28 02:45:10 +03:00
|
|
|
Flags: []cli.Flag{
|
|
|
|
cli.StringFlag{
|
|
|
|
Name: "dest",
|
|
|
|
Usage: "the 33-byte hex-encoded public key for the payment " +
|
|
|
|
"destination",
|
|
|
|
},
|
2017-03-03 01:23:16 +03:00
|
|
|
cli.Int64Flag{
|
2016-12-28 02:45:10 +03:00
|
|
|
Name: "amt",
|
|
|
|
Usage: "the amount to send expressed in satoshis",
|
|
|
|
},
|
2018-02-01 01:36:10 +03:00
|
|
|
cli.Int64Flag{
|
2018-04-19 17:28:13 +03:00
|
|
|
Name: "fee_limit",
|
2018-09-10 01:16:00 +03:00
|
|
|
Usage: "maximum fee allowed in satoshis when sending " +
|
2018-04-19 17:28:13 +03:00
|
|
|
"the payment",
|
2018-02-01 01:36:10 +03:00
|
|
|
},
|
|
|
|
cli.Int64Flag{
|
2018-04-19 17:28:13 +03:00
|
|
|
Name: "fee_limit_percent",
|
2018-09-10 01:16:00 +03:00
|
|
|
Usage: "percentage of the payment's amount used as the " +
|
2018-04-19 17:28:13 +03:00
|
|
|
"maximum fee allowed when sending the payment",
|
2018-02-01 01:36:10 +03:00
|
|
|
},
|
2018-02-15 16:14:56 +03:00
|
|
|
cli.Int64Flag{
|
|
|
|
Name: "final_cltv_delta",
|
|
|
|
Usage: "(optional) number of blocks the last hop has to reveal " +
|
|
|
|
"the preimage",
|
|
|
|
},
|
2019-06-26 15:35:03 +03:00
|
|
|
cli.BoolFlag{
|
|
|
|
Name: "use_mc",
|
|
|
|
Usage: "use mission control probabilities",
|
|
|
|
},
|
2019-10-11 22:47:15 +03:00
|
|
|
cltvLimitFlag,
|
2016-12-28 02:45:10 +03:00
|
|
|
},
|
2017-11-07 01:34:49 +03:00
|
|
|
Action: actionDecorator(queryRoutes),
|
2016-12-28 02:45:10 +03:00
|
|
|
}
|
|
|
|
|
2017-03-21 05:01:57 +03:00
|
|
|
func queryRoutes(ctx *cli.Context) error {
|
2016-12-28 02:45:10 +03:00
|
|
|
ctxb := context.Background()
|
2017-01-30 01:51:30 +03:00
|
|
|
client, cleanUp := getClient(ctx)
|
|
|
|
defer cleanUp()
|
2016-12-28 02:45:10 +03:00
|
|
|
|
2017-03-03 01:23:16 +03:00
|
|
|
var (
|
2018-04-19 17:28:13 +03:00
|
|
|
dest string
|
|
|
|
amt int64
|
|
|
|
err error
|
2017-03-03 01:23:16 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
args := ctx.Args()
|
|
|
|
|
|
|
|
switch {
|
|
|
|
case ctx.IsSet("dest"):
|
|
|
|
dest = ctx.String("dest")
|
|
|
|
case args.Present():
|
|
|
|
dest = args.First()
|
|
|
|
args = args.Tail()
|
|
|
|
default:
|
|
|
|
return fmt.Errorf("dest argument missing")
|
|
|
|
}
|
|
|
|
|
|
|
|
switch {
|
|
|
|
case ctx.IsSet("amt"):
|
|
|
|
amt = ctx.Int64("amt")
|
|
|
|
case args.Present():
|
|
|
|
amt, err = strconv.ParseInt(args.First(), 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("unable to decode amt argument: %v", err)
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
return fmt.Errorf("amt argument missing")
|
|
|
|
}
|
|
|
|
|
2018-04-19 17:28:13 +03:00
|
|
|
feeLimit, err := retrieveFeeLimit(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-02-01 01:36:10 +03:00
|
|
|
|
2017-03-21 05:01:57 +03:00
|
|
|
req := &lnrpc.QueryRoutesRequest{
|
2019-06-26 15:35:03 +03:00
|
|
|
PubKey: dest,
|
|
|
|
Amt: amt,
|
|
|
|
FeeLimit: feeLimit,
|
|
|
|
FinalCltvDelta: int32(ctx.Int("final_cltv_delta")),
|
|
|
|
UseMissionControl: ctx.Bool("use_mc"),
|
2019-10-11 22:47:15 +03:00
|
|
|
CltvLimit: uint32(ctx.Uint64(cltvLimitFlag.Name)),
|
2016-12-28 02:45:10 +03:00
|
|
|
}
|
|
|
|
|
2017-03-21 05:01:57 +03:00
|
|
|
route, err := client.QueryRoutes(ctxb, req)
|
2016-12-28 02:45:10 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-02-23 22:56:47 +03:00
|
|
|
printRespJSON(route)
|
2016-12-28 02:45:10 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-02-24 16:32:33 +03:00
|
|
|
var getNetworkInfoCommand = cli.Command{
|
2018-05-01 14:28:30 +03:00
|
|
|
Name: "getnetworkinfo",
|
|
|
|
Category: "Channels",
|
2018-04-20 10:14:51 +03:00
|
|
|
Usage: "Get statistical information about the current " +
|
2018-05-03 11:49:09 +03:00
|
|
|
"state of the network.",
|
|
|
|
Description: "Returns a set of statistics pertaining to the known " +
|
|
|
|
"channel graph",
|
2017-11-07 01:34:49 +03:00
|
|
|
Action: actionDecorator(getNetworkInfo),
|
2016-12-28 02:45:10 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func getNetworkInfo(ctx *cli.Context) error {
|
|
|
|
ctxb := context.Background()
|
2017-01-30 01:51:30 +03:00
|
|
|
client, cleanUp := getClient(ctx)
|
|
|
|
defer cleanUp()
|
2016-12-28 02:45:10 +03:00
|
|
|
|
|
|
|
req := &lnrpc.NetworkInfoRequest{}
|
|
|
|
|
|
|
|
netInfo, err := client.GetNetworkInfo(ctxb, req)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-12-05 14:59:36 +03:00
|
|
|
|
2017-02-23 22:56:47 +03:00
|
|
|
printRespJSON(netInfo)
|
2016-12-05 14:59:36 +03:00
|
|
|
return nil
|
2016-12-27 08:52:15 +03:00
|
|
|
}
|
2017-01-15 05:19:02 +03:00
|
|
|
|
2017-02-24 16:32:33 +03:00
|
|
|
var debugLevelCommand = cli.Command{
|
2017-11-23 22:40:14 +03:00
|
|
|
Name: "debuglevel",
|
|
|
|
Usage: "Set the debug level.",
|
2018-04-17 05:17:58 +03:00
|
|
|
Description: `Logging level for all subsystems {trace, debug, info, warn, error, critical, off}
|
2017-11-23 22:40:14 +03:00
|
|
|
You may also specify <subsystem>=<level>,<subsystem2>=<level>,... to set the log level for individual subsystems
|
2018-09-10 01:16:00 +03:00
|
|
|
|
2017-11-23 22:40:14 +03:00
|
|
|
Use show to list available subsystems`,
|
2017-01-15 05:19:02 +03:00
|
|
|
Flags: []cli.Flag{
|
|
|
|
cli.BoolFlag{
|
|
|
|
Name: "show",
|
|
|
|
Usage: "if true, then the list of available sub-systems will be printed out",
|
|
|
|
},
|
|
|
|
cli.StringFlag{
|
|
|
|
Name: "level",
|
2017-04-13 21:55:17 +03:00
|
|
|
Usage: "the level specification to target either a coarse logging level, or granular set of specific sub-systems with logging levels for each",
|
2017-01-15 05:19:02 +03:00
|
|
|
},
|
|
|
|
},
|
2017-11-07 01:34:49 +03:00
|
|
|
Action: actionDecorator(debugLevel),
|
2017-01-15 05:19:02 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func debugLevel(ctx *cli.Context) error {
|
|
|
|
ctxb := context.Background()
|
2017-01-30 01:51:30 +03:00
|
|
|
client, cleanUp := getClient(ctx)
|
|
|
|
defer cleanUp()
|
2017-01-15 05:19:02 +03:00
|
|
|
req := &lnrpc.DebugLevelRequest{
|
|
|
|
Show: ctx.Bool("show"),
|
|
|
|
LevelSpec: ctx.String("level"),
|
|
|
|
}
|
|
|
|
|
|
|
|
resp, err := client.DebugLevel(ctxb, req)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-02-23 22:56:47 +03:00
|
|
|
printRespJSON(resp)
|
2017-01-15 05:19:02 +03:00
|
|
|
return nil
|
|
|
|
}
|
2017-01-18 00:39:30 +03:00
|
|
|
|
2018-02-07 06:11:11 +03:00
|
|
|
var decodePayReqCommand = cli.Command{
|
2017-01-18 00:39:30 +03:00
|
|
|
Name: "decodepayreq",
|
2018-05-01 14:28:30 +03:00
|
|
|
Category: "Payments",
|
2017-03-03 01:23:16 +03:00
|
|
|
Usage: "Decode a payment request.",
|
2017-01-18 00:39:30 +03:00
|
|
|
Description: "Decode the passed payment request revealing the destination, payment hash and value of the payment request",
|
2017-03-03 01:23:16 +03:00
|
|
|
ArgsUsage: "pay_req",
|
2017-01-18 00:39:30 +03:00
|
|
|
Flags: []cli.Flag{
|
|
|
|
cli.StringFlag{
|
|
|
|
Name: "pay_req",
|
2017-09-05 19:11:04 +03:00
|
|
|
Usage: "the bech32 encoded payment request",
|
2017-01-18 00:39:30 +03:00
|
|
|
},
|
|
|
|
},
|
2017-11-07 01:34:49 +03:00
|
|
|
Action: actionDecorator(decodePayReq),
|
2017-01-18 00:39:30 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func decodePayReq(ctx *cli.Context) error {
|
|
|
|
ctxb := context.Background()
|
2017-01-30 01:51:30 +03:00
|
|
|
client, cleanUp := getClient(ctx)
|
|
|
|
defer cleanUp()
|
2017-01-18 00:39:30 +03:00
|
|
|
|
2017-03-03 01:23:16 +03:00
|
|
|
var payreq string
|
|
|
|
|
|
|
|
switch {
|
|
|
|
case ctx.IsSet("pay_req"):
|
|
|
|
payreq = ctx.String("pay_req")
|
|
|
|
case ctx.Args().Present():
|
|
|
|
payreq = ctx.Args().First()
|
|
|
|
default:
|
|
|
|
return fmt.Errorf("pay_req argument missing")
|
2017-01-18 00:39:30 +03:00
|
|
|
}
|
|
|
|
|
2017-01-30 01:56:31 +03:00
|
|
|
resp, err := client.DecodePayReq(ctxb, &lnrpc.PayReqString{
|
2017-03-03 01:23:16 +03:00
|
|
|
PayReq: payreq,
|
2017-01-30 01:56:31 +03:00
|
|
|
})
|
2017-01-18 00:39:30 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-02-23 22:56:47 +03:00
|
|
|
printRespJSON(resp)
|
2017-01-18 00:39:30 +03:00
|
|
|
return nil
|
|
|
|
}
|
2017-03-04 11:23:04 +03:00
|
|
|
|
2017-03-09 07:44:32 +03:00
|
|
|
var listChainTxnsCommand = cli.Command{
|
2017-03-04 11:23:04 +03:00
|
|
|
Name: "listchaintxns",
|
2018-05-01 14:28:30 +03:00
|
|
|
Category: "On-chain",
|
2017-03-04 11:23:04 +03:00
|
|
|
Usage: "List transactions from the wallet.",
|
|
|
|
Description: "List all transactions an address of the wallet was involved in.",
|
2017-11-07 01:34:49 +03:00
|
|
|
Action: actionDecorator(listChainTxns),
|
2017-03-04 11:23:04 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func listChainTxns(ctx *cli.Context) error {
|
|
|
|
ctxb := context.Background()
|
|
|
|
client, cleanUp := getClient(ctx)
|
|
|
|
defer cleanUp()
|
|
|
|
|
|
|
|
resp, err := client.GetTransactions(ctxb, &lnrpc.GetTransactionsRequest{})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-03-09 07:44:32 +03:00
|
|
|
printRespJSON(resp)
|
2017-03-04 11:23:04 +03:00
|
|
|
return nil
|
|
|
|
}
|
2017-05-12 00:55:56 +03:00
|
|
|
|
|
|
|
var stopCommand = cli.Command{
|
2017-11-23 22:40:14 +03:00
|
|
|
Name: "stop",
|
|
|
|
Usage: "Stop and shutdown the daemon.",
|
|
|
|
Description: `
|
2018-09-10 01:16:00 +03:00
|
|
|
Gracefully stop all daemon subsystems before stopping the daemon itself.
|
2017-11-23 22:40:14 +03:00
|
|
|
This is equivalent to stopping it using CTRL-C.`,
|
|
|
|
Action: actionDecorator(stopDaemon),
|
2017-05-12 00:55:56 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func stopDaemon(ctx *cli.Context) error {
|
|
|
|
ctxb := context.Background()
|
|
|
|
client, cleanUp := getClient(ctx)
|
|
|
|
defer cleanUp()
|
|
|
|
|
|
|
|
_, err := client.StopDaemon(ctxb, &lnrpc.StopRequest{})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2017-04-20 05:33:09 +03:00
|
|
|
|
|
|
|
var signMessageCommand = cli.Command{
|
|
|
|
Name: "signmessage",
|
2018-05-01 14:28:30 +03:00
|
|
|
Category: "Wallet",
|
2018-05-03 11:49:09 +03:00
|
|
|
Usage: "Sign a message with the node's private key.",
|
2017-04-20 05:33:09 +03:00
|
|
|
ArgsUsage: "msg",
|
2017-11-23 22:40:14 +03:00
|
|
|
Description: `
|
2018-09-10 01:16:00 +03:00
|
|
|
Sign msg with the resident node's private key.
|
|
|
|
Returns the signature as a zbase32 string.
|
|
|
|
|
2017-11-23 22:40:14 +03:00
|
|
|
Positional arguments and flags can be used interchangeably but not at the same time!`,
|
2017-04-20 05:33:09 +03:00
|
|
|
Flags: []cli.Flag{
|
|
|
|
cli.StringFlag{
|
|
|
|
Name: "msg",
|
|
|
|
Usage: "the message to sign",
|
|
|
|
},
|
|
|
|
},
|
2017-11-07 01:34:49 +03:00
|
|
|
Action: actionDecorator(signMessage),
|
2017-04-20 05:33:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func signMessage(ctx *cli.Context) error {
|
|
|
|
ctxb := context.Background()
|
|
|
|
client, cleanUp := getClient(ctx)
|
|
|
|
defer cleanUp()
|
|
|
|
|
|
|
|
var msg []byte
|
|
|
|
|
|
|
|
switch {
|
|
|
|
case ctx.IsSet("msg"):
|
|
|
|
msg = []byte(ctx.String("msg"))
|
|
|
|
case ctx.Args().Present():
|
|
|
|
msg = []byte(ctx.Args().First())
|
|
|
|
default:
|
|
|
|
return fmt.Errorf("msg argument missing")
|
|
|
|
}
|
|
|
|
|
|
|
|
resp, err := client.SignMessage(ctxb, &lnrpc.SignMessageRequest{Msg: msg})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
printRespJSON(resp)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var verifyMessageCommand = cli.Command{
|
|
|
|
Name: "verifymessage",
|
2018-05-01 14:28:30 +03:00
|
|
|
Category: "Wallet",
|
2018-05-03 11:49:09 +03:00
|
|
|
Usage: "Verify a message signed with the signature.",
|
2017-04-20 05:33:09 +03:00
|
|
|
ArgsUsage: "msg signature",
|
2017-11-23 22:40:14 +03:00
|
|
|
Description: `
|
|
|
|
Verify that the message was signed with a properly-formed signature
|
|
|
|
The signature must be zbase32 encoded and signed with the private key of
|
|
|
|
an active node in the resident node's channel database.
|
|
|
|
|
|
|
|
Positional arguments and flags can be used interchangeably but not at the same time!`,
|
2017-04-20 05:33:09 +03:00
|
|
|
Flags: []cli.Flag{
|
|
|
|
cli.StringFlag{
|
|
|
|
Name: "msg",
|
|
|
|
Usage: "the message to verify",
|
|
|
|
},
|
|
|
|
cli.StringFlag{
|
2017-04-29 14:44:29 +03:00
|
|
|
Name: "sig",
|
2017-04-20 05:33:09 +03:00
|
|
|
Usage: "the zbase32 encoded signature of the message",
|
|
|
|
},
|
|
|
|
},
|
2017-11-07 01:34:49 +03:00
|
|
|
Action: actionDecorator(verifyMessage),
|
2017-04-20 05:33:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func verifyMessage(ctx *cli.Context) error {
|
|
|
|
ctxb := context.Background()
|
|
|
|
client, cleanUp := getClient(ctx)
|
|
|
|
defer cleanUp()
|
|
|
|
|
|
|
|
var (
|
2017-04-29 14:44:29 +03:00
|
|
|
msg []byte
|
|
|
|
sig string
|
2017-04-20 05:33:09 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
args := ctx.Args()
|
|
|
|
|
|
|
|
switch {
|
|
|
|
case ctx.IsSet("msg"):
|
|
|
|
msg = []byte(ctx.String("msg"))
|
|
|
|
case args.Present():
|
|
|
|
msg = []byte(ctx.Args().First())
|
|
|
|
args = args.Tail()
|
|
|
|
default:
|
|
|
|
return fmt.Errorf("msg argument missing")
|
|
|
|
}
|
|
|
|
|
|
|
|
switch {
|
2017-04-29 14:44:29 +03:00
|
|
|
case ctx.IsSet("sig"):
|
|
|
|
sig = ctx.String("sig")
|
2017-04-20 05:33:09 +03:00
|
|
|
case args.Present():
|
2017-04-29 14:44:29 +03:00
|
|
|
sig = args.First()
|
2017-04-20 05:33:09 +03:00
|
|
|
default:
|
|
|
|
return fmt.Errorf("signature argument missing")
|
|
|
|
}
|
|
|
|
|
2017-04-29 14:44:29 +03:00
|
|
|
req := &lnrpc.VerifyMessageRequest{Msg: msg, Signature: sig}
|
2017-04-20 05:33:09 +03:00
|
|
|
resp, err := client.VerifyMessage(ctxb, req)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
printRespJSON(resp)
|
|
|
|
return nil
|
|
|
|
}
|
2017-08-22 10:29:08 +03:00
|
|
|
|
|
|
|
var feeReportCommand = cli.Command{
|
2018-05-01 14:28:30 +03:00
|
|
|
Name: "feereport",
|
|
|
|
Category: "Channels",
|
2018-05-03 11:49:09 +03:00
|
|
|
Usage: "Display the current fee policies of all active channels.",
|
2018-09-10 01:16:00 +03:00
|
|
|
Description: `
|
2018-01-18 21:32:24 +03:00
|
|
|
Returns the current fee policies of all active channels.
|
2017-12-17 01:13:17 +03:00
|
|
|
Fee policies can be updated using the updatechanpolicy command.`,
|
2017-11-07 01:34:49 +03:00
|
|
|
Action: actionDecorator(feeReport),
|
2017-08-22 10:29:08 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func feeReport(ctx *cli.Context) error {
|
|
|
|
ctxb := context.Background()
|
|
|
|
client, cleanUp := getClient(ctx)
|
|
|
|
defer cleanUp()
|
|
|
|
|
|
|
|
req := &lnrpc.FeeReportRequest{}
|
|
|
|
resp, err := client.FeeReport(ctxb, req)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
printRespJSON(resp)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-12-17 01:13:17 +03:00
|
|
|
var updateChannelPolicyCommand = cli.Command{
|
2018-04-20 10:14:51 +03:00
|
|
|
Name: "updatechanpolicy",
|
|
|
|
Category: "Channels",
|
|
|
|
Usage: "Update the channel policy for all channels, or a single " +
|
2018-05-03 11:49:09 +03:00
|
|
|
"channel.",
|
2019-08-20 03:56:13 +03:00
|
|
|
ArgsUsage: "base_fee_msat fee_rate time_lock_delta " +
|
|
|
|
"[--max_htlc_msat=N] [channel_point]",
|
2017-11-23 22:40:14 +03:00
|
|
|
Description: `
|
2017-12-17 01:13:17 +03:00
|
|
|
Updates the channel policy for all channels, or just a particular channel
|
|
|
|
identified by its channel point. The update will be committed, and
|
2017-11-23 22:40:14 +03:00
|
|
|
broadcast to the rest of the network within the next batch.
|
|
|
|
Channel points are encoded as: funding_txid:output_index`,
|
2017-08-22 10:29:08 +03:00
|
|
|
Flags: []cli.Flag{
|
|
|
|
cli.Int64Flag{
|
|
|
|
Name: "base_fee_msat",
|
|
|
|
Usage: "the base fee in milli-satoshis that will " +
|
|
|
|
"be charged for each forwarded HTLC, regardless " +
|
|
|
|
"of payment size",
|
|
|
|
},
|
|
|
|
cli.StringFlag{
|
|
|
|
Name: "fee_rate",
|
|
|
|
Usage: "the fee rate that will be charged " +
|
|
|
|
"proportionally based on the value of each " +
|
2019-07-03 02:20:53 +03:00
|
|
|
"forwarded HTLC, the lowest possible rate is 0 " +
|
|
|
|
"with a granularity of 0.000001 (millionths)",
|
2017-08-22 10:29:08 +03:00
|
|
|
},
|
2017-12-17 01:13:17 +03:00
|
|
|
cli.Int64Flag{
|
|
|
|
Name: "time_lock_delta",
|
|
|
|
Usage: "the CLTV delta that will be applied to all " +
|
|
|
|
"forwarded HTLCs",
|
|
|
|
},
|
2019-11-15 13:24:58 +03:00
|
|
|
cli.Uint64Flag{
|
|
|
|
Name: "min_htlc_msat",
|
|
|
|
Usage: "if set, the min HTLC size that will be applied " +
|
|
|
|
"to all forwarded HTLCs. If unset, the min HTLC " +
|
|
|
|
"is left unchanged.",
|
|
|
|
},
|
2019-08-20 03:56:13 +03:00
|
|
|
cli.Uint64Flag{
|
|
|
|
Name: "max_htlc_msat",
|
|
|
|
Usage: "if set, the max HTLC size that will be applied " +
|
|
|
|
"to all forwarded HTLCs. If unset, the max HTLC " +
|
|
|
|
"is left unchanged.",
|
|
|
|
},
|
2017-08-22 10:29:08 +03:00
|
|
|
cli.StringFlag{
|
|
|
|
Name: "chan_point",
|
|
|
|
Usage: "The channel whose fee policy should be " +
|
|
|
|
"updated, if nil the policies for all channels " +
|
|
|
|
"will be updated. Takes the form of: txid:output_index",
|
|
|
|
},
|
|
|
|
},
|
2017-12-17 01:13:17 +03:00
|
|
|
Action: actionDecorator(updateChannelPolicy),
|
2017-08-22 10:29:08 +03:00
|
|
|
}
|
|
|
|
|
2018-12-10 07:16:11 +03:00
|
|
|
func parseChanPoint(s string) (*lnrpc.ChannelPoint, error) {
|
|
|
|
split := strings.Split(s, ":")
|
|
|
|
if len(split) != 2 {
|
|
|
|
return nil, fmt.Errorf("expecting chan_point to be in format of: " +
|
|
|
|
"txid:index")
|
|
|
|
}
|
|
|
|
|
|
|
|
index, err := strconv.ParseInt(split[1], 10, 32)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("unable to decode output index: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
txid, err := chainhash.NewHashFromStr(split[0])
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("unable to parse hex string: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return &lnrpc.ChannelPoint{
|
|
|
|
FundingTxid: &lnrpc.ChannelPoint_FundingTxidBytes{
|
|
|
|
FundingTxidBytes: txid[:],
|
|
|
|
},
|
|
|
|
OutputIndex: uint32(index),
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2017-12-17 01:13:17 +03:00
|
|
|
func updateChannelPolicy(ctx *cli.Context) error {
|
2017-08-22 10:29:08 +03:00
|
|
|
ctxb := context.Background()
|
|
|
|
client, cleanUp := getClient(ctx)
|
|
|
|
defer cleanUp()
|
|
|
|
|
|
|
|
var (
|
2017-12-17 01:13:17 +03:00
|
|
|
baseFee int64
|
|
|
|
feeRate float64
|
|
|
|
timeLockDelta int64
|
|
|
|
err error
|
2017-08-22 10:29:08 +03:00
|
|
|
)
|
|
|
|
args := ctx.Args()
|
|
|
|
|
|
|
|
switch {
|
|
|
|
case ctx.IsSet("base_fee_msat"):
|
|
|
|
baseFee = ctx.Int64("base_fee_msat")
|
|
|
|
case args.Present():
|
|
|
|
baseFee, err = strconv.ParseInt(args.First(), 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("unable to decode base_fee_msat: %v", err)
|
|
|
|
}
|
|
|
|
args = args.Tail()
|
|
|
|
default:
|
|
|
|
return fmt.Errorf("base_fee_msat argument missing")
|
|
|
|
}
|
|
|
|
|
|
|
|
switch {
|
|
|
|
case ctx.IsSet("fee_rate"):
|
|
|
|
feeRate = ctx.Float64("fee_rate")
|
|
|
|
case args.Present():
|
|
|
|
feeRate, err = strconv.ParseFloat(args.First(), 64)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("unable to decode fee_rate: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
args = args.Tail()
|
|
|
|
default:
|
|
|
|
return fmt.Errorf("fee_rate argument missing")
|
|
|
|
}
|
|
|
|
|
2017-12-17 01:13:17 +03:00
|
|
|
switch {
|
|
|
|
case ctx.IsSet("time_lock_delta"):
|
|
|
|
timeLockDelta = ctx.Int64("time_lock_delta")
|
|
|
|
case args.Present():
|
|
|
|
timeLockDelta, err = strconv.ParseInt(args.First(), 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("unable to decode time_lock_delta: %v",
|
|
|
|
err)
|
|
|
|
}
|
|
|
|
|
|
|
|
args = args.Tail()
|
|
|
|
default:
|
|
|
|
return fmt.Errorf("time_lock_delta argument missing")
|
|
|
|
}
|
|
|
|
|
2017-08-22 10:29:08 +03:00
|
|
|
var (
|
|
|
|
chanPoint *lnrpc.ChannelPoint
|
|
|
|
chanPointStr string
|
|
|
|
)
|
2017-12-17 01:13:17 +03:00
|
|
|
|
2017-08-22 10:29:08 +03:00
|
|
|
switch {
|
|
|
|
case ctx.IsSet("chan_point"):
|
|
|
|
chanPointStr = ctx.String("chan_point")
|
|
|
|
case args.Present():
|
|
|
|
chanPointStr = args.First()
|
|
|
|
}
|
|
|
|
|
|
|
|
if chanPointStr != "" {
|
2018-12-10 07:16:11 +03:00
|
|
|
chanPoint, err = parseChanPoint(chanPointStr)
|
2017-08-22 10:29:08 +03:00
|
|
|
if err != nil {
|
2018-12-10 07:16:11 +03:00
|
|
|
return fmt.Errorf("unable to parse chan point: %v", err)
|
2017-08-22 10:29:08 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-17 01:13:17 +03:00
|
|
|
req := &lnrpc.PolicyUpdateRequest{
|
|
|
|
BaseFeeMsat: baseFee,
|
|
|
|
FeeRate: feeRate,
|
|
|
|
TimeLockDelta: uint32(timeLockDelta),
|
2019-08-20 03:56:13 +03:00
|
|
|
MaxHtlcMsat: ctx.Uint64("max_htlc_msat"),
|
2017-08-22 10:29:08 +03:00
|
|
|
}
|
|
|
|
|
2019-11-15 13:24:58 +03:00
|
|
|
if ctx.IsSet("min_htlc_msat") {
|
|
|
|
req.MinHtlcMsat = ctx.Uint64("min_htlc_msat")
|
|
|
|
req.MinHtlcMsatSpecified = true
|
|
|
|
}
|
|
|
|
|
2017-08-22 10:29:08 +03:00
|
|
|
if chanPoint != nil {
|
2017-12-17 01:13:17 +03:00
|
|
|
req.Scope = &lnrpc.PolicyUpdateRequest_ChanPoint{
|
2017-08-22 10:29:08 +03:00
|
|
|
ChanPoint: chanPoint,
|
|
|
|
}
|
|
|
|
} else {
|
2017-12-17 01:13:17 +03:00
|
|
|
req.Scope = &lnrpc.PolicyUpdateRequest_Global{
|
2017-08-22 10:29:08 +03:00
|
|
|
Global: true,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-17 01:13:17 +03:00
|
|
|
resp, err := client.UpdateChannelPolicy(ctxb, req)
|
2017-08-22 10:29:08 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
printRespJSON(resp)
|
|
|
|
return nil
|
|
|
|
}
|
2018-02-28 09:24:37 +03:00
|
|
|
|
|
|
|
var forwardingHistoryCommand = cli.Command{
|
|
|
|
Name: "fwdinghistory",
|
2018-05-01 14:28:30 +03:00
|
|
|
Category: "Payments",
|
2018-05-03 11:49:09 +03:00
|
|
|
Usage: "Query the history of all forwarded HTLCs.",
|
2018-02-28 09:24:37 +03:00
|
|
|
ArgsUsage: "start_time [end_time] [index_offset] [max_events]",
|
|
|
|
Description: `
|
2018-05-03 11:49:09 +03:00
|
|
|
Query the HTLC switch's internal forwarding log for all completed
|
2018-02-28 09:24:37 +03:00
|
|
|
payment circuits (HTLCs) over a particular time range (--start_time and
|
|
|
|
--end_time). The start and end times are meant to be expressed in
|
2019-07-09 01:18:35 +03:00
|
|
|
seconds since the Unix epoch. If --start_time isn't provided,
|
|
|
|
then 24 hours ago is used. If --end_time isn't provided,
|
|
|
|
then the current time is used.
|
2018-02-28 09:24:37 +03:00
|
|
|
|
|
|
|
The max number of events returned is 50k. The default number is 100,
|
|
|
|
callers can use the --max_events param to modify this value.
|
|
|
|
|
|
|
|
Finally, callers can skip a series of events using the --index_offset
|
|
|
|
parameter. Each response will contain the offset index of the last
|
|
|
|
entry. Using this callers can manually paginate within a time slice.
|
|
|
|
`,
|
|
|
|
Flags: []cli.Flag{
|
|
|
|
cli.Int64Flag{
|
|
|
|
Name: "start_time",
|
|
|
|
Usage: "the starting time for the query, expressed in " +
|
|
|
|
"seconds since the unix epoch",
|
|
|
|
},
|
|
|
|
cli.Int64Flag{
|
|
|
|
Name: "end_time",
|
|
|
|
Usage: "the end time for the query, expressed in " +
|
|
|
|
"seconds since the unix epoch",
|
|
|
|
},
|
|
|
|
cli.Int64Flag{
|
|
|
|
Name: "index_offset",
|
|
|
|
Usage: "the number of events to skip",
|
|
|
|
},
|
|
|
|
cli.Int64Flag{
|
|
|
|
Name: "max_events",
|
|
|
|
Usage: "the max number of events to return",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Action: actionDecorator(forwardingHistory),
|
|
|
|
}
|
|
|
|
|
|
|
|
func forwardingHistory(ctx *cli.Context) error {
|
|
|
|
ctxb := context.Background()
|
|
|
|
client, cleanUp := getClient(ctx)
|
|
|
|
defer cleanUp()
|
|
|
|
|
|
|
|
var (
|
|
|
|
startTime, endTime uint64
|
|
|
|
indexOffset, maxEvents uint32
|
|
|
|
err error
|
|
|
|
)
|
|
|
|
args := ctx.Args()
|
|
|
|
|
|
|
|
switch {
|
|
|
|
case ctx.IsSet("start_time"):
|
|
|
|
startTime = ctx.Uint64("start_time")
|
|
|
|
case args.Present():
|
|
|
|
startTime, err = strconv.ParseUint(args.First(), 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("unable to decode start_time %v", err)
|
|
|
|
}
|
|
|
|
args = args.Tail()
|
2019-10-04 01:12:39 +03:00
|
|
|
default:
|
|
|
|
now := time.Now()
|
|
|
|
startTime = uint64(now.Add(-time.Hour * 24).Unix())
|
2018-02-28 09:24:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
switch {
|
|
|
|
case ctx.IsSet("end_time"):
|
|
|
|
endTime = ctx.Uint64("end_time")
|
|
|
|
case args.Present():
|
|
|
|
endTime, err = strconv.ParseUint(args.First(), 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("unable to decode end_time: %v", err)
|
|
|
|
}
|
|
|
|
args = args.Tail()
|
|
|
|
}
|
|
|
|
|
|
|
|
switch {
|
|
|
|
case ctx.IsSet("index_offset"):
|
|
|
|
indexOffset = uint32(ctx.Int64("index_offset"))
|
|
|
|
case args.Present():
|
|
|
|
i, err := strconv.ParseInt(args.First(), 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("unable to decode index_offset: %v", err)
|
|
|
|
}
|
|
|
|
indexOffset = uint32(i)
|
|
|
|
args = args.Tail()
|
|
|
|
}
|
|
|
|
|
|
|
|
switch {
|
|
|
|
case ctx.IsSet("max_events"):
|
|
|
|
maxEvents = uint32(ctx.Int64("max_events"))
|
|
|
|
case args.Present():
|
|
|
|
m, err := strconv.ParseInt(args.First(), 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("unable to decode max_events: %v", err)
|
|
|
|
}
|
|
|
|
maxEvents = uint32(m)
|
|
|
|
args = args.Tail()
|
|
|
|
}
|
|
|
|
|
|
|
|
req := &lnrpc.ForwardingHistoryRequest{
|
|
|
|
StartTime: startTime,
|
|
|
|
EndTime: endTime,
|
|
|
|
IndexOffset: indexOffset,
|
|
|
|
NumMaxEvents: maxEvents,
|
|
|
|
}
|
|
|
|
resp, err := client.ForwardingHistory(ctxb, req)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
printRespJSON(resp)
|
|
|
|
return nil
|
|
|
|
}
|
2018-12-10 07:16:11 +03:00
|
|
|
|
|
|
|
var exportChanBackupCommand = cli.Command{
|
|
|
|
Name: "exportchanbackup",
|
|
|
|
Category: "Channels",
|
|
|
|
Usage: "Obtain a static channel back up for a selected channels, " +
|
|
|
|
"or all known channels",
|
|
|
|
ArgsUsage: "[chan_point] [--all] [--output_file]",
|
|
|
|
Description: `
|
|
|
|
This command allows a user to export a Static Channel Backup (SCB) for
|
2019-04-12 21:14:32 +03:00
|
|
|
a selected channel. SCB's are encrypted backups of a channel's initial
|
2019-04-04 17:07:06 +03:00
|
|
|
state that are encrypted with a key derived from the seed of a user. In
|
2018-12-10 07:16:11 +03:00
|
|
|
the case of partial or complete data loss, the SCB will allow the user
|
|
|
|
to reclaim settled funds in the channel at its final state. The
|
|
|
|
exported channel backups can be restored at a later time using the
|
|
|
|
restorechanbackup command.
|
|
|
|
|
|
|
|
This command will return one of two types of channel backups depending
|
|
|
|
on the set of passed arguments:
|
|
|
|
|
|
|
|
* If a target channel point is specified, then a single channel
|
|
|
|
backup containing only the information for that channel will be
|
|
|
|
returned.
|
|
|
|
|
|
|
|
* If the --all flag is passed, then a multi-channel backup will be
|
|
|
|
returned. A multi backup is a single encrypted blob (displayed in
|
|
|
|
hex encoding) that contains several channels in a single cipher
|
|
|
|
text.
|
2019-04-04 17:07:06 +03:00
|
|
|
|
2018-12-10 07:16:11 +03:00
|
|
|
Both of the backup types can be restored using the restorechanbackup
|
|
|
|
command.
|
|
|
|
`,
|
|
|
|
Flags: []cli.Flag{
|
|
|
|
cli.StringFlag{
|
|
|
|
Name: "chan_point",
|
|
|
|
Usage: "the target channel to obtain an SCB for",
|
|
|
|
},
|
|
|
|
cli.BoolFlag{
|
|
|
|
Name: "all",
|
|
|
|
Usage: "if specified, then a multi backup of all " +
|
|
|
|
"active channels will be returned",
|
|
|
|
},
|
|
|
|
cli.StringFlag{
|
|
|
|
Name: "output_file",
|
|
|
|
Usage: `
|
|
|
|
if specified, then rather than printing a JSON output
|
|
|
|
of the static channel backup, a serialized version of
|
|
|
|
the backup (either Single or Multi) will be written to
|
|
|
|
the target file, this is the same format used by lnd in
|
|
|
|
its channels.backup file `,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Action: actionDecorator(exportChanBackup),
|
|
|
|
}
|
|
|
|
|
|
|
|
func exportChanBackup(ctx *cli.Context) error {
|
|
|
|
ctxb := context.Background()
|
|
|
|
client, cleanUp := getClient(ctx)
|
|
|
|
defer cleanUp()
|
|
|
|
|
|
|
|
// Show command help if no arguments provided
|
|
|
|
if ctx.NArg() == 0 && ctx.NumFlags() == 0 {
|
|
|
|
cli.ShowCommandHelp(ctx, "exportchanbackup")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
err error
|
|
|
|
chanPointStr string
|
|
|
|
)
|
|
|
|
args := ctx.Args()
|
|
|
|
|
|
|
|
switch {
|
|
|
|
case ctx.IsSet("chan_point"):
|
|
|
|
chanPointStr = ctx.String("chan_point")
|
|
|
|
|
|
|
|
case args.Present():
|
|
|
|
chanPointStr = args.First()
|
|
|
|
|
|
|
|
case !ctx.IsSet("all"):
|
|
|
|
return fmt.Errorf("must specify chan_point if --all isn't set")
|
|
|
|
}
|
|
|
|
|
|
|
|
if chanPointStr != "" {
|
|
|
|
chanPointRPC, err := parseChanPoint(chanPointStr)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
chanBackup, err := client.ExportChannelBackup(
|
|
|
|
ctxb, &lnrpc.ExportChannelBackupRequest{
|
|
|
|
ChanPoint: chanPointRPC,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
txid, err := chainhash.NewHash(
|
|
|
|
chanPointRPC.GetFundingTxidBytes(),
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
chanPoint := wire.OutPoint{
|
|
|
|
Hash: *txid,
|
|
|
|
Index: chanPointRPC.OutputIndex,
|
|
|
|
}
|
|
|
|
|
|
|
|
printJSON(struct {
|
|
|
|
ChanPoint string `json:"chan_point"`
|
2019-12-20 12:05:08 +03:00
|
|
|
ChanBackup []byte `json:"chan_backup"`
|
2018-12-10 07:16:11 +03:00
|
|
|
}{
|
2019-12-20 12:05:08 +03:00
|
|
|
ChanPoint: chanPoint.String(),
|
|
|
|
ChanBackup: chanBackup.ChanBackup,
|
|
|
|
})
|
2018-12-10 07:16:11 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if !ctx.IsSet("all") {
|
|
|
|
return fmt.Errorf("if a channel isn't specified, -all must be")
|
|
|
|
}
|
|
|
|
|
|
|
|
chanBackup, err := client.ExportAllChannelBackups(
|
|
|
|
ctxb, &lnrpc.ChanBackupExportRequest{},
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if ctx.IsSet("output_file") {
|
|
|
|
return ioutil.WriteFile(
|
|
|
|
ctx.String("output_file"),
|
|
|
|
chanBackup.MultiChanBackup.MultiChanBackup,
|
|
|
|
0666,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO(roasbeef): support for export | restore ?
|
|
|
|
|
|
|
|
var chanPoints []string
|
|
|
|
for _, chanPoint := range chanBackup.MultiChanBackup.ChanPoints {
|
|
|
|
txid, err := chainhash.NewHash(chanPoint.GetFundingTxidBytes())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
chanPoints = append(chanPoints, wire.OutPoint{
|
|
|
|
Hash: *txid,
|
|
|
|
Index: chanPoint.OutputIndex,
|
|
|
|
}.String())
|
|
|
|
}
|
|
|
|
|
2019-12-20 12:05:08 +03:00
|
|
|
printRespJSON(chanBackup)
|
|
|
|
|
2018-12-10 07:16:11 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-03-11 03:55:12 +03:00
|
|
|
var verifyChanBackupCommand = cli.Command{
|
|
|
|
Name: "verifychanbackup",
|
|
|
|
Category: "Channels",
|
|
|
|
Usage: "Verify an existing channel backup",
|
2019-04-04 17:20:54 +03:00
|
|
|
ArgsUsage: "[--single_backup] [--multi_backup] [--multi_file]",
|
2019-03-11 03:55:12 +03:00
|
|
|
Description: `
|
|
|
|
This command allows a user to verify an existing Single or Multi channel
|
|
|
|
backup for integrity. This is useful when a user has a backup, but is
|
2019-04-04 17:07:06 +03:00
|
|
|
unsure as to if it's valid or for the target node.
|
2019-03-11 03:55:12 +03:00
|
|
|
|
|
|
|
The command will accept backups in one of three forms:
|
|
|
|
|
|
|
|
* A single channel packed SCB, which can be obtained from
|
|
|
|
exportchanbackup. This should be passed in hex encoded format.
|
|
|
|
|
|
|
|
* A packed multi-channel SCB, which couples several individual
|
|
|
|
static channel backups in single blob.
|
|
|
|
|
|
|
|
* A file path which points to a packed multi-channel backup within a
|
|
|
|
file, using the same format that lnd does in its channels.backup
|
|
|
|
file.
|
|
|
|
`,
|
|
|
|
Flags: []cli.Flag{
|
|
|
|
cli.StringFlag{
|
|
|
|
Name: "single_backup",
|
|
|
|
Usage: "a hex encoded single channel backup obtained " +
|
|
|
|
"from exportchanbackup",
|
|
|
|
},
|
|
|
|
cli.StringFlag{
|
|
|
|
Name: "multi_backup",
|
|
|
|
Usage: "a hex encoded multi-channel backup obtained " +
|
|
|
|
"from exportchanbackup",
|
|
|
|
},
|
|
|
|
cli.StringFlag{
|
|
|
|
Name: "multi_file",
|
|
|
|
Usage: "the path to a multi-channel back up file",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Action: actionDecorator(verifyChanBackup),
|
|
|
|
}
|
|
|
|
|
|
|
|
func verifyChanBackup(ctx *cli.Context) error {
|
|
|
|
ctxb := context.Background()
|
|
|
|
client, cleanUp := getClient(ctx)
|
|
|
|
defer cleanUp()
|
|
|
|
|
|
|
|
// Show command help if no arguments provided
|
|
|
|
if ctx.NArg() == 0 && ctx.NumFlags() == 0 {
|
|
|
|
cli.ShowCommandHelp(ctx, "verifychanbackup")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
backups, err := parseChanBackups(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
verifyReq := lnrpc.ChanBackupSnapshot{}
|
|
|
|
|
|
|
|
if backups.GetChanBackups() != nil {
|
|
|
|
verifyReq.SingleChanBackups = backups.GetChanBackups()
|
|
|
|
}
|
|
|
|
if backups.GetMultiChanBackup() != nil {
|
|
|
|
verifyReq.MultiChanBackup = &lnrpc.MultiChanBackup{
|
|
|
|
MultiChanBackup: backups.GetMultiChanBackup(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
resp, err := client.VerifyChanBackup(ctxb, &verifyReq)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
printRespJSON(resp)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-12-10 07:16:11 +03:00
|
|
|
var restoreChanBackupCommand = cli.Command{
|
|
|
|
Name: "restorechanbackup",
|
|
|
|
Category: "Channels",
|
|
|
|
Usage: "Restore an existing single or multi-channel static channel " +
|
|
|
|
"backup",
|
|
|
|
ArgsUsage: "[--single_backup] [--multi_backup] [--multi_file=",
|
|
|
|
Description: `
|
2019-07-04 19:02:26 +03:00
|
|
|
Allows a user to restore a Static Channel Backup (SCB) that was
|
2018-12-10 07:16:11 +03:00
|
|
|
obtained either via the exportchanbackup command, or from lnd's
|
|
|
|
automatically manged channels.backup file. This command should be used
|
|
|
|
if a user is attempting to restore a channel due to data loss on a
|
|
|
|
running node restored with the same seed as the node that created the
|
|
|
|
channel. If successful, this command will allows the user to recover
|
|
|
|
the settled funds stored in the recovered channels.
|
|
|
|
|
|
|
|
The command will accept backups in one of three forms:
|
|
|
|
|
|
|
|
* A single channel packed SCB, which can be obtained from
|
|
|
|
exportchanbackup. This should be passed in hex encoded format.
|
|
|
|
|
|
|
|
* A packed multi-channel SCB, which couples several individual
|
|
|
|
static channel backups in single blob.
|
|
|
|
|
|
|
|
* A file path which points to a packed multi-channel backup within a
|
|
|
|
file, using the same format that lnd does in its channels.backup
|
|
|
|
file.
|
|
|
|
`,
|
|
|
|
Flags: []cli.Flag{
|
|
|
|
cli.StringFlag{
|
|
|
|
Name: "single_backup",
|
|
|
|
Usage: "a hex encoded single channel backup obtained " +
|
|
|
|
"from exportchanbackup",
|
|
|
|
},
|
|
|
|
cli.StringFlag{
|
|
|
|
Name: "multi_backup",
|
|
|
|
Usage: "a hex encoded multi-channel backup obtained " +
|
|
|
|
"from exportchanbackup",
|
|
|
|
},
|
|
|
|
cli.StringFlag{
|
|
|
|
Name: "multi_file",
|
|
|
|
Usage: "the path to a multi-channel back up file",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Action: actionDecorator(restoreChanBackup),
|
|
|
|
}
|
|
|
|
|
2019-09-04 00:03:31 +03:00
|
|
|
// errMissingChanBackup is an error returned when we attempt to parse a channel
|
|
|
|
// backup from a CLI command and it is missing.
|
|
|
|
var errMissingChanBackup = errors.New("missing channel backup")
|
|
|
|
|
2018-12-10 07:16:11 +03:00
|
|
|
func parseChanBackups(ctx *cli.Context) (*lnrpc.RestoreChanBackupRequest, error) {
|
|
|
|
switch {
|
|
|
|
case ctx.IsSet("single_backup"):
|
|
|
|
packedBackup, err := hex.DecodeString(
|
|
|
|
ctx.String("single_backup"),
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("unable to decode single packed "+
|
|
|
|
"backup: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return &lnrpc.RestoreChanBackupRequest{
|
|
|
|
Backup: &lnrpc.RestoreChanBackupRequest_ChanBackups{
|
|
|
|
ChanBackups: &lnrpc.ChannelBackups{
|
|
|
|
ChanBackups: []*lnrpc.ChannelBackup{
|
|
|
|
{
|
|
|
|
ChanBackup: packedBackup,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}, nil
|
|
|
|
|
|
|
|
case ctx.IsSet("multi_backup"):
|
|
|
|
packedMulti, err := hex.DecodeString(
|
|
|
|
ctx.String("multi_backup"),
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("unable to decode multi packed "+
|
|
|
|
"backup: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return &lnrpc.RestoreChanBackupRequest{
|
|
|
|
Backup: &lnrpc.RestoreChanBackupRequest_MultiChanBackup{
|
|
|
|
MultiChanBackup: packedMulti,
|
|
|
|
},
|
|
|
|
}, nil
|
|
|
|
|
|
|
|
case ctx.IsSet("multi_file"):
|
|
|
|
packedMulti, err := ioutil.ReadFile(ctx.String("multi_file"))
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("unable to decode multi packed "+
|
|
|
|
"backup: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return &lnrpc.RestoreChanBackupRequest{
|
|
|
|
Backup: &lnrpc.RestoreChanBackupRequest_MultiChanBackup{
|
|
|
|
MultiChanBackup: packedMulti,
|
|
|
|
},
|
|
|
|
}, nil
|
|
|
|
|
|
|
|
default:
|
2019-09-04 00:03:31 +03:00
|
|
|
return nil, errMissingChanBackup
|
2018-12-10 07:16:11 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func restoreChanBackup(ctx *cli.Context) error {
|
|
|
|
ctxb := context.Background()
|
|
|
|
client, cleanUp := getClient(ctx)
|
|
|
|
defer cleanUp()
|
|
|
|
|
|
|
|
// Show command help if no arguments provided
|
|
|
|
if ctx.NArg() == 0 && ctx.NumFlags() == 0 {
|
|
|
|
cli.ShowCommandHelp(ctx, "restorechanbackup")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var req lnrpc.RestoreChanBackupRequest
|
|
|
|
|
|
|
|
backups, err := parseChanBackups(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
req.Backup = backups.Backup
|
|
|
|
|
|
|
|
_, err = client.RestoreChannelBackups(ctxb, &req)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("unable to restore chan backups: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|