2020-04-23 01:27:29 +00:00
|
|
|
# compile time options here
|
|
|
|
const
|
2020-05-06 09:26:08 +00:00
|
|
|
libp2p_pubsub_sign {.booldefine.} = true
|
|
|
|
libp2p_pubsub_verify {.booldefine.} = true
|
2020-04-23 01:27:29 +00:00
|
|
|
|
2019-12-08 21:06:58 +00:00
|
|
|
import
|
2020-05-18 19:04:05 +00:00
|
|
|
options, tables, chronos,
|
2020-06-19 17:29:43 +00:00
|
|
|
switch, peer, peerinfo, stream/connection, multiaddress,
|
2019-12-08 21:06:58 +00:00
|
|
|
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-06-01 06:41:32 +00:00
|
|
|
import
|
|
|
|
protocols/secure/noise,
|
|
|
|
protocols/secure/secio
|
2020-04-23 01:27:29 +00:00
|
|
|
|
2019-12-08 21:06:58 +00:00
|
|
|
export
|
2020-05-08 16:08:38 +00:00
|
|
|
switch, peer, peerinfo, connection, multiaddress, crypto
|
2019-12-08 21:06:58 +00:00
|
|
|
|
2020-06-01 06:41:32 +00:00
|
|
|
type
|
|
|
|
SecureProtocol* {.pure.} = enum
|
|
|
|
Noise,
|
|
|
|
Secio
|
|
|
|
|
2019-12-08 21:06:58 +00:00
|
|
|
proc newStandardSwitch*(privKey = none(PrivateKey),
|
2020-05-31 14:22:49 +00:00
|
|
|
address = MultiAddress.init("/ip4/127.0.0.1/tcp/0").tryGet(),
|
2019-12-23 18:45:12 +00:00
|
|
|
triggerSelf = false,
|
2020-05-06 09:26:08 +00:00
|
|
|
gossip = false,
|
2020-06-01 06:41:32 +00:00
|
|
|
secureManagers: openarray[SecureProtocol] = [
|
2020-06-20 10:56:55 +00:00
|
|
|
# array cos order matters
|
2020-06-11 06:38:47 +00:00
|
|
|
SecureProtocol.Secio,
|
2020-06-20 10:56:55 +00:00
|
|
|
SecureProtocol.Noise,
|
2020-06-01 06:41:32 +00:00
|
|
|
],
|
2020-05-06 09:26:08 +00:00
|
|
|
verifySignature = libp2p_pubsub_verify,
|
2020-05-05 15:55:02 +00:00
|
|
|
sign = libp2p_pubsub_sign,
|
2020-05-18 19:04:05 +00:00
|
|
|
transportFlags: set[ServerFlags] = {}): Switch =
|
2019-12-08 21:06:58 +00:00
|
|
|
proc createMplex(conn: Connection): Muxer =
|
2020-06-01 06:41:32 +00:00
|
|
|
newMplex(conn)
|
2019-12-08 21:06:58 +00:00
|
|
|
|
|
|
|
let
|
2020-05-18 05:25:55 +00:00
|
|
|
seckey = privKey.get(otherwise = PrivateKey.random(ECDSA).tryGet())
|
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)
|
2020-05-18 19:04:05 +00:00
|
|
|
transports = @[Transport(TcpTransport.init(transportFlags))]
|
2019-12-08 21:06:58 +00:00
|
|
|
muxers = {MplexCodec: mplexProvider}.toTable
|
|
|
|
identify = newIdentify(peerInfo)
|
2020-06-01 06:41:32 +00:00
|
|
|
|
2020-06-06 00:17:05 +00:00
|
|
|
var
|
2020-06-01 06:41:32 +00:00
|
|
|
secureManagerInstances: seq[Secure]
|
|
|
|
for sec in secureManagers:
|
|
|
|
case sec
|
|
|
|
of SecureProtocol.Noise:
|
|
|
|
secureManagerInstances &= newNoise(seckey).Secure
|
|
|
|
of SecureProtocol.Secio:
|
|
|
|
secureManagerInstances &= newSecio(seckey).Secure
|
2020-05-06 09:26:08 +00:00
|
|
|
|
2020-04-23 01:27:29 +00:00
|
|
|
let pubSub = if gossip:
|
2020-06-01 06:41:32 +00:00
|
|
|
newPubSub(GossipSub,
|
|
|
|
peerInfo = peerInfo,
|
|
|
|
triggerSelf = triggerSelf,
|
|
|
|
verifySignature = verifySignature,
|
|
|
|
sign = sign).PubSub
|
2020-04-23 01:27:29 +00:00
|
|
|
else:
|
2020-06-01 06:41:32 +00:00
|
|
|
newPubSub(FloodSub,
|
|
|
|
peerInfo = peerInfo,
|
|
|
|
triggerSelf = triggerSelf,
|
|
|
|
verifySignature = verifySignature,
|
|
|
|
sign = sign).PubSub
|
|
|
|
|
|
|
|
newSwitch(
|
|
|
|
peerInfo,
|
|
|
|
transports,
|
|
|
|
identify,
|
|
|
|
muxers,
|
|
|
|
secureManagers = secureManagerInstances,
|
|
|
|
pubSub = some(pubSub))
|