alby-hub/nip47/controllers/create_connection_controller.go
Roland 3e1d16d423
Feat: NWA auth for self-hosted hubs (#1016)
* feat: nwc create_connection command (WIP)

* feat: allow creating superuser apps from the ui

* fix: pass methods rather than scopes in create_connection method

* chore: add extra tests

* fix: use browser router in http mode

* fix: update links to not use hash router

* fix: add redirect from hash router url

* fix: return nostrWalletConnectUrl in nwc connection success event and message

* feat: publish nwa event

* chore: use nwc info event instead of nwa event

* feat: create custom alby go detail page

* chore: allow creating/editing apps with the same name

* fix: convert budget from msats to sats (#1111)

* chore: address NWA feedback

* chore: address feedback

- adjust button copy
- make create_connection methods consistent with http deeplink flow
- remove unused constant
- add empty string check before adding lud16 tag
- fix test

* feat: add support for notification_types in create_connection method

* fix: shorter button copy

* fix: do not include lud16 tag in published info event

* Feat: add lud16 to get_info response (#1128)

feat: add lud16 to get_info response

* chore: minor alby go screen improvements

* fix: incorrect unlock password error message to create app with superuser access

* chore: minor ui improvements on alby go detail page

* chore: avoid duplicate app names by adding a suffix

* chore: move scopes check to apps service, add new tests, DRY controller test setup

* fix: scopes component full access scopes and isolated scope group check

* fix: use supported capabilities for Alby Go

* fix: return correct app name

* chore: address minor comments

---------

Co-authored-by: im-adithya <imadithyavardhan@gmail.com>
2025-02-27 19:12:55 +07:00

135 lines
4.2 KiB
Go

package controllers
import (
"context"
"slices"
"time"
"github.com/getAlby/hub/constants"
"github.com/getAlby/hub/logger"
"github.com/getAlby/hub/nip47/models"
"github.com/getAlby/hub/nip47/permissions"
"github.com/nbd-wtf/go-nostr"
"github.com/sirupsen/logrus"
)
type createConnectionParams struct {
Pubkey string `json:"pubkey"` // pubkey of the app connection
Name string `json:"name"`
RequestMethods []string `json:"request_methods"`
NotificationTypes []string `json:"notification_types"`
MaxAmount uint64 `json:"max_amount"`
BudgetRenewal string `json:"budget_renewal"`
ExpiresAt *uint64 `json:"expires_at"` // unix timestamp
Isolated bool `json:"isolated"`
Metadata map[string]interface{} `json:"metadata,omitempty"`
}
type createConnectionResponse struct {
// pubkey is given, user requesting already knows relay.
WalletPubkey string `json:"wallet_pubkey"`
}
func (controller *nip47Controller) HandleCreateConnectionEvent(ctx context.Context, nip47Request *models.Request, requestEventId uint, publishResponse publishFunc) {
params := &createConnectionParams{}
resp := decodeRequest(nip47Request, params)
if resp != nil {
publishResponse(resp, nostr.Tags{})
return
}
logger.Logger.WithFields(logrus.Fields{
"request_event_id": requestEventId,
"params": params,
}).Info("creating app")
var expiresAt *time.Time
if params.ExpiresAt != nil {
expiresAtUnsigned := *params.ExpiresAt
expiresAtValue := time.Unix(int64(expiresAtUnsigned), 0)
expiresAt = &expiresAtValue
}
maxAmountSat := params.MaxAmount / 1000
// explicitly do not allow creating an app with create_connection permission
if slices.Contains(params.RequestMethods, models.CREATE_CONNECTION_METHOD) {
publishResponse(&models.Response{
ResultType: nip47Request.Method,
Error: &models.Error{
Code: constants.ERROR_INTERNAL,
Message: "cannot create a new app that has create_connection permission via NWC",
},
}, nostr.Tags{})
return
}
// ensure there is at least one request method
if len(params.RequestMethods) == 0 {
publishResponse(&models.Response{
ResultType: nip47Request.Method,
Error: &models.Error{
Code: constants.ERROR_INTERNAL,
Message: "No request methods provided",
},
}, nostr.Tags{})
return
}
supportedMethods := controller.lnClient.GetSupportedNIP47Methods()
if slices.ContainsFunc(params.RequestMethods, func(method string) bool {
return !slices.Contains(supportedMethods, method)
}) {
publishResponse(&models.Response{
ResultType: nip47Request.Method,
Error: &models.Error{
Code: constants.ERROR_INTERNAL,
Message: "One or more methods are not supported by the current LNClient",
},
}, nostr.Tags{})
return
}
scopes, err := permissions.RequestMethodsToScopes(params.RequestMethods)
supportedNotificationTypes := controller.lnClient.GetSupportedNIP47NotificationTypes()
if len(params.NotificationTypes) > 0 {
if slices.ContainsFunc(params.NotificationTypes, func(method string) bool {
return !slices.Contains(supportedNotificationTypes, method)
}) {
publishResponse(&models.Response{
ResultType: nip47Request.Method,
Error: &models.Error{
Code: constants.ERROR_INTERNAL,
Message: "One or more notification types are not supported by the current LNClient",
},
}, nostr.Tags{})
return
}
scopes = append(scopes, constants.NOTIFICATIONS_SCOPE)
}
app, _, err := controller.appsService.CreateApp(params.Name, params.Pubkey, maxAmountSat, params.BudgetRenewal, expiresAt, scopes, params.Isolated, params.Metadata)
if err != nil {
logger.Logger.WithFields(logrus.Fields{
"request_event_id": requestEventId,
}).WithError(err).Error("Failed to create app")
publishResponse(&models.Response{
ResultType: nip47Request.Method,
Error: &models.Error{
Code: constants.ERROR_INTERNAL,
Message: err.Error(),
},
}, nostr.Tags{})
return
}
responsePayload := createConnectionResponse{
WalletPubkey: *app.WalletPubkey,
}
publishResponse(&models.Response{
ResultType: nip47Request.Method,
Result: responsePayload,
}, nostr.Tags{})
}