findProviders method

Future<List<String>> findProviders(
  1. String cid
)

Finds providers for a given CID.

Returns a list of peer IDs that can provide the content identified by cid.

Implementation

Future<List<String>> findProviders(String cid) async {
  try {
    // First check if we have the content locally
    final hasLocal = await _container.get<DatastoreHandler>().hasBlock(cid);
    if (hasLocal) {
      // If we have it locally, return our own peer ID
      return [_container.get<NetworkHandler>().ipfsNode.peerID];
    }

    // Convert string CID to CID object
    final cidObj = CID.decode(cid);

    // Try finding providers through DHT
    final dhtProviders = await _container.get<DHTHandler>().findProviders(
      cidObj,
    );
    if (dhtProviders.isNotEmpty) {
      // Convert V_PeerInfo to String peer IDs
      return dhtProviders
          .map((p) => Base58().encode(Uint8List.fromList(p.peerId)))
          .toList();
    }

    // If DHT lookup fails, try finding through routing handler
    final routingProviders = await _container
        .get<ContentRoutingHandler>()
        .findProviders(cid);
    if (routingProviders.isNotEmpty) {
      return routingProviders;
    }

    // No providers found
    return [];
  } catch (e) {
    // print('Error finding providers for CID $cid: $e');
    return [];
  }
}