make_invoice_controller.go raw

   1  package controllers
   2  
   3  import (
   4  	"context"
   5  
   6  	"github.com/getAlby/go-nostr"
   7  	"github.com/getAlby/hub/logger"
   8  	"github.com/getAlby/hub/nip47/models"
   9  	"github.com/sirupsen/logrus"
  10  )
  11  
  12  type makeInvoiceParams struct {
  13  	Amount          uint64                 `json:"amount"` // msats (NIP-47)
  14  	Description     string                 `json:"description"`
  15  	DescriptionHash string                 `json:"description_hash"`
  16  	Expiry          uint64                 `json:"expiry"`
  17  	Metadata        map[string]interface{} `json:"metadata,omitempty"`
  18  }
  19  type makeInvoiceResponse struct {
  20  	models.Transaction
  21  }
  22  
  23  func (controller *nip47Controller) HandleMakeInvoiceEvent(ctx context.Context, nip47Request *models.Request, requestEventId uint, appId uint, publishResponse publishFunc) {
  24  
  25  	makeInvoiceParams := &makeInvoiceParams{}
  26  	resp := decodeRequest(nip47Request, makeInvoiceParams)
  27  	if resp != nil {
  28  		publishResponse(resp, nostr.Tags{})
  29  		return
  30  	}
  31  
  32  	logger.Logger.WithFields(logrus.Fields{
  33  		"app_id":           appId,
  34  		"request_event_id": requestEventId,
  35  		"amount":           makeInvoiceParams.Amount,
  36  		"description":      makeInvoiceParams.Description,
  37  		"description_hash": makeInvoiceParams.DescriptionHash,
  38  		"expiry":           makeInvoiceParams.Expiry,
  39  		"metadata":         makeInvoiceParams.Metadata,
  40  	}).Debug("Handling make_invoice request")
  41  
  42  	expiry := makeInvoiceParams.Expiry
  43  
  44  	transaction, err := controller.transactionsService.MakeInvoice(ctx, makeInvoiceParams.Amount, makeInvoiceParams.Description, makeInvoiceParams.DescriptionHash, expiry, makeInvoiceParams.Metadata, controller.lnClient, &appId, &requestEventId, nil)
  45  	if err != nil {
  46  		logger.Logger.WithFields(logrus.Fields{
  47  			"request_event_id": requestEventId,
  48  			"amount":           makeInvoiceParams.Amount,
  49  			"description":      makeInvoiceParams.Description,
  50  			"descriptionHash":  makeInvoiceParams.DescriptionHash,
  51  			"expiry":           makeInvoiceParams.Expiry,
  52  		}).Infof("Failed to make invoice: %v", err)
  53  
  54  		publishResponse(&models.Response{
  55  			ResultType: nip47Request.Method,
  56  			Error:      mapNip47Error(err),
  57  		}, nostr.Tags{})
  58  		return
  59  	}
  60  
  61  	nip47Transaction := models.ToNip47Transaction(transaction)
  62  	responsePayload := &makeInvoiceResponse{
  63  		Transaction: *nip47Transaction,
  64  	}
  65  
  66  	publishResponse(&models.Response{
  67  		ResultType: nip47Request.Method,
  68  		Result:     responsePayload,
  69  	}, nostr.Tags{})
  70  }
  71