multi_pay_keysend_controller.go raw

   1  package controllers
   2  
   3  import (
   4  	"context"
   5  	"sync"
   6  
   7  	"github.com/getAlby/go-nostr"
   8  	"github.com/getAlby/hub/db"
   9  	"github.com/getAlby/hub/nip47/models"
  10  )
  11  
  12  type multiPayKeysendParams struct {
  13  	Keysends []multiPayKeysendElement `json:"keysends"`
  14  }
  15  
  16  type multiPayKeysendElement struct {
  17  	payKeysendParams
  18  	Id string `json:"id"`
  19  }
  20  
  21  func (controller *nip47Controller) HandleMultiPayKeysendEvent(ctx context.Context, nip47Request *models.Request, requestEventId uint, app *db.App, publishResponse publishFunc) {
  22  	multiPayParams := &multiPayKeysendParams{}
  23  	resp := decodeRequest(nip47Request, multiPayParams)
  24  	if resp != nil {
  25  		publishResponse(resp, nostr.Tags{})
  26  		return
  27  	}
  28  
  29  	var wg sync.WaitGroup
  30  	wg.Add(len(multiPayParams.Keysends))
  31  	for _, keysendInfo := range multiPayParams.Keysends {
  32  		go func(keysendInfo multiPayKeysendElement) {
  33  			defer wg.Done()
  34  
  35  			keysendDTagValue := keysendInfo.Id
  36  			if keysendDTagValue == "" {
  37  				keysendDTagValue = keysendInfo.Pubkey
  38  			}
  39  			dTag := []string{"d", keysendDTagValue}
  40  
  41  			controller.
  42  				payKeysend(ctx, &keysendInfo.payKeysendParams, nip47Request, requestEventId, app, publishResponse, nostr.Tags{dTag})
  43  		}(keysendInfo)
  44  	}
  45  
  46  	wg.Wait()
  47  }
  48