mirror of https://github.com/waku-org/nwaku.git
feat(libwaku): add protected topic
This commit is contained in:
parent
049fbeabbb
commit
38729bc2c8
|
@ -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,
|
||||
|
|
|
@ -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,
|
||||
|
|
|
@ -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("")
|
||||
|
|
Loading…
Reference in New Issue