-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #127 from getAlby/keysend-implementation
Keysend implementation
- Loading branch information
Showing
10 changed files
with
339 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
package controllers | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
|
||
"github.com/getAlby/lndhub.go/lib" | ||
"github.com/getAlby/lndhub.go/lib/responses" | ||
"github.com/getAlby/lndhub.go/lib/service" | ||
"github.com/getAlby/lndhub.go/lnd" | ||
"github.com/getsentry/sentry-go" | ||
"github.com/labstack/echo/v4" | ||
"github.com/lightningnetwork/lnd/lnrpc" | ||
) | ||
|
||
// KeySendController : Key send controller struct | ||
type KeySendController struct { | ||
svc *service.LndhubService | ||
} | ||
|
||
func NewKeySendController(svc *service.LndhubService) *KeySendController { | ||
return &KeySendController{svc: svc} | ||
} | ||
|
||
type KeySendRequestBody struct { | ||
Amount int64 `json:"amount" validate:"required"` | ||
Destination string `json:"destination" validate:"required"` | ||
Memo string `json:"memo" validate:"omitempty"` | ||
} | ||
|
||
type KeySendResponseBody struct { | ||
RHash *lib.JavaScriptBuffer `json:"payment_hash,omitempty"` | ||
Amount int64 `json:"num_satoshis,omitempty"` | ||
Description string `json:"description,omitempty"` | ||
Destination string `json:"destination,omitempty"` | ||
DescriptionHashStr string `json:"description_hash,omitempty"` | ||
PaymentError string `json:"payment_error,omitempty"` | ||
PaymentPreimage *lib.JavaScriptBuffer `json:"payment_preimage,omitempty"` | ||
PaymentRoute *service.Route `json:"route,omitempty"` | ||
} | ||
|
||
// KeySend : Key send Controller | ||
func (controller *KeySendController) KeySend(c echo.Context) error { | ||
userID := c.Get("UserID").(int64) | ||
reqBody := KeySendRequestBody{} | ||
if err := c.Bind(&reqBody); err != nil { | ||
c.Logger().Errorf("Failed to load keysend request body: %v", err) | ||
return c.JSON(http.StatusBadRequest, responses.BadArgumentsError) | ||
} | ||
|
||
if err := c.Validate(&reqBody); err != nil { | ||
c.Logger().Errorf("Invalid keysend request body: %v", err) | ||
return c.JSON(http.StatusBadRequest, responses.BadArgumentsError) | ||
} | ||
|
||
lnPayReq := &lnd.LNPayReq{ | ||
PayReq: &lnrpc.PayReq{ | ||
Destination: reqBody.Destination, | ||
NumSatoshis: reqBody.Amount, | ||
Description: reqBody.Memo, | ||
}, | ||
Keysend: true, | ||
} | ||
|
||
invoice, err := controller.svc.AddOutgoingInvoice(c.Request().Context(), userID, "", lnPayReq) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
currentBalance, err := controller.svc.CurrentUserBalance(c.Request().Context(), userID) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if currentBalance < invoice.Amount { | ||
c.Logger().Errorf("User does not have enough balance invoice_id=%v user_id=%v balance=%v amount=%v", invoice.ID, userID, currentBalance, invoice.Amount) | ||
return c.JSON(http.StatusBadRequest, responses.NotEnoughBalanceError) | ||
} | ||
|
||
sendPaymentResponse, err := controller.svc.PayInvoice(c.Request().Context(), invoice) | ||
if err != nil { | ||
c.Logger().Errorf("Payment failed: %v", err) | ||
sentry.CaptureException(err) | ||
return c.JSON(http.StatusBadRequest, echo.Map{ | ||
"error": true, | ||
"code": 10, | ||
"message": fmt.Sprintf("Payment failed. Does the receiver have enough inbound capacity? (%v)", err), | ||
}) | ||
} | ||
|
||
responseBody := &KeySendResponseBody{} | ||
responseBody.RHash = &lib.JavaScriptBuffer{Data: sendPaymentResponse.PaymentHash} | ||
responseBody.Amount = invoice.Amount | ||
responseBody.Destination = invoice.DestinationPubkeyHex | ||
responseBody.Description = invoice.Memo | ||
responseBody.DescriptionHashStr = invoice.DescriptionHash | ||
responseBody.PaymentError = sendPaymentResponse.PaymentError | ||
responseBody.PaymentPreimage = &lib.JavaScriptBuffer{Data: sendPaymentResponse.PaymentPreimage} | ||
responseBody.PaymentRoute = sendPaymentResponse.PaymentRoute | ||
|
||
return c.JSON(http.StatusOK, responseBody) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
alter table invoices add column keysend boolean; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
package integration_tests | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"log" | ||
"testing" | ||
"time" | ||
|
||
"github.com/getAlby/lndhub.go/controllers" | ||
"github.com/getAlby/lndhub.go/lib" | ||
"github.com/getAlby/lndhub.go/lib/responses" | ||
"github.com/getAlby/lndhub.go/lib/service" | ||
"github.com/getAlby/lndhub.go/lib/tokens" | ||
"github.com/getAlby/lndhub.go/lnd" | ||
"github.com/go-playground/validator/v10" | ||
"github.com/labstack/echo/v4" | ||
"github.com/lightningnetwork/lnd/lnrpc" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/suite" | ||
) | ||
|
||
type KeySendTestSuite struct { | ||
TestSuite | ||
fundingClient *lnd.LNDWrapper | ||
service *service.LndhubService | ||
aliceLogin controllers.CreateUserResponseBody | ||
aliceToken string | ||
invoiceUpdateSubCancelFn context.CancelFunc | ||
} | ||
|
||
func (suite *KeySendTestSuite) SetupSuite() { | ||
lndClient, err := lnd.NewLNDclient(lnd.LNDoptions{ | ||
Address: lnd2RegtestAddress, | ||
MacaroonHex: lnd2RegtestMacaroonHex, | ||
}) | ||
if err != nil { | ||
log.Fatalf("Error setting up funding client: %v", err) | ||
} | ||
suite.fundingClient = lndClient | ||
|
||
svc, err := LndHubTestServiceInit(nil) | ||
if err != nil { | ||
log.Fatalf("Error initializing test service: %v", err) | ||
} | ||
users, userTokens, err := createUsers(svc, 1) | ||
if err != nil { | ||
log.Fatalf("Error creating test users: %v", err) | ||
} | ||
// Subscribe to LND invoice updates in the background | ||
// store cancel func to be called in tear down suite | ||
ctx, cancel := context.WithCancel(context.Background()) | ||
suite.invoiceUpdateSubCancelFn = cancel | ||
go svc.InvoiceUpdateSubscription(ctx) | ||
suite.service = svc | ||
e := echo.New() | ||
|
||
e.HTTPErrorHandler = responses.HTTPErrorHandler | ||
e.Validator = &lib.CustomValidator{Validator: validator.New()} | ||
suite.echo = e | ||
assert.Equal(suite.T(), 1, len(users)) | ||
assert.Equal(suite.T(), 1, len(userTokens)) | ||
suite.aliceLogin = users[0] | ||
suite.aliceToken = userTokens[0] | ||
suite.echo.Use(tokens.Middleware([]byte(suite.service.Config.JWTSecret))) | ||
suite.echo.GET("/balance", controllers.NewBalanceController(suite.service).Balance) | ||
suite.echo.POST("/addinvoice", controllers.NewAddInvoiceController(suite.service).AddInvoice) | ||
suite.echo.POST("/payinvoice", controllers.NewPayInvoiceController(suite.service).PayInvoice) | ||
suite.echo.POST("/keysend", controllers.NewKeySendController(suite.service).KeySend) | ||
} | ||
|
||
func (suite *KeySendTestSuite) TearDownSuite() { | ||
suite.invoiceUpdateSubCancelFn() | ||
} | ||
|
||
func (suite *KeySendTestSuite) TestKeysendPayment() { | ||
aliceFundingSats := 1000 | ||
externalSatRequested := 500 | ||
//fund alice account | ||
invoiceResponse := suite.createAddInvoiceReq(aliceFundingSats, "integration test external payment alice", suite.aliceToken) | ||
sendPaymentRequest := lnrpc.SendRequest{ | ||
PaymentRequest: invoiceResponse.PayReq, | ||
FeeLimit: nil, | ||
} | ||
_, err := suite.fundingClient.SendPaymentSync(context.Background(), &sendPaymentRequest) | ||
assert.NoError(suite.T(), err) | ||
|
||
//wait a bit for the callback event to hit | ||
time.Sleep(100 * time.Millisecond) | ||
|
||
suite.createKeySendReq(int64(externalSatRequested), "key send test", simnetLnd3PubKey, suite.aliceToken) | ||
|
||
// check that balance was reduced | ||
userId := getUserIdFromToken(suite.aliceToken) | ||
aliceBalance, err := suite.service.CurrentUserBalance(context.Background(), userId) | ||
if err != nil { | ||
fmt.Printf("Error when getting balance %v\n", err.Error()) | ||
} | ||
assert.Equal(suite.T(), int64(aliceFundingSats)-int64(externalSatRequested), aliceBalance) | ||
} | ||
|
||
func (suite *KeySendTestSuite) TestKeysendPaymentNonExistentDestination() { | ||
aliceFundingSats := 1000 | ||
externalSatRequested := 500 | ||
//fund alice account | ||
invoiceResponse := suite.createAddInvoiceReq(aliceFundingSats, "integration test external payment alice", suite.aliceToken) | ||
sendPaymentRequest := lnrpc.SendRequest{ | ||
PaymentRequest: invoiceResponse.PayReq, | ||
FeeLimit: nil, | ||
} | ||
_, err := suite.fundingClient.SendPaymentSync(context.Background(), &sendPaymentRequest) | ||
assert.NoError(suite.T(), err) | ||
|
||
//wait a bit for the callback event to hit | ||
time.Sleep(100 * time.Millisecond) | ||
|
||
suite.createKeySendReqError(int64(externalSatRequested), "key send test", "12345", suite.aliceToken) | ||
} | ||
|
||
func TestKeySendTestSuite(t *testing.T) { | ||
suite.Run(t, new(KeySendTestSuite)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.