Skip to content
This repository has been archived by the owner on Sep 2, 2024. It is now read-only.

Commit

Permalink
chore: return request_methods even without a permission (#441)
Browse files Browse the repository at this point in the history
  • Loading branch information
im-adithya authored Jun 24, 2024
1 parent aee9f09 commit 8ec14bb
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 41 deletions.
76 changes: 37 additions & 39 deletions nip47/controllers/get_info_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,56 +38,54 @@ func NewGetInfoController(permissionsService permissions.PermissionsService, lnC
}

func (controller *getInfoController) HandleGetInfoEvent(ctx context.Context, nip47Request *models.Request, requestEventId uint, app *db.App, checkPermission checkPermissionFunc, publishResponse publishFunc) {
// basic permissions check
resp := checkPermission(0)
if resp != nil {
publishResponse(resp, nostr.Tags{})
return
}

logger.Logger.WithFields(logrus.Fields{
"request_event_id": requestEventId,
}).Info("Getting info")

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,
Error: &models.Error{
Code: models.ERROR_INTERNAL,
Message: err.Error(),
},
}, nostr.Tags{})
return
}

network := info.Network
// Some implementations return "bitcoin" while NIP47 expects "mainnet"
if network == "bitcoin" {
network = "mainnet"
}

supportedNotifications := []string{}
if controller.permissionsService.PermitsNotifications(app) {
// TODO: this needs to be LNClient-specific
supportedNotifications = strings.Split(notifications.NOTIFICATION_TYPES, " ")
}

responsePayload := &getInfoResponse{
Alias: info.Alias,
Color: info.Color,
Pubkey: info.Pubkey,
Network: network,
BlockHeight: info.BlockHeight,
BlockHash: info.BlockHash,
Methods: controller.permissionsService.GetPermittedMethods(app),
Notifications: supportedNotifications,
}

// basic permissions check
hasPermission, _, _ := controller.permissionsService.HasPermission(app, nip47Request.Method, 0)
if hasPermission {
logger.Logger.WithFields(logrus.Fields{
"request_event_id": requestEventId,
}).Info("Getting info")

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,
Error: &models.Error{
Code: models.ERROR_INTERNAL,
Message: err.Error(),
},
}, nostr.Tags{})
return
}

network := info.Network
// Some implementations return "bitcoin" while NIP47 expects "mainnet"
if network == "bitcoin" {
network = "mainnet"
}

responsePayload.Alias = info.Alias
responsePayload.Color = info.Color
responsePayload.Pubkey = info.Pubkey
responsePayload.Network = network
responsePayload.BlockHeight = info.BlockHeight
responsePayload.BlockHash = info.BlockHash
}

publishResponse(&models.Response{
ResultType: nip47Request.Method,
Result: responsePayload,
Expand Down
20 changes: 18 additions & 2 deletions nip47/controllers/get_info_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,14 @@ func TestHandleGetInfoEvent_NoPermission(t *testing.T) {
err = svc.DB.Create(&dbRequestEvent).Error
assert.NoError(t, err)

appPermission := &db.AppPermission{
AppId: app.ID,
RequestMethod: models.GET_BALANCE_METHOD,
ExpiresAt: nil,
}
err = svc.DB.Create(appPermission).Error
assert.NoError(t, err)

checkPermission := func(amountMsat uint64) *models.Response {
return &models.Response{
ResultType: nip47Request.Method,
Expand All @@ -58,8 +66,16 @@ func TestHandleGetInfoEvent_NoPermission(t *testing.T) {
NewGetInfoController(permissionsSvc, svc.LNClient).
HandleGetInfoEvent(ctx, nip47Request, dbRequestEvent.ID, app, checkPermission, publishResponse)

assert.Nil(t, publishedResponse.Result)
assert.Equal(t, models.ERROR_RESTRICTED, publishedResponse.Error.Code)
assert.Nil(t, publishedResponse.Error)
nodeInfo := publishedResponse.Result.(*getInfoResponse)
assert.Empty(t, nodeInfo.Alias)
assert.Empty(t, nodeInfo.Color)
assert.Empty(t, nodeInfo.Pubkey)
assert.Empty(t, nodeInfo.Network)
assert.Empty(t, nodeInfo.BlockHeight)
assert.Empty(t, nodeInfo.BlockHash)
assert.Equal(t, []string{"get_balance"}, nodeInfo.Methods)
assert.Equal(t, []string{}, nodeInfo.Notifications)
}

func TestHandleGetInfoEvent_WithPermission(t *testing.T) {
Expand Down

0 comments on commit 8ec14bb

Please sign in to comment.