From 8bd8293c8c974d33f67b97edc8cfe75de059029c Mon Sep 17 00:00:00 2001 From: Tadge Dryja Date: Wed, 30 Dec 2015 23:56:57 -0400 Subject: [PATCH] can send messages from shell over grpc. doesn't do anything yet. --- cmd/lnshell/commands.go | 22 +++++++++++++++++++++- cmd/lnshell/lnshellmain.go | 34 +++++++++++++++++++++++++--------- rpcserver.go | 27 +++++++++++++++++++++++++++ 3 files changed, 73 insertions(+), 10 deletions(-) diff --git a/cmd/lnshell/commands.go b/cmd/lnshell/commands.go index 1ba5f218..2f9e4298 100644 --- a/cmd/lnshell/commands.go +++ b/cmd/lnshell/commands.go @@ -5,6 +5,7 @@ import ( "time" "google.golang.org/grpc" + "li.lan/labs/plasma/lnrpc" ) // connects via grpc to the ln node. default (hardcoded?) local:10K @@ -38,7 +39,18 @@ func RpcConnect(args []string) error { } func LnConnect(args []string) error { - fmt.Printf("lnconnect, %d args\n", len(args)) + // var err error + if len(args) == 0 { + return fmt.Errorf("need: lnc pubkeyhash@hostname or pkh (via pbx)") + } + + req := new(lnrpc.LNConnectRequest) + req.IdAtHost = args[0] + resp, err := z.LNConnect(stub, req) + if err != nil { + return err + } + fmt.Printf("connected. remote lnid is %x\n", resp.LnID) return nil } @@ -59,5 +71,13 @@ func LnChat(args []string) error { // msg := append([]byte{lnwire.MSGID_TEXTCHAT}, []byte(chat)...) fmt.Printf("will send text message: %s\n", chat) + req := new(lnrpc.LnChatRequest) + req.DestID = []byte("testID") + req.Msg = chat + _, err := z.LNChat(stub, req) + if err != nil { + return err + } + fmt.Printf("got response but there's nothing in it\n") return nil } diff --git a/cmd/lnshell/lnshellmain.go b/cmd/lnshell/lnshellmain.go index 6e523727..237ee165 100644 --- a/cmd/lnshell/lnshellmain.go +++ b/cmd/lnshell/lnshellmain.go @@ -6,22 +6,40 @@ import ( "log" "os" "strings" + + "golang.org/x/net/context" + "google.golang.org/grpc" + "li.lan/labs/plasma/lnrpc" ) +var z lnrpc.LightningClient +var stub context.Context + func main() { fmt.Printf("LNShell v0.0. \n") fmt.Printf("Connects to LN daemon, default on 127.0.0.1:10000.\n") - shellPrompt() + err := shellPrompt() + if err != nil { + log.Fatal(err) + } return } -func shellPrompt() { +func shellPrompt() error { + stub = context.Background() + opts := []grpc.DialOption{grpc.WithInsecure()} + conn, err := grpc.Dial("localhost:10000", opts...) + if err != nil { + return err + } + z = lnrpc.NewLightningClient(conn) + for { reader := bufio.NewReaderSize(os.Stdin, 4000) fmt.Printf("->") msg, err := reader.ReadString('\n') if err != nil { - log.Fatal(err) + return err } cmdslice := strings.Fields(msg) if len(cmdslice) < 1 { @@ -30,7 +48,7 @@ func shellPrompt() { fmt.Printf("entered command: %s\n", msg) err = Shellparse(cmdslice) if err != nil { - log.Fatal(err) + return err } } } @@ -60,15 +78,13 @@ func Shellparse(cmdslice []string) error { } return nil } - - if cmd == "rpc" { - err = RpcConnect(args) + if cmd == "lnl" { + err = LnListen(args) if err != nil { - fmt.Printf("RPC connect error: %s\n", err) + fmt.Printf("LN listen error: %s\n", err) } return nil } - fmt.Printf("Command not recognized.\n") return nil } diff --git a/rpcserver.go b/rpcserver.go index 4f1321f5..2d037344 100644 --- a/rpcserver.go +++ b/rpcserver.go @@ -2,6 +2,7 @@ package main import ( "encoding/hex" + "log" "github.com/btcsuite/btcutil" "github.com/btcsuite/btcwallet/waddrmgr" @@ -55,3 +56,29 @@ func (r *rpcServer) NewAddress(ctx context.Context, in *lnrpc.NewAddressRequest) return &lnrpc.NewAddressResponse{Address: addr.String()}, nil } + +// LNConnect +func (r *rpcServer) LNConnect(ctx context.Context, + in *lnrpc.LNConnectRequest) (*lnrpc.LnConnectResponse, error) { + + resp := new(lnrpc.LnConnectResponse) + resp.LnID = []byte("ya") + + return resp, nil +} + +// TCPListen +func (r *rpcServer) TCPListen(ctx context.Context, + in *lnrpc.TCPListenRequest) (*lnrpc.TCPListenResponse, error) { + + resp := new(lnrpc.TCPListenResponse) + return resp, nil +} + +// LNChat +func (r *rpcServer) LNChat(ctx context.Context, + in *lnrpc.LnChatRequest) (*lnrpc.LnChatResponse, error) { + log.Printf("requested to chat, message: %s\n", in.Msg) + resp := new(lnrpc.LnChatResponse) + return resp, nil +}