From 31f3df2183ec60a7c7033294d0dc815e2f64f670 Mon Sep 17 00:00:00 2001 From: Tadge Dryja Date: Wed, 30 Dec 2015 16:19:09 -0400 Subject: [PATCH] add gitignore, cli shell --- .gitignore | 35 ++++++++ cmd/lncli/commands.go | 8 +- cmd/lncli/main.go | 3 +- cmd/lncli/shell.go | 147 +++++++++++++++++++++++++++++++++ lnrpc/gen_protos.sh | 3 + lnrpc/rpc.pb.go | 188 ++++++++++++++++++++++++++++++++++++++++++ lnrpc/rpc.proto | 21 +++++ plasma.go | 2 +- rpcserver.go | 2 +- 9 files changed, 405 insertions(+), 4 deletions(-) create mode 100644 .gitignore create mode 100644 cmd/lncli/shell.go create mode 100755 lnrpc/gen_protos.sh create mode 100644 lnrpc/rpc.pb.go create mode 100644 lnrpc/rpc.proto diff --git a/.gitignore b/.gitignore new file mode 100644 index 00000000..5e85d365 --- /dev/null +++ b/.gitignore @@ -0,0 +1,35 @@ +# ---> Go +# Compiled Object files, Static and Dynamic libs (Shared Objects) +*.o +*.a +*.so + +# Folders +_obj +_test + +# Architecture specific extensions/prefixes +*.[568vq] +[568vq].out + +*.cgo1.go +*.cgo2.c +_cgo_defun.c +_cgo_gotypes.go +_cgo_export.* + +_testmain.go + +*.exe +*.test +*.prof + +plasma + +cmd/cmd +**.key +**.hex + +cmd/lncli/lncli + +test_wal/* diff --git a/cmd/lncli/commands.go b/cmd/lncli/commands.go index 71f57275..3a287251 100644 --- a/cmd/lncli/commands.go +++ b/cmd/lncli/commands.go @@ -7,7 +7,7 @@ import ( "github.com/codegangsta/cli" "golang.org/x/net/context" - "li.lan/labs/plasma/rpcprotos" + "li.lan/labs/plasma/lnrpc" ) func printRespJson(resp interface{}) { @@ -21,6 +21,12 @@ func printRespJson(resp interface{}) { out.WriteTo(os.Stdout) } +var ShellCommand = cli.Command{ + Name: "shell", + Usage: "enter interactive shell", + Action: shell, +} + var NewAddressCommand = cli.Command{ Name: "newaddress", Usage: "gets the next address in the HD chain", diff --git a/cmd/lncli/main.go b/cmd/lncli/main.go index 82d73888..bf6269aa 100644 --- a/cmd/lncli/main.go +++ b/cmd/lncli/main.go @@ -6,7 +6,7 @@ import ( "os" "github.com/codegangsta/cli" - "li.lan/labs/plasma/rpcprotos" + "li.lan/labs/plasma/lnrpc" "google.golang.org/grpc" ) @@ -52,6 +52,7 @@ func main() { app.Commands = []cli.Command{ NewAddressCommand, SendManyCommand, + ShellCommand, } if err := app.Run(os.Args); err != nil { fatal(err) diff --git a/cmd/lncli/shell.go b/cmd/lncli/shell.go new file mode 100644 index 00000000..c8105ac9 --- /dev/null +++ b/cmd/lncli/shell.go @@ -0,0 +1,147 @@ +package main + +import ( + "bufio" + "fmt" + "log" + "os" + "strings" + + "github.com/codegangsta/cli" + "google.golang.org/grpc" +) + +func shell(z *cli.Context) { + fmt.Printf("LN shell v0.0FTW\n") + for { + reader := bufio.NewReaderSize(os.Stdin, 4000) + fmt.Printf("->") + msg, err := reader.ReadString('\n') + if err != nil { + log.Fatal(err) + } + cmdslice := strings.Fields(msg) + if len(cmdslice) < 1 { + continue + } + fmt.Printf("entered command: %s\n", msg) + err = Shellparse(cmdslice) + if err != nil { + log.Fatal(err) + } + } +} + +func Shellparse(cmdslice []string) error { + var err error + var args []string + cmd := cmdslice[0] + if len(cmdslice) > 1 { + args = cmdslice[1:] + } + if cmd == "exit" || cmd == "quit" { + return fmt.Errorf("User exit") + } + // if cmd == "help" { + // Help() + // return nil + // } + + // if cmd == "pbx" { + // err = PbxConnect(args) + // if err != nil { + // fmt.Printf("pbx error: %s\n", err) + // } + // return nil + // } + // if cmd == "lnwho" { + // err = LnWho(args) + // if err != nil { + // fmt.Printf("LN WhoAreYou error: %s\n", err) + // } + // return nil + // } + // if cmd == "lnd" { + // err = LnDisconnect(args) + // if err != nil { + // fmt.Printf("LN disconnect error: %s\n", err) + // } + // return nil + // } + if cmd == "lnhi" { + err = LnChat(args) + if err != nil { + fmt.Printf("LN chat error: %s\n", err) + } + return nil + } + if cmd == "lnc" { + err = LnConnect(args) + if err != nil { + fmt.Printf("LN connect error: %s\n", err) + } + return nil + } + // if cmd == "lnl" { + // err = LnListen(args) + // if err != nil { + // fmt.Printf("LN listen error: %s\n", err) + // } + // return nil + // } + // if cmd == "txsend" { + // err = TxSend(args) + // if err != nil { + // fmt.Printf("txsend error: %s\n", err) + // } + // return nil + // } + if cmd == "rpc" { + err = RpcConnect(args) + if err != nil { + fmt.Printf("RPC connect error: %s\n", err) + } + return nil + } + + fmt.Printf("Command not recognized.\n") + return nil +} + +func RpcConnect(args []string) error { + // client := getClient(ctx) + opts := []grpc.DialOption{grpc.WithInsecure()} + conn, err := grpc.Dial("127.0.0.1:10000", opts...) + if err != nil { + return (err) + } + state, err := conn.State() + if err != nil { + return (err) + } + fmt.Printf("connection state: %s\n", state.String()) + err = conn.Close() + if err != nil { + return (err) + } + + return nil +} + +func LnConnect(args []string) error { + fmt.Printf("lnconnect, %d args\n", len(args)) + return nil +} + +// For testing. Syntax: lnhi hello world +func LnChat(args []string) error { + + var chat string + for _, s := range args { + chat += s + " " + } + // msg := append([]byte{lnwire.MSGID_TEXTCHAT}, []byte(chat)...) + + fmt.Printf("will send text message: %s\n", chat) + return nil +} diff --git a/lnrpc/gen_protos.sh b/lnrpc/gen_protos.sh new file mode 100755 index 00000000..b04f9191 --- /dev/null +++ b/lnrpc/gen_protos.sh @@ -0,0 +1,3 @@ +#!/bin/sh + +protoc -I . rpc.proto --go_out=plugins=grpc:. diff --git a/lnrpc/rpc.pb.go b/lnrpc/rpc.pb.go new file mode 100644 index 00000000..a403eed8 --- /dev/null +++ b/lnrpc/rpc.pb.go @@ -0,0 +1,188 @@ +// Code generated by protoc-gen-go. +// source: rpc.proto +// DO NOT EDIT! + +/* +Package lnrpc is a generated protocol buffer package. + +It is generated from these files: + rpc.proto + +It has these top-level messages: + SendManyRequest + SendManyResponse + NewAddressRequest + NewAddressResponse +*/ +package lnrpc + +import proto "github.com/golang/protobuf/proto" +import fmt "fmt" +import math "math" + +import ( + context "golang.org/x/net/context" + grpc "google.golang.org/grpc" +) + +// Reference imports to suppress errors if they are not otherwise used. +var _ = proto.Marshal +var _ = fmt.Errorf +var _ = math.Inf + +type SendManyRequest struct { + AddrToAmount map[string]int64 `protobuf:"bytes,1,rep,name=AddrToAmount" json:"AddrToAmount,omitempty" protobuf_key:"bytes,1,opt,name=key" protobuf_val:"varint,2,opt,name=value"` +} + +func (m *SendManyRequest) Reset() { *m = SendManyRequest{} } +func (m *SendManyRequest) String() string { return proto.CompactTextString(m) } +func (*SendManyRequest) ProtoMessage() {} +func (*SendManyRequest) Descriptor() ([]byte, []int) { return fileDescriptor0, []int{0} } + +func (m *SendManyRequest) GetAddrToAmount() map[string]int64 { + if m != nil { + return m.AddrToAmount + } + return nil +} + +type SendManyResponse struct { + Txid string `protobuf:"bytes,1,opt,name=txid" json:"txid,omitempty"` +} + +func (m *SendManyResponse) Reset() { *m = SendManyResponse{} } +func (m *SendManyResponse) String() string { return proto.CompactTextString(m) } +func (*SendManyResponse) ProtoMessage() {} +func (*SendManyResponse) Descriptor() ([]byte, []int) { return fileDescriptor0, []int{1} } + +type NewAddressRequest struct { +} + +func (m *NewAddressRequest) Reset() { *m = NewAddressRequest{} } +func (m *NewAddressRequest) String() string { return proto.CompactTextString(m) } +func (*NewAddressRequest) ProtoMessage() {} +func (*NewAddressRequest) Descriptor() ([]byte, []int) { return fileDescriptor0, []int{2} } + +type NewAddressResponse struct { + Address string `protobuf:"bytes,1,opt,name=address" json:"address,omitempty"` +} + +func (m *NewAddressResponse) Reset() { *m = NewAddressResponse{} } +func (m *NewAddressResponse) String() string { return proto.CompactTextString(m) } +func (*NewAddressResponse) ProtoMessage() {} +func (*NewAddressResponse) Descriptor() ([]byte, []int) { return fileDescriptor0, []int{3} } + +func init() { + proto.RegisterType((*SendManyRequest)(nil), "lnrpc.SendManyRequest") + proto.RegisterType((*SendManyResponse)(nil), "lnrpc.SendManyResponse") + proto.RegisterType((*NewAddressRequest)(nil), "lnrpc.NewAddressRequest") + proto.RegisterType((*NewAddressResponse)(nil), "lnrpc.NewAddressResponse") +} + +// Reference imports to suppress errors if they are not otherwise used. +var _ context.Context +var _ grpc.ClientConn + +// Client API for Lightning service + +type LightningClient interface { + SendMany(ctx context.Context, in *SendManyRequest, opts ...grpc.CallOption) (*SendManyResponse, error) + NewAddress(ctx context.Context, in *NewAddressRequest, opts ...grpc.CallOption) (*NewAddressResponse, error) +} + +type lightningClient struct { + cc *grpc.ClientConn +} + +func NewLightningClient(cc *grpc.ClientConn) LightningClient { + return &lightningClient{cc} +} + +func (c *lightningClient) SendMany(ctx context.Context, in *SendManyRequest, opts ...grpc.CallOption) (*SendManyResponse, error) { + out := new(SendManyResponse) + err := grpc.Invoke(ctx, "/lnrpc.Lightning/SendMany", in, out, c.cc, opts...) + if err != nil { + return nil, err + } + return out, nil +} + +func (c *lightningClient) NewAddress(ctx context.Context, in *NewAddressRequest, opts ...grpc.CallOption) (*NewAddressResponse, error) { + out := new(NewAddressResponse) + err := grpc.Invoke(ctx, "/lnrpc.Lightning/NewAddress", in, out, c.cc, opts...) + if err != nil { + return nil, err + } + return out, nil +} + +// Server API for Lightning service + +type LightningServer interface { + SendMany(context.Context, *SendManyRequest) (*SendManyResponse, error) + NewAddress(context.Context, *NewAddressRequest) (*NewAddressResponse, error) +} + +func RegisterLightningServer(s *grpc.Server, srv LightningServer) { + s.RegisterService(&_Lightning_serviceDesc, srv) +} + +func _Lightning_SendMany_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error) (interface{}, error) { + in := new(SendManyRequest) + if err := dec(in); err != nil { + return nil, err + } + out, err := srv.(LightningServer).SendMany(ctx, in) + if err != nil { + return nil, err + } + return out, nil +} + +func _Lightning_NewAddress_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error) (interface{}, error) { + in := new(NewAddressRequest) + if err := dec(in); err != nil { + return nil, err + } + out, err := srv.(LightningServer).NewAddress(ctx, in) + if err != nil { + return nil, err + } + return out, nil +} + +var _Lightning_serviceDesc = grpc.ServiceDesc{ + ServiceName: "lnrpc.Lightning", + HandlerType: (*LightningServer)(nil), + Methods: []grpc.MethodDesc{ + { + MethodName: "SendMany", + Handler: _Lightning_SendMany_Handler, + }, + { + MethodName: "NewAddress", + Handler: _Lightning_NewAddress_Handler, + }, + }, + Streams: []grpc.StreamDesc{}, +} + +var fileDescriptor0 = []byte{ + // 241 bytes of a gzipped FileDescriptorProto + 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x09, 0x6e, 0x88, 0x02, 0xff, 0xe2, 0xe2, 0x2c, 0x2a, 0x48, 0xd6, + 0x2b, 0x28, 0xca, 0x2f, 0xc9, 0x17, 0x62, 0xcd, 0xc9, 0x03, 0x72, 0x94, 0xda, 0x18, 0xb9, 0xf8, + 0x83, 0x53, 0xf3, 0x52, 0x7c, 0x13, 0xf3, 0x2a, 0x83, 0x52, 0x0b, 0x4b, 0x53, 0x8b, 0x4b, 0x84, + 0xec, 0xb8, 0x78, 0x1c, 0x53, 0x52, 0x8a, 0x42, 0xf2, 0x1d, 0x73, 0xf3, 0x4b, 0xf3, 0x4a, 0x24, + 0x18, 0x15, 0x98, 0x35, 0xb8, 0x8d, 0x34, 0xf4, 0xc0, 0x3a, 0xf4, 0xd0, 0x54, 0xeb, 0x21, 0x2b, + 0x75, 0xcd, 0x2b, 0x29, 0xaa, 0x94, 0x32, 0xe6, 0x12, 0xc4, 0x10, 0x14, 0xe2, 0xe6, 0x62, 0xce, + 0x4e, 0xad, 0x04, 0x9a, 0xc5, 0xa8, 0xc1, 0x29, 0xc4, 0xcb, 0xc5, 0x5a, 0x96, 0x98, 0x53, 0x9a, + 0x2a, 0xc1, 0x04, 0xe4, 0x32, 0x5b, 0x31, 0x59, 0x30, 0x2a, 0x29, 0x70, 0x09, 0x20, 0x4c, 0x2e, + 0x2e, 0xc8, 0xcf, 0x2b, 0x4e, 0x15, 0xe2, 0xe1, 0x62, 0x29, 0xa9, 0xc8, 0x4c, 0x81, 0x68, 0x52, + 0x12, 0xe6, 0x12, 0xf4, 0x4b, 0x2d, 0x07, 0x99, 0x9c, 0x5a, 0x5c, 0x0c, 0xb5, 0x5d, 0x49, 0x95, + 0x4b, 0x08, 0x59, 0x10, 0xaa, 0x91, 0x9f, 0x8b, 0x3d, 0x11, 0x22, 0x04, 0xd1, 0x6b, 0xd4, 0xcd, + 0xc8, 0xc5, 0xe9, 0x93, 0x99, 0x9e, 0x51, 0x92, 0x97, 0x99, 0x97, 0x2e, 0x64, 0xcd, 0xc5, 0x01, + 0xb3, 0x4b, 0x48, 0x0c, 0xbb, 0xb7, 0xa4, 0xc4, 0x31, 0xc4, 0xa1, 0x66, 0x3b, 0x72, 0x71, 0x21, + 0x6c, 0x14, 0x92, 0x80, 0x2a, 0xc3, 0x70, 0x99, 0x94, 0x24, 0x16, 0x19, 0x88, 0x11, 0x49, 0x6c, + 0xe0, 0x28, 0x30, 0x06, 0x04, 0x00, 0x00, 0xff, 0xff, 0x80, 0x74, 0x7a, 0x62, 0x8f, 0x01, 0x00, + 0x00, +} diff --git a/lnrpc/rpc.proto b/lnrpc/rpc.proto new file mode 100644 index 00000000..7ca0c2da --- /dev/null +++ b/lnrpc/rpc.proto @@ -0,0 +1,21 @@ +syntax = "proto3"; + +package lnrpc; + +service Lightning { + rpc SendMany(SendManyRequest) returns (SendManyResponse); + rpc NewAddress(NewAddressRequest) returns (NewAddressResponse); +} + +message SendManyRequest { + map AddrToAmount = 1; +} +message SendManyResponse { + string txid = 1; +} + + +message NewAddressRequest {} +message NewAddressResponse { + string address = 1; +} diff --git a/plasma.go b/plasma.go index 3027e167..e91b4803 100644 --- a/plasma.go +++ b/plasma.go @@ -9,8 +9,8 @@ import ( "google.golang.org/grpc" "google.golang.org/grpc/grpclog" + "li.lan/labs/plasma/lnrpc" "li.lan/labs/plasma/lnwallet" - "li.lan/labs/plasma/rpcprotos" ) //lightning == terrestrial plasma diff --git a/rpcserver.go b/rpcserver.go index e7887e86..4f1321f5 100644 --- a/rpcserver.go +++ b/rpcserver.go @@ -6,8 +6,8 @@ import ( "github.com/btcsuite/btcutil" "github.com/btcsuite/btcwallet/waddrmgr" "golang.org/x/net/context" + "li.lan/labs/plasma/lnrpc" "li.lan/labs/plasma/lnwallet" - "li.lan/labs/plasma/rpcprotos" ) var (