applyDefaults function

Future<void> applyDefaults(
  1. Config config
)

Apply default options to a Config instance if they haven't been set by the user.

Implementation

Future<void> applyDefaults(Config config) async {
  // Default Identity (KeyPair)
  if (config.peerKey == null) {
    config.peerKey = await crypto_ed25519.generateEd25519KeyPair();
  }

  // Default Transports
  // Note: Default transports like TCP might require connManager and resourceManager.
  // These are typically initialized in Config.newNode() or by Swarm.
  // It's safer for the main constructor (Libp2p.new_ or Config.newNode)
  // to add default transports if the list is empty, after core components
  // like ResourceManager and ConnManager are available.
  // For now, applyDefaults will not add a default transport directly.
  // The validation in Config.newNode() will ensure transports are provided.
  // if (config.transports.isEmpty) {
  //   // config.transports.add(TCPTransport(connManager: config.connManager!, resourceManager: ...)); // This shows dependency
  // }

  // Default Security Protocol
  // NoiseSecurity.create requires a KeyPair.
  // Ensure peerKey is available (defaulted above or provided by user).
  if (!config.insecure && config.securityProtocols.isEmpty) {
    if (config.peerKey != null) {
      config.securityProtocols.add(await NoiseSecurity.create(config.peerKey!));
    } else {
      // This case should not happen if peerKey is defaulted above.
      throw StateError('Cannot apply default Noise security: peerKey is null.');
    }
  }

  // Default Muxers (Yamux is already handled by the defaultMuxers Option if muxers list is empty)
  if (config.muxers.isEmpty) {
    // This re-applies the Yamux default if it wasn't added via Option.
    // The defaultMuxers Option is usually added by Libp2p.new_ if no muxer options are given.
    // However, direct Config manipulation might bypass this, so ensuring it here is safe.
    await config.withMuxer(
      '/yamux/1.0.0',
      (secureConn, isClient) {
        if (secureConn is! TransportConn) {
          throw ArgumentError(
              'Default Yamux factory (via applyDefaults) requires a TransportConn, '
              'but received ${secureConn.runtimeType}.');
        }
        return YamuxSession(
          secureConn,
          const MultiplexerConfig(),
          isClient,
        );
      },
    );
  }

  // Default Connection Manager
  if (config.connManager == null) {
    config.connManager = p2p_conn_mgr.ConnectionManager();
  }

  // Default Event Bus
  if (config.eventBus == null) {
    config.eventBus = BasicBus();
  }

  // Default Identify Service Settings
  config.identifyUserAgent ??= 'dart-libp2p/0.1.0'; // Example version
  config.identifyProtocolVersion ??= 'ipfs/0.1.0';
  config.disableSignedPeerRecord ??= false; // Enable signed records by default
  config.disableObservedAddrManager ??= false;

  // Default AddrsFactory
  config.addrsFactory ??= _defaultAddrsFactoryInternal; // Use internal version

  // Default service enable flags (already have defaults in Config, but can be asserted here if needed)
  // config.enablePing is already true by default in Config.
  // config.enableRelay is already false by default.
  // config.enableAutoNAT is already false by default.
  // config.enableHolePunching is already true by default.

  // Note: PeerStore and ResourceManager are created later in Config.newNode(),
  // but the defaults set here (like PeerKey for PeerStore) will be used by their creation.
}