From 63f984b2d580bda451a18a7ddc4306e3d8d3d637 Mon Sep 17 00:00:00 2001 From: Alexey Date: Thu, 25 Jul 2019 15:16:09 +0300 Subject: [PATCH] isSpent fix --- cli.js | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/cli.js b/cli.js index 8e4d915..3cd5de2 100755 --- a/cli.js +++ b/cli.js @@ -48,24 +48,32 @@ async function withdraw(note, receiver) { console.log('Getting current state from mixer contract') const events = await mixer.getPastEvents('Deposit', { fromBlock: mixer.deployedBlock, toBlock: 'latest' }) + let leafIndex + + const commitment = deposit.commitment.toString(16).padStart('66', '0x000000') const leaves = events .sort((a, b) => a.returnValues.leafIndex.sub(b.returnValues.leafIndex)) - .map(e => e.returnValues.commitment) + .map(e => { + if (e.returnValues.commitment.eq(commitment)) { + leafIndex = e.returnValues.leafIndex.toNumber() + } + return e.returnValues.commitment + }) const tree = new merkleTree(MERKLE_TREE_HEIGHT, EMPTY_ELEMENT, leaves) const validRoot = await mixer.methods.isKnownRoot(await tree.root()).call() - // todo make sure that function input is 32 bytes long - const isSpent = await mixer.methods.isSpent('0x' + deposit.nullifier.toString(16)).call() + const nullifierHash = pedersenHash(deposit.nullifier.leInt2Buff(32)) + const nullifierHashToCheck = nullifierHash.toString(16).padStart('66', '0x000000') + const isSpent = await mixer.methods.isSpent(nullifierHashToCheck).call() assert(validRoot === true) assert(isSpent === false) - const leafIndex = leaves.map(el => el.toString()).indexOf(deposit.commitment.toString()) assert(leafIndex >= 0) const { root, path_elements, path_index } = await tree.path(leafIndex) // Circuit input const input = { // public root: root, - nullifierHash: pedersenHash(deposit.nullifier.leInt2Buff(32)), + nullifierHash, receiver: bigInt(receiver), fee: bigInt(0),