Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Add ability to connect to peers using only PeerID #177

Open
herwig- opened this issue Jul 23, 2024 · 6 comments
Open

Add ability to connect to peers using only PeerID #177

herwig- opened this issue Jul 23, 2024 · 6 comments
Labels
enhancement New feature or request

Comments

@herwig-
Copy link

herwig- commented Jul 23, 2024

when starting the js-peer and accessing localhost:3000
I miss the Field "PeerID to connect to" and the button "Get Multiaddrs"

see:
grafik

@herwig- herwig- changed the title js-peer, Firefox and Chrome - no field "PeerID to connect to js-peer, Firefox and Chrome - no field "PeerID to connect to" Jul 23, 2024
@2color
Copy link
Collaborator

2color commented Jul 26, 2024

At some point this was removed (not exactly sure why as I wasn't the one who removed it). We could potentially re-introduce and resolve PeerIDs to Multiaddrs using the delegated routing.

@herwig-
Copy link
Author

herwig- commented Jul 28, 2024

it would be great to have an example where two peers find each other with the peer-id only (in the best case with a dht for detecting the multiadders)

@2color 2color added the enhancement New feature or request label Aug 13, 2024
@2color 2color changed the title js-peer, Firefox and Chrome - no field "PeerID to connect to" Add ability to connect to peers using only PeerID Aug 13, 2024
@Nkovaturient
Copy link

@2color hey, I would love to work on implementing this "Connect-By-PeerID" feature--
so far, I was diving my way around and somewhat midway.

  • Just need little guidance to solve few queries:--

I have implemented to catch peerId thru peerResponse and DHTResponse, however the output as received is unwelcomed.

export const findPeerById = (libp2p: Libp2pType) => async (peerIdStr: string) => {
  console.log(`🔍 Searching for PeerID: ${peerIdStr}`)
  try {
    const peerId = peerIdFromString(peerIdStr)
    
    // Step 1: Check if the peer is already known in the peerstore
    const knownPeer = await libp2p.peerStore.get(peerId)
    if (knownPeer?.addresses?.length) {
      console.log(`✅ Found peer in peerStore with ${knownPeer.addresses.length} addresses`)
      return knownPeer
    }

    // Step 2: Find the peer using DHT lookup with retries
    let event: QueryEvent | undefined
    let attempts = 0
    while (attempts < 3 && !event) {
      console.log(`🔄 DHT lookup attempt ${attempts + 1}`)
      event = await first(libp2p.services.dht.findPeer(peerId))
      attempts++
    }

    if (!event) {
      console.warn(`⚠️ No DHT response after ${attempts} attempts`)
      return null
    }

    // Step 3: Handle DHT response
    if (event.name === 'FINAL_PEER') {
      const peerData = event.peer
      if (peerData && peerData.multiaddrs?.length > 0) {
        // Convert to PeerInfo format
        const peerInfo = {
          id: peerData.id,
          addresses: peerData.multiaddrs.map(maddr => ({
            multiaddr: maddr,
            isCertified: true
          })),
          protocols: [],
          metadata: new Map(),
          tags: new Map()
        }
        console.log(`✅ Found peer via DHT with ${peerInfo.addresses.length} addresses`)
        return peerInfo
      }
    }

    // Step 4: Handle PEER_RESPONSE as fallback
    if (event.name === 'PEER_RESPONSE') {
      const peerEvent = event as PeerResponseEvent
      if (peerEvent.closer?.[0]) {
        const closestPeer = peerEvent.closer[0]
        // Convert to PeerInfo format
        const peerInfo = {
          id: closestPeer.id,
          addresses: closestPeer.multiaddrs.map(maddr => ({
            multiaddr: maddr,
            isCertified: true
          })),
          protocols: [],
          metadata: new Map(),
          tags: new Map()
        }
        console.log(`✅ Found closest peer via DHT with ${peerInfo.addresses.length} addresses`)
        return peerInfo
      }
    }

    console.warn(`⚠️ No valid peer information found`)
    return null

  } catch (error) {
    console.error(`❌ Peer lookup failed:`, error)
    return null
  }
}

OUTPUT from peerInfo(mainly thru knownpeer, and not DHT)


addresses
: 
(10) [{…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}]
id
: 
Ed25519PeerId {type: 'Ed25519', multihash: Digest, publicKey: Ed25519PublicKey, string: undefined, Symbol(@libp2p/peer-id): true}
metadata
: 
Map(3) {'AgentVersion' => Uint8Array(64), 'last-dial-success' => Uint8Array(13), 'ProtocolVersion' => Uint8Array(0)}
peerRecordEnvelope
: 
Uint8Array(737) [10, 36, 8, 1, 18, 32, 87, 101, 178, 119, 212, 235, 230, 116, 127, 160, 63, 160, 129, 209, 158, 114, 12, 73, 56, 97, 94, 174, 237, 156, 100, 176, 89, 177, 213, 203, 191, 239, 18, 2, 3, 1, 26, 242, 4, 10, 38, 0, 36, 8, 1, 18, 32, 87, 101, 178, 119, 212, 235, 230, 116, 127, 160, 63, 160, 129, 209, 158, 114, 12, 73, 56, 97, 94, 174, 237, 156, 100, 176, 89, 177, 213, 203, 191, 239, 16, 199, 222, 166, 171, 183, 153, 254, 142, 24, 26, 50, 10, 48, 4, …]
protocols
: 
(10) ['/floodsub/1.0.0', '/ipfs/id/1.0.0', '/ipfs/id/push/1.0.0', '/ipfs/kad/1.0.0', '/ipfs/ping/1.0.0', '/libp2p/circuit/relay/0.2.0/hop', '/libp2p/circuit/relay/0.2.0/stop', '/meshsub/1.0.0', '/meshsub/1.1.0', '/meshsub/1.2.0']
tags
: 
Map(7) {'kad-close' => {…}, 'kad-peer' => {…}, 'keep-alive-circuit-relay' => {…}, 'keep-alive-kad-dht' => {…}, 'universal-connectivity' => {…}, …}

Image

Image

@2color
Copy link
Collaborator

2color commented Feb 3, 2025

@Nkovaturient Please submit a PR instead. It makes it easier to review and provide feedback.

@Nkovaturient
Copy link

@Nkovaturient Please submit a PR instead. It makes it easier to review and provide feedback.

@2color sure!

@Nkovaturient
Copy link

@2color created a PR ✅ #210

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants