Save invoice destination

And save the node pubkey in the service to make it usable
This commit is contained in:
Michael Bumann
2022-01-20 15:40:32 +01:00
parent 7d6dc15b8c
commit 90a686677f
5 changed files with 48 additions and 26 deletions

View File

@@ -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 {

View File

@@ -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())

View File

@@ -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())
}

View File

@@ -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) {

17
main.go
View File

@@ -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