2024-02-22 18:05:05 +00:00
|
|
|
import
|
2024-03-08 16:19:10 +00:00
|
|
|
# Nimble packages
|
2024-02-22 18:05:05 +00:00
|
|
|
chronicles,
|
|
|
|
chronos,
|
|
|
|
libp2p/crypto/crypto,
|
|
|
|
eth/[keys, p2p/discoveryv5/enr],
|
2024-03-08 16:19:10 +00:00
|
|
|
nimcrypto/utils,
|
2024-02-22 18:05:05 +00:00
|
|
|
|
2024-03-08 16:19:10 +00:00
|
|
|
# Nimble packages - Waku
|
2024-02-22 18:05:05 +00:00
|
|
|
waku/[waku_core, waku_discv5],
|
|
|
|
waku/waku_noise/noise_types,
|
|
|
|
waku/node/[peer_manager, waku_node],
|
2024-03-08 16:19:10 +00:00
|
|
|
waku/common/logging,
|
2024-02-22 18:05:05 +00:00
|
|
|
|
2024-03-08 16:19:10 +00:00
|
|
|
# Local modules
|
|
|
|
./waku_handshake_utils,
|
|
|
|
./waku_node
|
2024-02-22 18:05:05 +00:00
|
|
|
|
|
|
|
type WakuPairResult* = object
|
|
|
|
wakuNode*: WakuNode
|
|
|
|
wakuHandshakeResult*: HandshakeResult
|
|
|
|
|
|
|
|
proc wakuPair*(rng: ref HmacDrbgContext, qr, qrMessageNameTagHex: string,
|
|
|
|
wakuPort, discv5Port: uint16,
|
|
|
|
requiredConnectedPeers: int,
|
|
|
|
pubSubTopic: PubsubTopic
|
|
|
|
): Future[WakuPairResult] {.async.} =
|
|
|
|
# Initiator static/ephemeral key initialization and commitment
|
|
|
|
let initiatorInfo = initAgentKeysAndCommitment(rng)
|
|
|
|
|
|
|
|
# Read the QR
|
|
|
|
let
|
|
|
|
qrMessageNameTag = fromHex(qrMessageNameTagHex)
|
|
|
|
# We set the contentTopic from the content topic parameters exchanged in the QR
|
|
|
|
contentTopic = initContentTopicFromQr(qr)
|
|
|
|
|
|
|
|
notice "Initializing Waku pairing", wakuPort = wakuPort,
|
|
|
|
discv5Port = discv5Port
|
|
|
|
|
|
|
|
notice "Initial information parsed from the QR", contentTopic = contentTopic,
|
|
|
|
qrMessageNameTag = qrMessageNameTag
|
|
|
|
|
|
|
|
var initiatorHSResult: HandshakeResult
|
|
|
|
|
|
|
|
# Start nwaku instance
|
|
|
|
let node = await startWakuNode(rng, wakuPort, discv5Port,
|
|
|
|
requiredConnectedPeers)
|
|
|
|
|
|
|
|
# Perform the handshake
|
|
|
|
initiatorHSResult = await initiatorHandshake(rng, node, pubSubTopic,
|
|
|
|
contentTopic, qr,
|
|
|
|
qrMessageNameTag,
|
|
|
|
initiatorInfo)
|
|
|
|
return WakuPairResult(wakuNode: node,
|
|
|
|
wakuHandshakeResult: initiatorHSResult)
|