alby-hub/nip47/controllers/make_invoice_controller.go

71 lines
2.3 KiB
Go
Raw Normal View History

2024-06-17 19:42:09 +07:00
package controllers
import (
"context"
"github.com/getAlby/go-nostr"
2024-07-05 20:32:40 +07:00
"github.com/getAlby/hub/logger"
"github.com/getAlby/hub/nip47/models"
2024-06-17 19:42:09 +07:00
"github.com/sirupsen/logrus"
)
type makeInvoiceParams struct {
feat: just in time channels with lsps2 (#2275) * feat: just in time channels with lsps2 * fix: clarify JIT receive channel fee * fix: fees * fix: fees 2 * fix: don't show low inbound when LSPS2 is active * fix: remove the receive limit below the input if LSPS2 is being used * fix: simplify * fix: bring back fee % for outgoing * fix: remove unneeded changes * fix: typo * fix: unneeded * fix: don't show open first channel is LSPS2 * feat: clearer JIT channel fee copy on receive screen * fix: add LSPS2 var info * fix: don't duplicate JIT fee hint on create invoice form * fix: make paymentDone a standard boolean * fix: update to golang:1.26 in Dockerfile * feat: read LSPS2 sources from channel suggestions, set minimum receive amount, update guide link * docs: update LDK_LSPS2_ADDRESSES to be used as an override * fix: only show minimum jit receive amount on validation error * fix: add more detail to receive error when receiving low amounts with jit * fix: do not use JIT when user has public channels * feat: add option to disable JIT * fix: isTrusted check, add jit property to event * fix: do not require node restart for toggling JIT * chore: simplify JIT alert * chore: add guide link on node settings JIT description * feat: fetch the lsp2info to have access to params like minimum/maximum payment size * refactor: share single learn-more link across JIT fee hint branches * fix: remove variable amount invoice support * fix: use lsps2info for min payment size and remove channelPeerSuggestion usage of minimumChannelSize * fix: only do amount validation according to lsps2Info values if jit is enabled in settings * feat: add jit first payment fee alert on receive via lightning address * fix: remove unnecessary conditional * fix: ensure at least one sat is left over when opening JIT channel * chore: remove hardcoded suggestions * chore: rename JIT enabled config variable * fix: ui checks when JIT is disabled * fix: amount input validation message * fix: formatting --------- Co-authored-by: anon <anon@anon.com> Co-authored-by: saunter <68239231+stackingsaunter@users.noreply.github.com> Co-authored-by: fmar <fmar@fmar> Co-authored-by: René Aaron <rene@twentyuno.net> Co-authored-by: Roland Bewick <roland.bewick@gmail.com>
2026-06-10 09:47:07 +02:00
Amount uint64 `json:"amount"` // msats (NIP-47)
Description string `json:"description"`
DescriptionHash string `json:"description_hash"`
Expiry uint64 `json:"expiry"`
Metadata map[string]interface{} `json:"metadata,omitempty"`
2024-06-17 19:42:09 +07:00
}
type makeInvoiceResponse struct {
models.Transaction
}
Feat: dynamic budgets (#226) * feat: add transactions table * feat: add transactions service with makeinvoice method * feat: use internal transactions WIP * feat: check unsettled transactions * feat: use transactions service in nip47 package WIP * feat: use transactions service in NIP-47 handlers * fix: transaction list colors * fix: tests * chore: remove old payments table * fix: return budget usage as sats * feat: use internal transactions table for keysend payments * feat: consume nwc_payment_received event in transaction service (WIP) * feat: update existing transaction from nwc_payment_received event in transactions service * feat: update existing transaction from nwc_payment_sent event in transactions service * feat: handle async payment failed events in transactions service * fix: tests * feat: correctly implement NIP-47 NOT_FOUND error code * feat: intercept self payments * feat: isolated balance and visibility * feat: validate keysend payment does not exceed app internal balance * feat: budget check in transactions service, correctly pass payment errors to NIP-47 response also reduce query duplication * fix: order transactions when looking up transaction * feat: add fee reserves to unsettled outgoing transactions * chore: rename app permissions max amount field * chore: rename transaction amount values to be clearly millisats * chore: merge balance type and visibility into isolated property on apps table * chore: move duplicated permission check from nip-47 controllers to handler * fix: app name in transaction list * Feat: permissions revamp v2 (#273) * feat: revamp permissions component * chore: changes * chore: changes * chore: add view mode for show app screen * chore: further changes * feat: new illustration for linking account (#254) * feat: new illustration for linking account * fix: update paths * fix: icon props (#256) * fix: use date from frontend * chore: add expiryselect component * chore: further changes * chore: further changes * chore: add date-fns for date picker * chore: further changes * chore: further changes * fix: add central LDK gossip node to help gossip new public channels (#262) * fix: make dialog responsive (#258) * chore: further changes * typo * chore: spacing issues * chore: use scopes from capabilities * chore: change scope type descriptions * chore: styling fixes * chore: fix typings * chore: budget renewal component * fix: LDK mark channel as inactive and show error if counterparty forwarding info missing (#267) fix: mark channel as inactive and show error if counterparty forwarding info missing * chore: remove unnecessary dark classes (#255) * fix: links to open first channel in sidebar and onboarding checklist (#268) * feat: improve migrate node UI (#269) * fix: migrate node copy (#270) * fix: stop nostr when app is shutdown and use context to stop lnclient (#271) * fix: permissions revamp WIP * feat: basic isolated apps UI * fix: new app connection, edit app connection, deep linking --------- Co-authored-by: im-adithya <imadithyavardhan@gmail.com> Co-authored-by: René Aaron <100827540+reneaaron@users.noreply.github.com> Co-authored-by: Michael Bumann <hello@michaelbumann.com> * fix: make transactions table ID autoincrement * fix: do not send unrelated notifications to isolated apps * fix: nip-47 notifications not receiving updated transaction state * fix: migrate existing tables to have autoincrementing primary keys (#274) * fix: migrate existing tables to have autoincrementing primary keys * fix: recreate request and response event tables after apps * fix: remove null from request_events app_id * fix: do not crash when reloading app created page * chore: update pragma commands, add busy_timeout * fix: remove unnecessary migration of user_configs table * chore: update comment on autoincrement migration * feat: sqlite database config improvements * fix: autoincrement migration to delete unlinked app permissions * chore: update tests for dynamic budgets (WIP) * fix: get balance tests * chore: add extra event handler tests * chore: add extra event handler test * chore: add extra multi_pay_invoice tests * chore: transactions service tests (WIP) * chore: add transactions service payment tests * chore: add tests for self payments * fix: notifications tests * chore: add notifications tests for transactions service * chore: add fee reserve tests for transactions service * chore: add list transactions tests * chore: add keysend tests for transactions service * feat: subscribe for payments and invoices (#281) * fix: db transaction should be passed as pointer * feat(lnd): subscribe for payments and invoices * chore: rearrange check for settled * chore: remove TODO * chore: retry on error and add select * chore: publish payment failed event * chore: use json logging * feat: add lnd notification types * chore: remove sleep * fix: incorrect key on transaction list items * fix: test * fix: disable isolated app type on non-supported backends --------- Co-authored-by: im-adithya <imadithyavardhan@gmail.com> Co-authored-by: René Aaron <100827540+reneaaron@users.noreply.github.com> Co-authored-by: Michael Bumann <hello@michaelbumann.com>
2024-07-19 23:30:22 +07:00
func (controller *nip47Controller) HandleMakeInvoiceEvent(ctx context.Context, nip47Request *models.Request, requestEventId uint, appId uint, publishResponse publishFunc) {
2024-06-17 19:42:09 +07:00
makeInvoiceParams := &makeInvoiceParams{}
Feat: dynamic budgets (#226) * feat: add transactions table * feat: add transactions service with makeinvoice method * feat: use internal transactions WIP * feat: check unsettled transactions * feat: use transactions service in nip47 package WIP * feat: use transactions service in NIP-47 handlers * fix: transaction list colors * fix: tests * chore: remove old payments table * fix: return budget usage as sats * feat: use internal transactions table for keysend payments * feat: consume nwc_payment_received event in transaction service (WIP) * feat: update existing transaction from nwc_payment_received event in transactions service * feat: update existing transaction from nwc_payment_sent event in transactions service * feat: handle async payment failed events in transactions service * fix: tests * feat: correctly implement NIP-47 NOT_FOUND error code * feat: intercept self payments * feat: isolated balance and visibility * feat: validate keysend payment does not exceed app internal balance * feat: budget check in transactions service, correctly pass payment errors to NIP-47 response also reduce query duplication * fix: order transactions when looking up transaction * feat: add fee reserves to unsettled outgoing transactions * chore: rename app permissions max amount field * chore: rename transaction amount values to be clearly millisats * chore: merge balance type and visibility into isolated property on apps table * chore: move duplicated permission check from nip-47 controllers to handler * fix: app name in transaction list * Feat: permissions revamp v2 (#273) * feat: revamp permissions component * chore: changes * chore: changes * chore: add view mode for show app screen * chore: further changes * feat: new illustration for linking account (#254) * feat: new illustration for linking account * fix: update paths * fix: icon props (#256) * fix: use date from frontend * chore: add expiryselect component * chore: further changes * chore: further changes * chore: add date-fns for date picker * chore: further changes * chore: further changes * fix: add central LDK gossip node to help gossip new public channels (#262) * fix: make dialog responsive (#258) * chore: further changes * typo * chore: spacing issues * chore: use scopes from capabilities * chore: change scope type descriptions * chore: styling fixes * chore: fix typings * chore: budget renewal component * fix: LDK mark channel as inactive and show error if counterparty forwarding info missing (#267) fix: mark channel as inactive and show error if counterparty forwarding info missing * chore: remove unnecessary dark classes (#255) * fix: links to open first channel in sidebar and onboarding checklist (#268) * feat: improve migrate node UI (#269) * fix: migrate node copy (#270) * fix: stop nostr when app is shutdown and use context to stop lnclient (#271) * fix: permissions revamp WIP * feat: basic isolated apps UI * fix: new app connection, edit app connection, deep linking --------- Co-authored-by: im-adithya <imadithyavardhan@gmail.com> Co-authored-by: René Aaron <100827540+reneaaron@users.noreply.github.com> Co-authored-by: Michael Bumann <hello@michaelbumann.com> * fix: make transactions table ID autoincrement * fix: do not send unrelated notifications to isolated apps * fix: nip-47 notifications not receiving updated transaction state * fix: migrate existing tables to have autoincrementing primary keys (#274) * fix: migrate existing tables to have autoincrementing primary keys * fix: recreate request and response event tables after apps * fix: remove null from request_events app_id * fix: do not crash when reloading app created page * chore: update pragma commands, add busy_timeout * fix: remove unnecessary migration of user_configs table * chore: update comment on autoincrement migration * feat: sqlite database config improvements * fix: autoincrement migration to delete unlinked app permissions * chore: update tests for dynamic budgets (WIP) * fix: get balance tests * chore: add extra event handler tests * chore: add extra event handler test * chore: add extra multi_pay_invoice tests * chore: transactions service tests (WIP) * chore: add transactions service payment tests * chore: add tests for self payments * fix: notifications tests * chore: add notifications tests for transactions service * chore: add fee reserve tests for transactions service * chore: add list transactions tests * chore: add keysend tests for transactions service * feat: subscribe for payments and invoices (#281) * fix: db transaction should be passed as pointer * feat(lnd): subscribe for payments and invoices * chore: rearrange check for settled * chore: remove TODO * chore: retry on error and add select * chore: publish payment failed event * chore: use json logging * feat: add lnd notification types * chore: remove sleep * fix: incorrect key on transaction list items * fix: test * fix: disable isolated app type on non-supported backends --------- Co-authored-by: im-adithya <imadithyavardhan@gmail.com> Co-authored-by: René Aaron <100827540+reneaaron@users.noreply.github.com> Co-authored-by: Michael Bumann <hello@michaelbumann.com>
2024-07-19 23:30:22 +07:00
resp := decodeRequest(nip47Request, makeInvoiceParams)
2024-06-17 19:42:09 +07:00
if resp != nil {
publishResponse(resp, nostr.Tags{})
return
}
logger.Logger.WithFields(logrus.Fields{
"app_id": appId,
2024-06-17 19:42:09 +07:00
"request_event_id": requestEventId,
"amount": makeInvoiceParams.Amount,
"description": makeInvoiceParams.Description,
"description_hash": makeInvoiceParams.DescriptionHash,
"expiry": makeInvoiceParams.Expiry,
"metadata": makeInvoiceParams.Metadata,
}).Debug("Handling make_invoice request")
2024-06-17 19:42:09 +07:00
expiry := makeInvoiceParams.Expiry
transaction, err := controller.transactionsService.MakeInvoice(ctx, makeInvoiceParams.Amount, makeInvoiceParams.Description, makeInvoiceParams.DescriptionHash, expiry, makeInvoiceParams.Metadata, controller.lnClient, &appId, &requestEventId, nil)
2024-06-17 19:42:09 +07:00
if err != nil {
logger.Logger.WithFields(logrus.Fields{
"request_event_id": requestEventId,
"amount": makeInvoiceParams.Amount,
"description": makeInvoiceParams.Description,
"descriptionHash": makeInvoiceParams.DescriptionHash,
"expiry": makeInvoiceParams.Expiry,
}).Infof("Failed to make invoice: %v", err)
publishResponse(&models.Response{
ResultType: nip47Request.Method,
feat: add hold invoices support for LND & LDK (#1298) * feat: add LND hold invoices support * fix: add HOLD_INVOICE_ACCEPTED_NOTIFICATION to notifications list returned by get_info * fix: revert * fix: remove unneeded null checks * docs: add extra event type to README * feat: add LND hold invoices support * fix: add HOLD_INVOICE_ACCEPTED_NOTIFICATION to notifications list returned by get_info * fix: revert * fix: remove unneeded null checks * docs: add extra event type to README * fix: remove 0 expiry checking in the make_hold_invoice_controller * fix: use JSON logging * revert fly.toml * fix: duplicated check * fix: move publishing nwc_hold_invoice_accepted out of the transaction * fix: move publishing nwc_hold_invoice_accepted out of the transaction * fix: check the invoice state ACCEPTED before calling the lnClient.SettleHoldInvoice * fix: check the invoice state ACCEPTED before calling the lnClient.SettleHoldInvoice * fix: check the invoice state ACCEPTED before calling the lnClient.SettleHoldInvoice * fix: check the invoice state ACCEPTED before calling the lnClient.SettleHoldInvoice * fix: cancel hold invoice tests * fix: make hold invoice tests * fix: make hold invoice tests * fix: settle hold invoice tests * fix: resubscribe to pending hold invoices * fix: missing WatchHoldInvoice in the mocks * feat: add LDK impl * fix: cleanup * fix: payment_hash * fix: remove unneeded update * feat: add support for self payments for hold invoices (#1304) Co-authored-by: frnandu <frnandu@gmail.com> * fix: remove hold invoices scope * fix: remove hold invoices scope * fix: mock hold bolt11 expiry to 10 years * fix: sleep 1 second to give a change of lookupinvoice to read cancelled * fix: missing timeout param * feat: add hold invoice settle deadline to transactions (#1324) * chore: update mockery, remove unused hold invoice method * chore: remove unnecessary comment * chore: remove unused code * fix: do not return hold transaction if lookup failed * chore: remove unused code * fix: return correct errors from nip47 controllers, remove unused code * fix: failing test * chore: remove unnecessary code * fix: make error message more general * chore: remove unused code * fix: use correct context in lnd service * fix: unstable hold payments test --------- Co-authored-by: Roland Bewick <roland.bewick@gmail.com> Co-authored-by: Roland <33993199+rolznz@users.noreply.github.com>
2025-05-26 11:51:23 +02:00
Error: mapNip47Error(err),
2024-06-17 19:42:09 +07:00
}, nostr.Tags{})
return
}
Feat: dynamic budgets (#226) * feat: add transactions table * feat: add transactions service with makeinvoice method * feat: use internal transactions WIP * feat: check unsettled transactions * feat: use transactions service in nip47 package WIP * feat: use transactions service in NIP-47 handlers * fix: transaction list colors * fix: tests * chore: remove old payments table * fix: return budget usage as sats * feat: use internal transactions table for keysend payments * feat: consume nwc_payment_received event in transaction service (WIP) * feat: update existing transaction from nwc_payment_received event in transactions service * feat: update existing transaction from nwc_payment_sent event in transactions service * feat: handle async payment failed events in transactions service * fix: tests * feat: correctly implement NIP-47 NOT_FOUND error code * feat: intercept self payments * feat: isolated balance and visibility * feat: validate keysend payment does not exceed app internal balance * feat: budget check in transactions service, correctly pass payment errors to NIP-47 response also reduce query duplication * fix: order transactions when looking up transaction * feat: add fee reserves to unsettled outgoing transactions * chore: rename app permissions max amount field * chore: rename transaction amount values to be clearly millisats * chore: merge balance type and visibility into isolated property on apps table * chore: move duplicated permission check from nip-47 controllers to handler * fix: app name in transaction list * Feat: permissions revamp v2 (#273) * feat: revamp permissions component * chore: changes * chore: changes * chore: add view mode for show app screen * chore: further changes * feat: new illustration for linking account (#254) * feat: new illustration for linking account * fix: update paths * fix: icon props (#256) * fix: use date from frontend * chore: add expiryselect component * chore: further changes * chore: further changes * chore: add date-fns for date picker * chore: further changes * chore: further changes * fix: add central LDK gossip node to help gossip new public channels (#262) * fix: make dialog responsive (#258) * chore: further changes * typo * chore: spacing issues * chore: use scopes from capabilities * chore: change scope type descriptions * chore: styling fixes * chore: fix typings * chore: budget renewal component * fix: LDK mark channel as inactive and show error if counterparty forwarding info missing (#267) fix: mark channel as inactive and show error if counterparty forwarding info missing * chore: remove unnecessary dark classes (#255) * fix: links to open first channel in sidebar and onboarding checklist (#268) * feat: improve migrate node UI (#269) * fix: migrate node copy (#270) * fix: stop nostr when app is shutdown and use context to stop lnclient (#271) * fix: permissions revamp WIP * feat: basic isolated apps UI * fix: new app connection, edit app connection, deep linking --------- Co-authored-by: im-adithya <imadithyavardhan@gmail.com> Co-authored-by: René Aaron <100827540+reneaaron@users.noreply.github.com> Co-authored-by: Michael Bumann <hello@michaelbumann.com> * fix: make transactions table ID autoincrement * fix: do not send unrelated notifications to isolated apps * fix: nip-47 notifications not receiving updated transaction state * fix: migrate existing tables to have autoincrementing primary keys (#274) * fix: migrate existing tables to have autoincrementing primary keys * fix: recreate request and response event tables after apps * fix: remove null from request_events app_id * fix: do not crash when reloading app created page * chore: update pragma commands, add busy_timeout * fix: remove unnecessary migration of user_configs table * chore: update comment on autoincrement migration * feat: sqlite database config improvements * fix: autoincrement migration to delete unlinked app permissions * chore: update tests for dynamic budgets (WIP) * fix: get balance tests * chore: add extra event handler tests * chore: add extra event handler test * chore: add extra multi_pay_invoice tests * chore: transactions service tests (WIP) * chore: add transactions service payment tests * chore: add tests for self payments * fix: notifications tests * chore: add notifications tests for transactions service * chore: add fee reserve tests for transactions service * chore: add list transactions tests * chore: add keysend tests for transactions service * feat: subscribe for payments and invoices (#281) * fix: db transaction should be passed as pointer * feat(lnd): subscribe for payments and invoices * chore: rearrange check for settled * chore: remove TODO * chore: retry on error and add select * chore: publish payment failed event * chore: use json logging * feat: add lnd notification types * chore: remove sleep * fix: incorrect key on transaction list items * fix: test * fix: disable isolated app type on non-supported backends --------- Co-authored-by: im-adithya <imadithyavardhan@gmail.com> Co-authored-by: René Aaron <100827540+reneaaron@users.noreply.github.com> Co-authored-by: Michael Bumann <hello@michaelbumann.com>
2024-07-19 23:30:22 +07:00
nip47Transaction := models.ToNip47Transaction(transaction)
2024-06-17 19:42:09 +07:00
responsePayload := &makeInvoiceResponse{
Feat: dynamic budgets (#226) * feat: add transactions table * feat: add transactions service with makeinvoice method * feat: use internal transactions WIP * feat: check unsettled transactions * feat: use transactions service in nip47 package WIP * feat: use transactions service in NIP-47 handlers * fix: transaction list colors * fix: tests * chore: remove old payments table * fix: return budget usage as sats * feat: use internal transactions table for keysend payments * feat: consume nwc_payment_received event in transaction service (WIP) * feat: update existing transaction from nwc_payment_received event in transactions service * feat: update existing transaction from nwc_payment_sent event in transactions service * feat: handle async payment failed events in transactions service * fix: tests * feat: correctly implement NIP-47 NOT_FOUND error code * feat: intercept self payments * feat: isolated balance and visibility * feat: validate keysend payment does not exceed app internal balance * feat: budget check in transactions service, correctly pass payment errors to NIP-47 response also reduce query duplication * fix: order transactions when looking up transaction * feat: add fee reserves to unsettled outgoing transactions * chore: rename app permissions max amount field * chore: rename transaction amount values to be clearly millisats * chore: merge balance type and visibility into isolated property on apps table * chore: move duplicated permission check from nip-47 controllers to handler * fix: app name in transaction list * Feat: permissions revamp v2 (#273) * feat: revamp permissions component * chore: changes * chore: changes * chore: add view mode for show app screen * chore: further changes * feat: new illustration for linking account (#254) * feat: new illustration for linking account * fix: update paths * fix: icon props (#256) * fix: use date from frontend * chore: add expiryselect component * chore: further changes * chore: further changes * chore: add date-fns for date picker * chore: further changes * chore: further changes * fix: add central LDK gossip node to help gossip new public channels (#262) * fix: make dialog responsive (#258) * chore: further changes * typo * chore: spacing issues * chore: use scopes from capabilities * chore: change scope type descriptions * chore: styling fixes * chore: fix typings * chore: budget renewal component * fix: LDK mark channel as inactive and show error if counterparty forwarding info missing (#267) fix: mark channel as inactive and show error if counterparty forwarding info missing * chore: remove unnecessary dark classes (#255) * fix: links to open first channel in sidebar and onboarding checklist (#268) * feat: improve migrate node UI (#269) * fix: migrate node copy (#270) * fix: stop nostr when app is shutdown and use context to stop lnclient (#271) * fix: permissions revamp WIP * feat: basic isolated apps UI * fix: new app connection, edit app connection, deep linking --------- Co-authored-by: im-adithya <imadithyavardhan@gmail.com> Co-authored-by: René Aaron <100827540+reneaaron@users.noreply.github.com> Co-authored-by: Michael Bumann <hello@michaelbumann.com> * fix: make transactions table ID autoincrement * fix: do not send unrelated notifications to isolated apps * fix: nip-47 notifications not receiving updated transaction state * fix: migrate existing tables to have autoincrementing primary keys (#274) * fix: migrate existing tables to have autoincrementing primary keys * fix: recreate request and response event tables after apps * fix: remove null from request_events app_id * fix: do not crash when reloading app created page * chore: update pragma commands, add busy_timeout * fix: remove unnecessary migration of user_configs table * chore: update comment on autoincrement migration * feat: sqlite database config improvements * fix: autoincrement migration to delete unlinked app permissions * chore: update tests for dynamic budgets (WIP) * fix: get balance tests * chore: add extra event handler tests * chore: add extra event handler test * chore: add extra multi_pay_invoice tests * chore: transactions service tests (WIP) * chore: add transactions service payment tests * chore: add tests for self payments * fix: notifications tests * chore: add notifications tests for transactions service * chore: add fee reserve tests for transactions service * chore: add list transactions tests * chore: add keysend tests for transactions service * feat: subscribe for payments and invoices (#281) * fix: db transaction should be passed as pointer * feat(lnd): subscribe for payments and invoices * chore: rearrange check for settled * chore: remove TODO * chore: retry on error and add select * chore: publish payment failed event * chore: use json logging * feat: add lnd notification types * chore: remove sleep * fix: incorrect key on transaction list items * fix: test * fix: disable isolated app type on non-supported backends --------- Co-authored-by: im-adithya <imadithyavardhan@gmail.com> Co-authored-by: René Aaron <100827540+reneaaron@users.noreply.github.com> Co-authored-by: Michael Bumann <hello@michaelbumann.com>
2024-07-19 23:30:22 +07:00
Transaction: *nip47Transaction,
2024-06-17 19:42:09 +07:00
}
publishResponse(&models.Response{
ResultType: nip47Request.Method,
Result: responsePayload,
}, nostr.Tags{})
}