libp2p / js-libp2p

The JavaScript Implementation of libp2p networking stack.
https://libp2p.io
Other
2.29k stars 438 forks source link

TypeError: peer[0].getPeerId is not a function #2437

Closed matisalimbene closed 5 months ago

matisalimbene commented 5 months ago

when calling await node.dial(remotePeerId);

I get

2024-03-12 17:40:28 info: setting up winston logger
2024-03-12 17:40:29 info: starting up...
2024-03-12 17:40:29 info: ordbitdb path: /data/orbitdb/1
2024-03-12 17:40:29 info: blockstore path: /data/ipfs/1
2024-03-12 17:40:29 info: identityId: userA
2024-03-12 17:40:30 info: orbitdb address [/orbitdb/xxxx]
2024-03-12 17:40:30 info: orbitdb name [xxxx]
2024-03-12 17:40:30 info: libp2p peerId [12D3KooWGoTohkr7JoLztrej5yYWzeEyatgstHEnd5vaxQjsDv9x]
2024-03-12 17:40:30 info: remotePeerId [12D3KooWSRSsR4dNqLruVQjcRr2YL5dLvopZmkf1zsdL1vXkfyJL]
file:///usr/src/app/node_modules/libp2p/dist/src/get-peer.js:17
        const peerIdStr = peer[0].getPeerId();
                                  ^

TypeError: peer[0].getPeerId is not a function
    at getPeerAddress (file:///usr/src/app/node_modules/libp2p/dist/src/get-peer.js:17:35)
    at DefaultConnectionManager.openConnection (file:///usr/src/app/node_modules/libp2p/dist/src/connection-manager/index.js:299:28)
    at Libp2pNode.dial (file:///usr/src/app/node_modules/libp2p/dist/src/libp2p.js:213:50)
    at startUp (file:///usr/src/app/services/orbitdb.srv.js:115:32)
    at async file:///usr/src/app/startup/ipfs.js:4:1

Node.js v20.11.1
achingbrain commented 5 months ago

Is remotePeerId a string? If so you need to convert it to a PeerId first see the types of the peer arg in the API docs: https://libp2p.github.io/js-libp2p/interfaces/libp2p.index.Libp2p.html#dial

import { peerIdFromString } from '@libp2p/peer-id'

const peer = peerIdFromString('12D3KooWSR...')

If it's already a PeerId, could you share a bit more of your code or a link to a repo please?

matisalimbene commented 5 months ago

@achingbrain Thanks, that was it. It was a string. Now I’m getting the error ('The dial request has no valid addresses', codes.ERR_NO_VALID_ADDRESSES) but I understand that I need to provide a multiaddrs for the PeerID, correct?

If I get the data for my local peer, I see that it has no addresses:

{
  addresses: [],
  protocols: [
    '/floodsub/1.0.0',
    '/ipfs/bitswap/1.0.0',
    '/ipfs/bitswap/1.1.0',
    '/ipfs/bitswap/1.2.0',
    '/ipfs/id/1.0.0',
    '/ipfs/id/push/1.0.0',
    '/libp2p/circuit/relay/0.2.0/stop',
    '/meshsub/1.0.0',
    '/meshsub/1.1.0',
    '/orbitdb/heads/orbitdb/zdpuAwaoeS9hQ28vKwbAJFnJKMduyRnHrbugSFk4Ffuqkj77p',
    '/webrtc-signaling/0.0.1'
  ],
  metadata: Map(2) {
    'AgentVersion' => Uint8Array(31) [
      108, 105,  98, 112,  50, 112,  47,  49,
       46,  50,  46,  52,  32,  85, 115, 101,
      114,  65, 103, 101, 110, 116,  61, 118,
       50,  48,  46,  49,  49,  46,  48
    ],
    'ProtocolVersion' => Uint8Array(10) [
      105, 112, 102, 115, 47,
       48,  46,  49,  46, 48
    ]
  },
  tags: Map(0) {},
  id: PeerId(12D3KooWEyLcVkAsajY4NAXc2DYy5vM4ZPWpBB283DNjX44emrZc),
  peerRecordEnvelope: undefined
}

Does this mean that I need to create/configure my local peer manually instead of relying on libp2p? Thanks for your help.

achingbrain commented 5 months ago

If you are trying to dial a remote peer by it's peer id, it has to be discoverable by some means in order for your node to get the multiaddr to dial.

There are several peer discovery modules, such as @libp2p/mdns for local networks, @libp2p/bootstrap for connecting to nodes on startup, etc, or the peer should be resolvable via the peer routing - this means it's functioning as a DHT server (va @libp2p/kad-dht) or it's known to a delegated router or something else depending on your configuration.

If your peer is not discoverable then you'll need to know an explicit multiaddr to dial, and that multiaddr must use transports that the dialing node is configured with.

matisalimbene commented 5 months ago

Perfect. Thanks @achingbrain! Should I close this issue? It’s done for my part.