2019-07-13 13:57:49 +02:00
|
|
|
#!/usr/bin/env node
|
2019-07-17 13:12:57 +02:00
|
|
|
// Temporary demo client
|
2019-07-18 20:41:18 +02:00
|
|
|
// Works both in browser and node.js
|
2019-07-16 19:20:16 +02:00
|
|
|
const fs = require('fs')
|
|
|
|
const assert = require('assert')
|
|
|
|
const snarkjs = require('snarkjs')
|
2019-07-18 20:27:51 +02:00
|
|
|
const crypto = require('crypto')
|
|
|
|
const circomlib = require('circomlib')
|
2019-07-16 19:20:16 +02:00
|
|
|
const bigInt = snarkjs.bigInt
|
|
|
|
const merkleTree = require('./lib/MerkleTree')
|
|
|
|
const Web3 = require('web3')
|
|
|
|
const buildGroth16 = require('websnark/src/groth16')
|
|
|
|
const websnarkUtils = require('websnark/src/utils')
|
|
|
|
|
|
|
|
let web3, mixer, circuit, proving_key, groth16
|
|
|
|
let MERKLE_TREE_HEIGHT, AMOUNT, EMPTY_ELEMENT
|
|
|
|
const inBrowser = (typeof window !== 'undefined')
|
2019-07-13 16:45:08 +02:00
|
|
|
|
2019-07-18 20:27:51 +02:00
|
|
|
const rbigint = (nbytes) => snarkjs.bigInt.leBuff2int(crypto.randomBytes(nbytes))
|
|
|
|
const pedersenHash = (data) => circomlib.babyJub.unpackPoint(circomlib.pedersenHash.hash(data))[0]
|
|
|
|
|
2019-07-13 16:45:08 +02:00
|
|
|
function createDeposit(nullifier, secret) {
|
2019-07-16 19:20:16 +02:00
|
|
|
let deposit = { nullifier, secret }
|
|
|
|
deposit.preimage = Buffer.concat([deposit.nullifier.leInt2Buff(32), deposit.secret.leInt2Buff(32)])
|
2019-07-18 20:27:51 +02:00
|
|
|
deposit.commitment = pedersenHash(deposit.preimage)
|
2019-07-16 19:20:16 +02:00
|
|
|
return deposit
|
2019-07-13 16:45:08 +02:00
|
|
|
}
|
2019-07-13 13:57:49 +02:00
|
|
|
|
|
|
|
async function deposit() {
|
2019-07-18 20:27:51 +02:00
|
|
|
const deposit = createDeposit(rbigint(31), rbigint(31))
|
2019-07-13 13:57:49 +02:00
|
|
|
|
2019-07-16 19:20:16 +02:00
|
|
|
console.log('Submitting deposit transaction')
|
|
|
|
await mixer.methods.deposit('0x' + deposit.commitment.toString(16)).send({ value: AMOUNT, from: (await web3.eth.getAccounts())[0], gas:1e6 })
|
2019-07-13 13:57:49 +02:00
|
|
|
|
2019-07-16 19:20:16 +02:00
|
|
|
const note = '0x' + deposit.preimage.toString('hex')
|
|
|
|
console.log('Your note:', note)
|
|
|
|
return note
|
2019-07-13 13:57:49 +02:00
|
|
|
}
|
|
|
|
|
2019-07-15 21:19:34 +02:00
|
|
|
async function getBalance(receiver) {
|
|
|
|
const balance = await web3.eth.getBalance(receiver)
|
2019-07-17 23:59:04 +02:00
|
|
|
console.log('Balance is ', web3.utils.fromWei(balance))
|
2019-07-15 21:19:34 +02:00
|
|
|
}
|
|
|
|
|
2019-07-13 13:57:49 +02:00
|
|
|
async function withdraw(note, receiver) {
|
2019-07-16 19:20:16 +02:00
|
|
|
let buf = Buffer.from(note.slice(2), 'hex')
|
|
|
|
let deposit = createDeposit(bigInt.leBuff2int(buf.slice(0, 32)), bigInt.leBuff2int(buf.slice(32, 64)))
|
|
|
|
|
|
|
|
console.log('Getting current state from mixer contract')
|
2019-07-19 19:08:47 +02:00
|
|
|
const events = await mixer.getPastEvents('Deposit', { fromBlock: mixer.deployedBlock, toBlock: 'latest' })
|
2019-07-25 14:16:09 +02:00
|
|
|
let leafIndex
|
|
|
|
|
|
|
|
const commitment = deposit.commitment.toString(16).padStart('66', '0x000000')
|
2019-07-24 18:57:51 +02:00
|
|
|
const leaves = events
|
|
|
|
.sort((a, b) => a.returnValues.leafIndex.sub(b.returnValues.leafIndex))
|
2019-07-25 14:16:09 +02:00
|
|
|
.map(e => {
|
|
|
|
if (e.returnValues.commitment.eq(commitment)) {
|
|
|
|
leafIndex = e.returnValues.leafIndex.toNumber()
|
|
|
|
}
|
|
|
|
return e.returnValues.commitment
|
|
|
|
})
|
2019-07-16 19:20:16 +02:00
|
|
|
const tree = new merkleTree(MERKLE_TREE_HEIGHT, EMPTY_ELEMENT, leaves)
|
|
|
|
const validRoot = await mixer.methods.isKnownRoot(await tree.root()).call()
|
2019-07-25 14:16:09 +02:00
|
|
|
const nullifierHash = pedersenHash(deposit.nullifier.leInt2Buff(32))
|
|
|
|
const nullifierHashToCheck = nullifierHash.toString(16).padStart('66', '0x000000')
|
|
|
|
const isSpent = await mixer.methods.isSpent(nullifierHashToCheck).call()
|
2019-07-16 19:20:16 +02:00
|
|
|
assert(validRoot === true)
|
|
|
|
assert(isSpent === false)
|
2019-07-13 13:57:49 +02:00
|
|
|
|
2019-07-16 19:20:16 +02:00
|
|
|
assert(leafIndex >= 0)
|
|
|
|
const { root, path_elements, path_index } = await tree.path(leafIndex)
|
2019-07-13 13:57:49 +02:00
|
|
|
// Circuit input
|
|
|
|
const input = {
|
|
|
|
// public
|
|
|
|
root: root,
|
2019-07-25 14:16:09 +02:00
|
|
|
nullifierHash,
|
2019-07-13 13:57:49 +02:00
|
|
|
receiver: bigInt(receiver),
|
|
|
|
fee: bigInt(0),
|
|
|
|
|
|
|
|
// private
|
2019-07-19 18:37:38 +02:00
|
|
|
nullifier: deposit.nullifier,
|
2019-07-13 13:57:49 +02:00
|
|
|
secret: deposit.secret,
|
|
|
|
pathElements: path_elements,
|
|
|
|
pathIndex: path_index,
|
2019-07-16 19:20:16 +02:00
|
|
|
}
|
2019-07-13 13:57:49 +02:00
|
|
|
|
2019-07-16 19:20:16 +02:00
|
|
|
console.log('Generating SNARK proof')
|
2019-07-17 13:12:57 +02:00
|
|
|
console.time('Proof time')
|
2019-07-16 19:20:16 +02:00
|
|
|
const proof = await websnarkUtils.genWitnessAndProve(groth16, input, circuit, proving_key)
|
|
|
|
const { pi_a, pi_b, pi_c, publicSignals } = websnarkUtils.toSolidityInput(proof)
|
2019-07-17 13:12:57 +02:00
|
|
|
console.timeEnd('Proof time')
|
2019-07-13 13:57:49 +02:00
|
|
|
|
2019-07-16 19:20:16 +02:00
|
|
|
console.log('Submitting withdraw transaction')
|
|
|
|
await mixer.methods.withdraw(pi_a, pi_b, pi_c, publicSignals).send({ from: (await web3.eth.getAccounts())[0], gas: 1e6 })
|
|
|
|
console.log('Done')
|
2019-07-13 13:57:49 +02:00
|
|
|
}
|
|
|
|
|
2019-07-13 16:52:42 +02:00
|
|
|
async function init() {
|
2019-07-16 19:20:16 +02:00
|
|
|
let contractJson
|
2019-07-15 18:23:03 +02:00
|
|
|
if (inBrowser) {
|
2019-07-16 22:49:45 +02:00
|
|
|
web3 = new Web3(window.web3.currentProvider, null, { transactionConfirmationBlocks: 1 })
|
2019-07-16 19:20:16 +02:00
|
|
|
contractJson = await (await fetch('build/contracts/Mixer.json')).json()
|
|
|
|
circuit = await (await fetch('build/circuits/withdraw.json')).json()
|
|
|
|
proving_key = await (await fetch('build/circuits/withdraw_proving_key.bin')).arrayBuffer()
|
|
|
|
MERKLE_TREE_HEIGHT = 16
|
|
|
|
AMOUNT = 1e18
|
|
|
|
EMPTY_ELEMENT = 0
|
2019-07-15 18:23:03 +02:00
|
|
|
} else {
|
2019-07-16 22:49:45 +02:00
|
|
|
web3 = new Web3('http://localhost:8545', null, { transactionConfirmationBlocks: 1 })
|
2019-07-16 19:20:16 +02:00
|
|
|
contractJson = require('./build/contracts/Mixer.json')
|
|
|
|
circuit = require('./build/circuits/withdraw.json')
|
|
|
|
proving_key = fs.readFileSync('build/circuits/withdraw_proving_key.bin').buffer
|
|
|
|
require('dotenv').config()
|
|
|
|
MERKLE_TREE_HEIGHT = process.env.MERKLE_TREE_HEIGHT
|
|
|
|
AMOUNT = process.env.AMOUNT
|
|
|
|
EMPTY_ELEMENT = process.env.EMPTY_ELEMENT
|
2019-07-15 18:23:03 +02:00
|
|
|
}
|
2019-07-16 19:20:16 +02:00
|
|
|
groth16 = await buildGroth16()
|
|
|
|
let netId = await web3.eth.net.getId()
|
|
|
|
const tx = await web3.eth.getTransaction(contractJson.networks[netId].transactionHash)
|
|
|
|
mixer = new web3.eth.Contract(contractJson.abi, contractJson.networks[netId].address)
|
|
|
|
mixer.deployedBlock = tx.blockNumber
|
|
|
|
console.log('Loaded')
|
2019-07-13 16:52:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// ========== CLI related stuff below ==============
|
|
|
|
|
2019-07-13 16:45:08 +02:00
|
|
|
function printHelp(code = 0) {
|
|
|
|
console.log(`Usage:
|
2019-07-15 21:19:34 +02:00
|
|
|
Submit a deposit from default eth account and return the resulting note
|
2019-07-13 13:57:49 +02:00
|
|
|
$ ./cli.js deposit
|
2019-07-15 21:19:34 +02:00
|
|
|
|
2019-07-13 13:57:49 +02:00
|
|
|
Withdraw a note to 'receiver' account
|
2019-07-13 16:45:08 +02:00
|
|
|
$ ./cli.js withdraw <note> <receiver>
|
2019-07-19 19:08:47 +02:00
|
|
|
|
2019-07-17 23:59:04 +02:00
|
|
|
Check address balance
|
|
|
|
$ ./cli.js balance <address>
|
2019-07-15 21:19:34 +02:00
|
|
|
|
2019-07-13 13:57:49 +02:00
|
|
|
Example:
|
|
|
|
$ ./cli.js deposit
|
|
|
|
...
|
|
|
|
Your note: 0x1941fa999e2b4bfeec3ce53c2440c3bc991b1b84c9bb650ea19f8331baf621001e696487e2a2ee54541fa12f49498d71e24d00b1731a8ccd4f5f5126f3d9f400
|
2019-07-15 21:19:34 +02:00
|
|
|
|
2019-07-13 13:57:49 +02:00
|
|
|
$ ./cli.js withdraw 0x1941fa999e2b4bfeec3ce53c2440c3bc991b1b84c9bb650ea19f8331baf621001e696487e2a2ee54541fa12f49498d71e24d00b1731a8ccd4f5f5126f3d9f400 0xee6249BA80596A4890D1BD84dbf5E4322eA4E7f0
|
2019-07-16 19:20:16 +02:00
|
|
|
`)
|
|
|
|
process.exit(code)
|
2019-07-13 13:57:49 +02:00
|
|
|
}
|
|
|
|
|
2019-07-15 18:25:54 +02:00
|
|
|
if (inBrowser) {
|
2019-07-16 19:20:16 +02:00
|
|
|
window.deposit = deposit
|
2019-07-15 18:25:54 +02:00
|
|
|
window.withdraw = async () => {
|
2019-07-16 19:20:16 +02:00
|
|
|
const note = prompt('Enter the note to withdraw')
|
|
|
|
const receiver = (await web3.eth.getAccounts())[0]
|
|
|
|
await withdraw(note, receiver)
|
|
|
|
}
|
|
|
|
init()
|
2019-07-15 18:25:54 +02:00
|
|
|
} else {
|
2019-07-16 19:20:16 +02:00
|
|
|
const args = process.argv.slice(2)
|
2019-07-15 18:25:54 +02:00
|
|
|
if (args.length === 0) {
|
2019-07-16 19:20:16 +02:00
|
|
|
printHelp()
|
2019-07-15 18:25:54 +02:00
|
|
|
} else {
|
|
|
|
switch (args[0]) {
|
2019-07-16 19:20:16 +02:00
|
|
|
case 'deposit':
|
|
|
|
if (args.length === 1) {
|
2019-07-16 22:49:45 +02:00
|
|
|
init().then(() => deposit()).then(() => process.exit(0)).catch(err => {console.log(err); process.exit(1)})
|
2019-07-16 19:20:16 +02:00
|
|
|
}
|
2019-07-15 18:25:54 +02:00
|
|
|
else
|
2019-07-16 19:20:16 +02:00
|
|
|
printHelp(1)
|
|
|
|
break
|
|
|
|
case 'balance':
|
|
|
|
if (args.length === 2 && /^0x[0-9a-fA-F]{40}$/.test(args[1])) {
|
2019-07-16 22:49:45 +02:00
|
|
|
init().then(() => getBalance(args[1])).then(() => process.exit(0)).catch(err => {console.log(err); process.exit(1)})
|
2019-07-17 23:59:04 +02:00
|
|
|
} else
|
|
|
|
printHelp(1)
|
2019-07-16 19:20:16 +02:00
|
|
|
break
|
|
|
|
case 'withdraw':
|
|
|
|
if (args.length === 3 && /^0x[0-9a-fA-F]{128}$/.test(args[1]) && /^0x[0-9a-fA-F]{40}$/.test(args[2])) {
|
2019-07-16 22:49:45 +02:00
|
|
|
init().then(() => withdraw(args[1], args[2])).then(() => process.exit(0)).catch(err => {console.log(err); process.exit(1)})
|
2019-07-16 19:20:16 +02:00
|
|
|
}
|
2019-07-15 18:25:54 +02:00
|
|
|
else
|
2019-07-16 19:20:16 +02:00
|
|
|
printHelp(1)
|
|
|
|
break
|
2019-07-15 18:25:54 +02:00
|
|
|
|
2019-07-16 19:20:16 +02:00
|
|
|
default:
|
|
|
|
printHelp(1)
|
2019-07-15 18:25:54 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|