import '../config/bootstrap_relays.dart'; import '../config/nip85_defaults.dart'; import '../config/broadcast_defaults.dart'; import '../config/logger_defaults.dart'; import '../config/request_defaults.dart'; import '../data_layer/repositories/signers/bip340_event_signer.dart'; import '../domain_layer/entities/auth_handler.dart'; import '../domain_layer/entities/cashu/cashu_user_seedphrase.dart'; import '../domain_layer/entities/cache_eviction.dart'; import '../domain_layer/entities/event_filter.dart'; import '../domain_layer/entities/nip_85.dart'; import '../domain_layer/repositories/cache_manager.dart'; import '../domain_layer/repositories/event_signer.dart'; import '../domain_layer/repositories/event_verifier.dart'; import '../domain_layer/repositories/wallets_repo.dart'; import '../shared/logger/log_level.dart'; /// Configuration class for the Nostr Development Kit (NDK) /// /// This class holds various settings and dependencies required for /// the NDK to function properly class NdkConfig { /// The verifier used to validate Nostr events. E.g. RustEventVerifier(), Bip340EventVerifier EventVerifier eventVerifier; /// The cache manager (DB) used to store and retrieve Nostr data. E.g MemCacheManager() CacheManager cache; /// The wallets repository used to manage wallet data. E.g MemWalletsRepo() WalletsRepo? walletsRepo; /// Factory for creating EventSigner instances. Defaults to Bip340EventSigner. LocalEventSignerFactory eventSignerFactory; /// The engine mode to use for Nostr network operations (inbox/outbox mode). /// /// Defaults to [NdkEngine.RELAY_SETS]. NdkEngine engine; /// A list of relay URLs to ignore for inbox/outbox operations. List ignoreRelays; /// A list of initial relay URLs to use for bootstrapping the network. /// These connect on start. /// /// Defaults to [DEFAULT_BOOTSTRAP_RELAYS]. List bootstrapRelays; /// filters that are applied to the output stream List eventOutFilters; /// default timeout for queries \ /// this value is used if no individual timeout is set for a query Duration defaultQueryTimeout; /// timeout for broadcasts Duration defaultBroadcastTimeout; /// percentage of relays that need to respond with "OK" for the broadcast to be considered done \ /// value between 0.0 and 1.0 double defaultBroadcastConsiderDonePercent; /// cashu user seed phrase, required for using cashu features \ /// you can use CashuSeed.generateSeedPhrase() to generate a new seed phrase \ /// Store this securely! Seed phrase allow full access to cashu funds! final CashuUserSeedphrase? cashuUserSeedphrase; /// when false (default), automatic cashu quote completion on startup /// requires an explicit `Cashu.restore()` call for the mint/unit first; /// when true, it instead runs a bounded NUT-09 scan itself to verify the /// mint derivation counter before minting. See [Cashu.retrieveFunds]. bool autoVerifyMintCounters; /// whether to save broadcasted events to cache by default bool defaultBroadcastSaveToCache; /// log level LogLevel logLevel; /// User agent for websockets on dart:io, set globally on `WebSocket.userAgent`. /// /// Null (default) leaves the app's user agent untouched. Pass /// [RequestDefaults.DEFAULT_USER_AGENT] to identify as NDK. String? userAgent; /// Whether native WebSocket connections negotiate per-message compression. /// /// Defaults to true, preserving existing behavior. Disable this to reduce /// memory and codec overhead at the cost of sending more bytes for /// compressible traffic. Browser WebSocket APIs do not expose compression /// controls, so this option has no effect on web builds. bool webSocketCompression; /// Native WebSocket heartbeat interval. Null disables client pings. /// /// Longer intervals reduce idle network traffic, but also delay detection of /// silent connection failures (the pong timeout equals this interval). /// Browser WebSocket APIs do not expose heartbeat controls. Duration? webSocketPingInterval; /// Maximum exponential reconnect step, starting at 500 milliseconds. /// /// The default of 4 caps retries at 4 seconds. For example, 7 caps retries /// at 32 seconds. This only affects failed connections, not event delivery /// on healthy connections. int webSocketReconnectMaximumStep; /// Enable fetched ranges tracking. /// When enabled, NDK tracks which time ranges have been fetched from which relays. /// Disabled by default for performance. bool fetchedRangesEnabled; /// Has no effect. A connection now carries at most one identity, chosen when /// it is opened: an anonymous connection never answers a challenge and a /// bound one always does, so there is nothing left to choose. @Deprecated('Has no effect, a connection is bound to an identity or to none') bool eagerAuth; /// Timeout for AUTH callbacks (how long to wait for AUTH OK response). /// Defaults to 30 seconds. Duration authCallbackTimeout; /// Asked before an identity authenticates on a relay or a Blossom server. /// Without it, a request that does not pass `auth` reveals no identity. /// `(_, _) async => true` authenticates as the logged account wherever asked. AuthHandler? authHandler; /// Interval for retrying pending broadcast deliveries while relays remain connected. Duration pendingDeliveryRetryInterval; /// Whether persisted broadcast deliveries are retried automatically in the /// background. /// /// Disable this for short-lived clients such as command-line tools that /// should only perform the network work requested by the current command. bool pendingDeliveryRetriesEnabled; /// Default trusted providers for NIP-85 trusted assertions. List defaultTrustedProviders; /// Whether background cache eviction scheduling is enabled. bool cacheEvictionEnabled; /// Cache eviction policy used by the background scheduler. EvictionPolicy cacheEvictionPolicy; /// Delay before the first scheduled cache eviction run after startup. Duration cacheEvictionStartupDelay; /// Interval between background cache eviction runs. Duration cacheEvictionInterval; /// Whether to run cache eviction once on startup before periodic runs. bool runCacheEvictionOnStartup; /// Development aid, off by default. Flutter apps can pass `kDebugMode`. /// /// When enabled: /// - request ids sent to relays start with the request name, so relays /// see which usecase opened each subscription. Keep it off in production. bool debugMode; /// Creates a new instance of [NdkConfig]. /// /// [eventVerifier] The verifier used to validate Nostr events. \ /// [cache] The cache manager for storing and retrieving Nostr data. \ /// [eventSignerFactory] Factory for creating EventSigner instances (defaults to Bip340EventSigner). \ /// [engine] The engine mode to use (defaults to RELAY_SETS). \ /// [ignoreRelays] A list of relay URLs to ignore (defaults to an empty list). \ /// [bootstrapRelays] A list of initial relay URLs (defaults to DEFAULT_BOOTSTRAP_RELAYS). \ /// [eventOutFilters] A list of filters to apply to the output stream (defaults to an empty list). \ /// [defaultQueryTimeout] The default timeout for queries (defaults to DEFAULT_QUERY_TIMEOUT). \ /// [logLevel] The log level for the NDK (defaults to warning). /// [cashuUserSeedphrase] The cashu user seed phrase, required for using cashu features /// [webSocketCompression] Whether native WebSockets negotiate compression. NdkConfig({ required this.eventVerifier, required this.cache, this.eventSignerFactory = const Bip340EventSignerFactory(), this.walletsRepo, this.engine = NdkEngine.RELAY_SETS, this.ignoreRelays = const [], this.bootstrapRelays = DEFAULT_BOOTSTRAP_RELAYS, this.eventOutFilters = const [], this.defaultQueryTimeout = RequestDefaults.DEFAULT_QUERY_TIMEOUT, this.defaultBroadcastTimeout = BroadcastDefaults.TIMEOUT, this.defaultBroadcastConsiderDonePercent = BroadcastDefaults.CONSIDER_DONE_PERCENT, this.defaultBroadcastSaveToCache = BroadcastDefaults.SAVE_TO_CACHE, this.logLevel = defaultLogLevel, this.userAgent, this.webSocketCompression = true, this.webSocketPingInterval = const Duration(seconds: 10), this.webSocketReconnectMaximumStep = 4, this.cashuUserSeedphrase, this.autoVerifyMintCounters = false, this.fetchedRangesEnabled = false, // ignore: deprecated_member_use_from_same_package this.eagerAuth = false, this.authCallbackTimeout = RequestDefaults.DEFAULT_AUTH_CALLBACK_TIMEOUT, this.authHandler, this.pendingDeliveryRetryInterval = const Duration(seconds: 15), this.pendingDeliveryRetriesEnabled = true, this.defaultTrustedProviders = DEFAULT_NIP85_PROVIDERS, this.cacheEvictionEnabled = false, this.cacheEvictionPolicy = const EvictionPolicy(), this.cacheEvictionStartupDelay = const Duration(minutes: 1), this.cacheEvictionInterval = const Duration(hours: 1), this.runCacheEvictionOnStartup = true, this.debugMode = false, }); } /// Enum representing different engine modes for Nostr network operations. enum NdkEngine { /// Uses relay sets for network operations. // ignore: constant_identifier_names RELAY_SETS, /// Uses Just-In-Time (JIT) mode for network operations. // ignore: constant_identifier_names JIT, }