From f2d3c2455b09697a785510ad02291d76d0fdf9f3 Mon Sep 17 00:00:00 2001 From: Olaoluwa Osuntokun Date: Tue, 29 Dec 2015 20:31:03 -0600 Subject: [PATCH] cmd/lncli: add new package, implements cli for plasma --- cmd/lncli/commands.go | 58 ++++++++++++++++++++++++++++++++++++++++ cmd/lncli/main.go | 61 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 119 insertions(+) create mode 100644 cmd/lncli/commands.go create mode 100644 cmd/lncli/main.go diff --git a/cmd/lncli/commands.go b/cmd/lncli/commands.go new file mode 100644 index 00000000..f5fa29bb --- /dev/null +++ b/cmd/lncli/commands.go @@ -0,0 +1,58 @@ +package main + +import ( + "encoding/json" + "fmt" + + "github.com/codegangsta/cli" + "golang.org/x/net/context" + "li.lan/labs/plasma/rpcprotos" +) + +var NewAddressCommand = cli.Command{ + Name: "newaddress", + Usage: "gets the next address in the HD chain", + Action: newAddress, +} + +func newAddress(ctx *cli.Context) { + client := getClient(ctx) + + ctxb := context.Background() + addr, err := client.NewAddress(ctxb, &lnrpc.NewAddressRequest{}) + if err != nil { + fatal(err) + } + + fmt.Println(json.Marshal(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 + + fmt.Println(ctx.Args()) + + jsonMap := ctx.Args().Get(0) + if err := json.Unmarshal([]byte(jsonMap), &amountToAddr); err != nil { + fatal(err) + } + + fmt.Println("map: %v", amountToAddr) + + ctxb := context.Background() + client := getClient(ctx) + + txid, err := client.SendMany(ctxb, &lnrpc.SendManyRequest{amountToAddr}) + if err != nil { + fatal(err) + } + + fmt.Println(json.Marshal(txid)) +} diff --git a/cmd/lncli/main.go b/cmd/lncli/main.go new file mode 100644 index 00000000..82d73888 --- /dev/null +++ b/cmd/lncli/main.go @@ -0,0 +1,61 @@ +package main + +import ( + "flag" + "fmt" + "os" + + "github.com/codegangsta/cli" + "li.lan/labs/plasma/rpcprotos" + + "google.golang.org/grpc" +) + +var ( + serverAddr = flag.String("rpcserver", "127.0.0.1:10000", "The server address in the format of host:port") +) + +func fatal(err error) { + fmt.Fprintf(os.Stderr, "[lncli] %v\n", err) + os.Exit(1) +} + +func getClient(ctx *cli.Context) lnrpc.LightningClient { + conn := getClientConn(ctx) + return lnrpc.NewLightningClient(conn) +} + +func getClientConn(ctx *cli.Context) *grpc.ClientConn { + // TODO(roasbeef): macaroon based auth + opts := []grpc.DialOption{grpc.WithInsecure()} + + conn, err := grpc.Dial(ctx.GlobalString("rpcserver"), opts...) + if err != nil { + fatal(err) + } + + return conn +} + +func main() { + app := cli.NewApp() + app.Name = "lncli" + app.Version = "0.1" + app.Usage = "control plane for your LN daemon" + app.Flags = []cli.Flag{ + cli.StringFlag{ + Name: "rpcserver", + Value: "localhost:10000", + Usage: "host:port of plasma daemon", + }, + } + app.Commands = []cli.Command{ + NewAddressCommand, + SendManyCommand, + } + if err := app.Run(os.Args); err != nil { + fatal(err) + } + + // ctx := context.Background() +}