2015-12-30 03:31:03 +01:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2015-12-30 03:58:58 +01:00
|
|
|
"bytes"
|
2015-12-30 03:31:03 +01:00
|
|
|
"encoding/json"
|
2016-04-25 05:27:19 +02:00
|
|
|
"fmt"
|
2016-07-08 00:35:06 +02:00
|
|
|
"io"
|
2015-12-30 03:58:58 +01:00
|
|
|
"os"
|
2016-06-21 21:35:07 +02:00
|
|
|
"strings"
|
2015-12-30 03:31:03 +01:00
|
|
|
|
2016-06-21 21:35:07 +02:00
|
|
|
"github.com/Roasbeef/btcd/wire"
|
2015-12-30 03:31:03 +01:00
|
|
|
"github.com/codegangsta/cli"
|
2016-01-16 19:45:54 +01:00
|
|
|
"github.com/lightningnetwork/lnd/lnrpc"
|
2015-12-30 03:31:03 +01:00
|
|
|
"golang.org/x/net/context"
|
|
|
|
)
|
|
|
|
|
2016-06-21 21:35:07 +02:00
|
|
|
// TODO(roasbeef): cli logic for supporting both positional and unix style
|
|
|
|
// arguments.
|
|
|
|
|
2015-12-30 03:58:58 +01:00
|
|
|
func printRespJson(resp interface{}) {
|
|
|
|
b, err := json.Marshal(resp)
|
|
|
|
if err != nil {
|
|
|
|
fatal(err)
|
|
|
|
}
|
|
|
|
|
2016-06-21 21:35:07 +02:00
|
|
|
// TODO(roasbeef): disable 'omitempty' like behavior
|
|
|
|
|
2015-12-30 03:58:58 +01:00
|
|
|
var out bytes.Buffer
|
|
|
|
json.Indent(&out, b, "", "\t")
|
|
|
|
out.WriteTo(os.Stdout)
|
|
|
|
}
|
|
|
|
|
2015-12-30 21:19:09 +01:00
|
|
|
var ShellCommand = cli.Command{
|
2016-01-18 04:18:08 +01:00
|
|
|
Name: "shell",
|
|
|
|
Usage: "enter interactive shell",
|
|
|
|
Action: func(c *cli.Context) {
|
|
|
|
println("not implemented yet")
|
|
|
|
},
|
2015-12-30 21:19:09 +01:00
|
|
|
}
|
|
|
|
|
2015-12-30 03:31:03 +01:00
|
|
|
var NewAddressCommand = cli.Command{
|
|
|
|
Name: "newaddress",
|
2016-06-21 21:35:07 +02:00
|
|
|
Usage: "generates a new address. Three address types are supported: p2wkh, np2wkh, p2pkh",
|
2015-12-30 03:31:03 +01:00
|
|
|
Action: newAddress,
|
|
|
|
}
|
|
|
|
|
2016-06-29 22:01:08 +02:00
|
|
|
func newAddress(ctx *cli.Context) error {
|
2015-12-30 03:31:03 +01:00
|
|
|
client := getClient(ctx)
|
|
|
|
|
2016-04-25 05:27:19 +02:00
|
|
|
stringAddrType := ctx.Args().Get(0)
|
|
|
|
|
|
|
|
// Map the string encoded address type, to the concrete typed address
|
|
|
|
// type enum. An unrecognized address type will result in an error.
|
|
|
|
var addrType lnrpc.NewAddressRequest_AddressType
|
|
|
|
switch stringAddrType { // TODO(roasbeef): make them ints on the cli?
|
|
|
|
case "p2wkh":
|
|
|
|
addrType = lnrpc.NewAddressRequest_WITNESS_PUBKEY_HASH
|
|
|
|
case "np2wkh":
|
|
|
|
addrType = lnrpc.NewAddressRequest_NESTED_PUBKEY_HASH
|
|
|
|
case "p2pkh":
|
|
|
|
addrType = lnrpc.NewAddressRequest_PUBKEY_HASH
|
|
|
|
default:
|
2016-06-29 22:01:08 +02:00
|
|
|
return fmt.Errorf("invalid address type %v, support address type "+
|
|
|
|
"are: p2wkh, np2wkh, p2pkh", stringAddrType)
|
2016-04-25 05:27:19 +02:00
|
|
|
}
|
|
|
|
|
2015-12-30 03:31:03 +01:00
|
|
|
ctxb := context.Background()
|
2016-04-25 05:27:19 +02:00
|
|
|
addr, err := client.NewAddress(ctxb, &lnrpc.NewAddressRequest{
|
|
|
|
Type: addrType,
|
|
|
|
})
|
2015-12-30 03:31:03 +01:00
|
|
|
if err != nil {
|
2016-06-29 22:01:08 +02:00
|
|
|
return err
|
2015-12-30 03:31:03 +01:00
|
|
|
}
|
|
|
|
|
2015-12-30 03:58:58 +01:00
|
|
|
printRespJson(addr)
|
2016-06-29 22:01:08 +02:00
|
|
|
return nil
|
2015-12-30 03:31:03 +01:00
|
|
|
}
|
|
|
|
|
2016-06-29 20:29:21 +02:00
|
|
|
var SendCoinsCommand = cli.Command{
|
|
|
|
Name: "sendcoins",
|
|
|
|
Description: "send a specified amount of bitcoin to the passed address",
|
|
|
|
Usage: "sendcoins --addr=<bitcoin addresss> --amt=<num coins in satoshis>",
|
|
|
|
Flags: []cli.Flag{
|
|
|
|
cli.StringFlag{
|
|
|
|
Name: "addr",
|
|
|
|
Usage: "the bitcoin address to send coins to on-chain",
|
|
|
|
},
|
|
|
|
// TODO(roasbeef): switch to BTC on command line? int may not be sufficient
|
|
|
|
cli.IntFlag{
|
|
|
|
Name: "amt",
|
|
|
|
Usage: "the number of bitcoin denominated in satoshis to send",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Action: sendCoins,
|
|
|
|
}
|
|
|
|
|
2016-06-29 22:01:08 +02:00
|
|
|
func sendCoins(ctx *cli.Context) error {
|
2016-06-29 20:29:21 +02:00
|
|
|
ctxb := context.Background()
|
|
|
|
client := getClient(ctx)
|
|
|
|
|
|
|
|
req := &lnrpc.SendCoinsRequest{
|
|
|
|
Addr: ctx.String("addr"),
|
|
|
|
Amount: int64(ctx.Int("amt")),
|
|
|
|
}
|
|
|
|
txid, err := client.SendCoins(ctxb, req)
|
|
|
|
if err != nil {
|
2016-06-29 22:01:08 +02:00
|
|
|
return err
|
2016-06-29 20:29:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
printRespJson(txid)
|
2016-06-29 22:01:08 +02:00
|
|
|
return nil
|
2016-06-29 20:29:21 +02:00
|
|
|
}
|
|
|
|
|
2015-12-30 03:31:03 +01:00
|
|
|
var SendManyCommand = cli.Command{
|
|
|
|
Name: "sendmany",
|
2016-06-29 20:29:21 +02:00
|
|
|
Description: "create and broadcast a transaction paying the specified " +
|
2015-12-30 03:31:03 +01:00
|
|
|
"amount(s) to the passed address(es)",
|
2016-06-29 20:29:21 +02:00
|
|
|
Usage: `sendmany '{"ExampleAddr": NumCoinsInSatoshis, "SecondAddr": NumCoins}'`,
|
2015-12-30 03:31:03 +01:00
|
|
|
Action: sendMany,
|
|
|
|
}
|
|
|
|
|
2016-06-29 22:01:08 +02:00
|
|
|
func sendMany(ctx *cli.Context) error {
|
2015-12-30 03:31:03 +01:00
|
|
|
var amountToAddr map[string]int64
|
|
|
|
|
|
|
|
jsonMap := ctx.Args().Get(0)
|
|
|
|
if err := json.Unmarshal([]byte(jsonMap), &amountToAddr); err != nil {
|
2016-06-29 22:01:08 +02:00
|
|
|
return err
|
2015-12-30 03:31:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
ctxb := context.Background()
|
|
|
|
client := getClient(ctx)
|
|
|
|
|
|
|
|
txid, err := client.SendMany(ctxb, &lnrpc.SendManyRequest{amountToAddr})
|
|
|
|
if err != nil {
|
2016-06-29 22:01:08 +02:00
|
|
|
return err
|
2015-12-30 03:31:03 +01:00
|
|
|
}
|
|
|
|
|
2015-12-30 03:58:58 +01:00
|
|
|
printRespJson(txid)
|
2016-06-29 22:01:08 +02:00
|
|
|
return nil
|
2015-12-30 03:31:03 +01:00
|
|
|
}
|
2016-01-17 04:10:29 +01:00
|
|
|
|
|
|
|
var ConnectCommand = cli.Command{
|
|
|
|
Name: "connect",
|
|
|
|
Usage: "connect to a remote lnd peer: <lnid>@host",
|
|
|
|
Action: connectPeer,
|
|
|
|
}
|
|
|
|
|
2016-06-29 22:01:08 +02:00
|
|
|
func connectPeer(ctx *cli.Context) error {
|
2016-01-17 04:10:29 +01:00
|
|
|
ctxb := context.Background()
|
|
|
|
client := getClient(ctx)
|
|
|
|
|
|
|
|
targetAddress := ctx.Args().Get(0)
|
2016-06-21 21:35:07 +02:00
|
|
|
splitAddr := strings.Split(targetAddress, "@")
|
|
|
|
addr := &lnrpc.LightningAddress{
|
|
|
|
PubKeyHash: splitAddr[0],
|
|
|
|
Host: splitAddr[1],
|
|
|
|
}
|
|
|
|
req := &lnrpc.ConnectPeerRequest{addr}
|
2016-01-17 04:10:29 +01:00
|
|
|
|
|
|
|
lnid, err := client.ConnectPeer(ctxb, req)
|
|
|
|
if err != nil {
|
2016-06-29 22:01:08 +02:00
|
|
|
return err
|
2016-01-17 04:10:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
printRespJson(lnid)
|
2016-06-29 22:01:08 +02:00
|
|
|
return nil
|
2016-01-17 04:10:29 +01:00
|
|
|
}
|
2016-06-21 21:35:07 +02:00
|
|
|
|
|
|
|
// TODO(roasbeef): default number of confirmations
|
|
|
|
var OpenChannelCommand = cli.Command{
|
|
|
|
Name: "openchannel",
|
|
|
|
Description: "Attempt to open a new channel to an existing peer, " +
|
|
|
|
"blocking until the channel is 'open'. Once the channel is " +
|
|
|
|
"open, a channelPoint (txid:vout) of the funding output is " +
|
|
|
|
"returned.",
|
|
|
|
Usage: "openchannel --peer_id=X --local_amt=N --remote_amt=N --num_confs=N",
|
|
|
|
Flags: []cli.Flag{
|
|
|
|
cli.IntFlag{
|
|
|
|
Name: "peer_id",
|
|
|
|
Usage: "the id of the peer to open a channel with",
|
|
|
|
},
|
|
|
|
cli.IntFlag{
|
|
|
|
Name: "local_amt",
|
|
|
|
Usage: "the number of satoshis the wallet should commit to the channel",
|
|
|
|
},
|
|
|
|
cli.IntFlag{
|
|
|
|
Name: "remote_amt",
|
|
|
|
Usage: "the number of satoshis the remote peer should commit to the channel",
|
|
|
|
},
|
|
|
|
cli.IntFlag{
|
|
|
|
Name: "num_confs",
|
|
|
|
Usage: "the number of confirmations required before the " +
|
|
|
|
"channel is considered 'open'",
|
|
|
|
},
|
2016-07-08 00:35:06 +02:00
|
|
|
cli.BoolFlag{
|
|
|
|
Name: "block",
|
|
|
|
Usage: "block and wait until the channel is fully open",
|
|
|
|
},
|
2016-06-21 21:35:07 +02:00
|
|
|
},
|
|
|
|
Action: openChannel,
|
|
|
|
}
|
|
|
|
|
2016-06-29 22:01:08 +02:00
|
|
|
func openChannel(ctx *cli.Context) error {
|
2016-06-21 21:35:07 +02:00
|
|
|
// TODO(roasbeef): add deadline to context
|
|
|
|
ctxb := context.Background()
|
|
|
|
client := getClient(ctx)
|
|
|
|
|
|
|
|
req := &lnrpc.OpenChannelRequest{
|
|
|
|
TargetPeerId: int32(ctx.Int("peer_id")),
|
|
|
|
LocalFundingAmount: int64(ctx.Int("local_amt")),
|
|
|
|
RemoteFundingAmount: int64(ctx.Int("remote_amt")),
|
|
|
|
NumConfs: uint32(ctx.Int("num_confs")),
|
|
|
|
}
|
|
|
|
|
2016-07-08 00:35:06 +02:00
|
|
|
stream, err := client.OpenChannel(ctxb, req)
|
2016-06-21 21:35:07 +02:00
|
|
|
if err != nil {
|
2016-06-29 22:01:08 +02:00
|
|
|
return err
|
2016-06-21 21:35:07 +02:00
|
|
|
}
|
|
|
|
|
2016-07-08 00:35:06 +02:00
|
|
|
if !ctx.Bool("block") {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
for {
|
|
|
|
resp, err := stream.Recv()
|
|
|
|
if err == io.EOF {
|
|
|
|
return nil
|
|
|
|
} else if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
txid, err := wire.NewShaHash(resp.ChannelPoint.FundingTxid)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
index := resp.ChannelPoint.OutputIndex
|
|
|
|
printRespJson(struct {
|
|
|
|
ChannelPoint string `json:"channel_point"`
|
|
|
|
}{
|
|
|
|
ChannelPoint: fmt.Sprintf("%v:%v", txid, index),
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
2016-06-21 21:35:07 +02:00
|
|
|
}
|
|
|
|
|
2016-06-29 22:01:08 +02:00
|
|
|
return nil
|
2016-06-21 21:35:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// TODO(roasbeef): also allow short relative channel ID.
|
|
|
|
var CloseChannelCommand = cli.Command{
|
|
|
|
Name: "closechannel",
|
|
|
|
Description: "Close an existing channel. The channel can be closed either " +
|
|
|
|
"cooperatively, or uncooperatively (forced).",
|
|
|
|
Usage: "closechannel funding_txid output_index time_limit allow_force",
|
|
|
|
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.StringFlag{
|
|
|
|
Name: "time_limit",
|
|
|
|
Usage: "a relative deadline afterwhich the attempt should be " +
|
|
|
|
"abandonded",
|
|
|
|
},
|
|
|
|
cli.BoolFlag{
|
|
|
|
Name: "force",
|
|
|
|
Usage: "after the time limit has passed, attempted an " +
|
|
|
|
"uncooperative closure",
|
|
|
|
},
|
2016-07-08 00:35:06 +02:00
|
|
|
cli.BoolFlag{
|
|
|
|
Name: "block",
|
|
|
|
Usage: "block until the channel is closed",
|
|
|
|
},
|
2016-06-21 21:35:07 +02:00
|
|
|
},
|
|
|
|
Action: closeChannel,
|
|
|
|
}
|
|
|
|
|
2016-06-29 22:01:08 +02:00
|
|
|
func closeChannel(ctx *cli.Context) error {
|
2016-06-21 21:35:07 +02:00
|
|
|
ctxb := context.Background()
|
|
|
|
client := getClient(ctx)
|
|
|
|
|
2016-06-22 20:10:20 +02:00
|
|
|
txid, err := wire.NewShaHashFromStr(ctx.String("funding_txid"))
|
2016-06-21 21:35:07 +02:00
|
|
|
if err != nil {
|
2016-06-29 22:01:08 +02:00
|
|
|
return err
|
2016-06-21 21:35:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
req := &lnrpc.CloseChannelRequest{
|
|
|
|
ChannelPoint: &lnrpc.ChannelPoint{
|
2016-06-22 20:10:20 +02:00
|
|
|
FundingTxid: txid[:],
|
2016-06-21 21:35:07 +02:00
|
|
|
OutputIndex: uint32(ctx.Int("output_index")),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2016-07-08 00:35:06 +02:00
|
|
|
stream, err := client.CloseChannel(ctxb, req)
|
2016-06-21 21:35:07 +02:00
|
|
|
if err != nil {
|
2016-06-29 22:01:08 +02:00
|
|
|
return err
|
2016-06-21 21:35:07 +02:00
|
|
|
}
|
|
|
|
|
2016-07-08 00:35:06 +02:00
|
|
|
if !ctx.Bool("block") {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
for {
|
|
|
|
resp, err := stream.Recv()
|
|
|
|
if err == io.EOF {
|
|
|
|
return nil
|
|
|
|
} else if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
printRespJson(resp)
|
|
|
|
}
|
|
|
|
|
2016-06-29 22:01:08 +02:00
|
|
|
return nil
|
2016-06-21 21:35:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
var ListPeersCommand = cli.Command{
|
|
|
|
Name: "listpeers",
|
|
|
|
Description: "List all active, currently connected peers.",
|
|
|
|
Action: listPeers,
|
|
|
|
}
|
|
|
|
|
2016-06-29 22:01:08 +02:00
|
|
|
func listPeers(ctx *cli.Context) error {
|
2016-06-21 21:35:07 +02:00
|
|
|
ctxb := context.Background()
|
|
|
|
client := getClient(ctx)
|
|
|
|
|
|
|
|
req := &lnrpc.ListPeersRequest{}
|
|
|
|
resp, err := client.ListPeers(ctxb, req)
|
|
|
|
if err != nil {
|
2016-06-29 22:01:08 +02:00
|
|
|
return err
|
2016-06-21 21:35:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
printRespJson(resp)
|
2016-06-29 22:01:08 +02:00
|
|
|
return nil
|
2016-06-21 21:35:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
var WalletBalanceCommand = cli.Command{
|
|
|
|
Name: "walletbalance",
|
|
|
|
Description: "compute and display the wallet's current balance",
|
|
|
|
Usage: "walletbalance --witness_only=[true|false]",
|
|
|
|
Flags: []cli.Flag{
|
|
|
|
cli.BoolFlag{
|
|
|
|
Name: "witness_only",
|
|
|
|
Usage: "if only witness outputs should be considered when " +
|
|
|
|
"calculating the wallet's balance",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Action: walletBalance,
|
|
|
|
}
|
|
|
|
|
2016-06-29 22:01:08 +02:00
|
|
|
func walletBalance(ctx *cli.Context) error {
|
2016-06-21 21:35:07 +02:00
|
|
|
ctxb := context.Background()
|
|
|
|
client := getClient(ctx)
|
|
|
|
|
|
|
|
req := &lnrpc.WalletBalanceRequest{
|
|
|
|
WitnessOnly: ctx.Bool("witness_only"),
|
|
|
|
}
|
|
|
|
resp, err := client.WalletBalance(ctxb, req)
|
|
|
|
if err != nil {
|
2016-06-29 22:01:08 +02:00
|
|
|
return err
|
2016-06-21 21:35:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
printRespJson(resp)
|
2016-06-29 22:01:08 +02:00
|
|
|
return nil
|
2016-06-21 21:35:07 +02:00
|
|
|
}
|
2016-07-06 03:58:41 +02:00
|
|
|
|
|
|
|
var GetInfoCommand = cli.Command{
|
|
|
|
Name: "getinfo",
|
|
|
|
Description: "returns basic information related to the active daemon",
|
|
|
|
Action: getInfo,
|
|
|
|
}
|
|
|
|
|
|
|
|
func getInfo(ctx *cli.Context) error {
|
|
|
|
ctxb := context.Background()
|
|
|
|
client := getClient(ctx)
|
|
|
|
|
|
|
|
req := &lnrpc.GetInfoRequest{}
|
|
|
|
resp, err := client.GetInfo(ctxb, req)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
printRespJson(resp)
|
|
|
|
return nil
|
|
|
|
}
|
2016-07-08 00:35:58 +02:00
|
|
|
|
|
|
|
var PendingChannelsCommand = cli.Command{
|
|
|
|
Name: "pendingchannels",
|
|
|
|
Description: "display information pertaining to pending channels",
|
|
|
|
Usage: "pendingchannels --status=[all|opening|closing]",
|
|
|
|
Flags: []cli.Flag{
|
|
|
|
cli.BoolFlag{
|
|
|
|
Name: "open, o",
|
|
|
|
Usage: "display the status of new pending channels",
|
|
|
|
},
|
|
|
|
cli.BoolFlag{
|
|
|
|
Name: "close, c",
|
|
|
|
Usage: "display the status of channels being closed",
|
|
|
|
},
|
|
|
|
cli.BoolFlag{
|
|
|
|
Name: "all, a",
|
|
|
|
Usage: "display the status of channels in the " +
|
|
|
|
"process of being opened or closed",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Action: pendingChannels,
|
|
|
|
}
|
|
|
|
|
|
|
|
func pendingChannels(ctx *cli.Context) error {
|
|
|
|
ctxb := context.Background()
|
|
|
|
client := getClient(ctx)
|
|
|
|
|
|
|
|
var channelStatus lnrpc.ChannelStatus
|
|
|
|
switch {
|
|
|
|
case ctx.Bool("all"):
|
|
|
|
channelStatus = lnrpc.ChannelStatus_ALL
|
|
|
|
case ctx.Bool("open"):
|
|
|
|
channelStatus = lnrpc.ChannelStatus_OPENING
|
|
|
|
case ctx.Bool("close"):
|
|
|
|
channelStatus = lnrpc.ChannelStatus_CLOSING
|
|
|
|
default:
|
|
|
|
channelStatus = lnrpc.ChannelStatus_ALL
|
|
|
|
}
|
|
|
|
|
|
|
|
req := &lnrpc.PendingChannelRequest{channelStatus}
|
|
|
|
resp, err := client.PendingChannels(ctxb, req)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
printRespJson(resp)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|