Files
MichilisandClaude Fable 5.1 d04669b6d6 Add lnurlcash-go and the lnurlcash config block
Pin btcec/v2 at v2.3.3: v2.3.4 changed ecdsa.SignCompact's signature and
lnd/keychain (via ln-decodepay) no longer compiles against it. The
lnurlcash package gets its Config with validation and an AES-256-GCM
cipher for note secrets at rest.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
2026-11-13 21:43:16 +00:00

135 lines
4.2 KiB
Go

package internal
import (
"fmt"
"net/url"
"os"
"strings"
"github.com/LightningTipBot/LightningTipBot/internal/lnurlcash"
"github.com/jinzhu/configor"
log "github.com/sirupsen/logrus"
)
var Configuration = struct {
Bot BotConfiguration `yaml:"bot"`
Telegram TelegramConfiguration `yaml:"telegram"`
Database DatabaseConfiguration `yaml:"database"`
Lnbits LnbitsConfiguration `yaml:"lnbits"`
Generate GenerateConfiguration `yaml:"generate"`
Nostr NostrConfiguration `yaml:"nostr"`
// LNURLCash is the optional LUD-25 bearer note feature (see docs/lnurlcash).
LNURLCash lnurlcash.Config `yaml:"lnurlcash"`
}{}
type NostrConfiguration struct {
PrivateKey string `yaml:"private_key"`
}
type GenerateConfiguration struct {
OpenAiBearerToken string `yaml:"open_ai_bearer_token"`
DalleKey string `yaml:"dalle_key"`
DallePrice int64 `yaml:"dalle_price"`
Worker int `yaml:"worker"`
}
type SocksConfiguration struct {
Host string `yaml:"host"`
Username string `yaml:"username"`
Password string `yaml:"password"`
}
type BotConfiguration struct {
SocksProxy *SocksConfiguration `yaml:"socks_proxy,omitempty"`
TorProxy *SocksConfiguration `yaml:"tor_proxy,omitempty"`
LNURLServer string `yaml:"lnurl_server"`
LNURLServerUrl *url.URL `yaml:"-"`
LNURLHostName string `yaml:"lnurl_public_host_name"`
LNURLHostUrl *url.URL `yaml:"-"`
LNURLSendImage bool `yaml:"lnurl_image"`
AdminAPIHost string `yaml:"admin_api_host"`
AdminAPIToken string `yaml:"admin_api_token"`
}
type TelegramConfiguration struct {
MessageDisposeDuration int64 `yaml:"message_dispose_duration"`
ApiKey string `yaml:"api_key"`
}
type DatabaseConfiguration struct {
DbPath string `yaml:"db_path"`
ShopBuntDbPath string `yaml:"shop_buntdb_path"`
BuntDbPath string `yaml:"buntdb_path"`
TransactionsPath string `yaml:"transactions_path"`
GroupsDbPath string `yaml:"groupsdb_path"`
// LNURLCashPath is the sqlite file holding LNURLcash notes, batches and mints.
LNURLCashPath string `yaml:"lnurlcash_path"`
}
type LnbitsConfiguration struct {
AdminId string `yaml:"admin_id"`
AdminKey string `yaml:"admin_key"`
Url string `yaml:"url"`
LnbitsPublicUrl string `yaml:"lnbits_public_url"`
WebhookServer string `yaml:"webhook_server"`
WebhookServerUrl *url.URL `yaml:"-"`
}
func init() {
err := configor.Load(&Configuration, "config.yaml")
if err != nil {
panic(err)
}
webhookUrl, err := url.Parse(Configuration.Lnbits.WebhookServer)
if err != nil {
panic(err)
}
Configuration.Lnbits.WebhookServerUrl = webhookUrl
lnUrl, err := url.Parse(Configuration.Bot.LNURLServer)
if err != nil {
panic(err)
}
Configuration.Bot.LNURLServerUrl = lnUrl
hostname, err := url.Parse(Configuration.Bot.LNURLHostName)
if err != nil {
panic(err)
}
Configuration.Bot.LNURLHostUrl = hostname
if isTestBinary() {
// go test has no config.yaml; a missing LNbits URL must not stop
// unit tests of packages that import this one.
return
}
checkLnbitsConfiguration()
checkLNURLCashConfiguration()
}
// checkLNURLCashConfiguration refuses to start with a broken lnurlcash block:
// a half-configured note feature would mint money it cannot keep track of.
func checkLNURLCashConfiguration() {
if err := Configuration.LNURLCash.Validate(); err != nil {
panic(err)
}
if Configuration.LNURLCash.Enabled && Configuration.Database.LNURLCashPath == "" {
Configuration.Database.LNURLCashPath = "data/lnurlcash.db"
}
}
// isTestBinary reports whether the process is a go test binary.
func isTestBinary() bool {
return strings.HasSuffix(os.Args[0], ".test") || strings.HasSuffix(os.Args[0], ".test.exe")
}
func checkLnbitsConfiguration() {
if Configuration.Lnbits.Url == "" {
panic(fmt.Errorf("please configure a lnbits url"))
}
if Configuration.Lnbits.LnbitsPublicUrl == "" {
log.Warnf("Please specify a lnbits public url otherwise users won't be able to")
} else {
if !strings.HasSuffix(Configuration.Lnbits.LnbitsPublicUrl, "/") {
Configuration.Lnbits.LnbitsPublicUrl = Configuration.Lnbits.LnbitsPublicUrl + "/"
}
}
}