From 38729bc2c890b0ebf3a7b7579d08e50cf450c342 Mon Sep 17 00:00:00 2001 From: Richard Ramos Date: Mon, 16 Dec 2024 15:55:32 -0400 Subject: [PATCH] feat(libwaku): add protected topic --- library/libwaku.h | 6 ++++ library/libwaku.nim | 28 +++++++++++++++++++ .../requests/protocols/relay_request.nim | 19 +++++++++++-- 3 files changed, 50 insertions(+), 3 deletions(-) diff --git a/library/libwaku.h b/library/libwaku.h index c02f6da43..683589215 100644 --- a/library/libwaku.h +++ b/library/libwaku.h @@ -84,6 +84,12 @@ int waku_relay_subscribe(void* ctx, WakuCallBack callback, void* userData); +int waku_relay_add_protected_topic(void* ctx, + const char* pubSubTopic, + const char* publicKey, + WakuCallBack callback, + void* userData); + int waku_relay_unsubscribe(void* ctx, const char* pubSubTopic, WakuCallBack callback, diff --git a/library/libwaku.nim b/library/libwaku.nim index 13022f879..f0a2d80e6 100644 --- a/library/libwaku.nim +++ b/library/libwaku.nim @@ -360,6 +360,34 @@ proc waku_relay_subscribe( userData, ) +proc waku_relay_add_protected_topic( + ctx: ptr WakuContext, + pubSubTopic: cstring, + publicKey: cstring, + callback: WakuCallBack, + userData: pointer, +): cint {.dynlib, exportc, cdecl.} = + initializeLibrary() + checkLibwakuParams(ctx, callback, userData) + + let pst = pubSubTopic.alloc() + defer: + deallocShared(pst) + + let pubk = publicKey.alloc() + defer: + deallocShared(pubk) + + handleRequest( + ctx, + RequestType.RELAY, + RelayRequest.createShared( + RelayMsgType.ADD_PROTECTED_TOPIC, PubsubTopic($pst), publicKey = $pubk + ), + callback, + userData, + ) + proc waku_relay_unsubscribe( ctx: ptr WakuContext, pubSubTopic: cstring, diff --git a/library/waku_thread/inter_thread_communication/requests/protocols/relay_request.nim b/library/waku_thread/inter_thread_communication/requests/protocols/relay_request.nim index 8672aae6d..3cc75183d 100644 --- a/library/waku_thread/inter_thread_communication/requests/protocols/relay_request.nim +++ b/library/waku_thread/inter_thread_communication/requests/protocols/relay_request.nim @@ -2,7 +2,7 @@ import std/net import chronicles, chronos, stew/byteutils, results import ../../../../../waku/waku_core/message/message, - ../../../../../waku/factory/waku, + ../../../../../waku/factory/[external_config, validator_signed, waku], ../../../../../waku/waku_core/message, ../../../../../waku/waku_core/time, # Timestamp ../../../../../waku/waku_core/topics/pubsub_topic, @@ -17,6 +17,7 @@ type RelayMsgType* = enum ## to return the list of all connected peers to an specific pubsub topic LIST_MESH_PEERS ## to return the list of only the peers that conform the mesh for a particular pubsub topic + ADD_PROTECTED_TOPIC ## Protects a pubsub topic with a public key type ThreadSafeWakuMessage* = object payload: SharedSeq[byte] @@ -33,6 +34,7 @@ type RelayRequest* = object pubsubTopic: cstring relayEventCallback: WakuRelayHandler # not used in 'PUBLISH' requests message: ThreadSafeWakuMessage # only used in 'PUBLISH' requests + publicKey: cstring # only used in 'ADD_PROTECTED_TOPIC' requests proc createShared*( T: type RelayRequest, @@ -40,10 +42,12 @@ proc createShared*( pubsubTopic: PubsubTopic, relayEventCallback: WakuRelayHandler = nil, m = WakuMessage(), + publicKey: string = "", ): ptr type T = var ret = createShared(T) ret[].operation = op ret[].pubsubTopic = pubsubTopic.alloc() + ret[].publicKey = publicKey.alloc() ret[].relayEventCallback = relayEventCallback ret[].message = ThreadSafeWakuMessage( payload: allocSharedSeq(m.payload), @@ -64,7 +68,8 @@ proc destroyShared(self: ptr RelayRequest) = deallocSharedSeq(self[].message.meta) when defined(rln): deallocSharedSeq(self[].message.proof) - + deallocShared(self[].pubsubTopic) + deallocShared(self[].publicKey) deallocShared(self) proc toWakuMessage(m: ThreadSafeWakuMessage): WakuMessage = @@ -120,5 +125,13 @@ proc process*( error "LIST_MESH_PEERS failed", error = error return err($error) return ok($numPeersInMesh) - + of ADD_PROTECTED_TOPIC: + try: + let protectedShard = + ProtectedShard.parseCmdArg($self.pubsubTopic & ":" & $self.publicKey) + waku.node.wakuRelay.addSignedShardsValidator( + @[protectedShard], uint16(waku.node.wakuMetadata.clusterId) + ) + except ValueError: + return err(getCurrentExceptionMsg()) return ok("")