2015-12-26 09:09:17 +03:00
|
|
|
package main
|
2015-12-30 02:09:38 +03:00
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/hex"
|
2015-12-31 08:40:41 +03:00
|
|
|
"fmt"
|
2015-12-31 06:56:57 +03:00
|
|
|
"log"
|
2015-12-31 08:40:41 +03:00
|
|
|
"net"
|
2015-12-30 02:09:38 +03:00
|
|
|
|
|
|
|
"github.com/btcsuite/btcutil"
|
|
|
|
"github.com/btcsuite/btcwallet/waddrmgr"
|
|
|
|
"golang.org/x/net/context"
|
2015-12-31 08:40:41 +03:00
|
|
|
"li.lan/labs/plasma/lndc"
|
2015-12-30 23:19:09 +03:00
|
|
|
"li.lan/labs/plasma/lnrpc"
|
2015-12-30 02:09:38 +03:00
|
|
|
"li.lan/labs/plasma/lnwallet"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
defaultAccount uint32 = waddrmgr.DefaultAccountNum
|
|
|
|
)
|
|
|
|
|
|
|
|
// rpcServer...
|
|
|
|
type rpcServer struct {
|
|
|
|
lnwallet *lnwallet.LightningWallet
|
|
|
|
}
|
|
|
|
|
2015-12-30 02:21:42 +03:00
|
|
|
var _ lnrpc.LightningServer = (*rpcServer)(nil)
|
|
|
|
|
2015-12-30 02:09:38 +03:00
|
|
|
// newRpcServer...
|
|
|
|
func newRpcServer(wallet *lnwallet.LightningWallet) *rpcServer {
|
|
|
|
return &rpcServer{wallet}
|
|
|
|
}
|
|
|
|
|
|
|
|
// SendMany...
|
2015-12-30 02:21:42 +03:00
|
|
|
func (r *rpcServer) SendMany(ctx context.Context, in *lnrpc.SendManyRequest) (*lnrpc.SendManyResponse, error) {
|
2015-12-30 02:09:38 +03:00
|
|
|
|
|
|
|
sendMap := make(map[string]btcutil.Amount)
|
|
|
|
for addr, amt := range in.AddrToAmount {
|
|
|
|
sendMap[addr] = btcutil.Amount(amt)
|
|
|
|
}
|
|
|
|
|
|
|
|
txid, err := r.lnwallet.SendPairs(sendMap, defaultAccount, 1)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &lnrpc.SendManyResponse{Txid: hex.EncodeToString(txid[:])}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewAddress...
|
2015-12-30 02:21:42 +03:00
|
|
|
func (r *rpcServer) NewAddress(ctx context.Context, in *lnrpc.NewAddressRequest) (*lnrpc.NewAddressResponse, error) {
|
2015-12-30 02:09:38 +03:00
|
|
|
|
|
|
|
r.lnwallet.KeyGenMtx.Lock()
|
|
|
|
defer r.lnwallet.KeyGenMtx.Unlock()
|
|
|
|
|
|
|
|
addr, err := r.lnwallet.NewAddress(defaultAccount)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &lnrpc.NewAddressResponse{Address: addr.String()}, nil
|
|
|
|
}
|
2015-12-31 06:56:57 +03:00
|
|
|
|
|
|
|
// 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) {
|
2015-12-31 08:40:41 +03:00
|
|
|
// LnListen listens on the default port for incoming connections
|
|
|
|
//ignore args and launch listener goroutine
|
|
|
|
|
|
|
|
priv, err := r.lnwallet.ChannelDB.GetIdKey()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Printf("got a private key. %x\n", priv.Serialize())
|
|
|
|
|
|
|
|
// go TCPListener()
|
2015-12-31 06:56:57 +03:00
|
|
|
|
|
|
|
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
|
|
|
|
}
|
2015-12-31 08:40:41 +03:00
|
|
|
|
|
|
|
func TCPListener() {
|
|
|
|
listener, err := net.Listen("tcp", ":"+"2448")
|
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("TCP listen error: %s\n", err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Printf("Listening on %s\n", listener.Addr().String())
|
|
|
|
for {
|
|
|
|
con, err := listener.Accept() // this blocks
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("Listener error: %s\n", err.Error())
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
newConn, err := InitIncomingConn(con)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("InitConn error: %s\n", err.Error())
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
idslice := lndc.H160(newConn.RemotePub.SerializeCompressed())
|
|
|
|
var newId [16]byte
|
|
|
|
copy(newId[:], idslice[:16])
|
|
|
|
// CnMap[newId] = newConn
|
|
|
|
fmt.Printf("added %x to map\n", newId)
|
|
|
|
// go LNDCReceiver(newConn, newId)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func InitIncomingConn(con net.Conn) (*lndc.LNDConn, error) {
|
|
|
|
LNcon := new(lndc.LNDConn)
|
|
|
|
LNcon.Cn = con
|
|
|
|
err := LNcon.Setup(nil)
|
|
|
|
if err != nil {
|
|
|
|
return LNcon, err
|
|
|
|
}
|
|
|
|
fmt.Printf("Got connection from %s authed with pubkey %x",
|
|
|
|
LNcon.Cn.RemoteAddr().String(), LNcon.RemotePub.SerializeCompressed())
|
|
|
|
return LNcon, nil
|
|
|
|
|
|
|
|
}
|