create_app.go raw
1 package tests
2
3 import (
4 "time"
5
6 "github.com/getAlby/go-nostr"
7 "github.com/getAlby/hub/constants"
8 db "github.com/getAlby/hub/db"
9 "github.com/getAlby/hub/events"
10 "github.com/getAlby/hub/nip47/cipher"
11 "gorm.io/gorm"
12 )
13
14 type CreateAppFn func(svc *TestService, senderPrivkey string, nip47Encryption string) (app *db.App, nip47Cipher *cipher.Nip47Cipher, err error)
15
16 func CreateApp(svc *TestService) (app *db.App, cipher *cipher.Nip47Cipher, err error) {
17 return CreateAppWithPrivateKey(svc, "", constants.ENCRYPTION_TYPE_NIP44_V2)
18 }
19
20 func CreateAppWithPrivateKey(svc *TestService, senderPrivkey, nip47Encryption string) (app *db.App, nip47Cipher *cipher.Nip47Cipher, err error) {
21 senderPubkey := ""
22 if senderPrivkey != "" {
23 var err error
24 senderPubkey, err = nostr.GetPublicKey(senderPrivkey)
25 if err != nil {
26 return nil, nil, err
27 }
28 }
29
30 var expiresAt *time.Time
31 app, pairingSecretKey, err := svc.AppsService.CreateApp("test", senderPubkey, 0, "monthly", expiresAt, []string{constants.GET_INFO_SCOPE}, false, nil)
32 if pairingSecretKey == "" {
33 pairingSecretKey = senderPrivkey
34 }
35
36 nip47Cipher, err = cipher.NewNip47Cipher(nip47Encryption, *app.WalletPubkey, pairingSecretKey)
37 if err != nil {
38 return nil, nil, err
39 }
40
41 return app, nip47Cipher, nil
42 }
43
44 func CreateAppWithSharedWalletPubkey(svc *TestService, senderPrivkey, nip47Encryption string) (app *db.App, nip47Cipher *cipher.Nip47Cipher, err error) {
45
46 pairingPublicKey, _ := nostr.GetPublicKey(senderPrivkey)
47
48 app = &db.App{Name: "test", AppPubkey: pairingPublicKey, Isolated: false}
49
50 err = svc.DB.Transaction(func(tx *gorm.DB) error {
51 err := tx.Save(&app).Error
52 if err != nil {
53 return err
54 }
55
56 // commit transaction
57 return nil
58 })
59
60 if err != nil {
61 return nil, nil, err
62 }
63
64 svc.EventPublisher.Publish(&events.Event{
65 Event: "nwc_app_created",
66 Properties: map[string]interface{}{
67 "name": "test",
68 "id": app.ID,
69 },
70 })
71
72 nip47Cipher, err = cipher.NewNip47Cipher(nip47Encryption, svc.Keys.GetNostrPublicKey(), senderPrivkey)
73 return app, nip47Cipher, nil
74 }
75