isSpent fix

This commit is contained in:
Alexey 2019-07-25 15:16:09 +03:00
parent caaedbc959
commit 63f984b2d5
1 changed files with 13 additions and 5 deletions

18
cli.js
View File

@ -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),