How to use libp2p - 3 common examples

To help you get started, we’ve selected a few libp2p examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github libp2p / js-libp2p / examples / pnet-ipfs / index.js View on Github external
const privateLibp2pBundle = require('./libp2p-bundle')
const { mkdirp } = require('./utils')

// Create two separate repo paths so we can run two nodes and check their output
const repo1 = path.resolve('./tmp', 'repo1', '.ipfs')
const repo2 = path.resolve('./tmp', 'repo2', '.ipfs')
mkdirp(repo1)
mkdirp(repo2)

// Create a buffer and write the swarm key to it
const swarmKey = Buffer.alloc(95)
writeKey(swarmKey)

// This key is for the `TASK` mentioned in the writeFileSync calls below
const otherSwarmKey = Buffer.alloc(95)
writeKey(otherSwarmKey)

// Add the swarm key to both repos
const swarmKey1Path = path.resolve(repo1, 'swarm.key')
const swarmKey2Path = path.resolve(repo2, 'swarm.key')
fs.writeFileSync(swarmKey1Path, swarmKey)
// TASK: switch the commented out line below so we're using a different key, to see the nodes fail to connect
fs.writeFileSync(swarmKey2Path, swarmKey)
// fs.writeFileSync(swarmKey2Path, otherSwarmKey)

// Create the first ipfs node
const node1 = new IPFS({
  repo: repo1,
  libp2p: privateLibp2pBundle(swarmKey1Path),
  config: {
    Addresses: {
      // Set the swarm address so we dont get port collision on the nodes
github libp2p / js-libp2p / examples / pnet-ipfs / index.js View on Github external
const assert = require('assert').strict
const { generate: writeKey } = require('libp2p/src/pnet')
const path = require('path')
const fs = require('fs')
const privateLibp2pBundle = require('./libp2p-bundle')
const { mkdirp } = require('./utils')

// Create two separate repo paths so we can run two nodes and check their output
const repo1 = path.resolve('./tmp', 'repo1', '.ipfs')
const repo2 = path.resolve('./tmp', 'repo2', '.ipfs')
mkdirp(repo1)
mkdirp(repo2)

// Create a buffer and write the swarm key to it
const swarmKey = Buffer.alloc(95)
writeKey(swarmKey)

// This key is for the `TASK` mentioned in the writeFileSync calls below
const otherSwarmKey = Buffer.alloc(95)
writeKey(otherSwarmKey)

// Add the swarm key to both repos
const swarmKey1Path = path.resolve(repo1, 'swarm.key')
const swarmKey2Path = path.resolve(repo2, 'swarm.key')
fs.writeFileSync(swarmKey1Path, swarmKey)
// TASK: switch the commented out line below so we're using a different key, to see the nodes fail to connect
fs.writeFileSync(swarmKey2Path, swarmKey)
// fs.writeFileSync(swarmKey2Path, otherSwarmKey)

// Create the first ipfs node
const node1 = new IPFS({
  repo: repo1,
github polkadot-js / client / packages / client-p2p / src / create / node.ts View on Github external
export default async function createNode ({ externalIp, p2p: { address = defaults.ADDRESS, discoverStar = false, discoverBoot = true, nodes = [], port = defaults.PORT, type, wrtc } }: Config, { chain: { bootNodes = [] } }: ChainInterface, l: Logger): Promise {
  const envType = type || 'nodejs';
  const peerBook = await createPeerBook([]);
  const peerInfo = await createListener(envType, { address, discoverStar, externalIp, port });
  const modules = createModules(envType, peerInfo, { bootNodes, discoverBoot, discoverStar, nodes, wrtc });
  const addrs = peerInfo.multiaddrs.toArray().map((addr): string => addr.toString());

  l.log('creating Libp2p with addresses:');

  addrs.forEach((addr): void => l.log(`    ${addr}`));

  return new Libp2p({
    config: {
      ...config,
      peerDiscovery: {
        autoDial: false,
        enabled: true,
        webRTCStar: {
          enabled: discoverStar
        },
        websocketStar: {
          enabled: discoverStar
        }
      }
    },
    modules,
    peerBook,
    peerInfo

libp2p

JavaScript implementation of libp2p, a modular peer to peer network stack

Apache-2.0 OR MIT
Latest version published 4 days ago

Package Health Score

95 / 100
Full package analysis

Popular libp2p functions