From b8ecc8beea4d68af0484e83d004a98c255e5a6fc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joseph-Andr=C3=A9=20Turk?= Date: Fri, 30 Aug 2024 19:35:26 +0200 Subject: [PATCH] fix: remove debugging with fhe privKey from tests chore: fixed typo in jsdoc --- test/encryptedERC20/EncryptedERC20.fixture.ts | 2 + test/encryptedERC20/EncryptedERC20.ts | 173 +++++++++++++----- test/instance.ts | 63 +++++++ 3 files changed, 192 insertions(+), 46 deletions(-) diff --git a/test/encryptedERC20/EncryptedERC20.fixture.ts b/test/encryptedERC20/EncryptedERC20.fixture.ts index d84ca39..cd92f6a 100644 --- a/test/encryptedERC20/EncryptedERC20.fixture.ts +++ b/test/encryptedERC20/EncryptedERC20.fixture.ts @@ -1,3 +1,5 @@ +import { ethers } from "hardhat"; + import type { EncryptedERC20 } from "../../types"; import { getSigners } from "../signers"; diff --git a/test/encryptedERC20/EncryptedERC20.ts b/test/encryptedERC20/EncryptedERC20.ts index d612a9e..b210fd7 100644 --- a/test/encryptedERC20/EncryptedERC20.ts +++ b/test/encryptedERC20/EncryptedERC20.ts @@ -1,6 +1,6 @@ import { expect } from "chai"; -import { createInstances, decrypt64 } from "../instance"; +import { createInstances } from "../instance"; import { getSigners, initSigners } from "../signers"; import { deployEncryptedERC20Fixture } from "./EncryptedERC20.fixture"; @@ -21,9 +21,24 @@ describe("EncryptedERC20", function () { const transaction = await this.erc20.mint(1000); await transaction.wait(); - const balanceHandle = await this.erc20.balanceOf(this.signers.alice); - const balance = await decrypt64(balanceHandle); - expect(balance).to.equal(1000); + // Reencrypt Alice's balance + const balanceHandleAlice = await this.erc20.balanceOf(this.signers.alice); + const { publicKey: publicKeyAlice, privateKey: privateKeyAlice } = this.instances.alice.generateKeypair(); + const eip712 = this.instances.alice.createEIP712(publicKeyAlice, this.contractAddress); + const signatureAlice = await this.signers.alice.signTypedData( + eip712.domain, + { Reencrypt: eip712.types.Reencrypt }, + eip712.message, + ); + const balanceAlice = await this.instances.alice.reencrypt( + balanceHandleAlice, + privateKeyAlice, + publicKeyAlice, + signatureAlice.replace("0x", ""), + this.contractAddress, + this.signers.alice.address, + ); + expect(balanceAlice).to.equal(1000); const totalSupply = await this.erc20.totalSupply(); expect(totalSupply).to.equal(1000); @@ -45,21 +60,51 @@ describe("EncryptedERC20", function () { const t2 = await tx.wait(); expect(t2?.status).to.eq(1); - // Decrypt Alice's balance + // Reencrypt Alice's balance const balanceHandleAlice = await this.erc20.balanceOf(this.signers.alice); - const balanceAlice = await decrypt64(balanceHandleAlice); + const { publicKey: publicKeyAlice, privateKey: privateKeyAlice } = this.instances.alice.generateKeypair(); + const eip712 = this.instances.alice.createEIP712(publicKeyAlice, this.contractAddress); + const signatureAlice = await this.signers.alice.signTypedData( + eip712.domain, + { Reencrypt: eip712.types.Reencrypt }, + eip712.message, + ); + const balanceAlice = await this.instances.alice.reencrypt( + balanceHandleAlice, + privateKeyAlice, + publicKeyAlice, + signatureAlice.replace("0x", ""), + this.contractAddress, + this.signers.alice.address, + ); + expect(balanceAlice).to.equal(10000 - 1337); - // Decrypt Bob's balance + // Reencrypt Bob's balance const balanceHandleBob = await this.erc20.balanceOf(this.signers.bob); - const balanceBob = await decrypt64(balanceHandleBob); + + const { publicKey: publicKeyBob, privateKey: privateKeyBob } = this.instances.bob.generateKeypair(); + const eip712Bob = this.instances.bob.createEIP712(publicKeyBob, this.contractAddress); + const signatureBob = await this.signers.bob.signTypedData( + eip712Bob.domain, + { Reencrypt: eip712Bob.types.Reencrypt }, + eip712Bob.message, + ); + const balanceBob = await this.instances.bob.reencrypt( + balanceHandleBob, + privateKeyBob, + publicKeyBob, + signatureBob.replace("0x", ""), + this.contractAddress, + this.signers.bob.address, + ); + expect(balanceBob).to.equal(1337); }); - it("reencrypt - should transfer tokens between two users", async function () { - const transaction = await this.erc20.mint(10000); - const t1 = await transaction.wait(); - expect(t1?.status).to.eq(1); + it("should not transfer tokens between two users", async function () { + const transaction = await this.erc20.mint(1000); + await transaction.wait(); const input = this.instances.alice.createEncryptedInput(this.contractAddress, this.signers.alice.address); input.add64(1337); @@ -69,52 +114,46 @@ describe("EncryptedERC20", function () { encryptedTransferAmount.handles[0], encryptedTransferAmount.inputProof, ); - const t2 = await tx.wait(); - expect(t2?.status).to.eq(1); + await tx.wait(); - // Decrypt Alice's balance const balanceHandleAlice = await this.erc20.balanceOf(this.signers.alice); - const { publicKey, privateKey } = this.instances.alice.generateKeypair(); - const eip712 = this.instances.alice.createEIP712(publicKey, this.contractAddress); - const signature = await this.signers.alice.signTypedData( + const { publicKey: publicKeyAlice, privateKey: privateKeyAlice } = this.instances.alice.generateKeypair(); + const eip712 = this.instances.alice.createEIP712(publicKeyAlice, this.contractAddress); + const signatureAlice = await this.signers.alice.signTypedData( eip712.domain, { Reencrypt: eip712.types.Reencrypt }, eip712.message, ); const balanceAlice = await this.instances.alice.reencrypt( balanceHandleAlice, - privateKey, - publicKey, - signature.replace("0x", ""), + privateKeyAlice, + publicKeyAlice, + signatureAlice.replace("0x", ""), this.contractAddress, this.signers.alice.address, ); - expect(balanceAlice).to.equal(10000 - 1337); - }); + expect(balanceAlice).to.equal(1000); - it("should not transfer tokens between two users", async function () { - const transaction = await this.erc20.mint(1000); - await transaction.wait(); + // Reencrypt Bob's balance + const balanceHandleBob = await this.erc20.balanceOf(this.signers.bob); - const input = this.instances.alice.createEncryptedInput(this.contractAddress, this.signers.alice.address); - input.add64(1337); - const encryptedTransferAmount = input.encrypt(); - const tx = await this.erc20["transfer(address,bytes32,bytes)"]( + const { publicKey: publicKeyBob, privateKey: privateKeyBob } = this.instances.bob.generateKeypair(); + const eip712Bob = this.instances.bob.createEIP712(publicKeyBob, this.contractAddress); + const signatureBob = await this.signers.bob.signTypedData( + eip712Bob.domain, + { Reencrypt: eip712Bob.types.Reencrypt }, + eip712Bob.message, + ); + const balanceBob = await this.instances.bob.reencrypt( + balanceHandleBob, + privateKeyBob, + publicKeyBob, + signatureBob.replace("0x", ""), + this.contractAddress, this.signers.bob.address, - encryptedTransferAmount.handles[0], - encryptedTransferAmount.inputProof, ); - await tx.wait(); - // Decrypt Alice's balance - const balanceHandleAlice = await this.erc20.balanceOf(this.signers.alice); - const balanceAlice = await decrypt64(balanceHandleAlice); - expect(balanceAlice).to.equal(1000); - - // Decrypt Bob's balance - const balanceHandleBob = await this.erc20.balanceOf(this.signers.bob); - const balanceBob = await decrypt64(balanceHandleBob); expect(balanceBob).to.equal(0); }); @@ -146,12 +185,40 @@ describe("EncryptedERC20", function () { // Decrypt Alice's balance const balanceHandleAlice = await this.erc20.balanceOf(this.signers.alice); - const balanceAlice = await decrypt64(balanceHandleAlice); + const { publicKey: publicKeyAlice, privateKey: privateKeyAlice } = this.instances.alice.generateKeypair(); + const eip712 = this.instances.alice.createEIP712(publicKeyAlice, this.contractAddress); + const signatureAlice = await this.signers.alice.signTypedData( + eip712.domain, + { Reencrypt: eip712.types.Reencrypt }, + eip712.message, + ); + const balanceAlice = await this.instances.alice.reencrypt( + balanceHandleAlice, + privateKeyAlice, + publicKeyAlice, + signatureAlice.replace("0x", ""), + this.contractAddress, + this.signers.alice.address, + ); expect(balanceAlice).to.equal(10000); // check that transfer did not happen, as expected // Decrypt Bob's balance const balanceHandleBob = await this.erc20.balanceOf(this.signers.bob); - const balanceBob = await decrypt64(balanceHandleBob); + const { publicKey: publicKeyBob, privateKey: privateKeyBob } = this.instances.bob.generateKeypair(); + const eip712Bob = this.instances.bob.createEIP712(publicKeyBob, this.contractAddress); + const signatureBob = await this.signers.bob.signTypedData( + eip712Bob.domain, + { Reencrypt: eip712Bob.types.Reencrypt }, + eip712Bob.message, + ); + const balanceBob = await this.instances.bob.reencrypt( + balanceHandleBob, + privateKeyBob, + publicKeyBob, + signatureBob.replace("0x", ""), + this.contractAddress, + this.signers.bob.address, + ); expect(balanceBob).to.equal(0); // check that transfer did not happen, as expected const inputBob2 = this.instances.bob.createEncryptedInput(this.contractAddress, this.signers.bob.address); @@ -167,12 +234,26 @@ describe("EncryptedERC20", function () { // Decrypt Alice's balance const balanceHandleAlice2 = await this.erc20.balanceOf(this.signers.alice); - const balanceAlice2 = await decrypt64(balanceHandleAlice2); + const balanceAlice2 = await this.instances.alice.reencrypt( + balanceHandleAlice2, + privateKeyAlice, + publicKeyAlice, + signatureAlice.replace("0x", ""), + this.contractAddress, + this.signers.alice.address, + ); expect(balanceAlice2).to.equal(10000 - 1337); // check that transfer did happen this time // Decrypt Bob's balance const balanceHandleBob2 = await this.erc20.balanceOf(this.signers.bob); - const balanceBob2 = await decrypt64(balanceHandleBob2); - expect(balanceBob2).to.equal(1337); // check that transfer did happen this time + const balanceBob2 = await this.instances.bob.reencrypt( + balanceHandleBob2, + privateKeyBob, + publicKeyBob, + signatureBob.replace("0x", ""), + this.contractAddress, + this.signers.bob.address, + ); + expect(balanceBob2).to.equal(1337); // check that transfer did happen this time*/ }); }); diff --git a/test/instance.ts b/test/instance.ts index 4bcc9fb..ad52d2e 100644 --- a/test/instance.ts +++ b/test/instance.ts @@ -67,6 +67,15 @@ const getDecryptor = () => { return clientKeyDecryptor(clientKey); }; +/** + * @debug + * This function is intended for debugging purposes only. + * It cannot be used in production code, since it requires the FHE private key for decryption. + * In production, decryption is only possible via an asyncronous on-chain call to the Gateway. + * + * @param {bigint} a handle to decrypt + * @returns {bool} + */ export const decryptBool = async (handle: bigint): Promise => { if (network.name === "hardhat") { await awaitCoprocessor(); @@ -76,6 +85,15 @@ export const decryptBool = async (handle: bigint): Promise => { } }; +/** + * @debug + * This function is intended for debugging purposes only. + * It cannot be used in production code, since it requires the FHE private key for decryption. + * In production, decryption is only possible via an asyncronous on-chain call to the Gateway. + * + * @param {bigint} a handle to decrypt + * @returns {bigint} + */ export const decrypt4 = async (handle: bigint): Promise => { if (network.name === "hardhat") { await awaitCoprocessor(); @@ -85,6 +103,15 @@ export const decrypt4 = async (handle: bigint): Promise => { } }; +/** + * @debug + * This function is intended for debugging purposes only. + * It cannot be used in production code, since it requires the FHE private key for decryption. + * In production, decryption is only possible via an asyncronous on-chain call to the Gateway. + * + * @param {bigint} a handle to decrypt + * @returns {bigint} + */ export const decrypt8 = async (handle: bigint): Promise => { if (network.name === "hardhat") { await awaitCoprocessor(); @@ -94,6 +121,15 @@ export const decrypt8 = async (handle: bigint): Promise => { } }; +/** + * @debug + * This function is intended for debugging purposes only. + * It cannot be used in production code, since it requires the FHE private key for decryption. + * In production, decryption is only possible via an asyncronous on-chain call to the Gateway. + * + * @param {bigint} a handle to decrypt + * @returns {bigint} + */ export const decrypt16 = async (handle: bigint): Promise => { if (network.name === "hardhat") { await awaitCoprocessor(); @@ -103,6 +139,15 @@ export const decrypt16 = async (handle: bigint): Promise => { } }; +/** + * @debug + * This function is intended for debugging purposes only. + * It cannot be used in production code, since it requires the FHE private key for decryption. + * In production, decryption is only possible via an asyncronous on-chain call to the Gateway. + * + * @param {bigint} a handle to decrypt + * @returns {bigint} + */ export const decrypt32 = async (handle: bigint): Promise => { if (network.name === "hardhat") { await awaitCoprocessor(); @@ -112,6 +157,15 @@ export const decrypt32 = async (handle: bigint): Promise => { } }; +/** + * @debug + * This function is intended for debugging purposes only. + * It cannot be used in production code, since it requires the FHE private key for decryption. + * In production, decryption is only possible via an asyncronous on-chain call to the Gateway. + * + * @param {bigint} a handle to decrypt + * @returns {bigint} + */ export const decrypt64 = async (handle: bigint): Promise => { if (network.name === "hardhat") { await awaitCoprocessor(); @@ -121,6 +175,15 @@ export const decrypt64 = async (handle: bigint): Promise => { } }; +/** + * @debug + * This function is intended for debugging purposes only. + * It cannot be used in production code, since it requires the FHE private key for decryption. + * In production, decryption is only possible via an asyncronous on-chain call to the Gateway. + * + * @param {bigint} a handle to decrypt + * @returns {string} + */ export const decryptAddress = async (handle: bigint): Promise => { if (network.name === "hardhat") { await awaitCoprocessor();