2015-12-26 09:09:17 +03:00
|
|
|
package main
|
2015-12-30 02:09:38 +03:00
|
|
|
|
|
|
|
import (
|
2016-06-21 21:52:09 +03:00
|
|
|
"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-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"
|
2016-05-15 17:17:44 +03:00
|
|
|
"github.com/roasbeef/btcd/txscript"
|
|
|
|
"github.com/roasbeef/btcd/wire"
|
|
|
|
"github.com/roasbeef/btcutil"
|
|
|
|
"github.com/roasbeef/btcwallet/waddrmgr"
|
2015-12-30 02:09:38 +03:00
|
|
|
"golang.org/x/net/context"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
defaultAccount uint32 = waddrmgr.DefaultAccountNum
|
|
|
|
)
|
|
|
|
|
2016-06-21 21:52:09 +03:00
|
|
|
// rpcServer is a gRPC, RPC front end to the lnd daemon.
|
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
|
|
|
}
|
|
|
|
|
2016-06-21 21:52:09 +03:00
|
|
|
// A compile time check to ensure that rpcServer fully implements the
|
|
|
|
// LightningServer gRPC service.
|
2015-12-30 02:21:42 +03:00
|
|
|
var _ lnrpc.LightningServer = (*rpcServer)(nil)
|
|
|
|
|
2016-06-21 21:52:09 +03:00
|
|
|
// newRpcServer creates and returns a new instance of the rpcServer.
|
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-06-21 21:52:09 +03:00
|
|
|
// Start launches any helper goroutines required for the rpcServer
|
|
|
|
// to function.
|
2016-01-17 06:12:36 +03:00
|
|
|
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-06-21 21:52:09 +03:00
|
|
|
// Stop signals any active goroutines for a graceful closure.
|
2016-01-17 06:12:36 +03:00
|
|
|
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
|
|
|
|
2016-04-25 06:26:32 +03:00
|
|
|
close(r.quit)
|
|
|
|
|
2016-01-17 06:12:36 +03:00
|
|
|
return nil
|
2016-01-02 07:27:40 +03:00
|
|
|
}
|
|
|
|
|
2016-06-21 21:52:09 +03:00
|
|
|
// SendMany handles a request for a transaction create multiple specified
|
|
|
|
// outputs in parallel.
|
|
|
|
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-06-21 21:52:09 +03:00
|
|
|
// Instruct the wallet to create an transaction paying to the specified
|
|
|
|
// outputs, selecting any coins with at least one confirmation.
|
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
|
|
|
|
}
|
|
|
|
|
2016-04-25 06:26:32 +03:00
|
|
|
rpcsLog.Infof("Generated txid: %v", txid.String())
|
|
|
|
|
|
|
|
return &lnrpc.SendManyResponse{Txid: txid.String()}, nil
|
2015-12-30 02:09:38 +03:00
|
|
|
}
|
|
|
|
|
2016-06-21 21:52:09 +03:00
|
|
|
// NewAddress creates a new address under control of the local wallet.
|
2016-04-25 06:26:32 +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-04-25 06:26:32 +03:00
|
|
|
// Translate the gRPC proto address type to the wallet controller's
|
|
|
|
// available address types.
|
|
|
|
var addrType waddrmgr.AddressType
|
|
|
|
switch in.Type {
|
|
|
|
case lnrpc.NewAddressRequest_WITNESS_PUBKEY_HASH:
|
|
|
|
addrType = waddrmgr.WitnessPubKey
|
|
|
|
case lnrpc.NewAddressRequest_NESTED_PUBKEY_HASH:
|
|
|
|
addrType = waddrmgr.NestedWitnessPubKey
|
|
|
|
case lnrpc.NewAddressRequest_PUBKEY_HASH:
|
|
|
|
addrType = waddrmgr.PubKeyHash
|
|
|
|
}
|
|
|
|
|
|
|
|
addr, err := r.server.lnwallet.NewAddress(defaultAccount,
|
|
|
|
addrType)
|
2015-12-30 02:09:38 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2016-03-23 04:49:22 +03:00
|
|
|
rpcsLog.Infof("Generated new address: %v", addr.String())
|
2015-12-30 02:09:38 +03:00
|
|
|
return &lnrpc.NewAddressResponse{Address: addr.String()}, nil
|
|
|
|
}
|
2015-12-31 06:56:57 +03:00
|
|
|
|
2016-06-21 21:52:09 +03:00
|
|
|
// ConnectPeer attempts to establish a connection to a remote peer.
|
2016-01-17 06:12:36 +03:00
|
|
|
func (r *rpcServer) ConnectPeer(ctx context.Context,
|
|
|
|
in *lnrpc.ConnectPeerRequest) (*lnrpc.ConnectPeerResponse, error) {
|
2015-12-31 06:56:57 +03:00
|
|
|
|
2016-06-21 21:52:09 +03:00
|
|
|
if in.Addr == nil {
|
2016-01-02 07:27:40 +03:00
|
|
|
return nil, fmt.Errorf("need: lnc pubkeyhash@hostname")
|
|
|
|
}
|
|
|
|
|
2016-06-21 21:52:09 +03:00
|
|
|
idAtHost := fmt.Sprintf("%v@%v", in.Addr.PubKeyHash, in.Addr.Host)
|
|
|
|
rpcsLog.Debugf("Attempting to connect to peer %v", idAtHost)
|
|
|
|
|
|
|
|
peerAddr, err := lndc.LnAddrFromString(idAtHost)
|
|
|
|
if err != nil {
|
|
|
|
rpcsLog.Errorf("(connectpeer): error parsing ln addr: %v", err)
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
peerID, err := r.server.ConnectToPeer(peerAddr)
|
|
|
|
if err != nil {
|
|
|
|
rpcsLog.Errorf("(connectpeer): error connecting to peer: %v", err)
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
rpcsLog.Debugf("Connected to peer: %v", peerAddr.String())
|
|
|
|
return &lnrpc.ConnectPeerResponse{peerID}, nil
|
|
|
|
}
|
2016-06-21 22:32:32 +03:00
|
|
|
|
|
|
|
// OpenChannel attempts to open a singly funded channel specified in the
|
|
|
|
// request to a remote peer.
|
|
|
|
func (r *rpcServer) OpenChannel(ctx context.Context,
|
|
|
|
in *lnrpc.OpenChannelRequest) (*lnrpc.OpenChannelResponse, error) {
|
|
|
|
|
|
|
|
rpcsLog.Tracef("Recieved request to openchannel to peerid(%v) "+
|
|
|
|
"allocation(us=%v, them=%v) numconfs=%v", in.TargetPeerId,
|
|
|
|
in.LocalFundingAmount, in.RemoteFundingAmount, in.NumConfs)
|
|
|
|
|
|
|
|
localFundingAmt := btcutil.Amount(in.LocalFundingAmount)
|
|
|
|
remoteFundingAmt := btcutil.Amount(in.RemoteFundingAmount)
|
|
|
|
target := in.TargetPeerId
|
|
|
|
numConfs := in.NumConfs
|
|
|
|
resp, err := r.server.OpenChannel(target, localFundingAmt,
|
|
|
|
remoteFundingAmt, numConfs)
|
2015-12-31 18:29:00 +03:00
|
|
|
if err != nil {
|
2016-06-21 22:32:32 +03:00
|
|
|
rpcsLog.Errorf("unable to open channel to peerid(%v): %v",
|
|
|
|
target, err)
|
2015-12-31 18:29:00 +03:00
|
|
|
return nil, err
|
|
|
|
}
|
2016-01-02 07:27:40 +03:00
|
|
|
|
2016-06-22 21:10:20 +03:00
|
|
|
rpcsLog.Tracef("Opened channel with peerid(%v), ChannelPoint(%v)",
|
2016-06-21 22:32:32 +03:00
|
|
|
in.TargetPeerId, resp)
|
|
|
|
|
|
|
|
return &lnrpc.OpenChannelResponse{
|
|
|
|
&lnrpc.ChannelPoint{
|
|
|
|
FundingTxid: resp.Hash[:],
|
|
|
|
OutputIndex: resp.Index,
|
|
|
|
},
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// CloseChannel attempts to close an active channel identified by its channel
|
|
|
|
// point. The actions of this method can additionally be augmented to attempt
|
|
|
|
// a force close after a timeout period in the case of an inactive peer.
|
|
|
|
func (r *rpcServer) CloseChannel(ctx context.Context,
|
|
|
|
in *lnrpc.CloseChannelRequest) (*lnrpc.CloseChannelResponse, error) {
|
|
|
|
|
|
|
|
index := in.ChannelPoint.OutputIndex
|
|
|
|
txid, err := wire.NewShaHash(in.ChannelPoint.FundingTxid)
|
|
|
|
if err != nil {
|
|
|
|
rpcsLog.Errorf("(closechannel) invalid txid: %v", err)
|
2016-01-02 07:27:40 +03:00
|
|
|
return nil, err
|
|
|
|
}
|
2016-06-21 22:32:32 +03:00
|
|
|
targetChannelPoint := wire.NewOutPoint(txid, index)
|
|
|
|
|
|
|
|
rpcsLog.Tracef("Recieved closechannel request for ChannelPoint(%v)",
|
|
|
|
targetChannelPoint)
|
|
|
|
|
|
|
|
resp, err := r.server.CloseChannel(targetChannelPoint)
|
|
|
|
if err != nil {
|
|
|
|
rpcsLog.Errorf("Unable to close ChannelPoint(%v): %v",
|
|
|
|
targetChannelPoint, err)
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &lnrpc.CloseChannelResponse{resp}, nil
|
|
|
|
}
|
2016-01-02 07:27:40 +03:00
|
|
|
|
2016-06-21 21:52:09 +03:00
|
|
|
// ListPeers returns a verbose listing of all currently active peers.
|
|
|
|
func (r *rpcServer) ListPeers(ctx context.Context,
|
|
|
|
in *lnrpc.ListPeersRequest) (*lnrpc.ListPeersResponse, error) {
|
|
|
|
|
|
|
|
rpcsLog.Tracef("recieved listpeers request")
|
|
|
|
|
|
|
|
serverPeers := r.server.Peers()
|
|
|
|
resp := &lnrpc.ListPeersResponse{
|
|
|
|
Peers: make([]*lnrpc.Peer, 0, len(serverPeers)),
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, serverPeer := range serverPeers {
|
|
|
|
// TODO(roasbeef): add a snapshot method which grabs peer read mtx
|
|
|
|
peer := &lnrpc.Peer{
|
|
|
|
LightningId: hex.EncodeToString(serverPeer.lightningID[:]),
|
|
|
|
PeerId: serverPeer.id,
|
|
|
|
Address: serverPeer.conn.RemoteAddr().String(),
|
|
|
|
Inbound: serverPeer.inbound,
|
|
|
|
BytesRecv: atomic.LoadUint64(&serverPeer.bytesReceived),
|
|
|
|
BytesSent: atomic.LoadUint64(&serverPeer.bytesSent),
|
|
|
|
}
|
|
|
|
|
|
|
|
resp.Peers = append(resp.Peers, peer)
|
|
|
|
}
|
|
|
|
|
|
|
|
rpcsLog.Tracef("listpeers yielded %v peers", serverPeers)
|
|
|
|
|
|
|
|
return resp, nil
|
|
|
|
}
|
|
|
|
|
2016-06-21 21:46:27 +03:00
|
|
|
// WalletBalance returns the sum of all confirmed unspent outputs under control
|
|
|
|
// by the wallet. This method can be modified by having the request specify
|
|
|
|
// only witness outputs should be factored into the final output sum.
|
|
|
|
// TODO(roasbeef): split into total and confirmed/unconfirmed
|
|
|
|
func (r *rpcServer) WalletBalance(ctx context.Context,
|
|
|
|
in *lnrpc.WalletBalanceRequest) (*lnrpc.WalletBalanceResponse, error) {
|
|
|
|
|
|
|
|
var balance float64
|
|
|
|
|
|
|
|
if in.WitnessOnly {
|
|
|
|
witnessOutputs, err := r.server.lnwallet.ListUnspentWitness(1)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// We need to convert from BTC to satoshi here otherwise, and
|
|
|
|
// incorrect sum will be returned.
|
|
|
|
var outputSum btcutil.Amount
|
|
|
|
for _, witnessOutput := range witnessOutputs {
|
|
|
|
outputSum += btcutil.Amount(witnessOutput.Amount * 1e8)
|
|
|
|
}
|
|
|
|
|
|
|
|
balance = outputSum.ToBTC()
|
|
|
|
} else {
|
|
|
|
// TODO(roasbeef): make num confs a param
|
|
|
|
outputSum, err := r.server.lnwallet.CalculateBalance(1)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
balance = outputSum.ToBTC()
|
|
|
|
}
|
|
|
|
|
|
|
|
rpcsLog.Debugf("walletbalance query response: %v", balance)
|
|
|
|
|
|
|
|
return &lnrpc.WalletBalanceResponse{balance}, nil
|
2015-12-31 08:40:41 +03:00
|
|
|
}
|