mirror of https://github.com/waku-org/nwaku.git
feat(rln-relay): integrate get_leaf ffi api (#1790)
This commit is contained in:
parent
5fc5770da9
commit
940206b439
|
@ -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()
|
||||
|
|
|
@ -1,5 +1,4 @@
|
|||
import
|
||||
std/json,
|
||||
stint
|
||||
|
||||
import
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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](),
|
||||
|
|
Loading…
Reference in New Issue