diff --git a/package-lock.json b/package-lock.json index bed5665..b5f2812 100644 --- a/package-lock.json +++ b/package-lock.json @@ -685,9 +685,8 @@ } }, "@openzeppelin/gsn-provider": { - "version": "0.1.8", - "resolved": "https://registry.npmjs.org/@openzeppelin/gsn-provider/-/gsn-provider-0.1.8.tgz", - "integrity": "sha512-/XRvCMs2MufOKORPeFRaYnp4a1l3szAJ39v5v0NiBHvLIdS8WotcYG9izVaUQllQFQrGCcVkCM+nptutWCr+gg==", + "version": "git+https://github.com/peppersec/openzeppelin-gsn-provider.git#69a75ba8bf37cdf9c60ebba2b191ec2630f2fb50", + "from": "git+https://github.com/peppersec/openzeppelin-gsn-provider.git#69a75ba8bf37cdf9c60ebba2b191ec2630f2fb50", "dev": true, "requires": { "abi-decoder": "^2.1.0", @@ -3081,9 +3080,9 @@ } }, "abi-decoder": { - "version": "2.2.1", - "resolved": "https://registry.npmjs.org/abi-decoder/-/abi-decoder-2.2.1.tgz", - "integrity": "sha512-kqVn3TIRPPMHZT+ciHFbkjZBxeTPvJMg2+8b1fhA2aBBTC6ny6orRUgFYuLZrJz/mc7opdhmy/MHzcI9kdPDYw==", + "version": "2.2.2", + "resolved": "https://registry.npmjs.org/abi-decoder/-/abi-decoder-2.2.2.tgz", + "integrity": "sha512-viRNIt7FzBC9/Y99AVKsAvEMJsIe9Yc/PMrqYT7edSlZ4EnbXlxAq7hS08XTeMzjMP6DpoVrYyCwhSCskCPQqA==", "dev": true, "requires": { "web3-eth-abi": "^1.2.1", @@ -5374,61 +5373,37 @@ } }, "eccrypto": { - "version": "1.1.1", - "resolved": "https://registry.npmjs.org/eccrypto/-/eccrypto-1.1.1.tgz", - "integrity": "sha512-uTLiIWyckVdddMywc6ArT4n3qKJDAUgUXV7A5yzUpqxz2lG++5+hPODEhtOf3C/qBLuCIUHjSt2ttSdteKzeMw==", + "version": "1.1.2", + "resolved": "https://registry.npmjs.org/eccrypto/-/eccrypto-1.1.2.tgz", + "integrity": "sha512-4z/uF18h2TFdqqtFSUvlwRD9epzmeEEUZ4nVMv3ox+jy+V7AxU9s3nLoEDDbptTUlkAbAp5bPfhaS7H4naoFqg==", "dev": true, "requires": { - "acorn": "6.0.0", - "elliptic": "6.0.2", - "es6-promise": "3.0.2", - "nan": "2.11.1", - "secp256k1": "2.0.4" + "acorn": "7.1.0", + "elliptic": "6.5.1", + "es6-promise": "4.2.8", + "nan": "2.14.0", + "secp256k1": "3.7.1" }, "dependencies": { "acorn": { - "version": "6.0.0", - "resolved": "https://registry.npmjs.org/acorn/-/acorn-6.0.0.tgz", - "integrity": "sha512-vvZ8PwswGTM15ZXyk3I+SvpTm8UbF8iRnGiU/f9TPU6By7K1XTvfvusFtoQt0WYycudFSYW2lrJDivhBlGovvQ==", + "version": "7.1.0", + "resolved": "https://registry.npmjs.org/acorn/-/acorn-7.1.0.tgz", + "integrity": "sha512-kL5CuoXA/dgxlBbVrflsflzQ3PAas7RYZB52NOm/6839iVYJgKMJ3cQJD+t2i5+qFa8h3MDpEOJiS64E8JLnSQ==", "dev": true }, "elliptic": { - "version": "6.0.2", - "resolved": "https://registry.npmjs.org/elliptic/-/elliptic-6.0.2.tgz", - "integrity": "sha1-IZuWzZKqmIXZHTHB/ULqpetEg6k=", + "version": "6.5.1", + "resolved": "https://registry.npmjs.org/elliptic/-/elliptic-6.5.1.tgz", + "integrity": "sha512-xvJINNLbTeWQjrl6X+7eQCrIy/YPv5XCpKW6kB5mKvtnGILoLDcySuwomfdzt0BMdLNVnuRNTuzKNHj0bva1Cg==", "dev": true, "requires": { - "bn.js": "^4.0.0", + "bn.js": "^4.4.0", "brorand": "^1.0.1", "hash.js": "^1.0.0", - "inherits": "^2.0.1" - } - }, - "es6-promise": { - "version": "3.0.2", - "resolved": "https://registry.npmjs.org/es6-promise/-/es6-promise-3.0.2.tgz", - "integrity": "sha1-AQ1YWEI6XxGJeWZfRkhqlcbuK7Y=", - "dev": true - }, - "nan": { - "version": "2.11.1", - "resolved": "https://registry.npmjs.org/nan/-/nan-2.11.1.tgz", - "integrity": "sha512-iji6k87OSXa0CcrLl9z+ZiYSuR2o+c0bGuNmXdrhTQTakxytAFsC56SArGYoiHlJlFoHSnvmhpceZJaXkVuOtA==", - "dev": true - }, - "secp256k1": { - "version": "2.0.4", - "resolved": "https://registry.npmjs.org/secp256k1/-/secp256k1-2.0.4.tgz", - "integrity": "sha1-BFPkuhzABIWRIXTvShxZsPG2+kk=", - "dev": true, - "optional": true, - "requires": { - "bindings": "^1.2.1", - "bluebird": "^3.0.2", - "bn.js": "^4.1.1", - "elliptic": "^6.0.1", - "nan": "^2.0.9", - "object-assign": "^4.0.1" + "hmac-drbg": "^1.0.0", + "inherits": "^2.0.1", + "minimalistic-assert": "^1.0.0", + "minimalistic-crypto-utils": "^1.0.0" } } } @@ -5772,28 +5747,40 @@ "integrity": "sha1-Qa4u62XvpiJorr/qg6x9eSmbCIc=" }, "eth-crypto": { - "version": "1.4.0", - "resolved": "https://registry.npmjs.org/eth-crypto/-/eth-crypto-1.4.0.tgz", - "integrity": "sha512-BWeQFoGvutBnA4rH7IQTUHHbV+YludDueVxlqNXG3XXOMdrulLSqAPkxv8h4AlfBJnn50zJBmoyhJ11UIbz8EA==", + "version": "1.5.0", + "resolved": "https://registry.npmjs.org/eth-crypto/-/eth-crypto-1.5.0.tgz", + "integrity": "sha512-BSTlT/dnyB8it3MBDf1X/RCwVjbah44KYgPZNxIBpXRr7jEIXe1eF8h6ShXT+a3mpCb7SWd9aczhSB4KioCSZw==", "dev": true, "requires": { "@types/bn.js": "4.11.5", "babel-runtime": "6.26.0", - "eccrypto": "1.1.1", + "eccrypto": "1.1.2", "eth-lib": "0.2.8", - "ethereumjs-tx": "2.1.0", + "ethereumjs-tx": "2.1.1", "ethereumjs-util": "6.1.0", - "ethers": "4.0.33", + "ethers": "4.0.37", "secp256k1": "3.7.1" }, "dependencies": { - "ethereumjs-tx": { - "version": "2.1.0", - "resolved": "https://registry.npmjs.org/ethereumjs-tx/-/ethereumjs-tx-2.1.0.tgz", - "integrity": "sha512-q1PFhR5i93OjcoE0G3GGz7XvnpLiddcUSKr28hmMUzVHvvc/+PHmQTx4NrGQUUny7qBq9tEIcvMivdB7uphKtA==", + "elliptic": { + "version": "6.3.3", + "resolved": "https://registry.npmjs.org/elliptic/-/elliptic-6.3.3.tgz", + "integrity": "sha1-VILZZG1UvLif19mU/J4ulWiHbj8=", "dev": true, "requires": { - "ethereumjs-common": "^1.3.0", + "bn.js": "^4.4.0", + "brorand": "^1.0.1", + "hash.js": "^1.0.0", + "inherits": "^2.0.1" + } + }, + "ethereumjs-tx": { + "version": "2.1.1", + "resolved": "https://registry.npmjs.org/ethereumjs-tx/-/ethereumjs-tx-2.1.1.tgz", + "integrity": "sha512-QtVriNqowCFA19X9BCRPMgdVNJ0/gMBS91TQb1DfrhsbR748g4STwxZptFAwfqehMyrF8rDwB23w87PQwru0wA==", + "dev": true, + "requires": { + "ethereumjs-common": "^1.3.1", "ethereumjs-util": "^6.0.0" } }, @@ -5811,6 +5798,34 @@ "safe-buffer": "^5.1.1", "secp256k1": "^3.0.1" } + }, + "ethers": { + "version": "4.0.37", + "resolved": "https://registry.npmjs.org/ethers/-/ethers-4.0.37.tgz", + "integrity": "sha512-B7bDdyQ45A5lPr6k2HOkEKMtYOuqlfy+nNf8glnRvWidkDQnToKw1bv7UyrwlbsIgY2mE03UxTVtouXcT6Vvcw==", + "dev": true, + "requires": { + "@types/node": "^10.3.2", + "aes-js": "3.0.0", + "bn.js": "^4.4.0", + "elliptic": "6.3.3", + "hash.js": "1.1.3", + "js-sha3": "0.5.7", + "scrypt-js": "2.0.4", + "setimmediate": "1.0.4", + "uuid": "2.0.1", + "xmlhttprequest": "1.8.0" + } + }, + "hash.js": { + "version": "1.1.3", + "resolved": "https://registry.npmjs.org/hash.js/-/hash.js-1.1.3.tgz", + "integrity": "sha512-/UETyP0W22QILqS+6HowevwhEFJ3MBJnwTf75Qob9Wz9t0DPuisL8kW8YZMK62dHAKE1c1p+gY1TtOLY+USEHA==", + "dev": true, + "requires": { + "inherits": "^2.0.3", + "minimalistic-assert": "^1.0.0" + } } } }, diff --git a/package.json b/package.json index 815c08b..e1cf508 100644 --- a/package.json +++ b/package.json @@ -44,7 +44,7 @@ "devDependencies": { "@openzeppelin/contracts-ethereum-package": "^2.2.3", "@openzeppelin/gsn-helpers": "^0.2.0", - "@openzeppelin/gsn-provider": "^0.1.8", + "@openzeppelin/gsn-provider": "git+https://github.com/peppersec/openzeppelin-gsn-provider.git#69a75ba8bf37cdf9c60ebba2b191ec2630f2fb50", "@openzeppelin/network": "^0.2.9", "@openzeppelin/upgrades": "^2.5.3", "truffle-flattener": "^1.4.0" diff --git a/test/GSNsupport.test.js b/test/GSNsupport.test.js index 791fa0e..58c4ae7 100644 --- a/test/GSNsupport.test.js +++ b/test/GSNsupport.test.js @@ -6,13 +6,14 @@ require('chai') const fs = require('fs') const Web3 = require('web3') -const { toBN, toHex, toChecksumAddress } = require('web3-utils') +const { toBN, toHex, toChecksumAddress, toWei } = require('web3-utils') const { takeSnapshot, revertSnapshot } = require('../lib/ganacheHelper') const { deployRelayHub, fundRecipient } = require('@openzeppelin/gsn-helpers') const { GSNDevProvider, GSNProvider } = require('@openzeppelin/gsn-provider') const { ephemeral } = require('@openzeppelin/network') const Mixer = artifacts.require('./ETHMixer.sol') +const RelayHub = artifacts.require('./IRelayHub.sol') const { ETH_AMOUNT, MERKLE_TREE_HEIGHT, EMPTY_ELEMENT } = process.env const websnarkUtils = require('websnark/src/utils') @@ -51,20 +52,25 @@ contract('GSN support', accounts => { let relayHubAddress const sender = accounts[0] const operator = accounts[0] - const ownerAddress = accounts[8] - const relayerAddress = accounts[9] + const relayerOwnerAddress = accounts[8] + const relayerAddress = accounts[9]// '0x714992E1acbc7f888Be2A1784F0D23e73f4A1ead' const levels = MERKLE_TREE_HEIGHT || 16 const zeroValue = EMPTY_ELEMENT || 1337 const value = ETH_AMOUNT || '1000000000000000000' // 1 ether let snapshotId let prefix = 'test' let tree - const fee = bigInt(ETH_AMOUNT).shr(1) || bigInt(1e17) const receiver = getRandomReceiver() - const relayer = accounts[1] let groth16 let circuit let proving_key + let unstakeDelay = 604800 + let relayerTxFee = 20 // % + let signKey = ephemeral() + let gsnWeb3 + const postRelayedCallMaxGas = 100000 + const recipientCallsAtomicOverhead = 5000 + let postRelayMaxGas = toBN(postRelayedCallMaxGas + recipientCallsAtomicOverhead) before(async () => { tree = new MerkleTree( @@ -77,12 +83,29 @@ contract('GSN support', accounts => { relayHubAddress = toChecksumAddress(await deployRelayHub(web3, { from: sender })) + await fundRecipient(web3, { recipient: mixer.address, relayHubAddress }) - await mixer.upgradeRelayHub(relayHubAddress) + const currentHub = await mixer.getHubAddr() + if (relayHubAddress !== currentHub) { + await mixer.upgradeRelayHub(relayHubAddress) + } + const hub = await RelayHub.at(relayHubAddress) + await hub.stake(relayerAddress, unstakeDelay , { from: relayerOwnerAddress, value: toWei('1') }) + await hub.registerRelay(relayerTxFee, 'http://gsn-dev-relayer.openzeppelin.com/', { from: relayerAddress }) + snapshotId = await takeSnapshot() groth16 = await buildGroth16() circuit = require('../build/circuits/withdraw.json') proving_key = fs.readFileSync('build/circuits/withdraw_proving_key.bin').buffer + const provider = new GSNDevProvider('http://localhost:8545', { + signKey, + relayerOwner: relayerOwnerAddress, + relayerAddress, + verbose: true, + txFee: relayerTxFee + }) + gsnWeb3 = new Web3(provider, null, { transactionConfirmationBlocks: 1 }) + gsnMixer = new gsnWeb3.eth.Contract(mixer.abi, mixer.address) }) describe('#constructor', () => { @@ -93,9 +116,8 @@ contract('GSN support', accounts => { }) describe('#withdrawViaRelayer', () => { - it.only('should work', async () => { + it('should work', async () => { const gasPrice = toBN('20000000000') - const relayerTxFee = 10 // 20% const deposit = generateDeposit() const user = accounts[4] await tree.insert(deposit.commitment) @@ -137,55 +159,48 @@ contract('GSN support', accounts => { const balanceMixerBefore = await web3.eth.getBalance(mixer.address) const balanceHubBefore = await web3.eth.getBalance(relayHubAddress) const balanceRelayerBefore = await web3.eth.getBalance(relayerAddress) - const balanceRelayerOwnerBefore = await web3.eth.getBalance(ownerAddress) + const balanceRelayerOwnerBefore = await web3.eth.getBalance(relayerOwnerAddress) const balanceRecieverBefore = await web3.eth.getBalance(toHex(receiver.toString())) let isSpent = await mixer.isSpent(input.nullifierHash.toString(16).padStart(66, '0x00000')) isSpent.should.be.equal(false) - const account = ephemeral() - // const provider = new GSNProvider('https://kovan.infura.io/v3/c7463beadf2144e68646ff049917b716', { - const provider = new GSNProvider('http://localhost:8545', { - signKey: account, - ownerAddress, - relayerAddress, - verbose: true, - txFee: relayerTxFee - }) - // console.log('relayerAddress', relayerAddress) - const gsnWeb3 = new Web3(provider, null, { transactionConfirmationBlocks: 1 }) - gsnMixer = new gsnWeb3.eth.Contract(mixer.abi, mixer.address) const tx = await gsnMixer.methods.withdrawViaRelayer(pi_a, pi_b, pi_c, publicSignals).send({ - from: account.address, + from: signKey.address, gas: 3e6, gasPrice, value: 0 }) - // console.log('tx', tx) const { events, gasUsed } = tx - // console.log('events', events, gasUsed) const balanceMixerAfter = await web3.eth.getBalance(mixer.address) const balanceHubAfter = await web3.eth.getBalance(relayHubAddress) const balanceRelayerAfter = await web3.eth.getBalance(relayerAddress) - const balanceRelayerOwnerAfter = await web3.eth.getBalance(ownerAddress) + const balanceRelayerOwnerAfter = await web3.eth.getBalance(relayerOwnerAddress) const balanceRecieverAfter = await web3.eth.getBalance(toHex(receiver.toString())) - console.log('balanceMixerBefore, balanceMixerAfter', balanceMixerBefore.toString(), balanceMixerAfter.toString()) - console.log('balanceRecieverBefore, balanceRecieverAfter', balanceRecieverBefore.toString(), balanceRecieverAfter.toString()) - console.log('balanceHubBefore, balanceHubAfter', balanceHubBefore.toString(), balanceHubAfter.toString()) - console.log('balanceRelayerBefore, balanceRelayerAfter', balanceRelayerBefore.toString(), balanceRelayerAfter.toString(), toBN(balanceRelayerBefore).sub(toBN(balanceRelayerAfter)).toString()) - console.log('balanceRelayerOwnerBefore, balanceRelayerOwnerAfter', balanceRelayerOwnerBefore.toString(), balanceRelayerOwnerAfter.toString()) - // balanceMixerAfter.should.be.eq.BN(toBN(balanceMixerBefore).sub(toBN(value))) + // console.log('balanceMixerBefore, balanceMixerAfter', balanceMixerBefore.toString(), balanceMixerAfter.toString()) + // console.log('balanceRecieverBefore, balanceRecieverAfter', balanceRecieverBefore.toString(), balanceRecieverAfter.toString()) + // console.log('balanceHubBefore, balanceHubAfter', balanceHubBefore.toString(), balanceHubAfter.toString()) + // console.log('balanceRelayerBefore, balanceRelayerAfter', balanceRelayerBefore.toString(), balanceRelayerAfter.toString(), toBN(balanceRelayerBefore).sub(toBN(balanceRelayerAfter)).toString()) + // console.log('balanceRelayerOwnerBefore, balanceRelayerOwnerAfter', balanceRelayerOwnerBefore.toString(), balanceRelayerOwnerAfter.toString()) + balanceMixerAfter.should.be.eq.BN(toBN(balanceMixerBefore).sub(toBN(value))) const networkFee = toBN(gasUsed).mul(gasPrice) const chargedFee = networkFee.add(networkFee.div(toBN(relayerTxFee))) - console.log('networkFee, calc chargedFee', networkFee.toString(), chargedFee.toString()) - // const fee = toBN(value).sub(toBN(balanceRecieverAfter)) - // console.log('actual charged fee', fee.toString()) - balanceRelayerAfter.should.be.eq.BN(toBN(balanceRelayerBefore).sub(networkFee)) - // balanceRelayerOwnerAfter.should.be.eq.BN(toBN(balanceRelayerOwnerBefore)) - // balanceRecieverAfter.should.be.gt.BN(toBN(balanceRecieverBefore)) - // balanceHubAfter.should.be.eq.BN(toBN(balanceHubBefore).add(fee)) + // console.log('networkFee :', networkFee.toString()) + // console.log('calculated chargedFee :', chargedFee.toString()) + const actualFee = toBN(value).sub(toBN(balanceRecieverAfter)) + // console.log('actual fee :', actualFee.toString()) + // const postRelayMaxCost = postRelayMaxGas.mul(gasPrice) + // const actualFeeWithoutPostCall = actualFee.sub(postRelayMaxCost) + // console.log('actualFeeWithoutPostCall :', actualFeeWithoutPostCall.toString()) + networkFee.should.be.lt.BN(chargedFee) + chargedFee.should.be.lt.BN(actualFee) - // console.log('events.Withdraw.returnValues.nullifierHash', events.Withdraw.returnValues.nullifierHash.toString(), input.nullifierHash.toString()) - // events.Withdraw.returnValues.nullifierHash.should.be.eq.BN(toBN(input.nullifierHash.toString())) + balanceRelayerAfter.should.be.eq.BN(toBN(balanceRelayerBefore).sub(networkFee)) + balanceRelayerOwnerAfter.should.be.eq.BN(toBN(balanceRelayerOwnerBefore)) + balanceRecieverAfter.should.be.gt.BN(toBN(balanceRecieverBefore)) + balanceRecieverAfter.should.be.lt.BN(toBN(value).sub(chargedFee)) + balanceHubAfter.should.be.eq.BN(toBN(balanceHubBefore).add(actualFee)) + + toBN(events.Withdraw.returnValues.nullifierHash).should.be.eq.BN(toBN(input.nullifierHash.toString())) events.Withdraw.returnValues.relayer.should.be.eq.BN(relayerAddress) events.Withdraw.returnValues.to.should.be.eq.BN(toHex(receiver.toString()))