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"
|
2016-01-16 22:09:12 +03:00
|
|
|
|
2016-01-17 06:12:36 +03:00
|
|
|
"sync"
|
|
|
|
"sync/atomic"
|
2015-12-30 02:09:38 +03:00
|
|
|
|
2016-03-23 04:48:46 +03:00
|
|
|
"github.com/btcsuite/btcd/txscript"
|
|
|
|
"github.com/btcsuite/btcd/wire"
|
2015-12-30 02:09:38 +03:00
|
|
|
"github.com/btcsuite/btcutil"
|
|
|
|
"github.com/btcsuite/btcwallet/waddrmgr"
|
2016-01-17 21:45:07 +03:00
|
|
|
"github.com/lightningnetwork/lnd/lndc"
|
2016-01-16 21:38:48 +03:00
|
|
|
"github.com/lightningnetwork/lnd/lnrpc"
|
2015-12-30 02:09:38 +03:00
|
|
|
"golang.org/x/net/context"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
defaultAccount uint32 = waddrmgr.DefaultAccountNum
|
|
|
|
)
|
|
|
|
|
|
|
|
// rpcServer...
|
2016-01-17 06:07:44 +03:00
|
|
|
type rpcServer struct {
|
|
|
|
started int32 // To be used atomically.
|
|
|
|
shutdown int32 // To be used atomically.
|
|
|
|
|
|
|
|
server *server
|
|
|
|
|
|
|
|
wg sync.WaitGroup
|
|
|
|
|
|
|
|
quit chan struct{}
|
2015-12-30 02:09:38 +03:00
|
|
|
}
|
|
|
|
|
2015-12-30 02:21:42 +03:00
|
|
|
var _ lnrpc.LightningServer = (*rpcServer)(nil)
|
|
|
|
|
2015-12-30 02:09:38 +03:00
|
|
|
// newRpcServer...
|
2016-01-17 06:07:44 +03:00
|
|
|
func newRpcServer(s *server) *rpcServer {
|
|
|
|
return &rpcServer{server: s, quit: make(chan struct{}, 1)}
|
2015-12-30 02:09:38 +03:00
|
|
|
}
|
|
|
|
|
2016-01-17 06:12:36 +03:00
|
|
|
// Start...
|
|
|
|
func (r *rpcServer) Start() error {
|
|
|
|
if atomic.AddInt32(&r.started, 1) != 1 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-01-14 08:41:46 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-01-17 06:12:36 +03:00
|
|
|
// Stop...
|
|
|
|
func (r *rpcServer) Stop() error {
|
|
|
|
if atomic.AddInt32(&r.shutdown, 1) != 1 {
|
|
|
|
return nil
|
2016-01-02 07:27:40 +03:00
|
|
|
}
|
2016-01-17 06:12:36 +03:00
|
|
|
|
|
|
|
return nil
|
2016-01-02 07:27:40 +03:00
|
|
|
}
|
|
|
|
|
2015-12-30 02:09:38 +03:00
|
|
|
// 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
|
|
|
|
2016-03-23 04:48:46 +03:00
|
|
|
outputs := make([]*wire.TxOut, 0, len(in.AddrToAmount))
|
2015-12-30 02:09:38 +03:00
|
|
|
for addr, amt := range in.AddrToAmount {
|
2016-03-23 04:48:46 +03:00
|
|
|
addr, err := btcutil.DecodeAddress(addr, activeNetParams)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
pkscript, err := txscript.PayToAddrScript(addr)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
outputs = append(outputs, wire.NewTxOut(amt, pkscript))
|
2015-12-30 02:09:38 +03:00
|
|
|
}
|
|
|
|
|
2016-03-23 04:48:46 +03:00
|
|
|
txid, err := r.server.lnwallet.SendOutputs(outputs, defaultAccount, 1)
|
2015-12-30 02:09:38 +03:00
|
|
|
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
|
|
|
|
2016-01-17 06:12:36 +03:00
|
|
|
r.server.lnwallet.KeyGenMtx.Lock()
|
|
|
|
defer r.server.lnwallet.KeyGenMtx.Unlock()
|
2015-12-30 02:09:38 +03:00
|
|
|
|
2016-01-17 06:12:36 +03:00
|
|
|
addr, err := r.server.lnwallet.NewAddress(defaultAccount)
|
2015-12-30 02:09:38 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &lnrpc.NewAddressResponse{Address: addr.String()}, nil
|
|
|
|
}
|
2015-12-31 06:56:57 +03:00
|
|
|
|
2016-01-17 06:12:36 +03:00
|
|
|
// LNConnect...
|
|
|
|
func (r *rpcServer) ConnectPeer(ctx context.Context,
|
|
|
|
in *lnrpc.ConnectPeerRequest) (*lnrpc.ConnectPeerResponse, error) {
|
2015-12-31 06:56:57 +03:00
|
|
|
|
2016-01-02 07:27:40 +03:00
|
|
|
if len(in.IdAtHost) == 0 {
|
|
|
|
return nil, fmt.Errorf("need: lnc pubkeyhash@hostname")
|
|
|
|
}
|
|
|
|
|
2016-01-17 21:45:07 +03:00
|
|
|
peerAddr, err := lndc.LnAddrFromString(in.IdAtHost)
|
2015-12-31 18:29:00 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2016-01-02 07:27:40 +03:00
|
|
|
|
2016-01-17 06:12:36 +03:00
|
|
|
if err := r.server.ConnectToPeer(peerAddr); err != nil {
|
2016-01-02 07:27:40 +03:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2016-01-17 06:12:36 +03:00
|
|
|
return &lnrpc.ConnectPeerResponse{[]byte(peerAddr.String())}, nil
|
2015-12-31 08:40:41 +03:00
|
|
|
}
|