2020-04-23 01:27:29 +00:00
|
|
|
# compile time options here
|
|
|
|
const
|
|
|
|
libp2p_secure {.strdefine.} = ""
|
|
|
|
|
2019-12-08 21:06:58 +00:00
|
|
|
import
|
|
|
|
options, tables,
|
|
|
|
switch, peer, peerinfo, connection, multiaddress,
|
|
|
|
crypto/crypto, transports/[transport, tcptransport],
|
|
|
|
muxers/[muxer, mplex/mplex, mplex/types],
|
2020-04-23 01:27:29 +00:00
|
|
|
protocols/[identify, secure/secure],
|
2019-12-08 21:06:58 +00:00
|
|
|
protocols/pubsub/[pubsub, gossipsub, floodsub]
|
|
|
|
|
2020-04-23 01:27:29 +00:00
|
|
|
when libp2p_secure == "noise":
|
|
|
|
import protocols/secure/noise
|
|
|
|
else:
|
|
|
|
import protocols/secure/secio
|
|
|
|
|
2019-12-08 21:06:58 +00:00
|
|
|
export
|
|
|
|
switch, peer, peerinfo, connection, multiaddress, crypto
|
|
|
|
|
|
|
|
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): Switch =
|
2019-12-08 21:06:58 +00:00
|
|
|
proc createMplex(conn: Connection): Muxer =
|
|
|
|
result = newMplex(conn)
|
|
|
|
|
|
|
|
let
|
2020-01-07 08:04:33 +00:00
|
|
|
seckey = privKey.get(otherwise = PrivateKey.random(ECDSA))
|
2020-02-11 17:53:39 +00:00
|
|
|
peerInfo = PeerInfo.init(seckey, [address])
|
2019-12-08 21:06:58 +00:00
|
|
|
mplexProvider = newMuxerProvider(createMplex, MplexCodec)
|
|
|
|
transports = @[Transport(newTransport(TcpTransport))]
|
|
|
|
muxers = {MplexCodec: mplexProvider}.toTable
|
|
|
|
identify = newIdentify(peerInfo)
|
2020-04-23 01:27:29 +00:00
|
|
|
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, triggerSelf)
|
|
|
|
else:
|
|
|
|
PubSub newPubSub(FloodSub, peerInfo, triggerSelf)
|
2019-12-08 21:06:58 +00:00
|
|
|
|
|
|
|
result = newSwitch(peerInfo,
|
|
|
|
transports,
|
|
|
|
identify,
|
|
|
|
muxers,
|
|
|
|
secureManagers = secureManagers,
|
|
|
|
pubSub = some(pubSub))
|
|
|
|
|