From 918d8dcaf40b9c7027d7ab78322accd731d02d58 Mon Sep 17 00:00:00 2001 From: Hungry Warrior Date: Mon, 19 Aug 2024 09:07:23 +0000 Subject: [PATCH 1/8] Call markUserAsVerified() by eth_call --- tests/solidity/test/ComplianceBridge.js | 55 +++++++++++++++++++++---- 1 file changed, 48 insertions(+), 7 deletions(-) diff --git a/tests/solidity/test/ComplianceBridge.js b/tests/solidity/test/ComplianceBridge.js index adda24e6..5f63fbce 100644 --- a/tests/solidity/test/ComplianceBridge.js +++ b/tests/solidity/test/ComplianceBridge.js @@ -1,4 +1,4 @@ -const { expect } = require("chai"); +const { expect } = require("chai") const { ethers } = require("hardhat") const { sendShieldedTransaction, sendShieldedQuery } = require("./testUtils") @@ -8,29 +8,70 @@ describe('ComplianceBridge', () => { before(async () => { const [signer] = await ethers.getSigners() const ComplianceProxyFactory = await ethers.getContractFactory('ComplianceProxy') - contract = new ethers.Contract("0x30252afe8c1683fd184c99a3c44aa5d547d59dd4", ComplianceProxyFactory.interface, signer); + contract = new ethers.Contract("0x30252afe8c1683fd184c99a3c44aa5d547d59dd4", ComplianceProxyFactory.interface, signer) }) it('Should be able to add verification details', async () => { const [signer] = await ethers.getSigners() + // Make sure that signer was not verified yet + const isVerifiedRespBeforeCall = await sendShieldedQuery( + signer.provider, + contract.address, + contract.interface.encodeFunctionData("isUserVerified", [signer.address]) + ) + const isVerifiedBeforeCall = contract.interface.decodeFunctionResult("isUserVerified", isVerifiedRespBeforeCall) + expect(isVerifiedBeforeCall[0]).to.be.false + + // Try to call `markUserAsVerified` by using `eth_call` + const markUserAsVerifiedResp = await sendShieldedQuery( + signer.provider, + contract.address, + contract.interface.encodeFunctionData("markUserAsVerified", [signer.address]) + ) + const verificationId = contract.interface.decodeFunctionResult("markUserAsVerified", markUserAsVerifiedResp) + expect(verificationId.length).to.be.gt(0) + + // Confirm that verified status was not changed yet + const isVerifiedRespAfterCall = await sendShieldedQuery( + signer.provider, + contract.address, + contract.interface.encodeFunctionData("isUserVerified", [signer.address]) + ) + const isVerifiedAfterCall = contract.interface.decodeFunctionResult("isUserVerified", isVerifiedRespAfterCall) + expect(isVerifiedAfterCall[0]).to.be.false + + // const gas = await contract.estimateGas.markUserAsVerified(signer.address) + // console.log('eth_estimateGas(markUserAsVeried)', gas) + // + // // Confirm that verified status was not changed yet + // const isVerifiedRespAfterEstimateGas = await sendShieldedQuery( + // signer.provider, + // contract.address, + // contract.interface.encodeFunctionData("isUserVerified", [signer.address]) + // ) + // const isVerifiedAfterEstimateGas = contract.interface.decodeFunctionResult("isUserVerified", isVerifiedRespAfterEstimateGas) + // expect(isVerifiedAfterEstimateGas[0]).to.be.false + const tx = await sendShieldedTransaction( signer, contract.address, contract.interface.encodeFunctionData("markUserAsVerified", [signer.address]) ) + // What happens after tx confirmation const res = await tx.wait() const parsedLog = contract.interface.parseLog(res.logs[0]) expect(parsedLog.args.success).to.be.true - expect(parsedLog.args.data.length).to.be.greaterThan(0); + expect(parsedLog.args.data.length).to.be.greaterThan(0) - const isVerifiedResponse = await sendShieldedQuery( + // Confirm that verified status was changed after tx confirmation + const isVerifiedRespAfterTx = await sendShieldedQuery( signer.provider, contract.address, contract.interface.encodeFunctionData("isUserVerified", [signer.address]) ) - const result = contract.interface.decodeFunctionResult("isUserVerified", isVerifiedResponse) - expect(result[0]).to.be.true + const isVerifiedAfterTx = contract.interface.decodeFunctionResult("isUserVerified", isVerifiedRespAfterTx) + expect(isVerifiedAfterTx[0]).to.be.true }) it('Should be able to check for specific issuer of verification', async () => { @@ -67,7 +108,7 @@ describe('ComplianceBridge', () => { contract.interface.encodeFunctionData("getVerificationData", [signer.address]) ) const result = contract.interface.decodeFunctionResult("getVerificationData", verificationData) - expect(result[0].length).to.be.greaterThan(0); + expect(result[0].length).to.be.greaterThan(0) for (const details of result[0]) { expect(details.issuerAddress.length).to.be.greaterThan(0) expect(details.issuerAddress.toLowerCase()).to.be.equal(contract.address.toLowerCase()) From f9a62efddbd5005d86b12455a47393edabf83a54 Mon Sep 17 00:00:00 2001 From: Hungry Warrior Date: Mon, 19 Aug 2024 12:05:54 +0000 Subject: [PATCH 2/8] eth_estimateGas through unencrypted rpc --- tests/solidity/test/ComplianceBridge.js | 34 ++++++++++++++++--------- 1 file changed, 22 insertions(+), 12 deletions(-) diff --git a/tests/solidity/test/ComplianceBridge.js b/tests/solidity/test/ComplianceBridge.js index 5f63fbce..d494d087 100644 --- a/tests/solidity/test/ComplianceBridge.js +++ b/tests/solidity/test/ComplianceBridge.js @@ -2,13 +2,16 @@ const { expect } = require("chai") const { ethers } = require("hardhat") const { sendShieldedTransaction, sendShieldedQuery } = require("./testUtils") +const unencryptedProvider = new ethers.providers.JsonRpcProvider("http://localhost:8547") // Unencrypted rpc url + describe('ComplianceBridge', () => { + const CONTRACT_ADDRESS = "0x30252afe8c1683fd184c99a3c44aa5d547d59dd4" let contract before(async () => { const [signer] = await ethers.getSigners() const ComplianceProxyFactory = await ethers.getContractFactory('ComplianceProxy') - contract = new ethers.Contract("0x30252afe8c1683fd184c99a3c44aa5d547d59dd4", ComplianceProxyFactory.interface, signer) + contract = new ethers.Contract(CONTRACT_ADDRESS, ComplianceProxyFactory.interface, signer) }) it('Should be able to add verification details', async () => { @@ -21,6 +24,11 @@ describe('ComplianceBridge', () => { contract.interface.encodeFunctionData("isUserVerified", [signer.address]) ) const isVerifiedBeforeCall = contract.interface.decodeFunctionResult("isUserVerified", isVerifiedRespBeforeCall) + if (isVerifiedBeforeCall[0]) { + // If already verified, skip test case in order to pass over the next + console.log("issuer was already verified, skip") + return + } expect(isVerifiedBeforeCall[0]).to.be.false // Try to call `markUserAsVerified` by using `eth_call` @@ -41,17 +49,19 @@ describe('ComplianceBridge', () => { const isVerifiedAfterCall = contract.interface.decodeFunctionResult("isUserVerified", isVerifiedRespAfterCall) expect(isVerifiedAfterCall[0]).to.be.false - // const gas = await contract.estimateGas.markUserAsVerified(signer.address) - // console.log('eth_estimateGas(markUserAsVeried)', gas) - // - // // Confirm that verified status was not changed yet - // const isVerifiedRespAfterEstimateGas = await sendShieldedQuery( - // signer.provider, - // contract.address, - // contract.interface.encodeFunctionData("isUserVerified", [signer.address]) - // ) - // const isVerifiedAfterEstimateGas = contract.interface.decodeFunctionResult("isUserVerified", isVerifiedRespAfterEstimateGas) - // expect(isVerifiedAfterEstimateGas[0]).to.be.false + const ComplianceProxyFactory = await ethers.getContractFactory('ComplianceProxy') + const contractUnencrypted = new ethers.Contract(CONTRACT_ADDRESS, ComplianceProxyFactory.interface, unencryptedProvider) + const gas = await contractUnencrypted.estimateGas.markUserAsVerified(signer.address) + expect(gas.gt(ethers.BigNumber.from(0))).to.be.true + + // Confirm that verified status was not changed yet + const isVerifiedRespAfterEstimateGas = await sendShieldedQuery( + signer.provider, + contract.address, + contract.interface.encodeFunctionData("isUserVerified", [signer.address]) + ) + const isVerifiedAfterEstimateGas = contract.interface.decodeFunctionResult("isUserVerified", isVerifiedRespAfterEstimateGas) + expect(isVerifiedAfterEstimateGas[0]).to.be.false const tx = await sendShieldedTransaction( signer, From d4516ff82f42198cd63ca80e4bdad5040f5e528c Mon Sep 17 00:00:00 2001 From: Hungry Warrior Date: Tue, 20 Aug 2024 13:12:42 +0000 Subject: [PATCH 3/8] Separate tests for eth_call / eth_estimateGas --- tests/solidity/test/ComplianceBridge.js | 123 +++++++++++++++--------- 1 file changed, 77 insertions(+), 46 deletions(-) diff --git a/tests/solidity/test/ComplianceBridge.js b/tests/solidity/test/ComplianceBridge.js index d494d087..ca682322 100644 --- a/tests/solidity/test/ComplianceBridge.js +++ b/tests/solidity/test/ComplianceBridge.js @@ -17,52 +17,6 @@ describe('ComplianceBridge', () => { it('Should be able to add verification details', async () => { const [signer] = await ethers.getSigners() - // Make sure that signer was not verified yet - const isVerifiedRespBeforeCall = await sendShieldedQuery( - signer.provider, - contract.address, - contract.interface.encodeFunctionData("isUserVerified", [signer.address]) - ) - const isVerifiedBeforeCall = contract.interface.decodeFunctionResult("isUserVerified", isVerifiedRespBeforeCall) - if (isVerifiedBeforeCall[0]) { - // If already verified, skip test case in order to pass over the next - console.log("issuer was already verified, skip") - return - } - expect(isVerifiedBeforeCall[0]).to.be.false - - // Try to call `markUserAsVerified` by using `eth_call` - const markUserAsVerifiedResp = await sendShieldedQuery( - signer.provider, - contract.address, - contract.interface.encodeFunctionData("markUserAsVerified", [signer.address]) - ) - const verificationId = contract.interface.decodeFunctionResult("markUserAsVerified", markUserAsVerifiedResp) - expect(verificationId.length).to.be.gt(0) - - // Confirm that verified status was not changed yet - const isVerifiedRespAfterCall = await sendShieldedQuery( - signer.provider, - contract.address, - contract.interface.encodeFunctionData("isUserVerified", [signer.address]) - ) - const isVerifiedAfterCall = contract.interface.decodeFunctionResult("isUserVerified", isVerifiedRespAfterCall) - expect(isVerifiedAfterCall[0]).to.be.false - - const ComplianceProxyFactory = await ethers.getContractFactory('ComplianceProxy') - const contractUnencrypted = new ethers.Contract(CONTRACT_ADDRESS, ComplianceProxyFactory.interface, unencryptedProvider) - const gas = await contractUnencrypted.estimateGas.markUserAsVerified(signer.address) - expect(gas.gt(ethers.BigNumber.from(0))).to.be.true - - // Confirm that verified status was not changed yet - const isVerifiedRespAfterEstimateGas = await sendShieldedQuery( - signer.provider, - contract.address, - contract.interface.encodeFunctionData("isUserVerified", [signer.address]) - ) - const isVerifiedAfterEstimateGas = contract.interface.decodeFunctionResult("isUserVerified", isVerifiedRespAfterEstimateGas) - expect(isVerifiedAfterEstimateGas[0]).to.be.false - const tx = await sendShieldedTransaction( signer, contract.address, @@ -84,6 +38,83 @@ describe('ComplianceBridge', () => { expect(isVerifiedAfterTx[0]).to.be.true }) + it('Should not change state with eth_call', async () => { + const [signer] = await ethers.getSigners() + + for (let i = 0; i < 100; i++) { + const wallet = ethers.Wallet.createRandom() + if (signer.address === wallet.address) { + continue + } + + // Check if random wallet was not verified yet + const isVerifiedRespBeforeCall = await sendShieldedQuery( + signer.provider, + contract.address, + contract.interface.encodeFunctionData("isUserVerified", [wallet.address]) + ) + const isVerifiedBeforeCall = contract.interface.decodeFunctionResult("isUserVerified", isVerifiedRespBeforeCall) + if (isVerifiedBeforeCall[0]) { + continue + } + + // Try to call `markUserAsVerified` by using `eth_call` + const markUserAsVerifiedResp = await sendShieldedQuery( + signer.provider, + contract.address, + contract.interface.encodeFunctionData("markUserAsVerified", [wallet.address]) + ) + const verificationId = contract.interface.decodeFunctionResult("markUserAsVerified", markUserAsVerifiedResp) + expect(verificationId.length).to.be.gt(0) + + // Confirm that verified status was not changed yet + const isVerifiedRespAfterCall = await sendShieldedQuery( + signer.provider, + contract.address, + contract.interface.encodeFunctionData("isUserVerified", [wallet.address]) + ) + const isVerifiedAfterCall = contract.interface.decodeFunctionResult("isUserVerified", isVerifiedRespAfterCall) + expect(isVerifiedAfterCall[0]).to.be.false + } + }) + + it('Should not change state with eth_estimateGas', async () => { + const [signer] = await ethers.getSigners() + + const ComplianceProxyFactory = await ethers.getContractFactory('ComplianceProxy') + const contractUnencrypted = new ethers.Contract(CONTRACT_ADDRESS, ComplianceProxyFactory.interface, unencryptedProvider) + + for (let i = 0; i < 100; i++) { + const wallet = ethers.Wallet.createRandom() + if (signer.address === wallet.address) { + continue + } + + // Check if random wallet was not verified yet + const isVerifiedRespBeforeEstimateGas = await sendShieldedQuery( + signer.provider, + contract.address, + contract.interface.encodeFunctionData("isUserVerified", [wallet.address]) + ) + const isVerifiedBeforeEstimateGas = contract.interface.decodeFunctionResult("isUserVerified", isVerifiedRespBeforeEstimateGas) + if (isVerifiedBeforeEstimateGas[0]) { + continue + } + + const gas = await contractUnencrypted.estimateGas.markUserAsVerified(signer.address) + expect(gas.gt(ethers.BigNumber.from(0))).to.be.true + + // Confirm that verified status was not changed yet + const isVerifiedRespAfterEstimateGas = await sendShieldedQuery( + signer.provider, + contract.address, + contract.interface.encodeFunctionData("isUserVerified", [wallet.address]) + ) + const isVerifiedAfterEstimateGas = contract.interface.decodeFunctionResult("isUserVerified", isVerifiedRespAfterEstimateGas) + expect(isVerifiedAfterEstimateGas[0]).to.be.false + } + }) + it('Should be able to check for specific issuer of verification', async () => { const [signer] = await ethers.getSigners() From e86f684f5329f0742b46ae8fbdbdd2ca5cf2322b Mon Sep 17 00:00:00 2001 From: deep-quality-dev <84747155+deep-quality-dev@users.noreply.github.com> Date: Tue, 20 Aug 2024 09:15:42 -0400 Subject: [PATCH 4/8] Update ComplianceBridge.js --- tests/solidity/test/ComplianceBridge.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/tests/solidity/test/ComplianceBridge.js b/tests/solidity/test/ComplianceBridge.js index ca682322..1e3a42e0 100644 --- a/tests/solidity/test/ComplianceBridge.js +++ b/tests/solidity/test/ComplianceBridge.js @@ -22,7 +22,6 @@ describe('ComplianceBridge', () => { contract.address, contract.interface.encodeFunctionData("markUserAsVerified", [signer.address]) ) - // What happens after tx confirmation const res = await tx.wait() const parsedLog = contract.interface.parseLog(res.logs[0]) expect(parsedLog.args.success).to.be.true @@ -155,4 +154,4 @@ describe('ComplianceBridge', () => { expect(details.issuerAddress.toLowerCase()).to.be.equal(contract.address.toLowerCase()) } }) -}) \ No newline at end of file +}) From 2da676af81ac5bc9d9a3ce1ac773856f263df7e8 Mon Sep 17 00:00:00 2001 From: Hungry Warrior Date: Tue, 20 Aug 2024 14:44:59 +0000 Subject: [PATCH 5/8] Do only once --- tests/solidity/test/ComplianceBridge.js | 120 ++++++++++++------------ 1 file changed, 58 insertions(+), 62 deletions(-) diff --git a/tests/solidity/test/ComplianceBridge.js b/tests/solidity/test/ComplianceBridge.js index 1e3a42e0..287df208 100644 --- a/tests/solidity/test/ComplianceBridge.js +++ b/tests/solidity/test/ComplianceBridge.js @@ -40,41 +40,39 @@ describe('ComplianceBridge', () => { it('Should not change state with eth_call', async () => { const [signer] = await ethers.getSigners() - for (let i = 0; i < 100; i++) { - const wallet = ethers.Wallet.createRandom() - if (signer.address === wallet.address) { - continue - } - - // Check if random wallet was not verified yet - const isVerifiedRespBeforeCall = await sendShieldedQuery( - signer.provider, - contract.address, - contract.interface.encodeFunctionData("isUserVerified", [wallet.address]) - ) - const isVerifiedBeforeCall = contract.interface.decodeFunctionResult("isUserVerified", isVerifiedRespBeforeCall) - if (isVerifiedBeforeCall[0]) { - continue - } - - // Try to call `markUserAsVerified` by using `eth_call` - const markUserAsVerifiedResp = await sendShieldedQuery( - signer.provider, - contract.address, - contract.interface.encodeFunctionData("markUserAsVerified", [wallet.address]) - ) - const verificationId = contract.interface.decodeFunctionResult("markUserAsVerified", markUserAsVerifiedResp) - expect(verificationId.length).to.be.gt(0) - - // Confirm that verified status was not changed yet - const isVerifiedRespAfterCall = await sendShieldedQuery( - signer.provider, - contract.address, - contract.interface.encodeFunctionData("isUserVerified", [wallet.address]) - ) - const isVerifiedAfterCall = contract.interface.decodeFunctionResult("isUserVerified", isVerifiedRespAfterCall) - expect(isVerifiedAfterCall[0]).to.be.false + const wallet = ethers.Wallet.createRandom() + if (signer.address === wallet.address) { + return } + + // Check if random wallet was not verified yet + const isVerifiedRespBeforeCall = await sendShieldedQuery( + signer.provider, + contract.address, + contract.interface.encodeFunctionData("isUserVerified", [wallet.address]) + ) + const isVerifiedBeforeCall = contract.interface.decodeFunctionResult("isUserVerified", isVerifiedRespBeforeCall) + if (isVerifiedBeforeCall[0]) { + return + } + + // Try to call `markUserAsVerified` by using `eth_call` + const markUserAsVerifiedResp = await sendShieldedQuery( + signer.provider, + contract.address, + contract.interface.encodeFunctionData("markUserAsVerified", [wallet.address]) + ) + const verificationId = contract.interface.decodeFunctionResult("markUserAsVerified", markUserAsVerifiedResp) + expect(verificationId.length).to.be.gt(0) + + // Confirm that verified status was not changed yet + const isVerifiedRespAfterCall = await sendShieldedQuery( + signer.provider, + contract.address, + contract.interface.encodeFunctionData("isUserVerified", [wallet.address]) + ) + const isVerifiedAfterCall = contract.interface.decodeFunctionResult("isUserVerified", isVerifiedRespAfterCall) + expect(isVerifiedAfterCall[0]).to.be.false }) it('Should not change state with eth_estimateGas', async () => { @@ -83,35 +81,33 @@ describe('ComplianceBridge', () => { const ComplianceProxyFactory = await ethers.getContractFactory('ComplianceProxy') const contractUnencrypted = new ethers.Contract(CONTRACT_ADDRESS, ComplianceProxyFactory.interface, unencryptedProvider) - for (let i = 0; i < 100; i++) { - const wallet = ethers.Wallet.createRandom() - if (signer.address === wallet.address) { - continue - } - - // Check if random wallet was not verified yet - const isVerifiedRespBeforeEstimateGas = await sendShieldedQuery( - signer.provider, - contract.address, - contract.interface.encodeFunctionData("isUserVerified", [wallet.address]) - ) - const isVerifiedBeforeEstimateGas = contract.interface.decodeFunctionResult("isUserVerified", isVerifiedRespBeforeEstimateGas) - if (isVerifiedBeforeEstimateGas[0]) { - continue - } - - const gas = await contractUnencrypted.estimateGas.markUserAsVerified(signer.address) - expect(gas.gt(ethers.BigNumber.from(0))).to.be.true - - // Confirm that verified status was not changed yet - const isVerifiedRespAfterEstimateGas = await sendShieldedQuery( - signer.provider, - contract.address, - contract.interface.encodeFunctionData("isUserVerified", [wallet.address]) - ) - const isVerifiedAfterEstimateGas = contract.interface.decodeFunctionResult("isUserVerified", isVerifiedRespAfterEstimateGas) - expect(isVerifiedAfterEstimateGas[0]).to.be.false + const wallet = ethers.Wallet.createRandom() + if (signer.address === wallet.address) { + return + } + + // Check if random wallet was not verified yet + const isVerifiedRespBeforeEstimateGas = await sendShieldedQuery( + signer.provider, + contract.address, + contract.interface.encodeFunctionData("isUserVerified", [wallet.address]) + ) + const isVerifiedBeforeEstimateGas = contract.interface.decodeFunctionResult("isUserVerified", isVerifiedRespBeforeEstimateGas) + if (isVerifiedBeforeEstimateGas[0]) { + return } + + const gas = await contractUnencrypted.estimateGas.markUserAsVerified(signer.address) + expect(gas.gt(ethers.BigNumber.from(0))).to.be.true + + // Confirm that verified status was not changed yet + const isVerifiedRespAfterEstimateGas = await sendShieldedQuery( + signer.provider, + contract.address, + contract.interface.encodeFunctionData("isUserVerified", [wallet.address]) + ) + const isVerifiedAfterEstimateGas = contract.interface.decodeFunctionResult("isUserVerified", isVerifiedRespAfterEstimateGas) + expect(isVerifiedAfterEstimateGas[0]).to.be.false }) it('Should be able to check for specific issuer of verification', async () => { From 3ed9b26dda3189e68f42007d62225d962e15c73e Mon Sep 17 00:00:00 2001 From: Hungry Warrior Date: Tue, 20 Aug 2024 15:02:58 +0000 Subject: [PATCH 6/8] markUserAsVerified for random wallet --- tests/solidity/test/ComplianceBridge.js | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/tests/solidity/test/ComplianceBridge.js b/tests/solidity/test/ComplianceBridge.js index 287df208..0cc9ebdd 100644 --- a/tests/solidity/test/ComplianceBridge.js +++ b/tests/solidity/test/ComplianceBridge.js @@ -108,6 +108,25 @@ describe('ComplianceBridge', () => { ) const isVerifiedAfterEstimateGas = contract.interface.decodeFunctionResult("isUserVerified", isVerifiedRespAfterEstimateGas) expect(isVerifiedAfterEstimateGas[0]).to.be.false + + const tx = await sendShieldedTransaction( + signer, + contract.address, + contract.interface.encodeFunctionData("markUserAsVerified", [wallet.address]) + ) + const res = await tx.wait() + const parsedLog = contract.interface.parseLog(res.logs[0]) + expect(parsedLog.args.success).to.be.true + expect(parsedLog.args.data.length).to.be.greaterThan(0) + + // Confirm that verified status was changed after tx confirmation + const isVerifiedRespAfterTx = await sendShieldedQuery( + signer.provider, + contract.address, + contract.interface.encodeFunctionData("isUserVerified", [wallet.address]) + ) + const isVerifiedAfterTx = contract.interface.decodeFunctionResult("isUserVerified", isVerifiedRespAfterTx) + expect(isVerifiedAfterTx[0]).to.be.true }) it('Should be able to check for specific issuer of verification', async () => { From 2a59bb1c5afef8f8630cbb58de139cae377f7171 Mon Sep 17 00:00:00 2001 From: Hungry Warrior Date: Tue, 20 Aug 2024 15:40:12 +0000 Subject: [PATCH 7/8] Structure compliance bridge tests --- tests/solidity/test/ComplianceBridge.js | 304 +++++++++++++----------- 1 file changed, 166 insertions(+), 138 deletions(-) diff --git a/tests/solidity/test/ComplianceBridge.js b/tests/solidity/test/ComplianceBridge.js index 0cc9ebdd..4ff79ae3 100644 --- a/tests/solidity/test/ComplianceBridge.js +++ b/tests/solidity/test/ComplianceBridge.js @@ -1,11 +1,11 @@ -const { expect } = require("chai") -const { ethers } = require("hardhat") -const { sendShieldedTransaction, sendShieldedQuery } = require("./testUtils") +const { expect } = require('chai') +const { ethers } = require('hardhat') +const { sendShieldedTransaction, sendShieldedQuery } = require('./testUtils') -const unencryptedProvider = new ethers.providers.JsonRpcProvider("http://localhost:8547") // Unencrypted rpc url +const unencryptedProvider = new ethers.providers.JsonRpcProvider('http://localhost:8547') // Unencrypted rpc url describe('ComplianceBridge', () => { - const CONTRACT_ADDRESS = "0x30252afe8c1683fd184c99a3c44aa5d547d59dd4" + const CONTRACT_ADDRESS = '0x30252afe8c1683fd184c99a3c44aa5d547d59dd4' let contract before(async () => { @@ -14,105 +14,107 @@ describe('ComplianceBridge', () => { contract = new ethers.Contract(CONTRACT_ADDRESS, ComplianceProxyFactory.interface, signer) }) - it('Should be able to add verification details', async () => { - const [signer] = await ethers.getSigners() - - const tx = await sendShieldedTransaction( - signer, - contract.address, - contract.interface.encodeFunctionData("markUserAsVerified", [signer.address]) - ) - const res = await tx.wait() - const parsedLog = contract.interface.parseLog(res.logs[0]) - expect(parsedLog.args.success).to.be.true - expect(parsedLog.args.data.length).to.be.greaterThan(0) - - // Confirm that verified status was changed after tx confirmation - const isVerifiedRespAfterTx = await sendShieldedQuery( - signer.provider, - contract.address, - contract.interface.encodeFunctionData("isUserVerified", [signer.address]) - ) - const isVerifiedAfterTx = contract.interface.decodeFunctionResult("isUserVerified", isVerifiedRespAfterTx) - expect(isVerifiedAfterTx[0]).to.be.true - }) - - it('Should not change state with eth_call', async () => { - const [signer] = await ethers.getSigners() - - const wallet = ethers.Wallet.createRandom() - if (signer.address === wallet.address) { - return - } - - // Check if random wallet was not verified yet - const isVerifiedRespBeforeCall = await sendShieldedQuery( - signer.provider, - contract.address, - contract.interface.encodeFunctionData("isUserVerified", [wallet.address]) - ) - const isVerifiedBeforeCall = contract.interface.decodeFunctionResult("isUserVerified", isVerifiedRespBeforeCall) - if (isVerifiedBeforeCall[0]) { - return - } - - // Try to call `markUserAsVerified` by using `eth_call` - const markUserAsVerifiedResp = await sendShieldedQuery( - signer.provider, - contract.address, - contract.interface.encodeFunctionData("markUserAsVerified", [wallet.address]) - ) - const verificationId = contract.interface.decodeFunctionResult("markUserAsVerified", markUserAsVerifiedResp) - expect(verificationId.length).to.be.gt(0) - - // Confirm that verified status was not changed yet - const isVerifiedRespAfterCall = await sendShieldedQuery( - signer.provider, - contract.address, - contract.interface.encodeFunctionData("isUserVerified", [wallet.address]) - ) - const isVerifiedAfterCall = contract.interface.decodeFunctionResult("isUserVerified", isVerifiedRespAfterCall) - expect(isVerifiedAfterCall[0]).to.be.false + describe('Should not change any state', async () => { + it('eth_call', async () => { + const [signer] = await ethers.getSigners() + + const wallet = ethers.Wallet.createRandom() + if (signer.address === wallet.address) { + return + } + + // Check if random wallet was not verified yet + const isVerifiedRespBeforeCall = await sendShieldedQuery( + signer.provider, + contract.address, + contract.interface.encodeFunctionData('isUserVerified', [wallet.address]) + ) + const isVerifiedBeforeCall = contract.interface.decodeFunctionResult('isUserVerified', isVerifiedRespBeforeCall) + if (isVerifiedBeforeCall[0]) { + return + } + + // Try to call `markUserAsVerified` by using `eth_call` + const markUserAsVerifiedResp = await sendShieldedQuery( + signer.provider, + contract.address, + contract.interface.encodeFunctionData('markUserAsVerified', [wallet.address]) + ) + const verificationId = contract.interface.decodeFunctionResult('markUserAsVerified', markUserAsVerifiedResp) + expect(verificationId.length).to.be.gt(0) + + // Confirm that verified status was not changed yet + const isVerifiedRespAfterCall = await sendShieldedQuery( + signer.provider, + contract.address, + contract.interface.encodeFunctionData('isUserVerified', [wallet.address]) + ) + const isVerifiedAfterCall = contract.interface.decodeFunctionResult('isUserVerified', isVerifiedRespAfterCall) + expect(isVerifiedAfterCall[0]).to.be.false + }) + + it('eth_estimateGas', async () => { + const [signer] = await ethers.getSigners() + + const ComplianceProxyFactory = await ethers.getContractFactory('ComplianceProxy') + const contractUnencrypted = new ethers.Contract(CONTRACT_ADDRESS, ComplianceProxyFactory.interface, unencryptedProvider) + + const wallet = ethers.Wallet.createRandom() + if (signer.address === wallet.address) { + return + } + + // Check if random wallet was not verified yet + const isVerifiedRespBeforeEstimateGas = await sendShieldedQuery( + signer.provider, + contract.address, + contract.interface.encodeFunctionData('isUserVerified', [wallet.address]) + ) + const isVerifiedBeforeEstimateGas = contract.interface.decodeFunctionResult('isUserVerified', isVerifiedRespBeforeEstimateGas) + if (isVerifiedBeforeEstimateGas[0]) { + return + } + + const gas = await contractUnencrypted.estimateGas.markUserAsVerified(signer.address) + expect(gas.gt(ethers.BigNumber.from(0))).to.be.true + + // Confirm that verified status was not changed yet + const isVerifiedRespAfterEstimateGas = await sendShieldedQuery( + signer.provider, + contract.address, + contract.interface.encodeFunctionData('isUserVerified', [wallet.address]) + ) + const isVerifiedAfterEstimateGas = contract.interface.decodeFunctionResult('isUserVerified', isVerifiedRespAfterEstimateGas) + expect(isVerifiedAfterEstimateGas[0]).to.be.false + + const tx = await sendShieldedTransaction( + signer, + contract.address, + contract.interface.encodeFunctionData('markUserAsVerified', [wallet.address]) + ) + const res = await tx.wait() + const parsedLog = contract.interface.parseLog(res.logs[0]) + expect(parsedLog.args.success).to.be.true + expect(parsedLog.args.data.length).to.be.greaterThan(0) + + // Confirm that verified status was changed after tx confirmation + const isVerifiedRespAfterTx = await sendShieldedQuery( + signer.provider, + contract.address, + contract.interface.encodeFunctionData('isUserVerified', [wallet.address]) + ) + const isVerifiedAfterTx = contract.interface.decodeFunctionResult('isUserVerified', isVerifiedRespAfterTx) + expect(isVerifiedAfterTx[0]).to.be.true + }) }) - it('Should not change state with eth_estimateGas', async () => { + it('Should be able to add verification details', async () => { const [signer] = await ethers.getSigners() - const ComplianceProxyFactory = await ethers.getContractFactory('ComplianceProxy') - const contractUnencrypted = new ethers.Contract(CONTRACT_ADDRESS, ComplianceProxyFactory.interface, unencryptedProvider) - - const wallet = ethers.Wallet.createRandom() - if (signer.address === wallet.address) { - return - } - - // Check if random wallet was not verified yet - const isVerifiedRespBeforeEstimateGas = await sendShieldedQuery( - signer.provider, - contract.address, - contract.interface.encodeFunctionData("isUserVerified", [wallet.address]) - ) - const isVerifiedBeforeEstimateGas = contract.interface.decodeFunctionResult("isUserVerified", isVerifiedRespBeforeEstimateGas) - if (isVerifiedBeforeEstimateGas[0]) { - return - } - - const gas = await contractUnencrypted.estimateGas.markUserAsVerified(signer.address) - expect(gas.gt(ethers.BigNumber.from(0))).to.be.true - - // Confirm that verified status was not changed yet - const isVerifiedRespAfterEstimateGas = await sendShieldedQuery( - signer.provider, - contract.address, - contract.interface.encodeFunctionData("isUserVerified", [wallet.address]) - ) - const isVerifiedAfterEstimateGas = contract.interface.decodeFunctionResult("isUserVerified", isVerifiedRespAfterEstimateGas) - expect(isVerifiedAfterEstimateGas[0]).to.be.false - const tx = await sendShieldedTransaction( signer, contract.address, - contract.interface.encodeFunctionData("markUserAsVerified", [wallet.address]) + contract.interface.encodeFunctionData('markUserAsVerified', [signer.address]) ) const res = await tx.wait() const parsedLog = contract.interface.parseLog(res.logs[0]) @@ -123,50 +125,76 @@ describe('ComplianceBridge', () => { const isVerifiedRespAfterTx = await sendShieldedQuery( signer.provider, contract.address, - contract.interface.encodeFunctionData("isUserVerified", [wallet.address]) + contract.interface.encodeFunctionData('isUserVerified', [signer.address]) ) - const isVerifiedAfterTx = contract.interface.decodeFunctionResult("isUserVerified", isVerifiedRespAfterTx) + const isVerifiedAfterTx = contract.interface.decodeFunctionResult('isUserVerified', isVerifiedRespAfterTx) expect(isVerifiedAfterTx[0]).to.be.true }) - it('Should be able to check for specific issuer of verification', async () => { - const [signer] = await ethers.getSigners() - - const allowedIssuers = [contract.address] - const isVerifiedResponse = await sendShieldedQuery( - signer.provider, - contract.address, - contract.interface.encodeFunctionData("isUserVerifiedBy", [signer.address, allowedIssuers]) - ) - const result = contract.interface.decodeFunctionResult("isUserVerifiedBy", isVerifiedResponse) - expect(result[0]).to.be.true - }) - - it('Should be able to check for verification without issuers', async () => { - const [signer] = await ethers.getSigners() - - const isVerifiedResponse = await sendShieldedQuery( - signer.provider, - contract.address, - contract.interface.encodeFunctionData("isUserVerified", [signer.address]) - ) - const result = contract.interface.decodeFunctionResult("isUserVerified", isVerifiedResponse) - expect(result[0]).to.be.true - }) - - it('Should be able to get verification data', async () => { - const [signer] = await ethers.getSigners() - - const verificationData = await sendShieldedQuery( - signer.provider, - contract.address, - contract.interface.encodeFunctionData("getVerificationData", [signer.address]) - ) - const result = contract.interface.decodeFunctionResult("getVerificationData", verificationData) - expect(result[0].length).to.be.greaterThan(0) - for (const details of result[0]) { - expect(details.issuerAddress.length).to.be.greaterThan(0) - expect(details.issuerAddress.toLowerCase()).to.be.equal(contract.address.toLowerCase()) - } + describe('With verified user', async () => { + let verified + beforeEach(async () => { + const [signer] = await ethers.getSigners() + + waitingForVerified = ethers.Wallet.createRandom() + const tx = await sendShieldedTransaction( + signer, + contract.address, + contract.interface.encodeFunctionData('markUserAsVerified', [waitingForVerified.address]) + ) + await tx.wait() + + // Confirm that verified status was changed after tx confirmation + const isVerifiedRespAfterTx = await sendShieldedQuery( + signer.provider, + contract.address, + contract.interface.encodeFunctionData('isUserVerified', [waitingForVerified.address]) + ) + const isVerifiedAfterTx = contract.interface.decodeFunctionResult('isUserVerified', isVerifiedRespAfterTx) + expect(isVerifiedAfterTx[0]).to.be.true + + verified = waitingForVerified + }) + + it('Should be able to check for specific issuer of verification', async () => { + const [signer] = await ethers.getSigners() + + const allowedIssuers = [contract.address] + const isVerifiedResponse = await sendShieldedQuery( + signer.provider, + contract.address, + contract.interface.encodeFunctionData('isUserVerifiedBy', [verified.address, allowedIssuers]) + ) + const result = contract.interface.decodeFunctionResult('isUserVerifiedBy', isVerifiedResponse) + expect(result[0]).to.be.true + }) + + it('Should be able to check for verification without issuers', async () => { + const [signer] = await ethers.getSigners() + + const isVerifiedResponse = await sendShieldedQuery( + signer.provider, + contract.address, + contract.interface.encodeFunctionData('isUserVerified', [verified.address]) + ) + const result = contract.interface.decodeFunctionResult('isUserVerified', isVerifiedResponse) + expect(result[0]).to.be.true + }) + + it('Should be able to get verification data', async () => { + const [signer] = await ethers.getSigners() + + const verificationData = await sendShieldedQuery( + signer.provider, + contract.address, + contract.interface.encodeFunctionData('getVerificationData', [verified.address]) + ) + const result = contract.interface.decodeFunctionResult('getVerificationData', verificationData) + expect(result[0].length).to.be.greaterThan(0) + for (const details of result[0]) { + expect(details.issuerAddress.length).to.be.greaterThan(0) + expect(details.issuerAddress.toLowerCase()).to.be.equal(contract.address.toLowerCase()) + } + }) }) }) From 105423bd11a661060d981f948a320f0ca0da759f Mon Sep 17 00:00:00 2001 From: Hungry Warrior Date: Wed, 21 Aug 2024 09:11:51 +0000 Subject: [PATCH 8/8] Remove redundant code --- tests/solidity/test/ComplianceBridge.js | 20 +++++--------------- 1 file changed, 5 insertions(+), 15 deletions(-) diff --git a/tests/solidity/test/ComplianceBridge.js b/tests/solidity/test/ComplianceBridge.js index 4ff79ae3..8b67e95c 100644 --- a/tests/solidity/test/ComplianceBridge.js +++ b/tests/solidity/test/ComplianceBridge.js @@ -15,13 +15,10 @@ describe('ComplianceBridge', () => { }) describe('Should not change any state', async () => { - it('eth_call', async () => { + it('with eth_call', async () => { const [signer] = await ethers.getSigners() const wallet = ethers.Wallet.createRandom() - if (signer.address === wallet.address) { - return - } // Check if random wallet was not verified yet const isVerifiedRespBeforeCall = await sendShieldedQuery( @@ -30,9 +27,7 @@ describe('ComplianceBridge', () => { contract.interface.encodeFunctionData('isUserVerified', [wallet.address]) ) const isVerifiedBeforeCall = contract.interface.decodeFunctionResult('isUserVerified', isVerifiedRespBeforeCall) - if (isVerifiedBeforeCall[0]) { - return - } + expect(isVerifiedBeforeCall[0]).to.be.false // Try to call `markUserAsVerified` by using `eth_call` const markUserAsVerifiedResp = await sendShieldedQuery( @@ -53,16 +48,13 @@ describe('ComplianceBridge', () => { expect(isVerifiedAfterCall[0]).to.be.false }) - it('eth_estimateGas', async () => { + it('with eth_estimateGas', async () => { const [signer] = await ethers.getSigners() const ComplianceProxyFactory = await ethers.getContractFactory('ComplianceProxy') const contractUnencrypted = new ethers.Contract(CONTRACT_ADDRESS, ComplianceProxyFactory.interface, unencryptedProvider) const wallet = ethers.Wallet.createRandom() - if (signer.address === wallet.address) { - return - } // Check if random wallet was not verified yet const isVerifiedRespBeforeEstimateGas = await sendShieldedQuery( @@ -71,9 +63,7 @@ describe('ComplianceBridge', () => { contract.interface.encodeFunctionData('isUserVerified', [wallet.address]) ) const isVerifiedBeforeEstimateGas = contract.interface.decodeFunctionResult('isUserVerified', isVerifiedRespBeforeEstimateGas) - if (isVerifiedBeforeEstimateGas[0]) { - return - } + expect(isVerifiedBeforeEstimateGas[0]).to.be.false const gas = await contractUnencrypted.estimateGas.markUserAsVerified(signer.address) expect(gas.gt(ethers.BigNumber.from(0))).to.be.true @@ -136,7 +126,7 @@ describe('ComplianceBridge', () => { beforeEach(async () => { const [signer] = await ethers.getSigners() - waitingForVerified = ethers.Wallet.createRandom() + const waitingForVerified = ethers.Wallet.createRandom() const tx = await sendShieldedTransaction( signer, contract.address,