grain/config/Whitelist.go

58 lines
1.0 KiB
Go
Raw Normal View History

2024-09-02 00:51:02 +00:00
package config
import (
"fmt"
2024-09-02 00:51:02 +00:00
"grain/server/utils"
"strconv"
)
// Helper function to check if a pubkey or npub is whitelisted
func IsPubKeyWhitelisted(pubKey string) bool {
2024-09-02 00:51:02 +00:00
cfg := GetConfig()
if !cfg.PubkeyWhitelist.Enabled {
return true
}
// Check pubkeys
for _, whitelistedKey := range cfg.PubkeyWhitelist.Pubkeys {
if pubKey == whitelistedKey {
return true
}
}
// Check npubs
for _, npub := range cfg.PubkeyWhitelist.Npubs {
2024-09-02 00:51:02 +00:00
decodedPubKey, err := utils.DecodeNpub(npub)
if err != nil {
fmt.Println("Error decoding npub:", err)
continue
}
if pubKey == decodedPubKey {
return true
}
}
return false
}
func IsKindWhitelisted(kind int) bool {
2024-09-02 00:51:02 +00:00
cfg := GetConfig()
if !cfg.KindWhitelist.Enabled {
return true
}
// Check event kinds
for _, whitelistedKindStr := range cfg.KindWhitelist.Kinds {
whitelistedKind, err := strconv.Atoi(whitelistedKindStr)
if err != nil {
fmt.Println("Error converting whitelisted kind to int:", err)
continue
}
if kind == whitelistedKind {
return true
}
}
return false
}