mirror of
https://github.com/getAlby/hub.git
synced 2026-08-13 12:33:39 +02:00
Refactor: API cleanup (#338)
This commit is contained in:
parent
a05aafb22b
commit
073d669e03
62 changed files with 3106 additions and 2808 deletions
79
db/db_service.go
Normal file
79
db/db_service.go
Normal file
|
|
@ -0,0 +1,79 @@
|
|||
package db
|
||||
|
||||
import (
|
||||
"encoding/hex"
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/getAlby/nostr-wallet-connect/nip47"
|
||||
"github.com/nbd-wtf/go-nostr"
|
||||
"github.com/sirupsen/logrus"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type dbService struct {
|
||||
db *gorm.DB
|
||||
logger *logrus.Logger
|
||||
}
|
||||
|
||||
func NewDBService(db *gorm.DB, logger *logrus.Logger) *dbService {
|
||||
return &dbService{
|
||||
db: db,
|
||||
logger: logger,
|
||||
}
|
||||
}
|
||||
|
||||
func (dbSvc *dbService) CreateApp(name string, pubkey string, maxAmount int, budgetRenewal string, expiresAt *time.Time, requestMethods []string) (*App, string, error) {
|
||||
var pairingPublicKey string
|
||||
var pairingSecretKey string
|
||||
if pubkey == "" {
|
||||
pairingSecretKey = nostr.GeneratePrivateKey()
|
||||
pairingPublicKey, _ = nostr.GetPublicKey(pairingSecretKey)
|
||||
} else {
|
||||
pairingPublicKey = pubkey
|
||||
//validate public key
|
||||
decoded, err := hex.DecodeString(pairingPublicKey)
|
||||
if err != nil || len(decoded) != 32 {
|
||||
dbSvc.logger.WithField("pairingPublicKey", pairingPublicKey).Error("Invalid public key format")
|
||||
return nil, "", fmt.Errorf("invalid public key format: %s", pairingPublicKey)
|
||||
}
|
||||
}
|
||||
|
||||
app := App{Name: name, NostrPubkey: pairingPublicKey}
|
||||
|
||||
err := dbSvc.db.Transaction(func(tx *gorm.DB) error {
|
||||
err := tx.Save(&app).Error
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
for _, m := range requestMethods {
|
||||
//if we don't know this method, we return an error
|
||||
if !strings.Contains(nip47.CAPABILITIES, m) {
|
||||
return fmt.Errorf("did not recognize request method: %s", m)
|
||||
}
|
||||
appPermission := AppPermission{
|
||||
App: app,
|
||||
RequestMethod: m,
|
||||
ExpiresAt: expiresAt,
|
||||
//these fields are only relevant for pay_invoice
|
||||
MaxAmount: maxAmount,
|
||||
BudgetRenewal: budgetRenewal,
|
||||
}
|
||||
err = tx.Create(&appPermission).Error
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
// commit transaction
|
||||
return nil
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
dbSvc.logger.WithError(err).Error("Failed to save app")
|
||||
return nil, "", err
|
||||
}
|
||||
|
||||
return &app, pairingSecretKey, nil
|
||||
}
|
||||
83
db/models.go
Normal file
83
db/models.go
Normal file
|
|
@ -0,0 +1,83 @@
|
|||
package db
|
||||
|
||||
import "time"
|
||||
|
||||
type UserConfig struct {
|
||||
ID uint
|
||||
Key string
|
||||
Value string
|
||||
Encrypted bool
|
||||
CreatedAt time.Time
|
||||
UpdatedAt time.Time
|
||||
}
|
||||
|
||||
type App struct {
|
||||
ID uint
|
||||
Name string `validate:"required"`
|
||||
Description string
|
||||
NostrPubkey string `validate:"required"`
|
||||
CreatedAt time.Time
|
||||
UpdatedAt time.Time
|
||||
}
|
||||
|
||||
type AppPermission struct {
|
||||
ID uint
|
||||
AppId uint `validate:"required"`
|
||||
App App
|
||||
RequestMethod string `validate:"required"`
|
||||
MaxAmount int
|
||||
BudgetRenewal string
|
||||
ExpiresAt *time.Time
|
||||
CreatedAt time.Time
|
||||
UpdatedAt time.Time
|
||||
}
|
||||
|
||||
type RequestEvent struct {
|
||||
ID uint
|
||||
AppId *uint
|
||||
App App
|
||||
NostrId string `validate:"required"`
|
||||
Content string
|
||||
State string
|
||||
CreatedAt time.Time
|
||||
UpdatedAt time.Time
|
||||
}
|
||||
|
||||
type ResponseEvent struct {
|
||||
ID uint
|
||||
NostrId string `validate:"required"`
|
||||
RequestId uint `validate:"required"`
|
||||
Content string
|
||||
State string
|
||||
RepliedAt time.Time
|
||||
CreatedAt time.Time
|
||||
UpdatedAt time.Time
|
||||
}
|
||||
|
||||
type Payment struct {
|
||||
ID uint
|
||||
AppId uint `validate:"required"`
|
||||
App App
|
||||
RequestEventId uint `validate:"required"`
|
||||
RequestEvent RequestEvent
|
||||
Amount uint // in sats
|
||||
PaymentRequest string
|
||||
Preimage *string
|
||||
CreatedAt time.Time
|
||||
UpdatedAt time.Time
|
||||
}
|
||||
|
||||
type DBService interface {
|
||||
CreateApp(name string, pubkey string, maxAmount int, budgetRenewal string, expiresAt *time.Time, requestMethods []string) (*App, string, error)
|
||||
}
|
||||
|
||||
const (
|
||||
REQUEST_EVENT_STATE_HANDLER_EXECUTING = "executing"
|
||||
REQUEST_EVENT_STATE_HANDLER_EXECUTED = "executed"
|
||||
REQUEST_EVENT_STATE_HANDLER_ERROR = "error"
|
||||
)
|
||||
const (
|
||||
RESPONSE_EVENT_STATE_PUBLISH_CONFIRMED = "confirmed"
|
||||
RESPONSE_EVENT_STATE_PUBLISH_FAILED = "failed"
|
||||
RESPONSE_EVENT_STATE_PUBLISH_UNCONFIRMED = "unconfirmed"
|
||||
)
|
||||
Loading…
Add table
Add a link
Reference in a new issue