Skip to content

Commit

Permalink
Merge pull request #177 from getAlby/go-fmt
Browse files Browse the repository at this point in the history
go fmt
  • Loading branch information
kiwiidb authored Nov 25, 2023
2 parents 5e60f6a + a54a1c8 commit 6814e1b
Show file tree
Hide file tree
Showing 8 changed files with 75 additions and 77 deletions.
44 changes: 22 additions & 22 deletions alby.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ func (svc *AlbyOAuthService) MakeInvoice(ctx context.Context, senderPubkey strin
"description": description,
"descriptionHash": descriptionHash,
"expiry": expiry,
}).Errorf("amount must be 1000 msat or greater");
}).Errorf("amount must be 1000 msat or greater")
return "", "", errors.New("amount must be 1000 msat or greater")
}

Expand Down Expand Up @@ -199,17 +199,17 @@ func (svc *AlbyOAuthService) LookupInvoice(ctx context.Context, senderPubkey str
}).Error
if err != nil {
svc.Logger.WithFields(logrus.Fields{
"senderPubkey": senderPubkey,
"paymentHash": paymentHash,
"senderPubkey": senderPubkey,
"paymentHash": paymentHash,
}).Errorf("App not found: %v", err)
return "", false, err
}

svc.Logger.WithFields(logrus.Fields{
"senderPubkey": senderPubkey,
"paymentHash": paymentHash,
"appId": app.ID,
"userId": app.User.ID,
"senderPubkey": senderPubkey,
"paymentHash": paymentHash,
"appId": app.ID,
"userId": app.User.ID,
}).Info("Processing lookup invoice request")
tok, err := svc.FetchUserToken(ctx, app)
if err != nil {
Expand All @@ -232,38 +232,38 @@ func (svc *AlbyOAuthService) LookupInvoice(ctx context.Context, senderPubkey str
resp, err := client.Do(req)
if err != nil {
svc.Logger.WithFields(logrus.Fields{
"senderPubkey": senderPubkey,
"paymentHash": paymentHash,
"appId": app.ID,
"userId": app.User.ID,
"senderPubkey": senderPubkey,
"paymentHash": paymentHash,
"appId": app.ID,
"userId": app.User.ID,
}).Errorf("Failed to lookup invoice: %v", err)
return "", false, err
}

if resp.StatusCode < 300 {
responsePayload := &LookupInvoiceResponse{}
err = json.NewDecoder(resp.Body).Decode(responsePayload)
if err != nil {
return "", false, err
}
svc.Logger.WithFields(logrus.Fields{
"senderPubkey": senderPubkey,
"paymentHash": paymentHash,
"appId": app.ID,
"userId": app.User.ID,
"paymentRequest": responsePayload.PaymentRequest,
"settled": responsePayload.Settled,
"senderPubkey": senderPubkey,
"paymentHash": paymentHash,
"appId": app.ID,
"userId": app.User.ID,
"paymentRequest": responsePayload.PaymentRequest,
"settled": responsePayload.Settled,
}).Info("Lookup invoice successful")
return responsePayload.PaymentRequest, responsePayload.Settled, nil
}

errorPayload := &ErrorResponse{}
err = json.NewDecoder(resp.Body).Decode(errorPayload)
svc.Logger.WithFields(logrus.Fields{
"senderPubkey": senderPubkey,
"paymentHash": paymentHash,
"appId": app.ID,
"userId": app.User.ID,
"senderPubkey": senderPubkey,
"paymentHash": paymentHash,
"appId": app.ID,
"userId": app.User.ID,
"APIHttpStatus": resp.StatusCode,
}).Errorf("Lookup invoice failed %s", string(errorPayload.Message))
return "", false, errors.New(errorPayload.Message)
Expand Down
16 changes: 8 additions & 8 deletions echo_handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -241,8 +241,8 @@ func getEndOfBudgetString(endOfBudget time.Time) (result string) {

func (svc *Service) AppsNewHandler(c echo.Context) error {
appName := c.QueryParam("name")
if (appName == "") {
// c - for client (deprecated)
if appName == "" {
// c - for client (deprecated)
appName = c.QueryParam("c")
}
pubkey := c.QueryParam("pubkey")
Expand Down Expand Up @@ -297,7 +297,7 @@ func (svc *Service) AppsNewHandler(c echo.Context) error {
for k, v := range nip47MethodDescriptions {
requestMethodHelper[k] = &RequestMethodHelper{
Description: v,
Icon: nip47MethodIcons[k],
Icon: nip47MethodIcons[k],
}
}

Expand Down Expand Up @@ -354,7 +354,7 @@ func (svc *Service) AppsCreateHandler(c echo.Context) error {
expiresAt := time.Time{}
if c.FormValue("ExpiresAt") != "" {
expiresAt, err = time.Parse(time.RFC3339, c.FormValue("ExpiresAt"))
if (err != nil) {
if err != nil {
return fmt.Errorf("Invalid ExpiresAt: %v", err)
}
}
Expand Down Expand Up @@ -405,16 +405,16 @@ func (svc *Service) AppsCreateHandler(c echo.Context) error {
return c.Redirect(302, "/apps")
}

publicRelayUrl := svc.cfg.PublicRelay;
if (publicRelayUrl == "") {
publicRelayUrl = svc.cfg.Relay;
publicRelayUrl := svc.cfg.PublicRelay
if publicRelayUrl == "" {
publicRelayUrl = svc.cfg.Relay
}

if c.FormValue("returnTo") != "" {
returnToUrl, err := url.Parse(c.FormValue("returnTo"))
if err == nil {
query := returnToUrl.Query()
query.Add("relay", publicRelayUrl);
query.Add("relay", publicRelayUrl)
query.Add("pubkey", svc.cfg.IdentityPubkey)
if user.LightningAddress != "" {
query.Add("lud16", user.LightningAddress)
Expand Down
6 changes: 3 additions & 3 deletions handle_balance_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ func (svc *Service) HandleGetBalanceEvent(ctx context.Context, request *Nip47Req
return svc.createResponse(event, Nip47Response{
ResultType: NIP_47_GET_BALANCE_METHOD,
Error: &Nip47Error{
Code: code,
Message: message,
}}, ss)
Code: code,
Message: message,
}}, ss)
}

svc.Logger.WithFields(logrus.Fields{
Expand Down
38 changes: 19 additions & 19 deletions handle_lookup_invoice_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@ func (svc *Service) HandleLookupInvoiceEvent(ctx context.Context, request *Nip47
return svc.createResponse(event, Nip47Response{
ResultType: NIP_47_LOOKUP_INVOICE_METHOD,
Error: &Nip47Error{
Code: code,
Message: message,
}}, ss)
Code: code,
Message: message,
}}, ss)
}

// TODO: move to a shared generic function
Expand All @@ -54,23 +54,23 @@ func (svc *Service) HandleLookupInvoiceEvent(ctx context.Context, request *Nip47
}

svc.Logger.WithFields(logrus.Fields{
"eventId": event.ID,
"eventKind": event.Kind,
"appId": app.ID,
"invoice": lookupInvoiceParams.Invoice,
"paymentHash": lookupInvoiceParams.PaymentHash,
"eventId": event.ID,
"eventKind": event.Kind,
"appId": app.ID,
"invoice": lookupInvoiceParams.Invoice,
"paymentHash": lookupInvoiceParams.PaymentHash,
}).Info("Looking up invoice")

paymentHash := lookupInvoiceParams.PaymentHash

if (paymentHash == "") {
if paymentHash == "" {
paymentRequest, err := decodepay.Decodepay(lookupInvoiceParams.Invoice)
if err != nil {
svc.Logger.WithFields(logrus.Fields{
"eventId": event.ID,
"eventKind": event.Kind,
"appId": app.ID,
"invoice": lookupInvoiceParams.Invoice,
"invoice": lookupInvoiceParams.Invoice,
}).Errorf("Failed to decode bolt11 invoice: %v", err)

return svc.createResponse(event, Nip47Response{
Expand All @@ -87,11 +87,11 @@ func (svc *Service) HandleLookupInvoiceEvent(ctx context.Context, request *Nip47
invoice, paid, err := svc.lnClient.LookupInvoice(ctx, event.PubKey, paymentHash)
if err != nil {
svc.Logger.WithFields(logrus.Fields{
"eventId": event.ID,
"eventKind": event.Kind,
"appId": app.ID,
"invoice": lookupInvoiceParams.Invoice,
"paymentHash": lookupInvoiceParams.PaymentHash,
"eventId": event.ID,
"eventKind": event.Kind,
"appId": app.ID,
"invoice": lookupInvoiceParams.Invoice,
"paymentHash": lookupInvoiceParams.PaymentHash,
}).Infof("Failed to lookup invoice: %v", err)
nostrEvent.State = NOSTR_EVENT_STATE_HANDLER_ERROR
svc.db.Save(&nostrEvent)
Expand All @@ -104,16 +104,16 @@ func (svc *Service) HandleLookupInvoiceEvent(ctx context.Context, request *Nip47
}, ss)
}

responsePayload := &Nip47LookupInvoiceResponse {
responsePayload := &Nip47LookupInvoiceResponse{
Invoice: invoice,
Paid: paid,
Paid: paid,
}

nostrEvent.State = NOSTR_EVENT_STATE_HANDLER_EXECUTED
svc.db.Save(&nostrEvent)
return svc.createResponse(event, Nip47Response{
ResultType: NIP_47_LOOKUP_INVOICE_METHOD,
Result: responsePayload,
Result: responsePayload,
},
ss)
ss)
}
16 changes: 7 additions & 9 deletions handle_make_invoice_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
)

func (svc *Service) HandleMakeInvoiceEvent(ctx context.Context, request *Nip47Request, event *nostr.Event, app App, ss []byte) (result *nostr.Event, err error) {

// TODO: move to a shared function
nostrEvent := NostrEvent{App: app, NostrId: event.ID, Content: event.Content, State: "received"}
err = svc.db.Create(&nostrEvent).Error
Expand All @@ -38,9 +38,9 @@ func (svc *Service) HandleMakeInvoiceEvent(ctx context.Context, request *Nip47Re
return svc.createResponse(event, Nip47Response{
ResultType: NIP_47_MAKE_INVOICE_METHOD,
Error: &Nip47Error{
Code: code,
Message: message,
}}, ss)
Code: code,
Message: message,
}}, ss)
}

// TODO: move to a shared generic function
Expand Down Expand Up @@ -81,8 +81,6 @@ 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 All @@ -106,15 +104,15 @@ func (svc *Service) HandleMakeInvoiceEvent(ctx context.Context, request *Nip47Re
}

responsePayload := &Nip47MakeInvoiceResponse{
Invoice: invoice,
Invoice: invoice,
PaymentHash: paymentHash,
}

nostrEvent.State = NOSTR_EVENT_STATE_HANDLER_EXECUTED
svc.db.Save(&nostrEvent)
return svc.createResponse(event, Nip47Response{
ResultType: NIP_47_MAKE_INVOICE_METHOD,
Result: responsePayload,
Result: responsePayload,
},
ss)
ss)
}
6 changes: 3 additions & 3 deletions handle_payment_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,9 @@ func (svc *Service) HandlePayInvoiceEvent(ctx context.Context, request *Nip47Req
return svc.createResponse(event, Nip47Response{
ResultType: NIP_47_PAY_INVOICE_METHOD,
Error: &Nip47Error{
Code: code,
Message: message,
}}, ss)
Code: code,
Message: message,
}}, ss)
}

payment := Payment{App: app, NostrEvent: nostrEvent, PaymentRequest: bolt11, Amount: uint(paymentRequest.MSatoshi / 1000)}
Expand Down
10 changes: 5 additions & 5 deletions lnd.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ 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) {
var descriptionHashBytes []byte

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

Expand All @@ -68,7 +68,7 @@ func (svc *LNDService) MakeInvoice(ctx context.Context, senderPubkey string, amo
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
Expand All @@ -87,12 +87,12 @@ func (svc *LNDService) LookupInvoice(ctx context.Context, senderPubkey string, p
return "", false, errors.New("Payment hash must be 32 bytes hex")
}

lndInvoice, err := svc.client.LookupInvoice(ctx, &lnrpc.PaymentHash{ RHash: paymentHashBytes })
lndInvoice, err := svc.client.LookupInvoice(ctx, &lnrpc.PaymentHash{RHash: paymentHashBytes})
if err != nil {
return "", false, err
}
return lndInvoice.PaymentRequest, lndInvoice.State == *lnrpc.Invoice_SETTLED.Enum(), nil;

return lndInvoice.PaymentRequest, lndInvoice.State == *lnrpc.Invoice_SETTLED.Enum(), nil
}

func (svc *LNDService) SendPaymentSync(ctx context.Context, senderPubkey, payReq string) (preimage string, err error) {
Expand Down
16 changes: 8 additions & 8 deletions service.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ 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,
NIP_47_PAY_INVOICE_METHOD: true,
NIP_47_GET_BALANCE_METHOD: true,
NIP_47_MAKE_INVOICE_METHOD: true,
NIP_47_LOOKUP_INVOICE_METHOD: true,
}

Expand Down Expand Up @@ -69,8 +69,8 @@ func (svc *Service) StartSubscription(ctx context.Context, sub *nostr.Subscripti
resp, err := svc.HandleEvent(ctx, event)
if err != nil {
svc.Logger.WithFields(logrus.Fields{
"eventId": event.ID,
"eventKind": event.Kind,
"eventId": event.ID,
"eventKind": event.Kind,
}).Errorf("Failed to process event: %v", err)
}
if resp != nil {
Expand Down Expand Up @@ -201,9 +201,9 @@ func (svc *Service) HandleEvent(ctx context.Context, event *nostr.Event) (result
return svc.createResponse(event, Nip47Response{
ResultType: nip47Request.Method,
Error: &Nip47Error{
Code: NIP_47_ERROR_NOT_IMPLEMENTED,
Message: fmt.Sprintf("Unknown method: %s", nip47Request.Method),
}}, ss)
Code: NIP_47_ERROR_NOT_IMPLEMENTED,
Message: fmt.Sprintf("Unknown method: %s", nip47Request.Method),
}}, ss)
}
}

Expand Down

0 comments on commit 6814e1b

Please sign in to comment.