lnd.xprv/cmd/lncli/commands.go

116 lines
2.5 KiB
Go
Raw Normal View History

package main
import (
"bytes"
"encoding/json"
"fmt"
"os"
"github.com/codegangsta/cli"
2016-01-16 21:45:54 +03:00
"github.com/lightningnetwork/lnd/lnrpc"
"golang.org/x/net/context"
)
func printRespJson(resp interface{}) {
b, err := json.Marshal(resp)
if err != nil {
fatal(err)
}
var out bytes.Buffer
json.Indent(&out, b, "", "\t")
out.WriteTo(os.Stdout)
}
2015-12-30 23:19:09 +03:00
var ShellCommand = cli.Command{
Name: "shell",
Usage: "enter interactive shell",
Action: func(c *cli.Context) {
println("not implemented yet")
},
2015-12-30 23:19:09 +03:00
}
var NewAddressCommand = cli.Command{
Name: "newaddress",
Usage: "Generates a new address. Three address types are supported: p2wkh, np2wkh, p2pkh",
Action: newAddress,
}
func newAddress(ctx *cli.Context) {
client := getClient(ctx)
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:
fatal(fmt.Errorf("invalid address type %v, support address type "+
"are: p2wkh, np2wkh, p2pkh", stringAddrType))
}
ctxb := context.Background()
addr, err := client.NewAddress(ctxb, &lnrpc.NewAddressRequest{
Type: addrType,
})
if err != nil {
fatal(err)
}
printRespJson(addr)
}
var SendManyCommand = cli.Command{
Name: "sendmany",
Usage: "create and broadcast a transaction paying the specified " +
"amount(s) to the passed address(es)",
Action: sendMany,
}
func sendMany(ctx *cli.Context) {
var amountToAddr map[string]int64
jsonMap := ctx.Args().Get(0)
if err := json.Unmarshal([]byte(jsonMap), &amountToAddr); err != nil {
fatal(err)
}
ctxb := context.Background()
client := getClient(ctx)
txid, err := client.SendMany(ctxb, &lnrpc.SendManyRequest{amountToAddr})
if err != nil {
fatal(err)
}
printRespJson(txid)
}
var ConnectCommand = cli.Command{
Name: "connect",
Usage: "connect to a remote lnd peer: <lnid>@host",
Action: connectPeer,
}
func connectPeer(ctx *cli.Context) {
ctxb := context.Background()
client := getClient(ctx)
targetAddress := ctx.Args().Get(0)
req := &lnrpc.ConnectPeerRequest{targetAddress}
lnid, err := client.ConnectPeer(ctxb, req)
if err != nil {
fatal(err)
}
printRespJson(lnid)
}