From d7a4f0b2462b14bd8b27e38006402f3f0dfe5572 Mon Sep 17 00:00:00 2001 From: Emil Ivanichkov Date: Thu, 22 Feb 2024 20:05:05 +0200 Subject: [PATCH] feat(waku-utils): Add function that performs pairing A single function that set-up and start nwaku instance, init and finalize handshake (pairing) and return 2 objects - Waku node & handshake result. They can be used for future message transfers. --- libs/waku_utils/waku_pair.nim | 53 +++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 libs/waku_utils/waku_pair.nim diff --git a/libs/waku_utils/waku_pair.nim b/libs/waku_utils/waku_pair.nim new file mode 100644 index 0000000..56baefd --- /dev/null +++ b/libs/waku_utils/waku_pair.nim @@ -0,0 +1,53 @@ +import + chronicles, + chronos, + libp2p/crypto/crypto, + eth/[keys, p2p/discoveryv5/enr], + nimcrypto/utils + +import + waku/[waku_core, waku_discv5], + waku/waku_noise/noise_types, + waku/node/[peer_manager, waku_node], + waku/common/logging + +import ./waku_handshake_utils +import ./waku_node + +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)