From 4e9f5c88231a011f2ce15955d8c15e1e68ea4e32 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20=27birdy=27=20Danjou?= Date: Thu, 18 Jan 2024 11:12:20 +0100 Subject: [PATCH 1/3] deps() update to fhevmjs 0.3.1 --- package-lock.json | 48 +++++++++++++-------------- package.json | 2 +- tasks/taskIdentity.ts | 2 +- tasks/utils.ts | 14 ++++---- test/blindAuction/BlindAuction.ts | 6 ++-- test/encryptedERC20/EncryptedERC20.ts | 14 ++++---- test/governor/Comp.ts | 4 +-- test/identity/compliantERC20.ts | 8 ++--- test/identity/identityRegistry.ts | 2 +- test/instance.ts | 24 +++++++------- 10 files changed, 61 insertions(+), 63 deletions(-) diff --git a/package-lock.json b/package-lock.json index 65b5c38f..f24fdd42 100644 --- a/package-lock.json +++ b/package-lock.json @@ -34,7 +34,7 @@ "eslint": "^8.28.0", "eslint-config-prettier": "^8.5.0", "ethers": "^6.8.0", - "fhevmjs": "^0.2.0", + "fhevmjs": "^0.3.1", "fs-extra": "^10.1.0", "ganache": "^7.9.0", "hardhat": "^2.19.4", @@ -5270,9 +5270,9 @@ } }, "node_modules/fhevmjs": { - "version": "0.2.0", - "resolved": "https://registry.npmjs.org/fhevmjs/-/fhevmjs-0.2.0.tgz", - "integrity": "sha512-2+GwV7DKtvRobp56E0ZR71vMgvurlyXjfIAWy2h+ChO8uN4JbCF1fgeY/EjzcKcwu59yEB7xkIRggu8OkzlP5A==", + "version": "0.3.1", + "resolved": "https://registry.npmjs.org/fhevmjs/-/fhevmjs-0.3.1.tgz", + "integrity": "sha512-CTn/aavXeMmQmO3ltLNIXV5kUsysfPDdiafVyb3c0NGDAQ3ltVwh4Af4qw4ey7xwhlS50WBBZc7EoKM3SIWnRg==", "dev": true, "dependencies": { "commander": "^11.0.0", @@ -5280,9 +5280,9 @@ "ethers": "^6.6.4", "libsodium": "^0.7.11", "libsodium-wrappers": "^0.7.11", - "node-tfhe": "^0.3.1", + "node-tfhe": "^0.4.1", "sha3": "^2.1.4", - "tfhe": "^0.3.1" + "tfhe": "^0.4.1" }, "bin": { "fhevm": "bin/fhevm.js" @@ -5742,7 +5742,6 @@ "resolved": "https://registry.npmjs.org/bufferutil/-/bufferutil-4.0.5.tgz", "integrity": "sha512-HTm14iMQKK2FjFLRTM5lAVcyaUzOnqbPtesFIvREgXpJHdQm8bWS+GkQgIkfaBYRHuCnea7w8UVNfwiAQhlr9A==", "dev": true, - "hasInstallScript": true, "optional": true, "dependencies": { "node-gyp-build": "^4.3.0" @@ -6100,7 +6099,6 @@ "resolved": "https://registry.npmjs.org/utf-8-validate/-/utf-8-validate-5.0.7.tgz", "integrity": "sha512-vLt1O5Pp+flcArHGIyKEQq883nBt8nN8tVBcoL0qUXj2XT1n7p70yGIq2VK98I5FdZ1YHc0wk/koOnHjnXWk1Q==", "dev": true, - "hasInstallScript": true, "optional": true, "dependencies": { "node-gyp-build": "^4.3.0" @@ -8068,9 +8066,9 @@ } }, "node_modules/node-tfhe": { - "version": "0.3.1", - "resolved": "https://registry.npmjs.org/node-tfhe/-/node-tfhe-0.3.1.tgz", - "integrity": "sha512-re5tBgPMwoSWl6b/pL13MWKnsBUv9nldAXoWkokG1aiUm9P0aOjJoz0J9+zOwV/dB0i9xN/TI//mnqF3uWAOIg==", + "version": "0.4.1", + "resolved": "https://registry.npmjs.org/node-tfhe/-/node-tfhe-0.4.1.tgz", + "integrity": "sha512-GysZYDRnvL8UyywaK/+/Rvqvpdk5pIRR8MaCqEJmaG3rGo/8OXwPzb6WJaAyYNZz2VmimCGBxznkuNVRviFHMQ==", "dev": true }, "node_modules/nofilter": { @@ -10431,9 +10429,9 @@ "dev": true }, "node_modules/tfhe": { - "version": "0.3.1", - "resolved": "https://registry.npmjs.org/tfhe/-/tfhe-0.3.1.tgz", - "integrity": "sha512-PqnsUVChETviwd5ebfJoj9yueUxIKPaSsfSGVmgvpUsUqAlianAx+4UH9hEmjDxgshSRDgm1Fp0/rRgCx2BYog==", + "version": "0.4.1", + "resolved": "https://registry.npmjs.org/tfhe/-/tfhe-0.4.1.tgz", + "integrity": "sha512-Ok8CrrryBjtgCbPliDG3YuAFX0oJLS4y9pCajF6TSORyKYIDVtfOKbVntG2gZFOeUukUTHSI5FVofqhX5GkPTw==", "dev": true }, "node_modules/then-request": { @@ -15229,9 +15227,9 @@ } }, "fhevmjs": { - "version": "0.2.0", - "resolved": "https://registry.npmjs.org/fhevmjs/-/fhevmjs-0.2.0.tgz", - "integrity": "sha512-2+GwV7DKtvRobp56E0ZR71vMgvurlyXjfIAWy2h+ChO8uN4JbCF1fgeY/EjzcKcwu59yEB7xkIRggu8OkzlP5A==", + "version": "0.3.1", + "resolved": "https://registry.npmjs.org/fhevmjs/-/fhevmjs-0.3.1.tgz", + "integrity": "sha512-CTn/aavXeMmQmO3ltLNIXV5kUsysfPDdiafVyb3c0NGDAQ3ltVwh4Af4qw4ey7xwhlS50WBBZc7EoKM3SIWnRg==", "dev": true, "requires": { "commander": "^11.0.0", @@ -15239,9 +15237,9 @@ "ethers": "^6.6.4", "libsodium": "^0.7.11", "libsodium-wrappers": "^0.7.11", - "node-tfhe": "^0.3.1", + "node-tfhe": "^0.4.1", "sha3": "^2.1.4", - "tfhe": "^0.3.1" + "tfhe": "^0.4.1" } }, "file-entry-cache": { @@ -17300,9 +17298,9 @@ "integrity": "sha512-NTZVKn9IylLwUzaKjkas1e4u2DLNcV4rdYagA4PWdPwW87Bi7z+BznyKSRwS/761tV/lzCGXplWsiaMjLqP2zQ==" }, "node-tfhe": { - "version": "0.3.1", - "resolved": "https://registry.npmjs.org/node-tfhe/-/node-tfhe-0.3.1.tgz", - "integrity": "sha512-re5tBgPMwoSWl6b/pL13MWKnsBUv9nldAXoWkokG1aiUm9P0aOjJoz0J9+zOwV/dB0i9xN/TI//mnqF3uWAOIg==", + "version": "0.4.1", + "resolved": "https://registry.npmjs.org/node-tfhe/-/node-tfhe-0.4.1.tgz", + "integrity": "sha512-GysZYDRnvL8UyywaK/+/Rvqvpdk5pIRR8MaCqEJmaG3rGo/8OXwPzb6WJaAyYNZz2VmimCGBxznkuNVRviFHMQ==", "dev": true }, "nofilter": { @@ -19079,9 +19077,9 @@ "dev": true }, "tfhe": { - "version": "0.3.1", - "resolved": "https://registry.npmjs.org/tfhe/-/tfhe-0.3.1.tgz", - "integrity": "sha512-PqnsUVChETviwd5ebfJoj9yueUxIKPaSsfSGVmgvpUsUqAlianAx+4UH9hEmjDxgshSRDgm1Fp0/rRgCx2BYog==", + "version": "0.4.1", + "resolved": "https://registry.npmjs.org/tfhe/-/tfhe-0.4.1.tgz", + "integrity": "sha512-Ok8CrrryBjtgCbPliDG3YuAFX0oJLS4y9pCajF6TSORyKYIDVtfOKbVntG2gZFOeUukUTHSI5FVofqhX5GkPTw==", "dev": true }, "then-request": { diff --git a/package.json b/package.json index 4d22a5ac..8948555f 100644 --- a/package.json +++ b/package.json @@ -66,7 +66,7 @@ "eslint": "^8.28.0", "eslint-config-prettier": "^8.5.0", "ethers": "^6.8.0", - "fhevmjs": "^0.2.0", + "fhevmjs": "^0.3.1", "fs-extra": "^10.1.0", "ganache": "^7.9.0", "hardhat": "^2.19.4", diff --git a/tasks/taskIdentity.ts b/tasks/taskIdentity.ts index b5ed0c27..1e5ef584 100644 --- a/tasks/taskIdentity.ts +++ b/tasks/taskIdentity.ts @@ -160,7 +160,7 @@ task('task:identity:balanceOf') const instance = await createInstance(erc20Address, signers[user], ethers); - const token = instance.getTokenSignature(erc20Address)!; + const token = instance.getPublicKey(erc20Address)!; const balance = await erc20.connect(signers[user]).balanceOf(signers[user], token.publicKey, token.signature); console.log(`=> ${chalk.bold('Balance')}: ${instance.decrypt(erc20Address, balance)} tokens`); diff --git a/tasks/utils.ts b/tasks/utils.ts index 36836bd7..27a6f2f4 100644 --- a/tasks/utils.ts +++ b/tasks/utils.ts @@ -25,21 +25,21 @@ export const createInstance = async (contractAddress: string, account: Signer, e publicKey = decoded[0]; } const instance = await fhevmjs.createInstance({ chainId, publicKey }); - await generateToken(contractAddress, account, instance); + await generatePublicKey(contractAddress, account, instance); return instance; }; -const generateToken = async (contractAddress: string, signer: Signer, instance: FhevmInstance) => { +const generatePublicKey = async (contractAddress: string, signer: Signer, instance: FhevmInstance) => { // Generate token to decrypt - const generatedToken = instance.generateToken({ + const generatedToken = instance.generatePublicKey({ verifyingContract: contractAddress, }); // Sign the public key const signature = await signer.signTypedData( - generatedToken.token.domain, - { Reencrypt: generatedToken.token.types.Reencrypt }, // Need to remove EIP712Domain from types - generatedToken.token.message, + generatedToken.eip712.domain, + { Reencrypt: generatedToken.eip712.types.Reencrypt }, // Need to remove EIP712Domain from types + generatedToken.eip712.message, ); - instance.setTokenSignature(contractAddress, signature); + instance.setSignature(contractAddress, signature); }; diff --git a/test/blindAuction/BlindAuction.ts b/test/blindAuction/BlindAuction.ts index 254603f8..f1819886 100644 --- a/test/blindAuction/BlindAuction.ts +++ b/test/blindAuction/BlindAuction.ts @@ -68,14 +68,14 @@ describe('BlindAuction', function () { const txAliceStop = await this.blindAuction.connect(this.signers.alice).stop(); await txAliceStop.wait(); - const tokenCarol = this.instances.carol.getTokenSignature(this.contractAddress)!; + const tokenCarol = this.instances.carol.getPublicKey(this.contractAddress)!; const carolBidAmountCheckEnc = await this.blindAuction .connect(this.signers.carol) .getBid(tokenCarol.publicKey, tokenCarol.signature); const carolBidAmountCheckDec = this.instances.carol.decrypt(this.contractAddress, carolBidAmountCheckEnc); expect(carolBidAmountCheckDec).to.equal(20); - const tokenBob = this.instances.bob.getTokenSignature(this.contractAddress)!; + const tokenBob = this.instances.bob.getPublicKey(this.contractAddress)!; const bobBidAmountCheckEnc = await this.blindAuction .connect(this.signers.bob) .getBid(tokenBob.publicKey, tokenBob.signature); @@ -101,7 +101,7 @@ describe('BlindAuction', function () { await txCarolWithdraw.wait(); const instance = await createInstances(this.contractERC20Address, ethers, this.signers); - const tokenAlice = instance.alice.getTokenSignature(this.contractERC20Address)!; + const tokenAlice = instance.alice.getPublicKey(this.contractERC20Address)!; const encryptedBalanceAlice = await this.erc20.balanceOf( this.signers.alice, tokenAlice.publicKey, diff --git a/test/encryptedERC20/EncryptedERC20.ts b/test/encryptedERC20/EncryptedERC20.ts index 53fcc6d9..b42864ca 100644 --- a/test/encryptedERC20/EncryptedERC20.ts +++ b/test/encryptedERC20/EncryptedERC20.ts @@ -22,7 +22,7 @@ describe('EncryptedERC20', function () { const transaction = await this.erc20.mint(1000); await transaction.wait(); // Call the method - const token = this.instances.alice.getTokenSignature(this.contractAddress) || { + const token = this.instances.alice.getPublicKey(this.contractAddress) || { signature: '', publicKey: '', }; @@ -44,7 +44,7 @@ describe('EncryptedERC20', function () { const tx = await this.erc20['transfer(address,bytes)'](this.signers.bob.address, encryptedTransferAmount); await tx.wait(); - const tokenAlice = this.instances.alice.getTokenSignature(this.contractAddress)!; + const tokenAlice = this.instances.alice.getPublicKey(this.contractAddress)!; const encryptedBalanceAlice = await this.erc20.balanceOf( this.signers.alice, @@ -59,7 +59,7 @@ describe('EncryptedERC20', function () { const bobErc20 = this.erc20.connect(this.signers.bob); - const tokenBob = this.instances.bob.getTokenSignature(this.contractAddress)!; + const tokenBob = this.instances.bob.getPublicKey(this.contractAddress)!; const encryptedBalanceBob = await bobErc20.balanceOf(this.signers.bob, tokenBob.publicKey, tokenBob.signature); @@ -77,7 +77,7 @@ describe('EncryptedERC20', function () { const tx = await this.erc20['transfer(address,bytes)'](this.signers.bob.address, encryptedTransferAmount); await tx.wait(); - const tokenAlice = this.instances.alice.getTokenSignature(this.contractAddress)!; + const tokenAlice = this.instances.alice.getPublicKey(this.contractAddress)!; const encryptedBalanceAlice = await this.erc20.balanceOf( this.signers.alice, @@ -92,7 +92,7 @@ describe('EncryptedERC20', function () { const bobErc20 = this.erc20.connect(this.signers.bob); - const tokenBob = this.instances.bob.getTokenSignature(this.contractAddress)!; + const tokenBob = this.instances.bob.getPublicKey(this.contractAddress)!; const encryptedBalanceBob = await bobErc20.balanceOf(this.signers.bob, tokenBob.publicKey, tokenBob.signature); @@ -119,7 +119,7 @@ describe('EncryptedERC20', function () { ); await tx2.wait(); - const tokenAlice = this.instances.alice.getTokenSignature(this.contractAddress)!; + const tokenAlice = this.instances.alice.getPublicKey(this.contractAddress)!; const encryptedBalanceAlice = await this.erc20.balanceOf( this.signers.alice, tokenAlice.publicKey, @@ -130,7 +130,7 @@ describe('EncryptedERC20', function () { const balanceAlice = this.instances.alice.decrypt(this.contractAddress, encryptedBalanceAlice); expect(balanceAlice).to.equal(10000); // check that transfer did not happen, as expected - const tokenBob = this.instances.bob.getTokenSignature(this.contractAddress)!; + const tokenBob = this.instances.bob.getPublicKey(this.contractAddress)!; const encryptedBalanceBob = await bobErc20.balanceOf(this.signers.bob, tokenBob.publicKey, tokenBob.signature); // Decrypt the balance const balanceBob = this.instances.bob.decrypt(this.contractAddress, encryptedBalanceBob); diff --git a/test/governor/Comp.ts b/test/governor/Comp.ts index 500c4d25..5cfab3bd 100644 --- a/test/governor/Comp.ts +++ b/test/governor/Comp.ts @@ -27,7 +27,7 @@ describe('Comp', function () { await transferTransac.wait(); - const aliceToken = this.instances.alice.getTokenSignature(this.contractAddress) || { + const aliceToken = this.instances.alice.getPublicKey(this.contractAddress) || { signature: '', publicKey: '', }; @@ -36,7 +36,7 @@ describe('Comp', function () { const aliceBalance = this.instances.alice.decrypt(this.contractAddress, encryptedAliceBalance); expect(aliceBalance).to.equal(800000); - const bobToken = this.instances.bob.getTokenSignature(this.contractAddress) || { + const bobToken = this.instances.bob.getPublicKey(this.contractAddress) || { signature: '', publicKey: '', }; diff --git a/test/identity/compliantERC20.ts b/test/identity/compliantERC20.ts index c9044933..5349e030 100644 --- a/test/identity/compliantERC20.ts +++ b/test/identity/compliantERC20.ts @@ -82,7 +82,7 @@ describe('CompliantERC20', function () { const country1Admin = new ethers.Wallet(WALLET_COUNTRY1_PK).connect(ethers.provider); const country1Instance = await createInstance(this.contractAddress, country1Admin, ethers); - const token = country1Instance.getTokenSignature(this.contractAddress) || { + const token = country1Instance.getPublicKey(this.contractAddress) || { signature: '', publicKey: '', }; @@ -109,7 +109,7 @@ describe('CompliantERC20', function () { expect(encryptedDaveBalance).to.throw; - const carolToken = this.instances.carol.getTokenSignature(this.contractAddress) || { + const carolToken = this.instances.carol.getPublicKey(this.contractAddress) || { signature: '', publicKey: '', }; @@ -172,12 +172,12 @@ describe('CompliantERC20', function () { const txT2 = await this.identifiedErc20['transfer(address,bytes)'](this.signers.dave, amount10k); await Promise.all([txT1.wait(), txT2.wait()]); - const carolToken = this.instances.carol.getTokenSignature(this.contractAddress) || { + const carolToken = this.instances.carol.getPublicKey(this.contractAddress) || { signature: '', publicKey: '', }; - const daveToken = this.instances.dave.getTokenSignature(this.contractAddress) || { + const daveToken = this.instances.dave.getPublicKey(this.contractAddress) || { signature: '', publicKey: '', }; diff --git a/test/identity/identityRegistry.ts b/test/identity/identityRegistry.ts index 44b102ab..9c565eaf 100644 --- a/test/identity/identityRegistry.ts +++ b/test/identity/identityRegistry.ts @@ -39,7 +39,7 @@ describe('Identity', function () { await allowed.wait(); // Carol use this token to access information - const token = this.instances.carol.getTokenSignature(this.contractAddress) || { + const token = this.instances.carol.getPublicKey(this.contractAddress) || { signature: '', publicKey: '', }; diff --git a/test/instance.ts b/test/instance.ts index 3841d202..bf90a8c3 100644 --- a/test/instance.ts +++ b/test/instance.ts @@ -49,7 +49,7 @@ export const createInstance = async (contractAddress: string, account: Signer, e publicKey = decoded[0]; } let instance = await fhevmjs.createInstance({ chainId: 31337, publicKey: '0x00' }); // 31337 is hardhat node's default chainId - await generateToken(contractAddress, account, instance); + await generatePublicKey(contractAddress, account, instance); if (network === 'hardhat') { instance.encrypt8 = createUintToUint8ArrayFunction(8); @@ -58,24 +58,24 @@ export const createInstance = async (contractAddress: string, account: Signer, e instance.decrypt = (_, hexadecimalString) => Number(BigInt(hexadecimalString)); } else { instance = await fhevmjs.createInstance({ chainId, publicKey }); - await generateToken(contractAddress, account, instance); + await generatePublicKey(contractAddress, account, instance); } return instance; }; -const generateToken = async (contractAddress: string, signer: Signer, instance: FhevmInstance) => { +const generatePublicKey = async (contractAddress: string, signer: Signer, instance: FhevmInstance) => { // Generate token to decrypt - const generatedToken = instance.generateToken({ + const generatedToken = instance.generatePublicKey({ verifyingContract: contractAddress, }); // Sign the public key const signature = await signer.signTypedData( - generatedToken.token.domain, - { Reencrypt: generatedToken.token.types.Reencrypt }, // Need to remove EIP712Domain from types - generatedToken.token.message, + generatedToken.eip712.domain, + { Reencrypt: generatedToken.eip712.types.Reencrypt }, // Need to remove EIP712Domain from types + generatedToken.eip712.message, ); - instance.setTokenSignature(contractAddress, signature); + instance.setSignature(contractAddress, signature); }; function createUintToUint8ArrayFunction(numBits: number) { @@ -85,16 +85,16 @@ function createUintToUint8ArrayFunction(numBits: number) { return function (uint: number) { // Applying modulus operation to ensure the value fits in numBits - let value = uint % maxValue; + const value = uint % maxValue; // Create a buffer of 32 bytes - let buffer = new ArrayBuffer(totalBytes); - let view = new DataView(buffer); + const buffer = new ArrayBuffer(totalBytes); + const view = new DataView(buffer); // Fill the relevant part of the buffer byte by byte for (let i = 0; i < numBytes; i++) { // Calculate the value for each byte and set it in the last numBytes of the buffer - let byteValue = (value >> (8 * (numBytes - i - 1))) & 0xff; + const byteValue = (value >> (8 * (numBytes - i - 1))) & 0xff; view.setUint8(totalBytes - numBytes + i, byteValue); } From e7f25d7d36425c34653704616e3099d786d8a2e3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20=27birdy=27=20Danjou?= Date: Thu, 18 Jan 2024 11:15:58 +0100 Subject: [PATCH 2/3] fix() remove useless coverage command --- package.json | 1 - 1 file changed, 1 deletion(-) diff --git a/package.json b/package.json index 8948555f..394bdbae 100644 --- a/package.json +++ b/package.json @@ -10,7 +10,6 @@ "scripts": { "clean": "rimraf ./artifacts ./cache ./coverage ./types ./coverage.json && npm run typechain", "compile": "cross-env TS_NODE_TRANSPILE_ONLY=true hardhat compile", - "coverage": "hardhat coverage --solcoverjs ./.solcover.js --temp artifacts --testfiles \"test/**/*.ts\" && pnpm typechain", "deploy:contracts": "hardhat deploy", "lint": "npm run lint:sol && npm run lint:ts && npm run prettier:check", "lint:ts": "eslint --ignore-path ./.eslintignore --ext .js,.ts .", From cba380d853a8542cce557cb25e7369e49089c5bd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20=27birdy=27=20Danjou?= Date: Thu, 18 Jan 2024 11:23:45 +0100 Subject: [PATCH 3/3] docs() small adjustment in documentation --- docs/client/examples/getbalance.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/docs/client/examples/getbalance.md b/docs/client/examples/getbalance.md index 8352d1bb..3e7e197f 100644 --- a/docs/client/examples/getbalance.md +++ b/docs/client/examples/getbalance.md @@ -51,9 +51,9 @@ const getBalance = async () => { // Sign the public key const signature = await signer.signTypedData( - generatedToken.token.domain, - { Reencrypt: generatedToken.token.types.Reencrypt }, // Need to remove EIP712Domain from types - generatedToken.token.message, + generatedToken.eip712.domain, + { Reencrypt: generatedToken.eip712.types.Reencrypt }, // Need to remove EIP712Domain from types + generatedToken.eip712.message, ); instance.setPublicKeySignature(CONTRACT_ADDRESS, signature);