2015-12-30 05:31:03 +03:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
2017-07-26 02:22:06 +03:00
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
2015-12-30 05:31:03 +03:00
|
|
|
|
2016-01-16 21:45:54 +03:00
|
|
|
"github.com/lightningnetwork/lnd/lnrpc"
|
2017-07-26 02:22:06 +03:00
|
|
|
"github.com/roasbeef/btcutil"
|
2016-07-26 20:42:35 +03:00
|
|
|
"github.com/urfave/cli"
|
2015-12-30 05:31:03 +03:00
|
|
|
|
2017-07-26 02:22:06 +03:00
|
|
|
flags "github.com/btcsuite/go-flags"
|
2015-12-30 05:31:03 +03:00
|
|
|
"google.golang.org/grpc"
|
2017-07-26 02:22:06 +03:00
|
|
|
"google.golang.org/grpc/credentials"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
defaultConfigFilename = "lnd.conf"
|
|
|
|
defaultTLSCertFilename = "tls.cert"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
lndHomeDir = btcutil.AppDataDir("lnd", false)
|
|
|
|
defaultConfigFile = filepath.Join(lndHomeDir, defaultConfigFilename)
|
|
|
|
defaultTLSCertPath = filepath.Join(lndHomeDir, defaultTLSCertFilename)
|
2015-12-30 05:31:03 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
func fatal(err error) {
|
|
|
|
fmt.Fprintf(os.Stderr, "[lncli] %v\n", err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
2017-01-30 01:51:30 +03:00
|
|
|
func getClient(ctx *cli.Context) (lnrpc.LightningClient, func()) {
|
2015-12-30 05:31:03 +03:00
|
|
|
conn := getClientConn(ctx)
|
2017-01-30 01:51:30 +03:00
|
|
|
|
|
|
|
cleanUp := func() {
|
|
|
|
conn.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
return lnrpc.NewLightningClient(conn), cleanUp
|
2015-12-30 05:31:03 +03:00
|
|
|
}
|
|
|
|
|
2017-07-26 02:22:06 +03:00
|
|
|
type config struct {
|
|
|
|
TLSCertPath string `long:"tlscertpath" description:"path to TLS certificate"`
|
|
|
|
}
|
|
|
|
|
2015-12-30 05:31:03 +03:00
|
|
|
func getClientConn(ctx *cli.Context) *grpc.ClientConn {
|
|
|
|
// TODO(roasbeef): macaroon based auth
|
2015-12-31 09:28:22 +03:00
|
|
|
// * http://www.grpc.io/docs/guides/auth.html
|
|
|
|
// * http://research.google.com/pubs/pub41892.html
|
|
|
|
// * https://github.com/go-macaroon/macaroon
|
2017-07-26 02:22:06 +03:00
|
|
|
cfg := config{
|
|
|
|
TLSCertPath: defaultTLSCertPath,
|
|
|
|
}
|
|
|
|
|
|
|
|
// We want only the TLS certificate information from the configuration
|
|
|
|
// file at this time, so ignore anything else. We can always add fields
|
|
|
|
// as we need them. When specifying a file on the `lncli` command line,
|
|
|
|
// this should work with just a trusted CA cert assuming the server's
|
|
|
|
// cert file contains the entire chain from the CA to the server's cert.
|
|
|
|
parser := flags.NewParser(&cfg, flags.IgnoreUnknown)
|
|
|
|
iniParser := flags.NewIniParser(parser)
|
|
|
|
if err := iniParser.ParseFile(ctx.GlobalString("config")); err != nil {
|
|
|
|
fatal(err)
|
|
|
|
}
|
|
|
|
if ctx.GlobalString("tlscertpath") != defaultTLSCertPath {
|
|
|
|
cfg.TLSCertPath = ctx.GlobalString("tlscertpath")
|
|
|
|
}
|
|
|
|
cfg.TLSCertPath = cleanAndExpandPath(cfg.TLSCertPath)
|
|
|
|
creds, err := credentials.NewClientTLSFromFile(cfg.TLSCertPath, "")
|
|
|
|
if err != nil {
|
|
|
|
fatal(err)
|
|
|
|
}
|
|
|
|
opts := []grpc.DialOption{grpc.WithTransportCredentials(creds)}
|
2015-12-30 05:31:03 +03:00
|
|
|
|
|
|
|
conn, err := grpc.Dial(ctx.GlobalString("rpcserver"), opts...)
|
|
|
|
if err != nil {
|
|
|
|
fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return conn
|
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
app := cli.NewApp()
|
|
|
|
app.Name = "lncli"
|
2017-04-12 07:18:48 +03:00
|
|
|
app.Version = "0.2"
|
2016-12-28 02:45:10 +03:00
|
|
|
app.Usage = "control plane for your Lightning Network Daemon (lnd)"
|
2015-12-30 05:31:03 +03:00
|
|
|
app.Flags = []cli.Flag{
|
|
|
|
cli.StringFlag{
|
|
|
|
Name: "rpcserver",
|
2016-03-23 04:47:10 +03:00
|
|
|
Value: "localhost:10009",
|
2016-01-16 21:45:54 +03:00
|
|
|
Usage: "host:port of ln daemon",
|
2015-12-30 05:31:03 +03:00
|
|
|
},
|
2017-07-26 02:22:06 +03:00
|
|
|
cli.StringFlag{
|
|
|
|
Name: "config",
|
|
|
|
Value: defaultConfigFile,
|
|
|
|
Usage: "path to config file for TLS cert path",
|
|
|
|
},
|
|
|
|
cli.StringFlag{
|
|
|
|
Name: "tlscertpath",
|
|
|
|
Value: defaultTLSCertPath,
|
|
|
|
Usage: "path to TLS certificate",
|
|
|
|
},
|
2015-12-30 05:31:03 +03:00
|
|
|
}
|
|
|
|
app.Commands = []cli.Command{
|
2017-02-24 16:32:33 +03:00
|
|
|
newAddressCommand,
|
|
|
|
sendManyCommand,
|
|
|
|
sendCoinsCommand,
|
|
|
|
connectCommand,
|
2017-05-06 01:54:25 +03:00
|
|
|
disconnectCommand,
|
2017-02-24 16:32:33 +03:00
|
|
|
openChannelCommand,
|
|
|
|
closeChannelCommand,
|
|
|
|
listPeersCommand,
|
|
|
|
walletBalanceCommand,
|
|
|
|
channelBalanceCommand,
|
|
|
|
getInfoCommand,
|
|
|
|
pendingChannelsCommand,
|
|
|
|
sendPaymentCommand,
|
|
|
|
addInvoiceCommand,
|
|
|
|
lookupInvoiceCommand,
|
|
|
|
listInvoicesCommand,
|
|
|
|
listChannelsCommand,
|
|
|
|
listPaymentsCommand,
|
|
|
|
describeGraphCommand,
|
|
|
|
getChanInfoCommand,
|
|
|
|
getNodeInfoCommand,
|
2017-03-21 05:01:57 +03:00
|
|
|
queryRoutesCommand,
|
2017-02-24 16:32:33 +03:00
|
|
|
getNetworkInfoCommand,
|
|
|
|
debugLevelCommand,
|
|
|
|
decodePayReqComamnd,
|
2017-03-09 07:44:32 +03:00
|
|
|
listChainTxnsCommand,
|
2017-05-12 00:55:56 +03:00
|
|
|
stopCommand,
|
2017-04-20 05:33:09 +03:00
|
|
|
signMessageCommand,
|
|
|
|
verifyMessageCommand,
|
2015-12-30 05:31:03 +03:00
|
|
|
}
|
2016-01-17 06:10:29 +03:00
|
|
|
|
2015-12-30 05:31:03 +03:00
|
|
|
if err := app.Run(os.Args); err != nil {
|
|
|
|
fatal(err)
|
|
|
|
}
|
|
|
|
}
|
2017-07-26 02:22:06 +03:00
|
|
|
|
|
|
|
// cleanAndExpandPath expands environment variables and leading ~ in the
|
|
|
|
// passed path, cleans the result, and returns it.
|
|
|
|
// This function is taken from https://github.com/btcsuite/btcd
|
|
|
|
func cleanAndExpandPath(path string) string {
|
|
|
|
// Expand initial ~ to OS specific home directory.
|
|
|
|
if strings.HasPrefix(path, "~") {
|
|
|
|
homeDir := filepath.Dir(lndHomeDir)
|
|
|
|
path = strings.Replace(path, "~", homeDir, 1)
|
|
|
|
}
|
|
|
|
|
|
|
|
// NOTE: The os.ExpandEnv doesn't work with Windows-style %VARIABLE%,
|
|
|
|
// but the variables can still be expanded via POSIX-style $VARIABLE.
|
|
|
|
return filepath.Clean(os.ExpandEnv(path))
|
|
|
|
}
|