2019-03-07 02:22:46 +00:00
|
|
|
package loop
|
2019-03-06 20:13:50 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"crypto/tls"
|
|
|
|
"encoding/hex"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
|
2019-03-06 23:53:17 +00:00
|
|
|
"github.com/lightninglabs/loop/looprpc"
|
2019-03-06 20:13:50 +00:00
|
|
|
"github.com/lightningnetwork/lnd/lntypes"
|
|
|
|
|
|
|
|
"github.com/btcsuite/btcd/btcec"
|
|
|
|
"github.com/btcsuite/btcutil"
|
|
|
|
"google.golang.org/grpc"
|
|
|
|
"google.golang.org/grpc/credentials"
|
|
|
|
)
|
|
|
|
|
|
|
|
type swapServerClient interface {
|
2019-03-07 04:32:24 +00:00
|
|
|
GetLoopOutTerms(ctx context.Context) (
|
|
|
|
*LoopOutTerms, error)
|
2019-03-06 20:13:50 +00:00
|
|
|
|
2019-03-12 15:10:37 +00:00
|
|
|
GetLoopInTerms(ctx context.Context) (
|
|
|
|
*LoopInTerms, error)
|
|
|
|
|
2019-03-07 04:32:24 +00:00
|
|
|
NewLoopOutSwap(ctx context.Context,
|
2019-03-06 20:13:50 +00:00
|
|
|
swapHash lntypes.Hash, amount btcutil.Amount,
|
|
|
|
receiverKey [33]byte) (
|
2019-03-07 04:32:24 +00:00
|
|
|
*newLoopOutResponse, error)
|
2019-03-12 15:10:37 +00:00
|
|
|
|
|
|
|
NewLoopInSwap(ctx context.Context,
|
|
|
|
swapHash lntypes.Hash, amount btcutil.Amount,
|
|
|
|
senderKey [33]byte, swapInvoice string) (
|
|
|
|
*newLoopInResponse, error)
|
2019-03-06 20:13:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type grpcSwapServerClient struct {
|
2019-03-06 23:53:17 +00:00
|
|
|
server looprpc.SwapServerClient
|
2019-03-06 20:13:50 +00:00
|
|
|
conn *grpc.ClientConn
|
|
|
|
}
|
|
|
|
|
2019-03-07 04:32:24 +00:00
|
|
|
func newSwapServerClient(address string,
|
|
|
|
insecure bool) (*grpcSwapServerClient, error) {
|
|
|
|
|
2019-03-06 20:13:50 +00:00
|
|
|
serverConn, err := getSwapServerConn(address, insecure)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-03-06 23:53:17 +00:00
|
|
|
server := looprpc.NewSwapServerClient(serverConn)
|
2019-03-06 20:13:50 +00:00
|
|
|
|
|
|
|
return &grpcSwapServerClient{
|
|
|
|
conn: serverConn,
|
|
|
|
server: server,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2019-03-07 04:32:24 +00:00
|
|
|
func (s *grpcSwapServerClient) GetLoopOutTerms(ctx context.Context) (
|
|
|
|
*LoopOutTerms, error) {
|
2019-03-06 20:13:50 +00:00
|
|
|
|
|
|
|
rpcCtx, rpcCancel := context.WithTimeout(ctx, serverRPCTimeout)
|
|
|
|
defer rpcCancel()
|
2019-03-07 04:32:24 +00:00
|
|
|
quoteResp, err := s.server.LoopOutQuote(rpcCtx,
|
|
|
|
&looprpc.ServerLoopOutQuoteRequest{},
|
2019-03-06 20:13:50 +00:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
dest, err := hex.DecodeString(quoteResp.SwapPaymentDest)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if len(dest) != 33 {
|
|
|
|
return nil, errors.New("invalid payment dest")
|
|
|
|
}
|
|
|
|
var destArray [33]byte
|
|
|
|
copy(destArray[:], dest)
|
|
|
|
|
2019-03-07 04:32:24 +00:00
|
|
|
return &LoopOutTerms{
|
2019-03-06 20:13:50 +00:00
|
|
|
MinSwapAmount: btcutil.Amount(quoteResp.MinSwapAmount),
|
|
|
|
MaxSwapAmount: btcutil.Amount(quoteResp.MaxSwapAmount),
|
|
|
|
PrepayAmt: btcutil.Amount(quoteResp.PrepayAmt),
|
|
|
|
SwapFeeBase: btcutil.Amount(quoteResp.SwapFeeBase),
|
|
|
|
SwapFeeRate: quoteResp.SwapFeeRate,
|
|
|
|
CltvDelta: quoteResp.CltvDelta,
|
|
|
|
SwapPaymentDest: destArray,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2019-03-12 15:10:37 +00:00
|
|
|
func (s *grpcSwapServerClient) GetLoopInTerms(ctx context.Context) (
|
|
|
|
*LoopInTerms, error) {
|
|
|
|
|
|
|
|
rpcCtx, rpcCancel := context.WithTimeout(ctx, serverRPCTimeout)
|
|
|
|
defer rpcCancel()
|
|
|
|
quoteResp, err := s.server.LoopInQuote(rpcCtx,
|
|
|
|
&looprpc.ServerLoopInQuoteRequest{},
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &LoopInTerms{
|
|
|
|
MinSwapAmount: btcutil.Amount(quoteResp.MinSwapAmount),
|
|
|
|
MaxSwapAmount: btcutil.Amount(quoteResp.MaxSwapAmount),
|
|
|
|
SwapFeeBase: btcutil.Amount(quoteResp.SwapFeeBase),
|
|
|
|
SwapFeeRate: quoteResp.SwapFeeRate,
|
|
|
|
CltvDelta: quoteResp.CltvDelta,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2019-03-07 04:32:24 +00:00
|
|
|
func (s *grpcSwapServerClient) NewLoopOutSwap(ctx context.Context,
|
|
|
|
swapHash lntypes.Hash, amount btcutil.Amount,
|
|
|
|
receiverKey [33]byte) (*newLoopOutResponse, error) {
|
2019-03-06 20:13:50 +00:00
|
|
|
|
|
|
|
rpcCtx, rpcCancel := context.WithTimeout(ctx, serverRPCTimeout)
|
|
|
|
defer rpcCancel()
|
2019-03-07 04:32:24 +00:00
|
|
|
swapResp, err := s.server.NewLoopOutSwap(rpcCtx,
|
|
|
|
&looprpc.ServerLoopOutRequest{
|
2019-03-06 20:13:50 +00:00
|
|
|
SwapHash: swapHash[:],
|
|
|
|
Amt: uint64(amount),
|
|
|
|
ReceiverKey: receiverKey[:],
|
|
|
|
},
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var senderKey [33]byte
|
|
|
|
copy(senderKey[:], swapResp.SenderKey)
|
|
|
|
|
|
|
|
// Validate sender key.
|
|
|
|
_, err = btcec.ParsePubKey(senderKey[:], btcec.S256())
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("invalid sender key: %v", err)
|
|
|
|
}
|
|
|
|
|
2019-03-07 04:32:24 +00:00
|
|
|
return &newLoopOutResponse{
|
2019-03-06 20:13:50 +00:00
|
|
|
swapInvoice: swapResp.SwapInvoice,
|
|
|
|
prepayInvoice: swapResp.PrepayInvoice,
|
|
|
|
senderKey: senderKey,
|
|
|
|
expiry: swapResp.Expiry,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2019-03-12 15:10:37 +00:00
|
|
|
func (s *grpcSwapServerClient) NewLoopInSwap(ctx context.Context,
|
|
|
|
swapHash lntypes.Hash, amount btcutil.Amount, senderKey [33]byte,
|
|
|
|
swapInvoice string) (*newLoopInResponse, error) {
|
|
|
|
|
|
|
|
rpcCtx, rpcCancel := context.WithTimeout(ctx, serverRPCTimeout)
|
|
|
|
defer rpcCancel()
|
|
|
|
swapResp, err := s.server.NewLoopInSwap(rpcCtx,
|
|
|
|
&looprpc.ServerLoopInRequest{
|
|
|
|
SwapHash: swapHash[:],
|
|
|
|
Amt: uint64(amount),
|
|
|
|
SenderKey: senderKey[:],
|
|
|
|
SwapInvoice: swapInvoice,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var receiverKey [33]byte
|
|
|
|
copy(receiverKey[:], swapResp.ReceiverKey)
|
|
|
|
|
|
|
|
// Validate receiver key.
|
|
|
|
_, err = btcec.ParsePubKey(receiverKey[:], btcec.S256())
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("invalid sender key: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return &newLoopInResponse{
|
|
|
|
receiverKey: receiverKey,
|
|
|
|
expiry: swapResp.Expiry,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2019-03-06 20:13:50 +00:00
|
|
|
func (s *grpcSwapServerClient) Close() {
|
|
|
|
s.conn.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
// getSwapServerConn returns a connection to the swap server.
|
|
|
|
func getSwapServerConn(address string, insecure bool) (*grpc.ClientConn, error) {
|
|
|
|
// Create a dial options array.
|
|
|
|
opts := []grpc.DialOption{}
|
|
|
|
if insecure {
|
|
|
|
opts = append(opts, grpc.WithInsecure())
|
|
|
|
} else {
|
|
|
|
creds := credentials.NewTLS(&tls.Config{})
|
|
|
|
opts = append(opts, grpc.WithTransportCredentials(creds))
|
|
|
|
}
|
|
|
|
|
|
|
|
conn, err := grpc.Dial(address, opts...)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("unable to connect to RPC server: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return conn, nil
|
|
|
|
}
|
|
|
|
|
2019-03-07 04:32:24 +00:00
|
|
|
type newLoopOutResponse struct {
|
2019-03-06 20:13:50 +00:00
|
|
|
swapInvoice string
|
|
|
|
prepayInvoice string
|
|
|
|
senderKey [33]byte
|
|
|
|
expiry int32
|
|
|
|
}
|
2019-03-12 15:10:37 +00:00
|
|
|
|
|
|
|
type newLoopInResponse struct {
|
|
|
|
receiverKey [33]byte
|
|
|
|
expiry int32
|
|
|
|
}
|