Skip to content

Instantly share code, notes, and snippets.

@vasco-santos
Last active September 24, 2020 17:31
Show Gist options
  • Save vasco-santos/c00d266c94b024b21c2d75192c0cad2b to your computer and use it in GitHub Desktop.
Save vasco-santos/c00d266c94b024b21c2d75192c0cad2b to your computer and use it in GitHub Desktop.
libp2p routing setup
const Libp2p = require('libp2p')
const KadDht = require('libp2p-kad-dht')
const DelegatedContentRouter = require('libp2p-delegated-content-routing')
const DelegatedPeerRouter = require('libp2p-delegated-peer-routing')
const ipfsHttpClient = require('ipfs-http-client')
const PeerId = require('peer-id')
const CID = require('cids')
const peerId = await PeerId.create()
const delegateContentRouter = new DelegatedContentRouter(peerId, ipfsHttpClient({
 protocol: 'https',
 port: 443,
 host: 'node0.delegate.ipfs.io'
}))
const delegatePeerRouter = new DelegatedPeerRouter(ipfsHttpClient({
 protocol: 'https',
 port: 443,
 host: 'node0.delegate.ipfs.io'
}))
const node = await Libp2p.create({
peerId,
  // …
  modules: {
// … other required modules
  dht: KadDht,
  contentRouting: [delegateContentRouter],
  peerRouting: [delegatePeerRouter]
  }
})
await node.start()
// Content Routing
const cid = new CID('QmS4ustL54uo8FzR9455qaxZwuMiUhyvMcX9Ba8nUH4uVv')
await node.provide(cid)
console.log('providing %s', cid.toBaseEncodedString())
for await (const { id, multiaddrs } of node.findProviders(cid)) {
console.log('found peer', id, multiaddrs)
}
// Peer Routing
try {
const { id, multiaddrs } = await node.findPeer('peerid')
console.log('found peer details', id, multiaddrs)
} catch (err) {
console.error(err)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment