isSpent fix

This commit is contained in:
Alexey 2019-07-25 15:16:09 +03:00
parent caaedbc959
commit 63f984b2d5

18
cli.js
View File

@ -48,24 +48,32 @@ async function withdraw(note, receiver) {
console.log('Getting current state from mixer contract') console.log('Getting current state from mixer contract')
const events = await mixer.getPastEvents('Deposit', { fromBlock: mixer.deployedBlock, toBlock: 'latest' }) 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 const leaves = events
.sort((a, b) => a.returnValues.leafIndex.sub(b.returnValues.leafIndex)) .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 tree = new merkleTree(MERKLE_TREE_HEIGHT, EMPTY_ELEMENT, leaves)
const validRoot = await mixer.methods.isKnownRoot(await tree.root()).call() const validRoot = await mixer.methods.isKnownRoot(await tree.root()).call()
// todo make sure that function input is 32 bytes long const nullifierHash = pedersenHash(deposit.nullifier.leInt2Buff(32))
const isSpent = await mixer.methods.isSpent('0x' + deposit.nullifier.toString(16)).call() const nullifierHashToCheck = nullifierHash.toString(16).padStart('66', '0x000000')
const isSpent = await mixer.methods.isSpent(nullifierHashToCheck).call()
assert(validRoot === true) assert(validRoot === true)
assert(isSpent === false) assert(isSpent === false)
const leafIndex = leaves.map(el => el.toString()).indexOf(deposit.commitment.toString())
assert(leafIndex >= 0) assert(leafIndex >= 0)
const { root, path_elements, path_index } = await tree.path(leafIndex) const { root, path_elements, path_index } = await tree.path(leafIndex)
// Circuit input // Circuit input
const input = { const input = {
// public // public
root: root, root: root,
nullifierHash: pedersenHash(deposit.nullifier.leInt2Buff(32)), nullifierHash,
receiver: bigInt(receiver), receiver: bigInt(receiver),
fee: bigInt(0), fee: bigInt(0),