2024-06-17 19:42:09 +07:00
|
|
|
package controllers
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
2025-01-17 13:51:16 +07:00
|
|
|
"encoding/json"
|
2024-06-17 19:42:09 +07:00
|
|
|
|
2026-05-11 12:34:56 +07:00
|
|
|
"github.com/getAlby/go-nostr"
|
2024-07-19 23:30:22 +07:00
|
|
|
"github.com/getAlby/hub/constants"
|
2024-07-05 20:32:40 +07:00
|
|
|
"github.com/getAlby/hub/db"
|
|
|
|
|
"github.com/getAlby/hub/logger"
|
|
|
|
|
"github.com/getAlby/hub/nip47/models"
|
2024-06-17 19:42:09 +07:00
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type getInfoResponse struct {
|
2025-09-22 12:53:57 +05:30
|
|
|
Alias *string `json:"alias"`
|
|
|
|
|
Color *string `json:"color"`
|
|
|
|
|
Pubkey *string `json:"pubkey"`
|
|
|
|
|
Network *string `json:"network"`
|
|
|
|
|
BlockHeight *uint32 `json:"block_height"`
|
|
|
|
|
BlockHash *string `json:"block_hash"`
|
2025-02-27 19:12:55 +07:00
|
|
|
Methods []string `json:"methods"`
|
|
|
|
|
Notifications []string `json:"notifications"`
|
|
|
|
|
Metadata interface{} `json:"metadata,omitempty"`
|
2025-09-23 13:15:25 +07:00
|
|
|
LightningAddress *string `json:"lud16"`
|
2024-06-17 19:42:09 +07:00
|
|
|
}
|
|
|
|
|
|
2024-07-19 23:30:22 +07:00
|
|
|
func (controller *nip47Controller) HandleGetInfoEvent(ctx context.Context, nip47Request *models.Request, requestEventId uint, app *db.App, publishResponse publishFunc) {
|
2024-06-21 10:23:15 +05:30
|
|
|
supportedNotifications := []string{}
|
|
|
|
|
if controller.permissionsService.PermitsNotifications(app) {
|
2024-07-01 20:30:40 +07:00
|
|
|
supportedNotifications = controller.lnClient.GetSupportedNIP47NotificationTypes()
|
2024-06-21 10:23:15 +05:30
|
|
|
}
|
|
|
|
|
|
2024-06-17 19:42:09 +07:00
|
|
|
responsePayload := &getInfoResponse{
|
2024-07-01 20:30:40 +07:00
|
|
|
Methods: controller.permissionsService.GetPermittedMethods(app, controller.lnClient),
|
2024-06-21 10:23:15 +05:30
|
|
|
Notifications: supportedNotifications,
|
2024-06-17 19:42:09 +07:00
|
|
|
}
|
|
|
|
|
|
2026-03-31 21:26:02 +05:30
|
|
|
if app != nil {
|
|
|
|
|
metadata := map[string]interface{}{}
|
|
|
|
|
if app.Metadata != nil {
|
|
|
|
|
jsonErr := json.Unmarshal(app.Metadata, &metadata)
|
|
|
|
|
if jsonErr != nil {
|
|
|
|
|
logger.Logger.WithError(jsonErr).WithFields(logrus.Fields{
|
|
|
|
|
"id": app.ID,
|
|
|
|
|
"metadata": app.Metadata,
|
|
|
|
|
}).Error("Failed to deserialize app metadata")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if metadata["id"] == nil {
|
|
|
|
|
metadata["id"] = app.ID
|
|
|
|
|
}
|
|
|
|
|
if metadata["name"] == nil {
|
|
|
|
|
metadata["name"] = app.Name
|
|
|
|
|
}
|
|
|
|
|
if !app.Isolated {
|
|
|
|
|
lightningAddress, _ := controller.albyOAuthService.GetLightningAddress()
|
|
|
|
|
responsePayload.LightningAddress = &lightningAddress
|
|
|
|
|
} else if metadata[constants.METADATA_APPSTORE_APP_ID_KEY] == constants.SUBWALLET_APPSTORE_APP_ID && metadata["lud16"] != nil {
|
|
|
|
|
lightningAddress := metadata["lud16"].(string)
|
|
|
|
|
responsePayload.LightningAddress = &lightningAddress
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
responsePayload.Metadata = metadata
|
|
|
|
|
}
|
|
|
|
|
|
2024-06-24 13:56:38 +05:30
|
|
|
// basic permissions check
|
2024-10-27 23:28:27 -07:00
|
|
|
// this is inconsistent with other methods. Ideally we move fetching node info to a separate method,
|
|
|
|
|
// so that get_info does not require its own scope. This would require a change in the NIP-47 spec.
|
2024-07-19 23:30:22 +07:00
|
|
|
hasPermission, _, _ := controller.permissionsService.HasPermission(app, constants.GET_INFO_SCOPE)
|
2024-06-24 13:56:38 +05:30
|
|
|
if hasPermission {
|
|
|
|
|
logger.Logger.WithFields(logrus.Fields{
|
|
|
|
|
"request_event_id": requestEventId,
|
2024-08-21 17:09:16 +07:00
|
|
|
}).Debug("Getting info")
|
2024-06-24 13:56:38 +05:30
|
|
|
|
|
|
|
|
info, err := controller.lnClient.GetInfo(ctx)
|
|
|
|
|
if err != nil {
|
|
|
|
|
logger.Logger.WithFields(logrus.Fields{
|
|
|
|
|
"request_event_id": requestEventId,
|
|
|
|
|
}).Infof("Failed to fetch node info: %v", err)
|
|
|
|
|
|
|
|
|
|
publishResponse(&models.Response{
|
|
|
|
|
ResultType: nip47Request.Method,
|
2025-05-26 11:51:23 +02:00
|
|
|
Error: mapNip47Error(err),
|
2024-06-24 13:56:38 +05:30
|
|
|
}, nostr.Tags{})
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
network := info.Network
|
|
|
|
|
// Some implementations return "bitcoin" while NIP47 expects "mainnet"
|
|
|
|
|
if network == "bitcoin" {
|
|
|
|
|
network = "mainnet"
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-22 12:53:57 +05:30
|
|
|
responsePayload.Alias = &info.Alias
|
|
|
|
|
responsePayload.Color = &info.Color
|
|
|
|
|
responsePayload.Pubkey = &info.Pubkey
|
|
|
|
|
responsePayload.Network = &network
|
|
|
|
|
responsePayload.BlockHeight = &info.BlockHeight
|
|
|
|
|
responsePayload.BlockHash = &info.BlockHash
|
2024-06-24 13:56:38 +05:30
|
|
|
}
|
|
|
|
|
|
2024-06-17 19:42:09 +07:00
|
|
|
publishResponse(&models.Response{
|
|
|
|
|
ResultType: nip47Request.Method,
|
|
|
|
|
Result: responsePayload,
|
|
|
|
|
}, nostr.Tags{})
|
|
|
|
|
}
|