diff --git a/tests/v2/waku_rln_relay/test_waku_rln_relay.nim b/tests/v2/waku_rln_relay/test_waku_rln_relay.nim index 12ad887941..12444bfa8f 100644 --- a/tests/v2/waku_rln_relay/test_waku_rln_relay.nim +++ b/tests/v2/waku_rln_relay/test_waku_rln_relay.nim @@ -147,6 +147,39 @@ suite "Waku rln relay": check: memberAdded + test "getMember Nim wrapper": + # create an RLN instance which also includes an empty Merkle tree + let rlnInstance = createRLNInstance() + require: + rlnInstance.isOk() + let rln = rlnInstance.get() + # generate an identity credential + let idCredentialRes = membershipKeyGen(rln) + require: + idCredentialRes.isOk() + + let idCredential = idCredentialRes.get() + let pkBuffer = toBuffer(idCredential.idCommitment) + let pkBufferPtr = unsafeAddr(pkBuffer) + + let + root1 {.noinit.}: Buffer = Buffer() + rootPtr1 = unsafeAddr(root1) + getRootSuccessful1 = getRoot(rlnInstance.get(), rootPtr1) + + # add the member to the tree + let memberAdded = updateNextMember(rln, pkBufferPtr) + require: + memberAdded + + let leafRes = getMember(rln, 0) + require: + leafRes.isOk() + let leaf = leafRes.get() + let leafHex = leaf.inHex() + check: + leafHex == idCredential.idCommitment.inHex() + test "delete_member Nim wrapper": # create an RLN instance which also includes an empty Merkle tree let rlnInstance = createRLNInstance() diff --git a/waku/v2/waku_rln_relay/constants.nim b/waku/v2/waku_rln_relay/constants.nim index 75a7008376..c0632e25fa 100644 --- a/waku/v2/waku_rln_relay/constants.nim +++ b/waku/v2/waku_rln_relay/constants.nim @@ -1,5 +1,4 @@ import - std/json, stint import diff --git a/waku/v2/waku_rln_relay/rln/rln_interface.nim b/waku/v2/waku_rln_relay/rln/rln_interface.nim index 9b5f3d487c..728927036d 100644 --- a/waku/v2/waku_rln_relay/rln/rln_interface.nim +++ b/waku/v2/waku_rln_relay/rln/rln_interface.nim @@ -54,6 +54,11 @@ proc set_leaf*(ctx: ptr RLN, index: uint, input_buffer: ptr Buffer): bool {.impo ## the input_buffer holds a serialized leaf of 32 bytes ## the return bool value indicates the success or failure of the operation +proc get_leaf*(ctx: ptr RLN, index: uint, output_buffer: ptr Buffer): bool {.importc: "get_leaf".} +## gets the leaf at position index in the tree stored by ctx +## the output_buffer holds a serialized leaf of 32 bytes +## the return bool value indicates the success or failure of the operation + proc init_tree_with_leaves*(ctx: ptr RLN, input_buffer: ptr Buffer): bool {.importc: "init_tree_with_leaves".} ## sets multiple leaves in the tree stored by ctx to the value passed by input_buffer ## the input_buffer holds a serialized vector of leaves (32 bytes each) diff --git a/waku/v2/waku_rln_relay/rln/wrappers.nim b/waku/v2/waku_rln_relay/rln/wrappers.nim index ae812604ed..b23e095a77 100644 --- a/waku/v2/waku_rln_relay/rln/wrappers.nim +++ b/waku/v2/waku_rln_relay/rln/wrappers.nim @@ -271,6 +271,25 @@ proc insertMember*(rlnInstance: ptr RLN, idComm: IDCommitment): bool = let memberAdded = update_next_member(rlnInstance, pkBufferPtr) return memberAdded +proc getMember*(rlnInstance: ptr RLN, index: MembershipIndex): RlnRelayResult[IDCommitment] = + ## returns the member at the given index + ## returns an error if the index is out of bounds + ## returns the member if the index is valid + var + idCommitment {.noinit.}: Buffer = Buffer() + idCommitmentPtr = addr(idCommitment) + memberRetrieved = get_leaf(rlnInstance, index, idCommitmentPtr) + + if not memberRetrieved: + return err("could not get the member") + + if not idCommitment.len == 32: + return err("wrong output size") + + let idCommitmentValue = (cast[ptr array[32, byte]](idCommitment.`ptr`))[] + + return ok(@idCommitmentValue) + proc atomicWrite*(rlnInstance: ptr RLN, index = none(MembershipIndex), idComms = newSeq[IDCommitment](),