make_hold_invoice_controller.go raw
1 package controllers
2
3 import (
4 "context"
5
6 "github.com/getAlby/go-nostr"
7 "github.com/getAlby/hub/constants"
8 "github.com/getAlby/hub/logger"
9 "github.com/getAlby/hub/nip47/models"
10 "github.com/sirupsen/logrus"
11 )
12
13 type makeHoldInvoiceParams struct {
14 Amount uint64 `json:"amount"`
15 PaymentHash string `json:"payment_hash"`
16 Description string `json:"description"`
17 DescriptionHash string `json:"description_hash"`
18 Expiry uint64 `json:"expiry"`
19 MinCltvExpiryDelta *uint64 `json:"min_cltv_expiry_delta"`
20 Metadata map[string]interface{} `json:"metadata,omitempty"`
21 }
22 type makeHoldInvoiceResponse struct {
23 models.Transaction
24 }
25
26 func (controller *nip47Controller) HandleMakeHoldInvoiceEvent(ctx context.Context, nip47Request *models.Request, requestEventId uint, appId uint, publishResponse func(*models.Response, nostr.Tags)) {
27 makeHoldInvoiceParams := &makeHoldInvoiceParams{}
28 decodeErrResp := decodeRequest(nip47Request, makeHoldInvoiceParams)
29 if decodeErrResp != nil {
30 publishResponse(decodeErrResp, nostr.Tags{})
31 return
32 }
33
34 if makeHoldInvoiceParams.PaymentHash == "" {
35 logger.Logger.WithFields(logrus.Fields{
36 "requestEventId": requestEventId,
37 "appId": appId,
38 }).Error("Payment hash is missing for make_hold_invoice")
39 publishResponse(&models.Response{
40 ResultType: nip47Request.Method,
41 Error: &models.Error{
42 Code: constants.ERROR_BAD_REQUEST,
43 Message: "payment_hash is required for make_hold_invoice",
44 },
45 }, nostr.Tags{})
46 return
47 }
48
49 logger.Logger.WithFields(logrus.Fields{
50 "requestEventId": requestEventId,
51 "appId": appId,
52 "amount": makeHoldInvoiceParams.Amount,
53 "description": makeHoldInvoiceParams.Description,
54 "descriptionHash": makeHoldInvoiceParams.DescriptionHash,
55 "expiry": makeHoldInvoiceParams.Expiry,
56 "minCltvExpiryDelta": makeHoldInvoiceParams.MinCltvExpiryDelta,
57 "paymentHash": makeHoldInvoiceParams.PaymentHash,
58 "metadata": makeHoldInvoiceParams.Metadata,
59 }).Info("Making hold invoice")
60
61 requestEventIdUint := uint(requestEventId)
62 transaction, err := controller.transactionsService.MakeHoldInvoice(
63 ctx,
64 makeHoldInvoiceParams.Amount,
65 makeHoldInvoiceParams.Description,
66 makeHoldInvoiceParams.DescriptionHash,
67 makeHoldInvoiceParams.Expiry,
68 makeHoldInvoiceParams.PaymentHash,
69 makeHoldInvoiceParams.MinCltvExpiryDelta,
70 makeHoldInvoiceParams.Metadata,
71 controller.lnClient,
72 &appId,
73 &requestEventIdUint,
74 )
75
76 if err != nil {
77 logger.Logger.WithFields(logrus.Fields{
78 "request_event_id": requestEventId,
79 "appId": appId,
80 "amount": makeHoldInvoiceParams.Amount,
81 "description": makeHoldInvoiceParams.Description,
82 "descriptionHash": makeHoldInvoiceParams.DescriptionHash,
83 "expiry": makeHoldInvoiceParams.Expiry,
84 "minCltvExpiryDelta": makeHoldInvoiceParams.MinCltvExpiryDelta,
85 "paymentHash": makeHoldInvoiceParams.PaymentHash,
86 }).WithError(err).Error("Failed to make invoice")
87
88 publishResponse(&models.Response{
89 ResultType: nip47Request.Method,
90 Error: mapNip47Error(err),
91 }, nostr.Tags{})
92 return
93 }
94
95 nip47Transaction := models.ToNip47Transaction(transaction)
96
97 responsePayload := &makeHoldInvoiceResponse{
98 Transaction: *nip47Transaction,
99 }
100
101 publishResponse(&models.Response{
102 ResultType: nip47Request.Method,
103 Result: responsePayload,
104 }, nostr.Tags{})
105 }
106