-
Notifications
You must be signed in to change notification settings - Fork 31
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feat: lookup invoice (Alby + LND) #124
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
2b68664
feat: lookup invoice (Alby)
rolznz b9779ca
chore: update minimum amount error message
rolznz 820e3ef
Merge remote-tracking branch 'origin/main' into feat/lookup-invoice
rolznz 730caaa
feat: add lnd implementation of lookup_invoice
rolznz 364106e
fix: remove special handling for 404 response
rolznz 67cd682
chore: remove unnecessary else
rolznz 407eb71
chore: only use error from insert nostr event
rolznz b00ea20
chore: remove unused import
rolznz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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,115 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"fmt" | ||
|
||
"github.com/nbd-wtf/go-nostr" | ||
decodepay "github.com/nbd-wtf/ln-decodepay" | ||
"github.com/sirupsen/logrus" | ||
) | ||
|
||
func (svc *Service) HandleLookupInvoiceEvent(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 | ||
if err != nil { | ||
svc.Logger.WithFields(logrus.Fields{ | ||
"eventId": event.ID, | ||
"eventKind": event.Kind, | ||
"appId": app.ID, | ||
}).Errorf("Failed to save nostr event: %v", err) | ||
return nil, err | ||
} | ||
|
||
// TODO: move to a shared function | ||
hasPermission, code, message := svc.hasPermission(&app, event, request.Method, nil) | ||
|
||
if !hasPermission { | ||
svc.Logger.WithFields(logrus.Fields{ | ||
"eventId": event.ID, | ||
"eventKind": event.Kind, | ||
"appId": app.ID, | ||
}).Errorf("App does not have permission: %s %s", code, message) | ||
|
||
return svc.createResponse(event, Nip47Response{Error: &Nip47Error{ | ||
Code: code, | ||
Message: message, | ||
}}, ss) | ||
} | ||
|
||
// TODO: move to a shared generic function | ||
lookupInvoiceParams := &Nip47LookupInvoiceParams{} | ||
err = json.Unmarshal(request.Params, lookupInvoiceParams) | ||
if err != nil { | ||
svc.Logger.WithFields(logrus.Fields{ | ||
"eventId": event.ID, | ||
"eventKind": event.Kind, | ||
"appId": app.ID, | ||
}).Errorf("Failed to decode nostr event: %v", err) | ||
return nil, err | ||
} | ||
|
||
svc.Logger.WithFields(logrus.Fields{ | ||
"eventId": event.ID, | ||
"eventKind": event.Kind, | ||
"appId": app.ID, | ||
"invoice": lookupInvoiceParams.Invoice, | ||
"paymentHash": lookupInvoiceParams.PaymentHash, | ||
}).Info("Looking up invoice") | ||
|
||
paymentHash := lookupInvoiceParams.PaymentHash | ||
|
||
if (paymentHash == "") { | ||
paymentRequest, err := decodepay.Decodepay(lookupInvoiceParams.Invoice) | ||
if err != nil { | ||
svc.Logger.WithFields(logrus.Fields{ | ||
rolznz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
"eventId": event.ID, | ||
"eventKind": event.Kind, | ||
"appId": app.ID, | ||
"invoice": lookupInvoiceParams.Invoice, | ||
}).Errorf("Failed to decode bolt11 invoice: %v", err) | ||
|
||
return svc.createResponse(event, Nip47Response{ | ||
Error: &Nip47Error{ | ||
Code: NIP_47_ERROR_INTERNAL, | ||
Message: fmt.Sprintf("Failed to decode bolt11 invoice: %s", err.Error()), | ||
}, | ||
}, ss) | ||
} | ||
paymentHash = paymentRequest.PaymentHash | ||
} | ||
|
||
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, | ||
}).Infof("Failed to lookup invoice: %v", err) | ||
nostrEvent.State = NOSTR_EVENT_STATE_HANDLER_ERROR | ||
svc.db.Save(&nostrEvent) | ||
return svc.createResponse(event, Nip47Response{ | ||
Error: &Nip47Error{ | ||
Code: NIP_47_ERROR_INTERNAL, | ||
Message: fmt.Sprintf("Something went wrong while looking up invoice: %s", err.Error()), | ||
}, | ||
}, ss) | ||
} | ||
|
||
responsePayload := &Nip47LookupInvoiceResponse { | ||
Invoice: invoice, | ||
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, | ||
}, | ||
ss) | ||
} |
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
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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If I was the author of the NIP PR I would drop the payment_request parameter and mandate payment_hash. But not really related to this PR so OK.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we give feedback? this is the time to do it
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the user always has access to the payment hash, as long as the invoice was generated through NWC.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I asked in the PR.