move pubkey checks to lnd client

This commit is contained in:
kiwiidb
2023-06-22 16:13:36 +02:00
parent c6b94e0d0d
commit def32f2d45
14 changed files with 60 additions and 46 deletions

View File

@@ -72,7 +72,7 @@ func (controller *KeySendController) KeySend(c echo.Context) error {
} }
} }
if reqBody.Destination == controller.svc.IdentityPubkey && reqBody.CustomRecords[strconv.Itoa(service.TLV_WALLET_ID)] == "" { if controller.svc.LndClient.IsIdentityPubkey(reqBody.Destination) && reqBody.CustomRecords[strconv.Itoa(service.TLV_WALLET_ID)] == "" {
return c.JSON(http.StatusBadRequest, &responses.ErrorResponse{ return c.JSON(http.StatusBadRequest, &responses.ErrorResponse{
Error: true, Error: true,
Code: 8, Code: 8,

View File

@@ -162,7 +162,7 @@ func (controller *KeySendController) SingleKeySend(c echo.Context, reqBody *KeyS
if reqBody.CustomRecords != nil { if reqBody.CustomRecords != nil {
customRecords = reqBody.CustomRecords customRecords = reqBody.CustomRecords
} }
if reqBody.Destination == controller.svc.IdentityPubkey && customRecords[strconv.Itoa(service.TLV_WALLET_ID)] == "" { if controller.svc.LndClient.IsIdentityPubkey(reqBody.Destination) && customRecords[strconv.Itoa(service.TLV_WALLET_ID)] == "" {
return nil, &responses.ErrorResponse{ return nil, &responses.ErrorResponse{
Error: true, Error: true,
Code: 8, Code: 8,

View File

@@ -283,7 +283,7 @@ func (suite *PaymentTestSuite) TestInternalPaymentKeysend() {
var buf bytes.Buffer var buf bytes.Buffer
assert.NoError(suite.T(), json.NewEncoder(&buf).Encode(ExpectedKeySendRequestBody{ assert.NoError(suite.T(), json.NewEncoder(&buf).Encode(ExpectedKeySendRequestBody{
Amount: int64(bobAmt), Amount: int64(bobAmt),
Destination: suite.service.IdentityPubkey, Destination: suite.service.LndClient.GetMainPubkey(),
Memo: memo, Memo: memo,
//add memo as WHATSAT_MESSAGE custom record //add memo as WHATSAT_MESSAGE custom record
CustomRecords: map[string]string{fmt.Sprint(service.TLV_WHATSAT_MESSAGE): memo, CustomRecords: map[string]string{fmt.Sprint(service.TLV_WHATSAT_MESSAGE): memo,

View File

@@ -248,6 +248,15 @@ func (mlnd *MockLND) DecodeBolt11(ctx context.Context, bolt11 string, options ..
} }
return result, nil return result, nil
} }
func (mlnd *MockLND) IsIdentityPubkey(pubkey string) (isOurPubkey bool) {
return pubkey == hex.EncodeToString(mlnd.pubKey.SerializeCompressed())
}
func (mlnd *MockLND) GetMainPubkey() (pubkey string) {
return hex.EncodeToString(mlnd.pubKey.SerializeCompressed())
}
func makePreimageHex() ([]byte, error) { func makePreimageHex() ([]byte, error) {
return randBytesFromStr(32, random.Hex) return randBytesFromStr(32, random.Hex)
} }

View File

@@ -144,3 +144,10 @@ func (mock *lndSubscriptionStartMockClient) DecodeBolt11(ctx context.Context, bo
func (mlnd *lndSubscriptionStartMockClient) TrackPayment(ctx context.Context, hash []byte, options ...grpc.CallOption) (*lnrpc.Payment, error) { func (mlnd *lndSubscriptionStartMockClient) TrackPayment(ctx context.Context, hash []byte, options ...grpc.CallOption) (*lnrpc.Payment, error) {
return nil, nil return nil, nil
} }
func (mlnd *lndSubscriptionStartMockClient) IsIdentityPubkey(pubkey string) (isOurPubkey bool) {
panic("not implemented") // TODO: Implement
}
func (mlnd *lndSubscriptionStartMockClient) GetMainPubkey() (pubkey string) {
panic("not implemented") // TODO: Implement
}

View File

@@ -19,7 +19,6 @@ import (
"github.com/getAlby/lndhub.go/rabbitmq" "github.com/getAlby/lndhub.go/rabbitmq"
"github.com/golang-jwt/jwt" "github.com/golang-jwt/jwt"
"github.com/labstack/echo/v4" "github.com/labstack/echo/v4"
"github.com/lightningnetwork/lnd/lnrpc"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"github.com/stretchr/testify/suite" "github.com/stretchr/testify/suite"
"github.com/uptrace/bun/migrate" "github.com/uptrace/bun/migrate"
@@ -66,10 +65,10 @@ func LndHubTestServiceInit(lndClientMock lnd.LightningClientWrapper) (svc *servi
c.RabbitMQLndhubInvoiceExchange = "test_lndhub_invoices" c.RabbitMQLndhubInvoiceExchange = "test_lndhub_invoices"
c.RabbitMQLndInvoiceExchange = "test_lnd_invoices" c.RabbitMQLndInvoiceExchange = "test_lnd_invoices"
amqpClient, err := rabbitmq.DialAMQP(c.RabbitMQUri) amqpClient, err := rabbitmq.DialAMQP(c.RabbitMQUri)
if err != nil { if err != nil {
return nil, err return nil, err
} }
rabbitmqClient, err = rabbitmq.NewClient(amqpClient, rabbitmqClient, err = rabbitmq.NewClient(amqpClient,
rabbitmq.WithLndInvoiceExchange(c.RabbitMQLndInvoiceExchange), rabbitmq.WithLndInvoiceExchange(c.RabbitMQLndInvoiceExchange),
@@ -105,11 +104,6 @@ func LndHubTestServiceInit(lndClientMock lnd.LightningClientWrapper) (svc *servi
Logger: logger, Logger: logger,
RabbitMQClient: rabbitmqClient, RabbitMQClient: rabbitmqClient,
} }
getInfo, err := lndClientMock.GetInfo(ctx, &lnrpc.GetInfoRequest{})
if err != nil {
logger.Fatalf("Error getting node info: %v", err)
}
svc.IdentityPubkey = getInfo.IdentityPubkey
svc.InvoicePubSub = service.NewPubsub() svc.InvoicePubSub = service.NewPubsub()
return svc, nil return svc, nil

View File

@@ -217,7 +217,7 @@ func (svc *LndhubService) PayInvoice(ctx context.Context, invoice *models.Invoic
// Check the destination pubkey if it is an internal invoice and going to our node // Check the destination pubkey if it is an internal invoice and going to our node
// Here we start using context.Background because we want to complete these calls // Here we start using context.Background because we want to complete these calls
// regardless of if the request's context is canceled or not. // regardless of if the request's context is canceled or not.
if svc.IdentityPubkey == invoice.DestinationPubkeyHex { if svc.LndClient.IsIdentityPubkey(invoice.DestinationPubkeyHex) {
paymentResponse, err = svc.SendInternalPayment(context.Background(), invoice) paymentResponse, err = svc.SendInternalPayment(context.Background(), invoice)
if err != nil { if err != nil {
svc.HandleFailedPayment(context.Background(), invoice, entry, err) svc.HandleFailedPayment(context.Background(), invoice, entry, err)
@@ -408,7 +408,7 @@ func (svc *LndhubService) AddIncomingInvoice(ctx context.Context, userID int64,
invoice.RHash = hex.EncodeToString(lnInvoiceResult.RHash) invoice.RHash = hex.EncodeToString(lnInvoiceResult.RHash)
invoice.Preimage = hex.EncodeToString(preimage) invoice.Preimage = hex.EncodeToString(preimage)
invoice.AddIndex = lnInvoiceResult.AddIndex invoice.AddIndex = lnInvoiceResult.AddIndex
invoice.DestinationPubkeyHex = svc.IdentityPubkey // Our node pubkey for incoming invoices invoice.DestinationPubkeyHex = svc.LndClient.GetMainPubkey() // Our node pubkey for incoming invoices
invoice.State = common.InvoiceStateOpen invoice.State = common.InvoiceStateOpen
_, err = svc.DB.NewUpdate().Model(&invoice).WherePK().Exec(ctx) _, err = svc.DB.NewUpdate().Model(&invoice).WherePK().Exec(ctx)

View File

@@ -46,7 +46,7 @@ func (svc *LndhubService) HandleInternalKeysendPayment(ctx context.Context, invo
RHash: hex.EncodeToString(pHash.Sum(nil)), RHash: hex.EncodeToString(pHash.Sum(nil)),
Preimage: hex.EncodeToString(preImage), Preimage: hex.EncodeToString(preImage),
DestinationCustomRecords: invoice.DestinationCustomRecords, DestinationCustomRecords: invoice.DestinationCustomRecords,
DestinationPubkeyHex: svc.IdentityPubkey, DestinationPubkeyHex: svc.LndClient.GetMainPubkey(),
AddIndex: invoice.AddIndex, AddIndex: invoice.AddIndex,
} }
//persist the incoming invoice //persist the incoming invoice
@@ -212,7 +212,7 @@ func (svc *LndhubService) createKeysendInvoice(ctx context.Context, rawInvoice *
RHash: hex.EncodeToString(rawInvoice.RHash), RHash: hex.EncodeToString(rawInvoice.RHash),
Preimage: hex.EncodeToString(rawInvoice.RPreimage), Preimage: hex.EncodeToString(rawInvoice.RPreimage),
DestinationCustomRecords: rawInvoice.Htlcs[0].CustomRecords, DestinationCustomRecords: rawInvoice.Htlcs[0].CustomRecords,
DestinationPubkeyHex: svc.IdentityPubkey, DestinationPubkeyHex: svc.LndClient.GetMainPubkey(),
AddIndex: rawInvoice.AddIndex, AddIndex: rawInvoice.AddIndex,
} }
return result, nil return result, nil

View File

@@ -25,7 +25,6 @@ type LndhubService struct {
LndClient lnd.LightningClientWrapper LndClient lnd.LightningClientWrapper
RabbitMQClient rabbitmq.Client RabbitMQClient rabbitmq.Client
Logger *lecho.Logger Logger *lecho.Logger
IdentityPubkey string
InvoicePubSub *Pubsub InvoicePubSub *Pubsub
} }

View File

@@ -135,7 +135,7 @@ func (svc *LndhubService) BalanceCheck(ctx context.Context, lnpayReq *lnd.LNPayR
} }
func (svc *LndhubService) CalcFeeLimit(destination string, amount int64) int64 { func (svc *LndhubService) CalcFeeLimit(destination string, amount int64) int64 {
if destination == svc.IdentityPubkey { if svc.LndClient.IsIdentityPubkey(destination) {
return 0 return 0
} }
limit := int64(10) limit := int64(10)

View File

@@ -16,6 +16,8 @@ type LightningClientWrapper interface {
SubscribePayment(ctx context.Context, req *routerrpc.TrackPaymentRequest, options ...grpc.CallOption) (SubscribePaymentWrapper, error) SubscribePayment(ctx context.Context, req *routerrpc.TrackPaymentRequest, options ...grpc.CallOption) (SubscribePaymentWrapper, error)
GetInfo(ctx context.Context, req *lnrpc.GetInfoRequest, options ...grpc.CallOption) (*lnrpc.GetInfoResponse, error) GetInfo(ctx context.Context, req *lnrpc.GetInfoRequest, options ...grpc.CallOption) (*lnrpc.GetInfoResponse, error)
DecodeBolt11(ctx context.Context, bolt11 string, options ...grpc.CallOption) (*lnrpc.PayReq, error) DecodeBolt11(ctx context.Context, bolt11 string, options ...grpc.CallOption) (*lnrpc.PayReq, error)
IsIdentityPubkey(pubkey string) (isOurPubkey bool)
GetMainPubkey() (pubkey string)
} }
type SubscribeInvoicesWrapper interface { type SubscribeInvoicesWrapper interface {

View File

@@ -31,11 +31,12 @@ type LNDoptions struct {
} }
type LNDWrapper struct { type LNDWrapper struct {
client lnrpc.LightningClient client lnrpc.LightningClient
routerClient routerrpc.RouterClient routerClient routerrpc.RouterClient
IdentityPubkey string
} }
func NewLNDclient(lndOptions LNDoptions) (result *LNDWrapper, err error) { func NewLNDclient(lndOptions LNDoptions, ctx context.Context) (result *LNDWrapper, err error) {
// Get credentials either from a hex string, a file or the system's certificate store // Get credentials either from a hex string, a file or the system's certificate store
var creds credentials.TransportCredentials var creds credentials.TransportCredentials
// if a hex string is provided // if a hex string is provided
@@ -92,10 +93,15 @@ func NewLNDclient(lndOptions LNDoptions) (result *LNDWrapper, err error) {
if err != nil { if err != nil {
return nil, err return nil, err
} }
lnClient := lnrpc.NewLightningClient(conn)
getInfo, err := lnClient.GetInfo(ctx, &lnrpc.GetInfoRequest{})
if err != nil {
return nil, err
}
return &LNDWrapper{ return &LNDWrapper{
client: lnrpc.NewLightningClient(conn), client: lnClient,
routerClient: routerrpc.NewRouterClient(conn), routerClient: routerrpc.NewRouterClient(conn),
IdentityPubkey: getInfo.IdentityPubkey,
}, nil }, nil
} }
@@ -128,3 +134,11 @@ func (wrapper *LNDWrapper) DecodeBolt11(ctx context.Context, bolt11 string, opti
func (wrapper *LNDWrapper) SubscribePayment(ctx context.Context, req *routerrpc.TrackPaymentRequest, options ...grpc.CallOption) (SubscribePaymentWrapper, error) { func (wrapper *LNDWrapper) SubscribePayment(ctx context.Context, req *routerrpc.TrackPaymentRequest, options ...grpc.CallOption) (SubscribePaymentWrapper, error) {
return wrapper.routerClient.TrackPaymentV2(ctx, req, options...) return wrapper.routerClient.TrackPaymentV2(ctx, req, options...)
} }
func (wrapper *LNDWrapper) IsIdentityPubkey(pubkey string) (isOurPubkey bool) {
return pubkey == wrapper.IdentityPubkey
}
func (wrapper *LNDWrapper) GetMainPubkey() (pubkey string) {
return wrapper.IdentityPubkey
}

11
main.go
View File

@@ -24,7 +24,6 @@ import (
"github.com/joho/godotenv" "github.com/joho/godotenv"
"github.com/kelseyhightower/envconfig" "github.com/kelseyhightower/envconfig"
"github.com/labstack/echo/v4" "github.com/labstack/echo/v4"
"github.com/lightningnetwork/lnd/lnrpc"
echoSwagger "github.com/swaggo/echo-swagger" echoSwagger "github.com/swaggo/echo-swagger"
"github.com/uptrace/bun/migrate" "github.com/uptrace/bun/migrate"
) )
@@ -105,15 +104,12 @@ func main() {
MacaroonHex: c.LNDMacaroonHex, MacaroonHex: c.LNDMacaroonHex,
CertFile: c.LNDCertFile, CertFile: c.LNDCertFile,
CertHex: c.LNDCertHex, CertHex: c.LNDCertHex,
}) }, startupCtx)
if err != nil { if err != nil {
logger.Fatalf("Error initializing the LND connection: %v", err) logger.Fatalf("Error initializing the LND connection: %v", err)
} }
getInfo, err := lndClient.GetInfo(startupCtx, &lnrpc.GetInfoRequest{})
if err != nil { logger.Infof("Connected to LND: %s ", lndClient.GetMainPubkey())
logger.Fatalf("Error getting node info: %v", err)
}
logger.Infof("Connected to LND: %s - %s", getInfo.Alias, getInfo.IdentityPubkey)
// If no RABBITMQ_URI was provided we will not attempt to create a client // If no RABBITMQ_URI was provided we will not attempt to create a client
// No rabbitmq features will be available in this case. // No rabbitmq features will be available in this case.
@@ -147,7 +143,6 @@ func main() {
DB: dbConn, DB: dbConn,
LndClient: lndClient, LndClient: lndClient,
Logger: logger, Logger: logger,
IdentityPubkey: getInfo.IdentityPubkey,
InvoicePubSub: service.NewPubsub(), InvoicePubSub: service.NewPubsub(),
RabbitMQClient: rabbitmqClient, RabbitMQClient: rabbitmqClient,
} }

View File

@@ -13,7 +13,6 @@ import (
"github.com/joho/godotenv" "github.com/joho/godotenv"
"github.com/kelseyhightower/envconfig" "github.com/kelseyhightower/envconfig"
"github.com/labstack/echo/v4" "github.com/labstack/echo/v4"
"github.com/lightningnetwork/lnd/lnrpc"
) )
// script to reconcile pending payments between the backup node and the database // script to reconcile pending payments between the backup node and the database
@@ -54,23 +53,18 @@ func main() {
MacaroonHex: c.LNDMacaroonHex, MacaroonHex: c.LNDMacaroonHex,
CertFile: c.LNDCertFile, CertFile: c.LNDCertFile,
CertHex: c.LNDCertHex, CertHex: c.LNDCertHex,
}) }, startupCtx)
if err != nil { if err != nil {
e.Logger.Fatalf("Error initializing the LND connection: %v", err) e.Logger.Fatalf("Error initializing the LND connection: %v", err)
} }
getInfo, err := lndClient.GetInfo(startupCtx, &lnrpc.GetInfoRequest{}) logger.Infof("Connected to LND: %s ", lndClient.GetMainPubkey())
if err != nil {
e.Logger.Fatalf("Error getting node info: %v", err)
}
logger.Infof("Connected to LND: %s - %s", getInfo.Alias, getInfo.IdentityPubkey)
svc := &service.LndhubService{ svc := &service.LndhubService{
Config: c, Config: c,
DB: dbConn, DB: dbConn,
LndClient: lndClient, LndClient: lndClient,
Logger: logger, Logger: logger,
IdentityPubkey: getInfo.IdentityPubkey, InvoicePubSub: service.NewPubsub(),
InvoicePubSub: service.NewPubsub(),
} }
err = svc.CheckAllPendingOutgoingPayments(startupCtx) err = svc.CheckAllPendingOutgoingPayments(startupCtx)