pay_keysend_controller.go raw
1 package controllers
2
3 import (
4 "context"
5
6 "github.com/getAlby/go-nostr"
7 "github.com/getAlby/hub/db"
8 "github.com/getAlby/hub/lnclient"
9 "github.com/getAlby/hub/logger"
10 "github.com/getAlby/hub/nip47/models"
11 "github.com/sirupsen/logrus"
12 )
13
14 type tlvRecord struct {
15 Type uint64 `json:"type"`
16 // hex-encoded value
17 Value string `json:"value"`
18 }
19
20 type payKeysendParams struct {
21 Amount uint64 `json:"amount"`
22 Pubkey string `json:"pubkey"`
23 Preimage string `json:"preimage"`
24 TLVRecords []tlvRecord `json:"tlv_records"`
25 }
26
27 func (controller *nip47Controller) HandlePayKeysendEvent(ctx context.Context, nip47Request *models.Request, requestEventId uint, app *db.App, publishResponse publishFunc, tags nostr.Tags) {
28 payKeysendParams := &payKeysendParams{}
29 resp := decodeRequest(nip47Request, payKeysendParams)
30 if resp != nil {
31 publishResponse(resp, tags)
32 return
33 }
34 controller.payKeysend(ctx, payKeysendParams, nip47Request, requestEventId, app, publishResponse, tags)
35 }
36
37 func (controller *nip47Controller) payKeysend(ctx context.Context, payKeysendParams *payKeysendParams, nip47Request *models.Request, requestEventId uint, app *db.App, publishResponse publishFunc, tags nostr.Tags) {
38 logger.Logger.WithFields(logrus.Fields{
39 "request_event_id": requestEventId,
40 "appId": app.ID,
41 "senderPubkey": payKeysendParams.Pubkey,
42 }).Info("Sending keysend payment")
43
44 tlvRecords := make([]lnclient.TLVRecord, 0, len(payKeysendParams.TLVRecords))
45 for _, r := range payKeysendParams.TLVRecords {
46 tlvRecords = append(tlvRecords, lnclient.TLVRecord{
47 Type: r.Type,
48 Value: r.Value,
49 })
50 }
51
52 transaction, err := controller.transactionsService.SendKeysend(payKeysendParams.Amount, payKeysendParams.Pubkey, tlvRecords, payKeysendParams.Preimage, controller.lnClient, &app.ID, &requestEventId)
53 if err != nil {
54 logger.Logger.WithFields(logrus.Fields{
55 "request_event_id": requestEventId,
56 "appId": app.ID,
57 "recipientPubkey": payKeysendParams.Pubkey,
58 }).Infof("Failed to send keysend payment: %v", err)
59 publishResponse(&models.Response{
60 ResultType: nip47Request.Method,
61 Error: mapNip47Error(err),
62 }, tags)
63 return
64 }
65
66 publishResponse(&models.Response{
67 ResultType: nip47Request.Method,
68 Result: payResponse{
69 Preimage: *transaction.Preimage,
70 FeesPaid: transaction.FeeMsat,
71 },
72 }, tags)
73 }
74