nim-libp2p/libp2p/standard_setup.nim

65 lines
2.4 KiB
Nim
Raw Normal View History

# compile time options here
const
libp2p_secure {.strdefine.} = ""
libp2p_pubsub_sign {.booldefine.} = true
libp2p_pubsub_verify {.booldefine.} = true
import
options, tables,
2020-05-05 15:55:02 +00:00
chronos,
switch, peer, peerinfo, connection, multiaddress,
crypto/crypto, transports/[transport, tcptransport],
muxers/[muxer, mplex/mplex, mplex/types],
protocols/[identify, secure/secure],
protocols/pubsub/[pubsub, gossipsub, floodsub]
when libp2p_secure == "noise":
import protocols/secure/noise
else:
import protocols/secure/secio
export
2020-05-05 15:55:02 +00:00
switch, peer, peerinfo, connection, multiaddress, crypto, ServerFlags
proc newStandardSwitch*(privKey = none(PrivateKey),
address = MultiAddress.init("/ip4/127.0.0.1/tcp/0"),
2019-12-23 18:45:12 +00:00
triggerSelf = false,
gossip = false,
verifySignature = libp2p_pubsub_verify,
2020-05-05 15:55:02 +00:00
sign = libp2p_pubsub_sign,
serverFlags: set[ServerFlags] = {}): Switch =
proc createMplex(conn: Connection): Muxer =
result = newMplex(conn)
let
2020-01-07 08:04:33 +00:00
seckey = privKey.get(otherwise = PrivateKey.random(ECDSA))
peerInfo = PeerInfo.init(seckey, [address])
mplexProvider = newMuxerProvider(createMplex, MplexCodec)
2020-05-05 15:55:02 +00:00
transports = @[Transport(newTransport(TcpTransport, serverFlags))]
muxers = {MplexCodec: mplexProvider}.toTable
identify = newIdentify(peerInfo)
when libp2p_secure == "noise":
let secureManagers = {NoiseCodec: newNoise(seckey).Secure}.toTable
else:
let secureManagers = {SecioCodec: newSecio(seckey).Secure}.toTable
let pubSub = if gossip:
PubSub newPubSub(GossipSub,
peerInfo = peerInfo,
triggerSelf = triggerSelf,
verifySignature = verifySignature,
sign = sign)
else:
PubSub newPubSub(FloodSub,
peerInfo = peerInfo,
triggerSelf = triggerSelf,
verifySignature = verifySignature,
sign = sign)
result = newSwitch(peerInfo,
transports,
identify,
muxers,
secureManagers = secureManagers,
pubSub = some(pubSub))