Skip to content

Commit

Permalink
feat(libwaku): add protected topic
Browse files Browse the repository at this point in the history
  • Loading branch information
richard-ramos committed Jan 7, 2025
1 parent 29726bf commit 7cbbd90
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 3 deletions.
6 changes: 6 additions & 0 deletions library/libwaku.h
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
28 changes: 28 additions & 0 deletions library/libwaku.nim
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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]
Expand All @@ -33,17 +34,20 @@ 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,
op: RelayMsgType,
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),
Expand All @@ -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 =
Expand Down Expand Up @@ -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("")

0 comments on commit 7cbbd90

Please sign in to comment.