sdk: cached mint keys queries.

This commit is contained in:
fiatjaf
2025-10-26 16:45:50 -03:00
parent 8b38e1aec9
commit a7b29c5c96
2 changed files with 31 additions and 0 deletions

View File

@@ -14,6 +14,7 @@ import (
"fiatjaf.com/nostr/sdk/hints/memoryh" "fiatjaf.com/nostr/sdk/hints/memoryh"
"fiatjaf.com/nostr/sdk/kvstore" "fiatjaf.com/nostr/sdk/kvstore"
kvstore_memory "fiatjaf.com/nostr/sdk/kvstore/memory" kvstore_memory "fiatjaf.com/nostr/sdk/kvstore/memory"
"github.com/btcsuite/btcd/btcec/v2"
) )
// System represents the core functionality of the SDK, providing access to // System represents the core functionality of the SDK, providing access to
@@ -41,6 +42,7 @@ type System struct {
FollowSetsCache cache.Cache32[GenericSets[nostr.PubKey, ProfileRef]] FollowSetsCache cache.Cache32[GenericSets[nostr.PubKey, ProfileRef]]
TopicSetsCache cache.Cache32[GenericSets[string, Topic]] TopicSetsCache cache.Cache32[GenericSets[string, Topic]]
ZapProviderCache cache.Cache32[nostr.PubKey] ZapProviderCache cache.Cache32[nostr.PubKey]
MintKeysCache cache.Cache32[map[uint64]*btcec.PublicKey]
Hints hints.HintsDB Hints hints.HintsDB
Pool *nostr.Pool Pool *nostr.Pool
RelayListRelays *RelayStream RelayListRelays *RelayStream
@@ -135,6 +137,9 @@ func NewSystem() *System {
if sys.ZapProviderCache == nil { if sys.ZapProviderCache == nil {
sys.ZapProviderCache = cache_memory.New[nostr.PubKey](8000) sys.ZapProviderCache = cache_memory.New[nostr.PubKey](8000)
} }
if sys.MintKeysCache == nil {
sys.MintKeysCache = cache_memory.New[map[uint64]*btcec.PublicKey](8000)
}
if sys.Store == nil { if sys.Store == nil {
sys.Store = &nullstore.NullStore{} sys.Store = &nullstore.NullStore{}

View File

@@ -2,12 +2,16 @@ package sdk
import ( import (
"context" "context"
"crypto/sha256"
"io" "io"
"net/http" "net/http"
"strings" "strings"
"time" "time"
"fiatjaf.com/nostr" "fiatjaf.com/nostr"
"fiatjaf.com/nostr/nip60"
"fiatjaf.com/nostr/nip60/client"
"github.com/btcsuite/btcd/btcec/v2"
"github.com/tidwall/gjson" "github.com/tidwall/gjson"
) )
@@ -47,3 +51,25 @@ func (sys *System) FetchZapProvider(ctx context.Context, pk nostr.PubKey) nostr.
sys.ZapProviderCache.SetWithTTL(pk, nostr.ZeroPK, time.Hour*2) sys.ZapProviderCache.SetWithTTL(pk, nostr.ZeroPK, time.Hour*2)
return nostr.ZeroPK return nostr.ZeroPK
} }
// FetchMintKeys fetches the active keyset from the given mint URL and parses the keys.
// It uses a cache to avoid repeated fetches.
func (sys *System) FetchMintKeys(ctx context.Context, mintURL string) (map[uint64]*btcec.PublicKey, error) {
hash := sha256.Sum256([]byte(mintURL))
if v, ok := sys.MintKeysCache.Get(hash); ok {
return v, nil
}
keyset, err := client.GetActiveKeyset(ctx, mintURL)
if err != nil {
return nil, err
}
ksKeys, err := nip60.ParseKeysetKeys(keyset.Keys)
if err != nil {
return nil, err
}
sys.MintKeysCache.SetWithTTL(hash, ksKeys, time.Hour*6)
return ksKeys, nil
}