2024-09-02 00:51:02 +00:00
|
|
|
package config
|
2024-08-07 21:24:38 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2024-09-02 00:51:02 +00:00
|
|
|
"grain/server/utils"
|
2024-08-07 21:24:38 +00:00
|
|
|
"strconv"
|
|
|
|
)
|
|
|
|
|
2024-10-16 13:57:58 +00:00
|
|
|
// Check if a pubkey or npub is whitelisted
|
2024-08-07 21:24:38 +00:00
|
|
|
func IsPubKeyWhitelisted(pubKey string) bool {
|
2024-10-16 13:57:58 +00:00
|
|
|
cfg := GetWhitelistConfig()
|
|
|
|
if !cfg.PubkeyWhitelist.Enabled {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, whitelistedKey := range cfg.PubkeyWhitelist.Pubkeys {
|
|
|
|
if pubKey == whitelistedKey {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, npub := range cfg.PubkeyWhitelist.Npubs {
|
|
|
|
decodedPubKey, err := utils.DecodeNpub(npub)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println("Error decoding npub:", err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if pubKey == decodedPubKey {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
2024-08-07 21:24:38 +00:00
|
|
|
}
|
|
|
|
|
2024-10-16 13:57:58 +00:00
|
|
|
// Check if a kind is whitelisted
|
2024-08-07 21:24:38 +00:00
|
|
|
func IsKindWhitelisted(kind int) bool {
|
2024-10-16 13:57:58 +00:00
|
|
|
cfg := GetWhitelistConfig()
|
|
|
|
if !cfg.KindWhitelist.Enabled {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|