feat(libwaku): add protected topic

This commit is contained in:
Richard Ramos 2024-12-16 15:55:32 -04:00
parent 049fbeabbb
commit 38729bc2c8
No known key found for this signature in database
GPG Key ID: 1CE87DB518195760
3 changed files with 50 additions and 3 deletions

View File

@ -84,6 +84,12 @@ int waku_relay_subscribe(void* ctx,
WakuCallBack callback, WakuCallBack callback,
void* userData); 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, int waku_relay_unsubscribe(void* ctx,
const char* pubSubTopic, const char* pubSubTopic,
WakuCallBack callback, WakuCallBack callback,

View File

@ -360,6 +360,34 @@ proc waku_relay_subscribe(
userData, 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( proc waku_relay_unsubscribe(
ctx: ptr WakuContext, ctx: ptr WakuContext,
pubSubTopic: cstring, pubSubTopic: cstring,

View File

@ -2,7 +2,7 @@ import std/net
import chronicles, chronos, stew/byteutils, results import chronicles, chronos, stew/byteutils, results
import import
../../../../../waku/waku_core/message/message, ../../../../../waku/waku_core/message/message,
../../../../../waku/factory/waku, ../../../../../waku/factory/[external_config, validator_signed, waku],
../../../../../waku/waku_core/message, ../../../../../waku/waku_core/message,
../../../../../waku/waku_core/time, # Timestamp ../../../../../waku/waku_core/time, # Timestamp
../../../../../waku/waku_core/topics/pubsub_topic, ../../../../../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 ## to return the list of all connected peers to an specific pubsub topic
LIST_MESH_PEERS LIST_MESH_PEERS
## to return the list of only the peers that conform the mesh for a particular pubsub topic ## 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 type ThreadSafeWakuMessage* = object
payload: SharedSeq[byte] payload: SharedSeq[byte]
@ -33,6 +34,7 @@ type RelayRequest* = object
pubsubTopic: cstring pubsubTopic: cstring
relayEventCallback: WakuRelayHandler # not used in 'PUBLISH' requests relayEventCallback: WakuRelayHandler # not used in 'PUBLISH' requests
message: ThreadSafeWakuMessage # only used in 'PUBLISH' requests message: ThreadSafeWakuMessage # only used in 'PUBLISH' requests
publicKey: cstring # only used in 'ADD_PROTECTED_TOPIC' requests
proc createShared*( proc createShared*(
T: type RelayRequest, T: type RelayRequest,
@ -40,10 +42,12 @@ proc createShared*(
pubsubTopic: PubsubTopic, pubsubTopic: PubsubTopic,
relayEventCallback: WakuRelayHandler = nil, relayEventCallback: WakuRelayHandler = nil,
m = WakuMessage(), m = WakuMessage(),
publicKey: string = "",
): ptr type T = ): ptr type T =
var ret = createShared(T) var ret = createShared(T)
ret[].operation = op ret[].operation = op
ret[].pubsubTopic = pubsubTopic.alloc() ret[].pubsubTopic = pubsubTopic.alloc()
ret[].publicKey = publicKey.alloc()
ret[].relayEventCallback = relayEventCallback ret[].relayEventCallback = relayEventCallback
ret[].message = ThreadSafeWakuMessage( ret[].message = ThreadSafeWakuMessage(
payload: allocSharedSeq(m.payload), payload: allocSharedSeq(m.payload),
@ -64,7 +68,8 @@ proc destroyShared(self: ptr RelayRequest) =
deallocSharedSeq(self[].message.meta) deallocSharedSeq(self[].message.meta)
when defined(rln): when defined(rln):
deallocSharedSeq(self[].message.proof) deallocSharedSeq(self[].message.proof)
deallocShared(self[].pubsubTopic)
deallocShared(self[].publicKey)
deallocShared(self) deallocShared(self)
proc toWakuMessage(m: ThreadSafeWakuMessage): WakuMessage = proc toWakuMessage(m: ThreadSafeWakuMessage): WakuMessage =
@ -120,5 +125,13 @@ proc process*(
error "LIST_MESH_PEERS failed", error = error error "LIST_MESH_PEERS failed", error = error
return err($error) return err($error)
return ok($numPeersInMesh) 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("") return ok("")