Skip to content
This repository has been archived by the owner on Jan 24, 2022. It is now read-only.

hoprnet/hopr-connect

Repository files navigation

HOPR Logo

HOPR

A project by the HOPR Association

HOPR is a privacy-preserving messaging protocol which enables the creation of a secure communication network via relay nodes powered by economic incentives using digital tokens.

hopr-connect

IMPORTANT: This repository is un-maintained. The development of hopr-connect has been integrated into https://github.com/hoprnet/hoprnet.

Disclaimer: support for libp2p test suite is still WIP, see KNOWN ISSUES

Description

A transport module for js-libp2p that handles NAT traversal automatically by using peers in the network and without requiring external resources such as public STUN or TURN servers.

Main features

See detailed architecture description for more information.

  • fully compatible with js-libp2p, see KNOWN ISSUES
  • automatic usage of WebRTC:
    • try direct TCP connection, if not succesful
    • use any other available peer in the network as signalling server
    • perform WebRTC handshake(s)
    • upgrade to direct connection if possible
    • otherwise fallback to existing relayed connection
  • use nodes in the network as STUN and TURN servers
  • reconnect handling

Information for VPS / docker / firewalls

HoprConnect binds to a TCP socket as given by the configuration. It also bind to a UDP socket on the same port to perform and answer STUN requests from WebRTC and at startup to publish its own public IPv4 address.

Usage

Dependencies

  • libp2p >= 0.31
  • Node.js 14.x, also tested with Node.js 12.x
  • yarn

Startup

Start a bootstrapServer

const libp2p = require('libp2p')
const MPLEX = require('libp2p-mplex')
import { NOISE } from '@chainsafe/libp2p-noise'
const PeerId = require('peer-id')

import HoprConnect from 'hopr-connect'
import { Multiaddr } from 'multiaddr'

const peerId = await PeerId.create({ keyType: 'secp256k1' })

const node = await libp2p.create({
  peerId,
  modules: {
    transport: [HoprConnect],
    streamMuxer: [MPLEX],
    connEncryption: [NOISE],
    peerDiscovery: [HoprConnect.discovery]
  },
  addresses: {
    listen: new Multiaddr(`/ip4/127.0.0.1/tcp/9091/p2p/${peerId.toB58String()}`)
  },
  dialer: {
    // Temporary fix
    addressSorter: (ma: Multiaddr) => ma
  }
})

Start another client

const libp2p = require('libp2p')
const MPLEX = require('libp2p-mplex')
import { NOISE } from '@chainsafe/libp2p-noise'
const PeerId = require('peer-id')

import HoprConnect from 'hopr-connect'
import { Multiaddr } from 'multiaddr'

const bootstrapId = '16Uiu2HAmCPgzWWQWNAn2E3UXx1G3CMzxbPfLr1SFzKqnFjDcbdwg' // Change this
const peerId = await PeerId.create({ keyType: 'secp256k1' })

const node = await libp2p.create({
  peerId
  modules: {
    transport: [HoprConnect],
    streamMuxer: [MPLEX],
    connEncryption: [NOISE],
    peerDiscovery: [HoprConnect.discovery]
  },
  addresses: {
    listen: new Multiaddr(`/ip4/127.0.0.1/tcp/9092/p2p/${peerId.toB58String()}`)
  },
  config: {
    HoprConnect: {
      bootstrapServers: [new Multiaddr(`/ip4/127.0.0.1/tcp/9091/p2p/${bootstrapId.toB58String()}`)],
      // Testing:
      __noDirectConnections: false, // set to true to simulate NAT
      __noWebRTCUpgrade: false // set to true to simulate bidirectional NAT
    }
  },
  dialer: {
    // Temporary fix
    addressSorter: (ma: Multiaddr) => ma,
  }
})

Known issues

  • IPv6 support disabled for the moment
  • [WIP] libp2p test suite

Contributors (in alphabetical order)

and the rest of the HOPR team!