From 90a686677fd3547af2a4d807bd3a5e577efe292c Mon Sep 17 00:00:00 2001 From: Michael Bumann Date: Thu, 20 Jan 2022 15:40:32 +0100 Subject: [PATCH] Save invoice destination And save the node pubkey in the service to make it usable --- db/models/invoice.go | 31 ++++++++++++++++--------------- lib/service/invoices.go | 13 ++++++++----- lib/service/ln.go | 5 +++++ lib/service/service.go | 8 +++++--- main.go | 17 ++++++++++++++--- 5 files changed, 48 insertions(+), 26 deletions(-) diff --git a/db/models/invoice.go b/db/models/invoice.go index afc61a4..548cd46 100644 --- a/db/models/invoice.go +++ b/db/models/invoice.go @@ -9,21 +9,22 @@ import ( // Invoice : Invoice Model type Invoice struct { - ID int64 `json:"id" bun:",pk,autoincrement"` - Type string `json:"type" validate:"required"` - UserID int64 `json:"user_id" validate:"required"` - User *User `bun:"rel:belongs-to,join:user_id=id"` - Amount int64 `json:"amount" validate:"gte=0"` - Memo string `json:"memo"` - DescriptionHash string `json:"description_hash" bun:",nullzero"` - PaymentRequest string `json:"payment_request"` - RHash string `json:"r_hash"` - Preimage string `json:"preimage" bun:",nullzero"` - State string `json:"state" bun:",default:'initialized'"` - AddIndex uint64 `json:"add_index" bun:",nullzero"` - CreatedAt time.Time `bun:",nullzero,notnull,default:current_timestamp"` - UpdatedAt bun.NullTime `json:"updated_at"` - SettledAt bun.NullTime `json:"settled_at"` + ID int64 `json:"id" bun:",pk,autoincrement"` + Type string `json:"type" validate:"required"` + UserID int64 `json:"user_id" validate:"required"` + User *User `bun:"rel:belongs-to,join:user_id=id"` + Amount int64 `json:"amount" validate:"gte=0"` + Memo string `json:"memo"` + DescriptionHash string `json:"description_hash" bun:",nullzero"` + PaymentRequest string `json:"payment_request"` + DestinationPubKey string `json:"destination_pub_key"` + RHash string `json:"r_hash"` + Preimage string `json:"preimage" bun:",nullzero"` + State string `json:"state" bun:",default:'initialized'"` + AddIndex uint64 `json:"add_index" bun:",nullzero"` + CreatedAt time.Time `bun:",nullzero,notnull,default:current_timestamp"` + UpdatedAt bun.NullTime `json:"updated_at"` + SettledAt bun.NullTime `json:"settled_at"` } func (i *Invoice) BeforeAppendModel(ctx context.Context, query bun.Query) error { diff --git a/lib/service/invoices.go b/lib/service/invoices.go index 11b4767..7fc6c08 100644 --- a/lib/service/invoices.go +++ b/lib/service/invoices.go @@ -114,12 +114,14 @@ func (svc *LndhubService) PayInvoice(invoice *models.Invoice) (*models.Transacti func (svc *LndhubService) AddOutgoingInvoice(userID int64, paymentRequest string, decodedInvoice zpay32.Invoice) (*models.Invoice, error) { // Initialize new DB invoice + destinationPubkey := hex.EncodeToString(decodedInvoice.Destination.SerializeCompressed()) invoice := models.Invoice{ - Type: "outgoing", - UserID: userID, - Memo: *decodedInvoice.Description, - PaymentRequest: paymentRequest, - State: "initialized", + Type: "outgoing", + UserID: userID, + Memo: *decodedInvoice.Description, + PaymentRequest: paymentRequest, + State: "initialized", + DestinationPubKey: destinationPubkey, } if decodedInvoice.DescriptionHash != nil { dh := *decodedInvoice.DescriptionHash @@ -176,6 +178,7 @@ func (svc *LndhubService) AddIncomingInvoice(userID int64, amount int64, memo, d invoice.PaymentRequest = lnInvoiceResult.PaymentRequest invoice.RHash = hex.EncodeToString(lnInvoiceResult.RHash) invoice.AddIndex = lnInvoiceResult.AddIndex + invoice.DestinationPubKey = svc.GetIdentPubKeyHex() // Our node pubkey for incoming invoices invoice.State = "created" _, err = svc.DB.NewUpdate().Model(&invoice).WherePK().Exec(context.TODO()) diff --git a/lib/service/ln.go b/lib/service/ln.go index a9f609a..2a7bf52 100644 --- a/lib/service/ln.go +++ b/lib/service/ln.go @@ -2,6 +2,7 @@ package service import ( "context" + "encoding/hex" "github.com/lightningnetwork/lnd/lnrpc" ) @@ -9,3 +10,7 @@ import ( func (svc *LndhubService) GetInfo(ctx context.Context) (*lnrpc.GetInfoResponse, error) { return svc.LndClient.GetInfo(ctx, &lnrpc.GetInfoRequest{}) } + +func (svc *LndhubService) GetIdentPubKeyHex() string { + return hex.EncodeToString(svc.IdentityPubkey.SerializeCompressed()) +} diff --git a/lib/service/service.go b/lib/service/service.go index 0a0da22..d5cd224 100644 --- a/lib/service/service.go +++ b/lib/service/service.go @@ -4,6 +4,7 @@ import ( "context" "fmt" + "github.com/btcsuite/btcd/btcec" "github.com/getAlby/lndhub.go/db/models" "github.com/getAlby/lndhub.go/lib/tokens" "github.com/labstack/gommon/random" @@ -15,9 +16,10 @@ import ( const alphaNumBytes = random.Alphanumeric type LndhubService struct { - Config *Config - DB *bun.DB - LndClient lnrpc.LightningClient + Config *Config + DB *bun.DB + LndClient lnrpc.LightningClient + IdentityPubkey *btcec.PublicKey } func (svc *LndhubService) GenerateToken(login, password, inRefreshToken string) (accessToken, refreshToken string, err error) { diff --git a/main.go b/main.go index b8cd7cd..962bd1c 100644 --- a/main.go +++ b/main.go @@ -2,6 +2,7 @@ package main import ( "context" + "encoding/hex" "fmt" "log" "net/http" @@ -9,6 +10,7 @@ import ( "os/signal" "time" + "github.com/btcsuite/btcd/btcec" "github.com/getAlby/lndhub.go/controllers" "github.com/getAlby/lndhub.go/db" "github.com/getAlby/lndhub.go/db/migrations" @@ -102,12 +104,21 @@ func main() { if err != nil { e.Logger.Fatalf("Error getting node info: %v", err) } + hexPubkey, err := hex.DecodeString(getInfo.IdentityPubkey) + if err != nil { + logger.Fatalf("Failed to decode IdentityPubkey: %v", err) + } + identityPubKey, err := btcec.ParsePubKey(hexPubkey[:], btcec.S256()) + if err != nil { + logger.Fatalf("Failed to parse node IdentityPubkey: %v", err) + } logger.Infof("Connected to LND: %s - %s", getInfo.Alias, getInfo.IdentityPubkey) svc := &service.LndhubService{ - Config: c, - DB: dbConn, - LndClient: lndClient, + Config: c, + DB: dbConn, + LndClient: lndClient, + IdentityPubkey: identityPubKey, } // Public endpoints for account creation and authentication