Created
August 22, 2019 18:33
-
-
Save Pierozi/b1b31b95f91797661a0dfdd399a75941 to your computer and use it in GitHub Desktop.
LibP2P Node with circuit
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict' | |
/* eslint-disable no-console */ | |
const Libp2p = require('libp2p') | |
const TCP = require('libp2p-tcp') | |
const Circuit = require('libp2p-circuit') | |
const SPDY = require('libp2p-spdy') | |
const SECIO = require('libp2p-secio') | |
const MulticastDNS = require('libp2p-mdns') | |
const DHT = require('libp2p-kad-dht') | |
const defaultsDeep = require('@nodeutils/defaults-deep') | |
const PeerId = require('peer-id') | |
const PeerInfo = require('peer-info') | |
const PeerBook = require('peer-book') | |
const Switch = require('libp2p-switch') | |
const relayId = "QmemquW1rBrxZ4nq1KvNSKgHiRqKhXQdcW4ZvWSMftVAuy"; | |
const relayPort = "54783"; | |
const createRelaySwitch = () => { | |
const listenerPeerId = PeerId.createFromB58String(relayId); | |
const listenerPeerInfo = new PeerInfo(listenerPeerId); | |
const listenerMultiaddr = `/ip4/127.0.0.1/tcp/${relayPort}`; | |
listenerPeerInfo.multiaddrs.add(listenerMultiaddr); | |
const peerBook = new PeerBook(); | |
//peerBook.put(listenerPeerInfo); | |
return new Switch(listenerPeerInfo, peerBook); | |
}; | |
class NodeCircuit extends Libp2p { | |
constructor (_options) { | |
const defaults = { | |
modules: { | |
transport: [ | |
TCP, | |
new Circuit(createRelaySwitch(), null) | |
], | |
streamMuxer: [ | |
SPDY | |
], | |
connEncryption: [ | |
SECIO | |
], | |
/** Encryption for private networks. Needs additional private key to work **/ | |
// connProtector: new Protector(/*protector specific opts*/), | |
/** Enable custom content routers, such as delegated routing **/ | |
// contentRouting: [ | |
// new DelegatedContentRouter(peerInfo.id) | |
// ], | |
/** Enable custom peer routers, such as delegated routing **/ | |
// peerRouting: [ | |
// new DelegatedPeerRouter() | |
// ], | |
peerDiscovery: [ | |
MulticastDNS | |
], | |
dht: DHT // DHT enables PeerRouting, ContentRouting and DHT itself components | |
}, | |
// libp2p config options (typically found on a config.json) | |
config: { // The config object is the part of the config that can go into a file, config.json. | |
peerDiscovery: { | |
autoDial: true, // Auto connect to discovered peers (limited by ConnectionManager minPeers) | |
mdns: { // mdns options | |
interval: 1000, // ms | |
enabled: true | |
}, | |
webrtcStar: { // webrtc-star options | |
interval: 1000, // ms | |
enabled: false | |
} | |
// .. other discovery module options. | |
}, | |
relay: { // Circuit Relay options | |
enabled: true, | |
hop: { | |
enabled: false, | |
active: false | |
} | |
}, | |
dht: { | |
kBucketSize: 20, | |
enabled: true, | |
randomWalk: { | |
enabled: true, // Allows to disable discovery (enabled by default) | |
interval: 300e3, | |
timeout: 10e3 | |
} | |
}, | |
// Enable/Disable Experimental features | |
EXPERIMENTAL: { // Experimental features ("behind a flag") | |
pubsub: false | |
} | |
} | |
} | |
// overload any defaults of your bundle using https://github.com/nodeutils/defaults-deep | |
super(defaultsDeep(_options, defaults)) | |
} | |
} | |
module.exports = NodeCircuit |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment