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-07-07 11:14:11 +00:00
|
|
|
options, tables, chronos, bearssl,
|
2020-07-01 06:25:09 +00:00
|
|
|
switch, peerid, 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],
|
2020-06-28 15:56:38 +00:00
|
|
|
protocols/pubsub/[pubsub, gossipsub, floodsub],
|
|
|
|
protocols/pubsub/rpc/message
|
2019-12-08 21:06:58 +00:00
|
|
|
|
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-07-01 06:25:09 +00:00
|
|
|
switch, peerid, 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-28 15:56:38 +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-06-28 15:56:38 +00:00
|
|
|
transportFlags: set[ServerFlags] = {},
|
2020-07-07 11:14:11 +00:00
|
|
|
msgIdProvider: MsgIdProvider = defaultMsgIdProvider,
|
2020-07-17 18:44:41 +00:00
|
|
|
rng = newRng(),
|
|
|
|
inTimeout: Duration = 1.minutes,
|
2020-07-17 19:46:24 +00:00
|
|
|
outTimeout: Duration = 1.minutes): Switch =
|
2019-12-08 21:06:58 +00:00
|
|
|
proc createMplex(conn: Connection): Muxer =
|
2020-07-17 18:44:41 +00:00
|
|
|
Mplex.init(
|
|
|
|
conn,
|
|
|
|
inTimeout = inTimeout,
|
|
|
|
outTimeout = outTimeout)
|
2019-12-08 21:06:58 +00:00
|
|
|
|
2020-07-07 11:14:11 +00:00
|
|
|
if rng == nil: # newRng could fail
|
|
|
|
raise (ref CatchableError)(msg: "Cannot initialize RNG")
|
|
|
|
|
2019-12-08 21:06:58 +00:00
|
|
|
let
|
2020-07-07 11:14:11 +00:00
|
|
|
seckey = privKey.get(otherwise = PrivateKey.random(ECDSA, rng[]).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:
|
2020-07-07 11:14:11 +00:00
|
|
|
secureManagerInstances &= newNoise(rng, seckey).Secure
|
2020-06-01 06:41:32 +00:00
|
|
|
of SecureProtocol.Secio:
|
2020-07-07 11:14:11 +00:00
|
|
|
secureManagerInstances &= newSecio(rng, 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,
|
2020-06-28 15:56:38 +00:00
|
|
|
sign = sign,
|
|
|
|
msgIdProvider = msgIdProvider).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,
|
2020-06-28 15:56:38 +00:00
|
|
|
sign = sign,
|
|
|
|
msgIdProvider = msgIdProvider).PubSub
|
2020-06-01 06:41:32 +00:00
|
|
|
|
|
|
|
newSwitch(
|
|
|
|
peerInfo,
|
|
|
|
transports,
|
|
|
|
identify,
|
|
|
|
muxers,
|
|
|
|
secureManagers = secureManagerInstances,
|
|
|
|
pubSub = some(pubSub))
|