Skip to content

Commit

Permalink
Merge pull request #1878 from stakwork/feat/add_data_to_keysend
Browse files Browse the repository at this point in the history
PR: data in bounty payout body
  • Loading branch information
elraphty authored Oct 4, 2024
2 parents 9218a55 + 40a9e11 commit b3bfd8c
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 19 deletions.
12 changes: 7 additions & 5 deletions handlers/bounty.go
Original file line number Diff line number Diff line change
Expand Up @@ -569,14 +569,16 @@ func (h *bountyHandler) MakeBountyPayment(w http.ResponseWriter, r *http.Request
// Get Bounty Assignee
assignee := h.db.GetPersonByPubkey(bounty.Assignee)

memoText := fmt.Sprintf("Payment For: %ss", bounty.Title)

// If the v2contactkey is present
if config.IsV2Payment {
url := fmt.Sprintf("%s/pay", config.V2BotUrl)

fmt.Println("IS V@ PAYMENT ====")
fmt.Println("IS V2 PAYMENT ====")

// Build v2 keysend payment data
bodyData := utils.BuildV2KeysendBodyData(amount, assignee.OwnerPubKey, assignee.OwnerRouteHint)
bodyData := utils.BuildV2KeysendBodyData(amount, assignee.OwnerPubKey, assignee.OwnerRouteHint, memoText)
jsonBody := []byte(bodyData)

req, _ := http.NewRequest(http.MethodPost, url, bytes.NewBuffer(jsonBody))
Expand Down Expand Up @@ -669,7 +671,7 @@ func (h *bountyHandler) MakeBountyPayment(w http.ResponseWriter, r *http.Request
} else { // Process v1 payment
url := fmt.Sprintf("%s/payment", config.RelayUrl)

bodyData := utils.BuildKeysendBodyData(amount, assignee.OwnerPubKey, assignee.OwnerRouteHint)
bodyData := utils.BuildKeysendBodyData(amount, assignee.OwnerPubKey, assignee.OwnerRouteHint, memoText)
jsonBody := []byte(bodyData)

req, _ := http.NewRequest(http.MethodPost, url, bytes.NewBuffer(jsonBody))
Expand Down Expand Up @@ -1209,7 +1211,7 @@ func (h *bountyHandler) PollInvoice(w http.ResponseWriter, r *http.Request) {
url := fmt.Sprintf("%s/pay", config.V2BotUrl)

// Build v2 keysend payment data
bodyData := utils.BuildV2KeysendBodyData(amount, invData.UserPubkey, invData.RouteHint)
bodyData := utils.BuildV2KeysendBodyData(amount, invData.UserPubkey, invData.RouteHint, "")
jsonBody := []byte(bodyData)

req, _ := http.NewRequest(http.MethodPost, url, bytes.NewBuffer(jsonBody))
Expand Down Expand Up @@ -1267,7 +1269,7 @@ func (h *bountyHandler) PollInvoice(w http.ResponseWriter, r *http.Request) {
} else {
url := fmt.Sprintf("%s/payment", config.RelayUrl)

bodyData := utils.BuildKeysendBodyData(amount, invData.UserPubkey, invData.RouteHint)
bodyData := utils.BuildKeysendBodyData(amount, invData.UserPubkey, invData.RouteHint, "")

jsonBody := []byte(bodyData)

Expand Down
16 changes: 10 additions & 6 deletions handlers/bounty_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1418,12 +1418,14 @@ func TestMakeBountyPayment(t *testing.T) {
bHandler2.getSocketConnections = mockGetSocketConnections
bHandler2.userHasAccess = mockUserHasAccessTrue

memoText := fmt.Sprintf("Payment For: %ss", bounty.Title)

expectedUrl := fmt.Sprintf("%s/payment", config.RelayUrl)
expectedBody := fmt.Sprintf(`{"amount": %d, "destination_key": "%s", "text": "memotext added for notification"}`, bountyAmount, person.OwnerPubKey)
expectedBody := fmt.Sprintf(`{"amount": %d, "destination_key": "%s", "text": "memotext added for notification", "data": "%s"}`, bountyAmount, person.OwnerPubKey, memoText)

expectedV2Url := fmt.Sprintf("%s/pay", botURL)
expectedV2Body :=
fmt.Sprintf(`{"amt_msat": %d, "dest": "%s", "route_hint": "%s", "wait": true}`, bountyAmount*1000, person.OwnerPubKey, person.OwnerRouteHint)
fmt.Sprintf(`{"amt_msat": %d, "dest": "%s", "route_hint": "%s", "data": "%s", "wait": true}`, bountyAmount*1000, person.OwnerPubKey, person.OwnerRouteHint, memoText)

r := io.NopCloser(bytes.NewReader([]byte(`"internal server error"`)))
if botURL != "" && botToken != "" {
Expand Down Expand Up @@ -1464,12 +1466,14 @@ func TestMakeBountyPayment(t *testing.T) {
bHandler.getSocketConnections = mockGetSocketConnections
bHandler.userHasAccess = mockUserHasAccessTrue

memoText := fmt.Sprintf("Payment For: %ss", bounty.Title)

expectedUrl := fmt.Sprintf("%s/payment", config.RelayUrl)
expectedBody := fmt.Sprintf(`{"amount": %d, "destination_key": "%s", "text": "memotext added for notification"}`, bountyAmount, person.OwnerPubKey)
expectedBody := fmt.Sprintf(`{"amount": %d, "destination_key": "%s", "text": "memotext added for notification", "data": "%s"}`, bountyAmount, person.OwnerPubKey, memoText)

expectedV2Url := fmt.Sprintf("%s/pay", botURL)
expectedV2Body :=
fmt.Sprintf(`{"amt_msat": %d, "dest": "%s", "route_hint": "%s", "wait": true}`, bountyAmount*1000, person.OwnerPubKey, person.OwnerRouteHint)
fmt.Sprintf(`{"amt_msat": %d, "dest": "%s", "route_hint": "%s", "data": "%s", "wait": true}`, bountyAmount*1000, person.OwnerPubKey, person.OwnerRouteHint, memoText)

if botURL != "" && botToken != "" {
rv2 := io.NopCloser(bytes.NewReader([]byte(`{"status": "COMPLETE", "tag": "", "preimage": "", "payment_hash": "" }`)))
Expand Down Expand Up @@ -1951,10 +1955,10 @@ func TestPollInvoice(t *testing.T) {
expectedPaymentUrl := fmt.Sprintf("%s/payment", config.RelayUrl)
expectedV2PaymentUrl := fmt.Sprintf("%s/pay", botURL)

expectedPaymentBody := fmt.Sprintf(`{"amount": %d, "destination_key": "%s", "text": "memotext added for notification"}`, bountyAmount, invoice.OwnerPubkey)
expectedPaymentBody := fmt.Sprintf(`{"amount": %d, "destination_key": "%s", "text": "memotext added for notification", "data": ""}`, bountyAmount, invoice.OwnerPubkey)

expectedV2PaymentBody :=
fmt.Sprintf(`{"amt_msat": %d, "dest": "%s", "route_hint": "%s", "wait": true}`, bountyAmount*1000, invoice.OwnerPubkey, invoiceData.RouteHint)
fmt.Sprintf(`{"amt_msat": %d, "dest": "%s", "route_hint": "%s", "data": "", "wait": true}`, bountyAmount*1000, invoice.OwnerPubkey, invoiceData.RouteHint)

r2 := io.NopCloser(bytes.NewReader([]byte(`{"success": true, "response": { "sumAmount": "1"}}`)))
r3 := io.NopCloser(bytes.NewReader([]byte(`{"status": "COMPLETE", "amt_msat": "", "timestamp": "" }`)))
Expand Down
2 changes: 1 addition & 1 deletion handlers/invoiceCron.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ func InitInvoiceCron() {

amount, _ := utils.ConvertStringToUint(inv.Amount)

bodyData := utils.BuildKeysendBodyData(amount, inv.User_pubkey, inv.Route_hint)
bodyData := utils.BuildKeysendBodyData(amount, inv.User_pubkey, inv.Route_hint, "")

jsonBody := []byte(bodyData)

Expand Down
13 changes: 6 additions & 7 deletions utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,25 +53,24 @@ func BuildSearchQuery(key string, term string) (string, string) {
return arg1, arg2
}

func BuildKeysendBodyData(amount uint, receiver_pubkey string, route_hint string) string {
func BuildKeysendBodyData(amount uint, receiver_pubkey string, route_hint string, memo string) string {
var bodyData string
memoText := "memotext added for notification"
if route_hint != "" {
bodyData = fmt.Sprintf(`{"amount": %d, "destination_key": "%s", "route_hint": "%s", "text": "%s"}`, amount, receiver_pubkey, route_hint, memoText)
bodyData = fmt.Sprintf(`{"amount": %d, "destination_key": "%s", "route_hint": "%s", "text": "%s", "data": "%s"}`, amount, receiver_pubkey, route_hint, memo, memo)
} else {
bodyData = fmt.Sprintf(`{"amount": %d, "destination_key": "%s", "text": "%s"}`, amount, receiver_pubkey, memoText)
bodyData = fmt.Sprintf(`{"amount": %d, "destination_key": "%s", "text": "%s", "data": "%s"}`, amount, receiver_pubkey, memo, memo)
}

return bodyData
}

func BuildV2KeysendBodyData(amount uint, receiver_pubkey string, route_hint string) string {
func BuildV2KeysendBodyData(amount uint, receiver_pubkey string, route_hint string, memo string) string {
amountMsat := amount * 1000
var bodyData string
if route_hint != "" {
bodyData = fmt.Sprintf(`{"amt_msat": %d, "dest": "%s", "route_hint": "%s", "wait": true}`, amountMsat, receiver_pubkey, route_hint)
bodyData = fmt.Sprintf(`{"amt_msat": %d, "dest": "%s", "route_hint": "%s", "data": "%s", "wait": true}`, amountMsat, receiver_pubkey, route_hint, memo)
} else {
bodyData = fmt.Sprintf(`{"amt_msat": %d, "dest": "%s", "route_hint": "", "wait": true}`, amountMsat, receiver_pubkey)
bodyData = fmt.Sprintf(`{"amt_msat": %d, "dest": "%s", "route_hint": "", , "data": "%s", "wait": true}`, amountMsat, receiver_pubkey, memo)
}

return bodyData
Expand Down

0 comments on commit b3bfd8c

Please sign in to comment.