Skip to content

Commit

Permalink
Merge pull request #123 from getAlby/feat/make-invoice-lnd
Browse files Browse the repository at this point in the history
feat: lnd implementation of MakeInvoice
  • Loading branch information
kiwiidb authored Aug 22, 2023
2 parents 65f275d + ace48ac commit 42a4112
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 4 deletions.
17 changes: 16 additions & 1 deletion alby.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,21 @@ func (svc *AlbyOAuthService) MakeInvoice(ctx context.Context, senderPubkey strin
}).Errorf("App not found: %v", err)
return "", "", err
}

// amount provided in msat, but Alby API currently only supports sats. Will get truncated to a whole sat value
var amountSat int64 = amount / 1000
// make sure amount is not converted to 0
if (amount > 0 && amountSat == 0) {
svc.Logger.WithFields(logrus.Fields{
"senderPubkey": senderPubkey,
"amount": amount,
"description": description,
"descriptionHash": descriptionHash,
"expiry": expiry,
}).Errorf("Value must be 1 sat or greater");
return "", "", errors.New("Value must be 1 sat or greater")
}

svc.Logger.WithFields(logrus.Fields{
"senderPubkey": senderPubkey,
"amount": amount,
Expand All @@ -110,7 +125,7 @@ func (svc *AlbyOAuthService) MakeInvoice(ctx context.Context, senderPubkey strin

body := bytes.NewBuffer([]byte{})
payload := &MakeInvoiceRequest{
Amount: amount,
Amount: amountSat,
Description: description,
DescriptionHash: descriptionHash,
// TODO: support expiry
Expand Down
19 changes: 18 additions & 1 deletion handle_make_invoice_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,21 @@ func (svc *Service) HandleMakeInvoiceEvent(ctx context.Context, request *Nip47Re
return nil, err
}

if makeInvoiceParams.Description != "" && makeInvoiceParams.DescriptionHash != "" {
svc.Logger.WithFields(logrus.Fields{
"eventId": event.ID,
"eventKind": event.Kind,
"appId": app.ID,
}).Errorf("Only one of description, description_hash can be provided")

return svc.createResponse(event, Nip47Response{
Error: &Nip47Error{
Code: NIP_47_OTHER,
Message: "Only one of description, description_hash can be provided",
},
}, ss)
}

svc.Logger.WithFields(logrus.Fields{
"eventId": event.ID,
"eventKind": event.Kind,
Expand All @@ -63,6 +78,8 @@ func (svc *Service) HandleMakeInvoiceEvent(ctx context.Context, request *Nip47Re
"expiry": makeInvoiceParams.Expiry,
}).Info("Making invoice")



invoice, paymentHash, err := svc.lnClient.MakeInvoice(ctx, event.PubKey, makeInvoiceParams.Amount, makeInvoiceParams.Description, makeInvoiceParams.DescriptionHash, makeInvoiceParams.Expiry)
if err != nil {
svc.Logger.WithFields(logrus.Fields{
Expand Down Expand Up @@ -92,7 +109,7 @@ func (svc *Service) HandleMakeInvoiceEvent(ctx context.Context, request *Nip47Re
nostrEvent.State = NOSTR_EVENT_STATE_HANDLER_EXECUTED
svc.db.Save(&nostrEvent)
return svc.createResponse(event, Nip47Response{
ResultType: NIP_47_GET_BALANCE_METHOD,
ResultType: NIP_47_MAKE_INVOICE_METHOD,
Result: responsePayload,
},
ss)
Expand Down
25 changes: 24 additions & 1 deletion lnd.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"context"
"encoding/hex"
"errors"
"fmt"

"github.com/sirupsen/logrus"
Expand Down Expand Up @@ -46,7 +47,29 @@ func (svc *LNDService) GetBalance(ctx context.Context, senderPubkey string) (bal
}

func (svc *LNDService) MakeInvoice(ctx context.Context, senderPubkey string, amount int64, description string, descriptionHash string, expiry int64) (invoice string, paymentHash string, err error) {
return "", "", fmt.Errorf("not implemented")
var descriptionHashBytes []byte

if descriptionHash != "" {
descriptionHashBytes, err = hex.DecodeString(descriptionHash)

if err != nil || len(descriptionHashBytes) != 32 {
svc.Logger.WithFields(logrus.Fields{
"senderPubkey": senderPubkey,
"amount": amount,
"description": description,
"descriptionHash": descriptionHash,
"expiry": expiry,
}).Errorf("Invalid description hash")
return "", "", errors.New("Description hash must be 32 bytes hex")
}
}

resp, err := svc.client.AddInvoice(ctx, &lnrpc.Invoice{ValueMsat: amount, Memo: description, DescriptionHash: descriptionHashBytes, Expiry: expiry})
if err != nil {
return "", "", err
}

return resp.GetPaymentRequest(), hex.EncodeToString(resp.GetRHash()), nil
}

func (svc *LNDService) SendPaymentSync(ctx context.Context, senderPubkey, payReq string) (preimage string, err error) {
Expand Down
1 change: 1 addition & 0 deletions models.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ const (
NIP_47_ERROR_UNAUTHORIZED = "UNAUTHORIZED"
NIP_47_ERROR_EXPIRED = "EXPIRED"
NIP_47_ERROR_RESTRICTED = "RESTRICTED"
NIP_47_OTHER = "OTHER"
NIP_47_CAPABILITIES = "pay_invoice,get_balance"
)

Expand Down
1 change: 1 addition & 0 deletions service.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ type Service struct {
var supportedMethods = map[string]bool{
NIP_47_PAY_INVOICE_METHOD: true,
NIP_47_GET_BALANCE_METHOD: true,
NIP_47_MAKE_INVOICE_METHOD: true,
}

func (svc *Service) GetUser(c echo.Context) (user *User, err error) {
Expand Down
1 change: 0 additions & 1 deletion service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ const nip47MakeInvoiceJson = `
"params": {
"amount": 1000,
"description": "[[\"text/identifier\",\"hello@getalby.com\"],[\"text/plain\",\"Sats for Alby\"]]",
"description_hash": "7da78494568d0c8afcd972ea5f5464d6f8d222025c04704e3d19c338ed61cd1f",
"expiry": 3600
}
}
Expand Down

0 comments on commit 42a4112

Please sign in to comment.