Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

js-peer: add ability to connect via peerID #210

Open
wants to merge 6 commits into
base: main
Choose a base branch
from

Conversation

Nkovaturient
Copy link

@Nkovaturient Nkovaturient commented Feb 3, 2025

Issue: #177

  • PR created as part and discussion, here -> Issue

Aims:

  • Example where two peers find each other with the peer-id only (in the best case with a dht for detecting the multiadders)
  • Function to fetch peerinfo, connect thru known peers in peerStore
  • DHT lookup attempts
  • Compnent to render the list of peerIds connected with the entered peerID

Description:
1)findByPeerId function aims to find through PeerStore or DHT Lookup
2)DHT response and peerResponse as fallback

Fix needed:

  • peerInfo response is retrieving a list of mutiaddrs alongwith bootstrap addrs, however on clicking connect thru either of them, an error of NoValidAddressesError: The dial request has no valid addresses or "DHT lookup failed"

  • OUTPUT from PEERINFO obj(mainly thru Known Peer, 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' => {…}, …}

Screenshot (476)

Screenshot (476)

  • why DHT lookup is failing?
  • How to fetch peerInfo by entering peerId of known connections with valid addrs

}

// Step 2: Find the peer using DHT lookup with retries
let event: QueryEvent | undefined;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of using the DHT service, you should use libp2p.peerRouting.findPeer(peerId) which abstracts from the specific peerRouting implementations. This way, it can benefit from both delegated routing and the DHT.

I would also reduce the scope of this PR and not introduce the kadDHT.

Copy link
Collaborator

@2color 2color Feb 4, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See https://github.com/ipfs-shipyard/www-helia-identify/blob/main/src/index.ts#L59-L86 for how to simplify.

You probably don't even need the onPorgress hook from findPeer.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

alryt will implement and fix as desired

Copy link
Collaborator

@2color 2color left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR, Neha.

Before a proper review:

  • Can you please run prettier on the files you edited in this PR to format changes (I've opened js-peer: add prettier for consistent styling #211 to address this). A lot of changes in this PR are code style changes adding unnecessary noise that makes reviewing harder.
  • Can you please remove any commented out code that isn't relevant for this.

@2color 2color changed the title Feat: Connect via peerID js-peer: add ability to connect via peerID Feb 4, 2025
@Nkovaturient
Copy link
Author

Thanks for the PR, Neha.

Before a proper review:

  • Can you please run prettier on the files you edited in this PR to format changes (I've opened js-peer: add prettier for consistent styling #211 to address this). A lot of changes in this PR are code style changes adding unnecessary noise that makes reviewing harder.
  • Can you please remove any commented out code that isn't relevant for this.

Roger that! @2color

@Nkovaturient
Copy link
Author

Hey, @2color
wanna update you with recent commits:-

  • ran prettier on files and code changes
  • modified kadDHT to peerRouting methods-indeed an effective way of finding peers
  • updated the peerFind handler in index.tsx
    Screenshot (486)

@Nkovaturient
Copy link
Author

Hey, kindly review and provide guidance--
summary of the key changes made for peer connectivity:

Basically findBypeerID is fetching a list of multiaddrs --> in peer-maddr.tsx -we are filtering it with valid transport stack --> then trying to connect with either of them handleConnect

Connection Handling:

  • Fixed connection object creation to use actual libp2p connection
  • Updated connection type interface to match simplified structure
  • Added proper connection state management

Transport Protocol Validation:

  • Added checks for WebTransport + certhash
  • Added checks for WebRTC + QUIC + certhash
  • Added external address validation

Connection Flow:

  • Parse multiaddr to extract peerID
  • Validate transport stack
  • Attempt connection via connectToMultiaddr
  • Store successful connection in state
  • Clear resolved multiaddrs after connection
  • Handle connection errors with user feedback

UI Improvements:

  • Added connection priority system
  • Show transport protocol badges
  • Display connection status indicators
  • Added loading states during connection

Screenshot (487)
Screenshot (488)

Screenshot (489)

@Nkovaturient
Copy link
Author

@2color any updates/guidance?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants