From 9bb0e2656f1f989fbe0fa8e0a70bc272325da542 Mon Sep 17 00:00:00 2001 From: gabrielmer <101006718+gabrielmer@users.noreply.github.com> Date: Tue, 7 Jan 2025 15:29:39 +0100 Subject: [PATCH] feat: adding protected shards (#13) --- waku/nwaku.go | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/waku/nwaku.go b/waku/nwaku.go index cc330a1..e7632b4 100644 --- a/waku/nwaku.go +++ b/waku/nwaku.go @@ -156,6 +156,15 @@ package waku resp) ); } + static void cGoWakuRelayAddProtectedShard(void* wakuCtx, int clusterId, int shardId, char* publicKey, void* resp) { + WAKU_CALL ( waku_relay_add_protected_shard(wakuCtx, + clusterId, + shardId, + publicKey, + (WakuCallBack) GoCallback, + resp) ); + } + static void cGoWakuRelayUnsubscribe(void* wakuCtx, char* pubSubTopic, void* resp) { WAKU_CALL ( waku_relay_unsubscribe(wakuCtx, @@ -303,6 +312,8 @@ package waku import "C" import ( "context" + "crypto/ecdsa" + "encoding/hex" "encoding/json" "errors" "fmt" @@ -314,6 +325,7 @@ import ( "unsafe" "github.com/ethereum/go-ethereum/common/hexutil" + "github.com/ethereum/go-ethereum/crypto" "github.com/ethereum/go-ethereum/log" "github.com/ethereum/go-ethereum/p2p/enode" "github.com/libp2p/go-libp2p/core/peer" @@ -754,6 +766,37 @@ func (n *WakuNode) RelaySubscribe(pubsubTopic string) error { return errors.New(errMsg) } +func (n *WakuNode) RelayAddProtectedShard(clusterId uint16, shardId uint16, pubkey *ecdsa.PublicKey) error { + if pubkey == nil { + return errors.New("error WakuRelayAddProtectedShard: pubkey can't be nil") + } + + keyHexStr := hex.EncodeToString(crypto.FromECDSAPub(pubkey)) + + wg := sync.WaitGroup{} + + var resp = C.allocResp(unsafe.Pointer(&wg)) + var cPublicKey = C.CString(keyHexStr) + + defer C.freeResp(resp) + defer C.free(unsafe.Pointer(cPublicKey)) + + if n.wakuCtx == nil { + return errors.New("wakuCtx is nil") + } + + wg.Add(1) + C.cGoWakuRelayAddProtectedShard(n.wakuCtx, C.int(clusterId), C.int(shardId), cPublicKey, resp) + wg.Wait() + + if C.getRet(resp) == C.RET_OK { + return nil + } + + errMsg := "error WakuRelayAddProtectedShard: " + C.GoStringN(C.getMyCharPtr(resp), C.int(C.getMyCharLen(resp))) + return errors.New(errMsg) +} + func (n *WakuNode) RelayUnsubscribe(pubsubTopic string) error { if pubsubTopic == "" { return errors.New("pubsub topic is empty")