Merge pull request #90 from getAlby/feature/wrap-lnd

Feature/wrap-lnd
This commit is contained in:
kiwiidb
2022-02-04 17:27:51 +01:00
committed by GitHub
4 changed files with 52 additions and 5 deletions

View File

@@ -8,6 +8,7 @@ import (
"time"
"github.com/getAlby/lndhub.go/db/models"
"github.com/getAlby/lndhub.go/lnd"
"github.com/getsentry/sentry-go"
"github.com/lightningnetwork/lnd/lnrpc"
"github.com/uptrace/bun"
@@ -95,7 +96,7 @@ func (svc *LndhubService) ProcessInvoiceUpdate(ctx context.Context, rawInvoice *
return nil
}
func (svc *LndhubService) ConnectInvoiceSubscription(ctx context.Context) (lnrpc.Lightning_SubscribeInvoicesClient, error) {
func (svc *LndhubService) ConnectInvoiceSubscription(ctx context.Context) (lnd.SubscribeInvoicesWrapper, error) {
var invoice models.Invoice
invoiceSubscriptionOptions := lnrpc.InvoiceSubscription{}
// Find the oldest NOT settled invoice with an add_index

View File

@@ -8,8 +8,8 @@ import (
"github.com/btcsuite/btcd/btcec"
"github.com/getAlby/lndhub.go/db/models"
"github.com/getAlby/lndhub.go/lib/tokens"
"github.com/getAlby/lndhub.go/lnd"
"github.com/labstack/gommon/random"
"github.com/lightningnetwork/lnd/lnrpc"
"github.com/uptrace/bun"
"github.com/ziflex/lecho/v3"
"golang.org/x/crypto/bcrypt"
@@ -20,7 +20,7 @@ const alphaNumBytes = random.Alphanumeric
type LndhubService struct {
Config *Config
DB *bun.DB
LndClient lnrpc.LightningClient
LndClient lnd.LightningClientWrapper
Logger *lecho.Logger
IdentityPubkey *btcec.PublicKey
}

20
lnd/interface.go Normal file
View File

@@ -0,0 +1,20 @@
package lnd
import (
"context"
"github.com/lightningnetwork/lnd/lnrpc"
"google.golang.org/grpc"
)
type LightningClientWrapper interface {
ListChannels(ctx context.Context, req *lnrpc.ListChannelsRequest, options ...grpc.CallOption) (*lnrpc.ListChannelsResponse, error)
SendPaymentSync(ctx context.Context, req *lnrpc.SendRequest, options ...grpc.CallOption) (*lnrpc.SendResponse, error)
AddInvoice(ctx context.Context, req *lnrpc.Invoice, options ...grpc.CallOption) (*lnrpc.AddInvoiceResponse, error)
SubscribeInvoices(ctx context.Context, req *lnrpc.InvoiceSubscription, options ...grpc.CallOption) (SubscribeInvoicesWrapper, error)
GetInfo(ctx context.Context, req *lnrpc.GetInfoRequest, options ...grpc.CallOption) (*lnrpc.GetInfoResponse, error)
}
type SubscribeInvoicesWrapper interface {
Recv() (*lnrpc.Invoice, error)
}

View File

@@ -1,6 +1,7 @@
package lnd
import (
"context"
"crypto/tls"
"crypto/x509"
"encoding/hex"
@@ -23,8 +24,11 @@ type LNDoptions struct {
MacaroonHex string
}
func NewLNDclient(lndOptions LNDoptions) (lnrpc.LightningClient, error) {
type LNDWrapper struct {
client lnrpc.LightningClient
}
func NewLNDclient(lndOptions LNDoptions) (result *LNDWrapper, err error) {
// Get credentials either from a hex string, a file or the system's certificate store
var creds credentials.TransportCredentials
// if a hex string is provided
@@ -82,5 +86,27 @@ func NewLNDclient(lndOptions LNDoptions) (lnrpc.LightningClient, error) {
return nil, err
}
return lnrpc.NewLightningClient(conn), nil
return &LNDWrapper{
client: lnrpc.NewLightningClient(conn),
}, nil
}
func (wrapper *LNDWrapper) ListChannels(ctx context.Context, req *lnrpc.ListChannelsRequest, options ...grpc.CallOption) (*lnrpc.ListChannelsResponse, error) {
return wrapper.client.ListChannels(ctx, req, options...)
}
func (wrapper *LNDWrapper) SendPaymentSync(ctx context.Context, req *lnrpc.SendRequest, options ...grpc.CallOption) (*lnrpc.SendResponse, error) {
return wrapper.client.SendPaymentSync(ctx, req, options...)
}
func (wrapper *LNDWrapper) AddInvoice(ctx context.Context, req *lnrpc.Invoice, options ...grpc.CallOption) (*lnrpc.AddInvoiceResponse, error) {
return wrapper.client.AddInvoice(ctx, req, options...)
}
func (wrapper *LNDWrapper) SubscribeInvoices(ctx context.Context, req *lnrpc.InvoiceSubscription, options ...grpc.CallOption) (SubscribeInvoicesWrapper, error) {
return wrapper.client.SubscribeInvoices(ctx, req, options...)
}
func (wrapper *LNDWrapper) GetInfo(ctx context.Context, req *lnrpc.GetInfoRequest, options ...grpc.CallOption) (*lnrpc.GetInfoResponse, error) {
return wrapper.client.GetInfo(ctx, req, options...)
}