Skip to content

Commit

Permalink
chore: use nwc info event instead of nwa event
Browse files Browse the repository at this point in the history
  • Loading branch information
rolznz committed Feb 1, 2025
1 parent 5c1163b commit e56c8e2
Show file tree
Hide file tree
Showing 14 changed files with 42 additions and 96 deletions.
1 change: 0 additions & 1 deletion alby/alby_oauth_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -674,7 +674,6 @@ func (svc *albyOAuthService) LinkAccount(ctx context.Context, lnClient lnclient.
scopes,
false,
nil,
"",
)

if err != nil {
Expand Down
1 change: 0 additions & 1 deletion api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,6 @@ func (api *api) CreateApp(createAppRequest *CreateAppRequest) (*CreateAppRespons
createAppRequest.Scopes,
createAppRequest.Isolated,
createAppRequest.Metadata,
"",
)

if err != nil {
Expand Down
9 changes: 4 additions & 5 deletions apps/apps_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import (
)

type AppsService interface {
CreateApp(name string, pubkey string, maxAmountSat uint64, budgetRenewal string, expiresAt *time.Time, scopes []string, isolated bool, metadata map[string]interface{}, nwaSecret string) (*db.App, string, error)
CreateApp(name string, pubkey string, maxAmountSat uint64, budgetRenewal string, expiresAt *time.Time, scopes []string, isolated bool, metadata map[string]interface{}) (*db.App, string, error)
DeleteApp(app *db.App) error
GetAppByPubkey(pubkey string) *db.App
}
Expand All @@ -39,7 +39,7 @@ func NewAppsService(db *gorm.DB, eventPublisher events.EventPublisher, keys keys
}
}

func (svc *appsService) CreateApp(name string, pubkey string, maxAmountSat uint64, budgetRenewal string, expiresAt *time.Time, scopes []string, isolated bool, metadata map[string]interface{}, nwaSecret string) (*db.App, string, error) {
func (svc *appsService) CreateApp(name string, pubkey string, maxAmountSat uint64, budgetRenewal string, expiresAt *time.Time, scopes []string, isolated bool, metadata map[string]interface{}) (*db.App, string, error) {
if isolated && (slices.Contains(scopes, constants.SIGN_MESSAGE_SCOPE)) {
// cannot sign messages because the isolated app is a custodial sub-wallet
return nil, "", errors.New("Sub-wallet app connection cannot have sign_message scope")
Expand Down Expand Up @@ -128,9 +128,8 @@ func (svc *appsService) CreateApp(name string, pubkey string, maxAmountSat uint6
svc.eventPublisher.Publish(&events.Event{
Event: "nwc_app_created",
Properties: map[string]interface{}{
"name": name,
"id": app.ID,
"nwa_secret": nwaSecret,
"name": name,
"id": app.ID,
},
})

Expand Down
5 changes: 2 additions & 3 deletions nip47/controllers/create_connection_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@ type createConnectionBudgetParams struct {
}

type createConnectionParams struct {
Pubkey string `json:"pubkey"` // pubkey of the app connection
NWASecret string `json:"nwaSecret"` // if connection is initiated through NWA
Pubkey string `json:"pubkey"` // pubkey of the app connection
Name string `json:"name"`
Methods []string `json:"methods"`
Budget createConnectionBudgetParams `json:"budget"`
Expand Down Expand Up @@ -82,7 +81,7 @@ func (controller *nip47Controller) HandleCreateConnectionEvent(ctx context.Conte
return
}

app, _, err := controller.appsService.CreateApp(params.Name, params.Pubkey, params.Budget.Budget, params.Budget.RenewalPeriod, expiresAt, scopes, params.Isolated, params.Metadata, params.NWASecret)
app, _, err := controller.appsService.CreateApp(params.Name, params.Pubkey, params.Budget.Budget, params.Budget.RenewalPeriod, expiresAt, scopes, params.Isolated, params.Metadata)
if err != nil {
logger.Logger.WithFields(logrus.Fields{
"request_event_id": requestEventId,
Expand Down
7 changes: 1 addition & 6 deletions nip47/controllers/create_connection_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ func TestHandleCreateConnectionEvent_PubkeyAlreadyExists(t *testing.T) {
pairingPublicKey, err := nostr.GetPublicKey(pairingSecretKey)
require.NoError(t, err)

_, _, err = svc.AppsService.CreateApp("Existing App", pairingPublicKey, 0, constants.BUDGET_RENEWAL_NEVER, nil, []string{models.GET_INFO_METHOD}, false, nil, "")
_, _, err = svc.AppsService.CreateApp("Existing App", pairingPublicKey, 0, constants.BUDGET_RENEWAL_NEVER, nil, []string{models.GET_INFO_METHOD}, false, nil)

nip47CreateConnectionJson := fmt.Sprintf(`
{
Expand Down Expand Up @@ -263,8 +263,3 @@ func TestHandleCreateConnectionEvent_DoNotAllowCreateConnectionMethod(t *testing
assert.Equal(t, models.CREATE_CONNECTION_METHOD, publishedResponse.ResultType)
assert.Nil(t, publishedResponse.Result)
}

func TestHandleCreateConnectionEvent_NWA(t *testing.T) {
// TODO; should publish event to relay
assert.True(t, false)
}
4 changes: 2 additions & 2 deletions nip47/controllers/get_info_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ func TestHandleGetInfoEvent_WithMetadata(t *testing.T) {
"a": 123,
}

app, _, err := svc.AppsService.CreateApp("test", "", 0, "monthly", nil, nil, false, metadata, "")
app, _, err := svc.AppsService.CreateApp("test", "", 0, "monthly", nil, nil, false, metadata)
assert.NoError(t, err)

nip47Request := &models.Request{}
Expand All @@ -157,7 +157,7 @@ func TestHandleGetInfoEvent_WithMetadata(t *testing.T) {

permissionsSvc := permissions.NewPermissionsService(svc.DB, svc.EventPublisher)
transactionsSvc := transactions.NewTransactionsService(svc.DB, svc.EventPublisher)
NewNip47Controller(svc.LNClient, svc.DB, svc.EventPublisher, permissionsSvc, transactionsSvc).
NewNip47Controller(svc.LNClient, svc.DB, svc.EventPublisher, permissionsSvc, transactionsSvc, svc.AppsService).
HandleGetInfoEvent(ctx, nip47Request, dbRequestEvent.ID, app, publishResponse)

assert.Nil(t, publishedResponse.Error)
Expand Down
2 changes: 1 addition & 1 deletion nip47/controllers/multi_pay_invoice_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ func TestHandleMultiPayInvoiceEvent_OneExpiredInvoice(t *testing.T) {

permissionsSvc := permissions.NewPermissionsService(svc.DB, svc.EventPublisher)
transactionsSvc := transactions.NewTransactionsService(svc.DB, svc.EventPublisher)
NewNip47Controller(svc.LNClient, svc.DB, svc.EventPublisher, permissionsSvc, transactionsSvc).
NewNip47Controller(svc.LNClient, svc.DB, svc.EventPublisher, permissionsSvc, transactionsSvc, svc.AppsService).
HandleMultiPayInvoiceEvent(ctx, nip47Request, requestEvent.ID, app, publishResponse)

assert.Equal(t, 2, len(responses))
Expand Down
2 changes: 1 addition & 1 deletion nip47/controllers/pay_invoice_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ func TestHandlePayInvoiceEvent_ExpiredInvoice(t *testing.T) {

permissionsSvc := permissions.NewPermissionsService(svc.DB, svc.EventPublisher)
transactionsSvc := transactions.NewTransactionsService(svc.DB, svc.EventPublisher)
NewNip47Controller(svc.LNClient, svc.DB, svc.EventPublisher, permissionsSvc, transactionsSvc).
NewNip47Controller(svc.LNClient, svc.DB, svc.EventPublisher, permissionsSvc, transactionsSvc, svc.AppsService).
HandlePayInvoiceEvent(ctx, nip47Request, dbRequestEvent.ID, app, publishResponse, nostr.Tags{})

assert.Nil(t, publishedResponse.Result)
Expand Down
25 changes: 17 additions & 8 deletions nip47/event_handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/getAlby/hub/alby"
"github.com/getAlby/hub/constants"
"github.com/getAlby/hub/db"
"github.com/getAlby/hub/nip47/cipher"
Expand Down Expand Up @@ -66,7 +67,8 @@ func doTestCreateResponse(t *testing.T, svc *tests.TestService, nip47Version str
},
}

nip47svc := NewNip47Service(svc.DB, svc.Cfg, svc.Keys, svc.EventPublisher)
albyOAuthSvc := alby.NewAlbyOAuthService(svc.DB, svc.Cfg, svc.Keys, svc.EventPublisher)
nip47svc := NewNip47Service(svc.DB, svc.Cfg, svc.Keys, svc.EventPublisher, albyOAuthSvc)

res, err := nip47svc.CreateResponse(reqEvent, nip47Response, nostr.Tags{}, nip47Cipher, svc.Keys.GetNostrSecretKey())
assert.NoError(t, err)
Expand Down Expand Up @@ -104,7 +106,8 @@ func TestHandleResponse_Nip44_WithPermission(t *testing.T) {
}

func doTestHandleResponse_WithPermission(t *testing.T, svc *tests.TestService, createAppFn tests.CreateAppFn, version string) {
nip47svc := NewNip47Service(svc.DB, svc.Cfg, svc.Keys, svc.EventPublisher)
albyOAuthSvc := alby.NewAlbyOAuthService(svc.DB, svc.Cfg, svc.Keys, svc.EventPublisher)
nip47svc := NewNip47Service(svc.DB, svc.Cfg, svc.Keys, svc.EventPublisher, albyOAuthSvc)

reqPrivateKey := nostr.GeneratePrivateKey()
reqPubkey, err := nostr.GetPublicKey(reqPrivateKey)
Expand Down Expand Up @@ -192,7 +195,8 @@ func TestHandleResponse_Nip44_DuplicateRequest(t *testing.T) {
}

func doTestHandleResponse_DuplicateRequest(t *testing.T, svc *tests.TestService, createAppFn tests.CreateAppFn, version string) {
nip47svc := NewNip47Service(svc.DB, svc.Cfg, svc.Keys, svc.EventPublisher)
albyOAuthSvc := alby.NewAlbyOAuthService(svc.DB, svc.Cfg, svc.Keys, svc.EventPublisher)
nip47svc := NewNip47Service(svc.DB, svc.Cfg, svc.Keys, svc.EventPublisher, albyOAuthSvc)

reqPrivateKey := nostr.GeneratePrivateKey()
reqPubkey, err := nostr.GetPublicKey(reqPrivateKey)
Expand Down Expand Up @@ -266,7 +270,8 @@ func TestHandleResponse_Nip44_NoPermission(t *testing.T) {
}

func doTestHandleResponse_NoPermission(t *testing.T, svc *tests.TestService, createAppFn tests.CreateAppFn, version string) {
nip47svc := NewNip47Service(svc.DB, svc.Cfg, svc.Keys, svc.EventPublisher)
albyOAuthSvc := alby.NewAlbyOAuthService(svc.DB, svc.Cfg, svc.Keys, svc.EventPublisher)
nip47svc := NewNip47Service(svc.DB, svc.Cfg, svc.Keys, svc.EventPublisher, albyOAuthSvc)

reqPrivateKey := nostr.GeneratePrivateKey()
reqPubkey, err := nostr.GetPublicKey(reqPrivateKey)
Expand Down Expand Up @@ -337,7 +342,8 @@ func TestHandleResponse_Nip44_OldRequestForPayment(t *testing.T) {
}

func doTestHandleResponse_OldRequestForPayment(t *testing.T, svc *tests.TestService, createAppFn tests.CreateAppFn, version string) {
nip47svc := NewNip47Service(svc.DB, svc.Cfg, svc.Keys, svc.EventPublisher)
albyOAuthSvc := alby.NewAlbyOAuthService(svc.DB, svc.Cfg, svc.Keys, svc.EventPublisher)
nip47svc := NewNip47Service(svc.DB, svc.Cfg, svc.Keys, svc.EventPublisher, albyOAuthSvc)

reqPrivateKey := nostr.GeneratePrivateKey()
reqPubkey, err := nostr.GetPublicKey(reqPrivateKey)
Expand Down Expand Up @@ -412,7 +418,8 @@ func TestHandleResponse_Nip44_IncorrectPubkey(t *testing.T) {
}

func doTestHandleResponse_IncorrectPubkey(t *testing.T, svc *tests.TestService, createAppFn tests.CreateAppFn, version string) {
nip47svc := NewNip47Service(svc.DB, svc.Cfg, svc.Keys, svc.EventPublisher)
albyOAuthSvc := alby.NewAlbyOAuthService(svc.DB, svc.Cfg, svc.Keys, svc.EventPublisher)
nip47svc := NewNip47Service(svc.DB, svc.Cfg, svc.Keys, svc.EventPublisher, albyOAuthSvc)

reqPrivateKey := nostr.GeneratePrivateKey()
reqPubkey, err := nostr.GetPublicKey(reqPrivateKey)
Expand Down Expand Up @@ -469,7 +476,8 @@ func TestHandleResponse_NoApp(t *testing.T) {
svc, err := tests.CreateTestService(t)
require.NoError(t, err)
defer svc.Remove()
nip47svc := NewNip47Service(svc.DB, svc.Cfg, svc.Keys, svc.EventPublisher)
albyOAuthSvc := alby.NewAlbyOAuthService(svc.DB, svc.Cfg, svc.Keys, svc.EventPublisher)
nip47svc := NewNip47Service(svc.DB, svc.Cfg, svc.Keys, svc.EventPublisher, albyOAuthSvc)

reqPrivateKey := nostr.GeneratePrivateKey()
reqPubkey, err := nostr.GetPublicKey(reqPrivateKey)
Expand Down Expand Up @@ -522,7 +530,8 @@ func TestHandleResponse_IncorrectVersions(t *testing.T) {
}

func doTestHandleResponse_IncorrectVersion(t *testing.T, svc *tests.TestService, appVersion, requestVersion string) {
nip47svc := NewNip47Service(svc.DB, svc.Cfg, svc.Keys, svc.EventPublisher)
albyOAuthSvc := alby.NewAlbyOAuthService(svc.DB, svc.Cfg, svc.Keys, svc.EventPublisher)
nip47svc := NewNip47Service(svc.DB, svc.Cfg, svc.Keys, svc.EventPublisher, albyOAuthSvc)

reqPrivateKey := nostr.GeneratePrivateKey()
reqPubkey, err := nostr.GetPublicKey(reqPrivateKey)
Expand Down
6 changes: 4 additions & 2 deletions nip47/nip47_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package nip47
import (
"context"

"github.com/getAlby/hub/alby"
"github.com/getAlby/hub/apps"
"github.com/getAlby/hub/config"
"github.com/getAlby/hub/events"
Expand All @@ -21,6 +22,7 @@ type nip47Service struct {
permissionsService permissions.PermissionsService
transactionsService transactions.TransactionsService
appsService apps.AppsService
albyOAuthSvc alby.AlbyOAuthService
nip47NotificationQueue notifications.Nip47NotificationQueue
cfg config.Config
keys keys.Keys
Expand All @@ -35,11 +37,10 @@ type Nip47Service interface {
GetNip47Info(ctx context.Context, relay *nostr.Relay, appWalletPubKey string) (*nostr.Event, error)
PublishNip47Info(ctx context.Context, relay nostrmodels.Relay, appWalletPubKey string, appWalletPrivKey string, lnClient lnclient.LNClient) (*nostr.Event, error)
PublishNip47InfoDeletion(ctx context.Context, relay nostrmodels.Relay, appWalletPubKey string, appWalletPrivKey string, infoEventId string) error
PublishNWAEvent(ctx context.Context, relay nostrmodels.Relay, nwaSecret string, appPubKey string, appWalletPubKey string, appWalletPrivKey string, lnClient lnclient.LNClient) (*nostr.Event, error)
CreateResponse(initialEvent *nostr.Event, content interface{}, tags nostr.Tags, cipher *cipher.Nip47Cipher, walletPrivKey string) (result *nostr.Event, err error)
}

func NewNip47Service(db *gorm.DB, cfg config.Config, keys keys.Keys, eventPublisher events.EventPublisher) *nip47Service {
func NewNip47Service(db *gorm.DB, cfg config.Config, keys keys.Keys, eventPublisher events.EventPublisher, albyOAuthSvc alby.AlbyOAuthService) *nip47Service {
return &nip47Service{
nip47NotificationQueue: notifications.NewNip47NotificationQueue(),
cfg: cfg,
Expand All @@ -49,6 +50,7 @@ func NewNip47Service(db *gorm.DB, cfg config.Config, keys keys.Keys, eventPublis
appsService: apps.NewAppsService(db, eventPublisher, keys),
eventPublisher: eventPublisher,
keys: keys,
albyOAuthSvc: albyOAuthSvc,
}
}

Expand Down
6 changes: 6 additions & 0 deletions nip47/publish_nip47_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,12 @@ func (svc *nip47Service) PublishNip47Info(ctx context.Context, relay nostrmodels
}
capabilities = svc.permissionsService.GetPermittedMethods(&app, lnClient)
permitsNotifications = svc.permissionsService.PermitsNotifications(&app)

// NWA: associate the info event with the app so that the app can receive the wallet pubkey
tags = append(tags, []string{"p", app.AppPubkey})
// also include the LN address for apps that use it (e.g. nostr apps assign lud16 to the profile)
lightningAddress, _ := svc.albyOAuthSvc.GetLightningAddress()
tags = append(tags, []string{"lud16", lightningAddress})
}
if permitsNotifications && len(lnClient.GetSupportedNIP47NotificationTypes()) > 0 {
capabilities = append(capabilities, "notifications")
Expand Down
53 changes: 0 additions & 53 deletions nip47/publish_nwa_event.go

This file was deleted.

11 changes: 0 additions & 11 deletions service/create_app_consumer.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,6 @@ func (s *createAppConsumer) ConsumeEvent(ctx context.Context, event *events.Even
logger.Logger.WithField("event", event).Error("Failed to get app id")
return
}
nwaSecret, ok := properties["nwa_secret"].(string)
if !ok {
logger.Logger.WithField("event", event).Error("Failed to get nwa secret")
return
}

app := db.App{}
err := s.svc.db.First(&app, &db.App{
Expand Down Expand Up @@ -65,12 +60,6 @@ func (s *createAppConsumer) ConsumeEvent(ctx context.Context, event *events.Even
if err != nil {
logger.Logger.WithError(err).Error("Could not publish NIP47 info")
}
if nwaSecret != "" {
_, err = s.svc.GetNip47Service().PublishNWAEvent(ctx, s.relay, nwaSecret, app.AppPubkey, walletPubKey, walletPrivKey, s.svc.lnClient)
if err != nil {
logger.Logger.WithError(err).Error("Could not publish NWA event")
}
}
err = s.svc.startAppWalletSubscription(ctx, s.relay, walletPubKey)
if err != nil {
logger.Logger.WithError(err).WithFields(logrus.Fields{
Expand Down
6 changes: 4 additions & 2 deletions service/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,14 +112,16 @@ func NewService(ctx context.Context) (*service, error) {

keys := keys.NewKeys()

albyOAuthSvc := alby.NewAlbyOAuthService(gormDB, cfg, keys, eventPublisher)

var wg sync.WaitGroup
svc := &service{
cfg: cfg,
ctx: ctx,
wg: &wg,
eventPublisher: eventPublisher,
albyOAuthSvc: alby.NewAlbyOAuthService(gormDB, cfg, keys, eventPublisher),
nip47Service: nip47.NewNip47Service(gormDB, cfg, keys, eventPublisher),
albyOAuthSvc: albyOAuthSvc,
nip47Service: nip47.NewNip47Service(gormDB, cfg, keys, eventPublisher, albyOAuthSvc),
transactionsService: transactions.NewTransactionsService(gormDB, eventPublisher),
db: gormDB,
keys: keys,
Expand Down

0 comments on commit e56c8e2

Please sign in to comment.