diff --git a/test/confidentialERC20/ConfidentialERC20.ts b/test/confidentialERC20/ConfidentialERC20.ts index e892ea9..151d341 100644 --- a/test/confidentialERC20/ConfidentialERC20.ts +++ b/test/confidentialERC20/ConfidentialERC20.ts @@ -27,9 +27,8 @@ describe("ConfidentialERC20", function () { // Reencrypt Alice's balance const balanceHandleAlice = await this.erc20.balanceOf(this.signers.alice); const balanceAlice = await reencryptEuint64( - this.signers, + this.signers.alice, this.fhevm, - "alice", balanceHandleAlice, this.contractAddress, ); @@ -59,9 +58,8 @@ describe("ConfidentialERC20", function () { // Reencrypt Alice's balance const balanceHandleAlice = await this.erc20.balanceOf(this.signers.alice); const balanceAlice = await reencryptEuint64( - this.signers, + this.signers.alice, this.fhevm, - "alice", balanceHandleAlice, this.contractAddress, ); @@ -69,17 +67,17 @@ describe("ConfidentialERC20", function () { // Reencrypt Bob's balance const balanceHandleBob = await this.erc20.balanceOf(this.signers.bob); - const balanceBob = await reencryptEuint64(this.signers, this.fhevm, "bob", balanceHandleBob, this.contractAddress); + const balanceBob = await reencryptEuint64(this.signers.bob, this.fhevm, balanceHandleBob, this.contractAddress); expect(balanceBob).to.equal(1337); // on the other hand, Bob should be unable to read Alice's balance await expect( - reencryptEuint64(this.signers, this.fhevm, "bob", balanceHandleAlice, this.contractAddress), + reencryptEuint64(this.signers.bob, this.fhevm, balanceHandleAlice, this.contractAddress), ).to.be.rejectedWith("User is not authorized to reencrypt this handle!"); // and should be impossible to call reencrypt if contractAddress === userAddress await expect( - reencryptEuint64(this.signers, this.fhevm, "alice", balanceHandleAlice, this.signers.alice.address), + reencryptEuint64(this.signers.alice, this.fhevm, balanceHandleAlice, this.signers.alice.address), ).to.be.rejectedWith("userAddress should not be equal to contractAddress when requesting reencryption!"); }); @@ -99,9 +97,8 @@ describe("ConfidentialERC20", function () { const balanceHandleAlice = await this.erc20.balanceOf(this.signers.alice); const balanceAlice = await reencryptEuint64( - this.signers, + this.signers.alice, this.fhevm, - "alice", balanceHandleAlice, this.contractAddress, ); @@ -109,7 +106,7 @@ describe("ConfidentialERC20", function () { // Reencrypt Bob's balance const balanceHandleBob = await this.erc20.balanceOf(this.signers.bob); - const balanceBob = await reencryptEuint64(this.signers, this.fhevm, "bob", balanceHandleBob, this.contractAddress); + const balanceBob = await reencryptEuint64(this.signers.bob, this.fhevm, balanceHandleBob, this.contractAddress); expect(balanceBob).to.equal(0); }); @@ -142,9 +139,8 @@ describe("ConfidentialERC20", function () { // Decrypt Alice's balance const balanceHandleAlice = await this.erc20.balanceOf(this.signers.alice); const balanceAlice = await reencryptEuint64( - this.signers, + this.signers.alice, this.fhevm, - "alice", balanceHandleAlice, this.contractAddress, ); @@ -152,7 +148,7 @@ describe("ConfidentialERC20", function () { // Decrypt Bob's balance const balanceHandleBob = await this.erc20.balanceOf(this.signers.bob); - const balanceBob = await reencryptEuint64(this.signers, this.fhevm, "bob", balanceHandleBob, this.contractAddress); + const balanceBob = await reencryptEuint64(this.signers.bob, this.fhevm, balanceHandleBob, this.contractAddress); expect(balanceBob).to.equal(0); // check that transfer did not happen, as expected const inputBob2 = this.fhevm.createEncryptedInput(this.contractAddress, this.signers.bob.address); @@ -169,9 +165,8 @@ describe("ConfidentialERC20", function () { // Decrypt Alice's balance const balanceHandleAlice2 = await this.erc20.balanceOf(this.signers.alice); const balanceAlice2 = await reencryptEuint64( - this.signers, + this.signers.alice, this.fhevm, - "alice", balanceHandleAlice2, this.contractAddress, ); @@ -180,9 +175,8 @@ describe("ConfidentialERC20", function () { // Decrypt Bob's balance const balanceHandleBob2 = await this.erc20.balanceOf(this.signers.bob); const balanceBob2 = await reencryptEuint64( - this.signers, + this.signers.bob, this.fhevm, - "bob", balanceHandleBob2, this.contractAddress, ); diff --git a/test/reencrypt.ts b/test/reencrypt.ts index 6b74b9e..6b7b0d0 100644 --- a/test/reencrypt.ts +++ b/test/reencrypt.ts @@ -1,11 +1,6 @@ +import { Signer } from "ethers"; import { FhevmInstance } from "fhevmjs/node"; -import { ACCOUNT_NAMES } from "./constants"; -import { Signers } from "./signers"; - -// Add type definition at the top of the file -type AccountName = (typeof ACCOUNT_NAMES)[number]; - const EBOOL_T = 0; const EUINT4_T = 1; const EUINT8_T = 2; @@ -36,137 +31,125 @@ export function verifyType(handle: bigint, expectedType: number) { } export async function reencryptEbool( - signers: Signers, - instances: FhevmInstance, - user: AccountName, + signer: Signer, + instance: FhevmInstance, handle: bigint, contractAddress: string, ): Promise { verifyType(handle, EBOOL_T); - return (await reencryptHandle(signers, instances, user, handle, contractAddress)) === 1n; + return (await reencryptHandle(signer, instance, handle, contractAddress)) === 1n; } export async function reencryptEuint4( - signers: Signers, - instances: FhevmInstance, - user: AccountName, + signer: Signer, + instance: FhevmInstance, handle: bigint, contractAddress: string, ): Promise { verifyType(handle, EUINT4_T); - return reencryptHandle(signers, instances, user, handle, contractAddress); + return reencryptHandle(signer, instance, handle, contractAddress); } export async function reencryptEuint8( - signers: Signers, - instances: FhevmInstance, - user: AccountName, + signer: Signer, + instance: FhevmInstance, handle: bigint, contractAddress: string, ): Promise { verifyType(handle, EUINT8_T); - return reencryptHandle(signers, instances, user, handle, contractAddress); + return reencryptHandle(signer, instance, handle, contractAddress); } export async function reencryptEuint16( - signers: Signers, - instances: FhevmInstance, - user: AccountName, + signer: Signer, + instance: FhevmInstance, handle: bigint, contractAddress: string, ): Promise { verifyType(handle, EUINT16_T); - return reencryptHandle(signers, instances, user, handle, contractAddress); + return reencryptHandle(signer, instance, handle, contractAddress); } export async function reencryptEuint32( - signers: Signers, - instances: FhevmInstance, - user: AccountName, + signer: Signer, + instance: FhevmInstance, handle: bigint, contractAddress: string, ): Promise { verifyType(handle, EUINT32_T); - return reencryptHandle(signers, instances, user, handle, contractAddress); + return reencryptHandle(signer, instance, handle, contractAddress); } export async function reencryptEuint64( - signers: Signers, - instances: FhevmInstance, - user: AccountName, + signer: Signer, + instance: FhevmInstance, handle: bigint, contractAddress: string, ): Promise { verifyType(handle, EUINT64_T); - return reencryptHandle(signers, instances, user, handle, contractAddress); + return reencryptHandle(signer, instance, handle, contractAddress); } export async function reencryptEuint128( - signers: Signers, - instances: FhevmInstance, - user: AccountName, + signer: Signer, + instance: FhevmInstance, handle: bigint, contractAddress: string, ): Promise { verifyType(handle, EUINT128_T); - return reencryptHandle(signers, instances, user, handle, contractAddress); + return reencryptHandle(signer, instance, handle, contractAddress); } export async function reencryptEaddress( - signers: Signers, - instances: FhevmInstance, - user: AccountName, + signer: Signer, + instance: FhevmInstance, handle: bigint, contractAddress: string, ): Promise { verifyType(handle, EUINT160_T); - const addressAsUint160: bigint = await reencryptHandle(signers, instances, user, handle, contractAddress); + const addressAsUint160: bigint = await reencryptHandle(signer, instance, handle, contractAddress); const handleStr = "0x" + addressAsUint160.toString(16).padStart(40, "0"); return handleStr; } export async function reencryptEuint256( - signers: Signers, - instances: FhevmInstance, - user: AccountName, + signer: Signer, + instance: FhevmInstance, handle: bigint, contractAddress: string, ): Promise { verifyType(handle, EUINT256_T); - return reencryptHandle(signers, instances, user, handle, contractAddress); + return reencryptHandle(signer, instance, handle, contractAddress); } export async function reencryptEbytes64( - signers: Signers, - instances: FhevmInstance, - user: AccountName, + signer: Signer, + instance: FhevmInstance, handle: bigint, contractAddress: string, ): Promise { verifyType(handle, EBYTES64_T); - return reencryptHandle(signers, instances, user, handle, contractAddress); + return reencryptHandle(signer, instance, handle, contractAddress); } export async function reencryptEbytes128( - signers: Signers, - instances: FhevmInstance, - user: AccountName, + signer: Signer, + instance: FhevmInstance, handle: bigint, contractAddress: string, ): Promise { verifyType(handle, EBYTES128_T); - return reencryptHandle(signers, instances, user, handle, contractAddress); + return reencryptHandle(signer, instance, handle, contractAddress); } export async function reencryptEbytes256( - signers: Signers, - instances: FhevmInstance, - user: AccountName, + signer: Signer, + instance: FhevmInstance, handle: bigint, contractAddress: string, ): Promise { verifyType(handle, EBYTES256_T); - return reencryptHandle(signers, instances, user, handle, contractAddress); + return reencryptHandle(signer, instance, handle, contractAddress); } /** @@ -174,19 +157,14 @@ export async function reencryptEbytes256( * It does not verify types. */ async function reencryptHandle( - signers: Signers, + signer: Signer, instance: FhevmInstance, - user: AccountName, handle: bigint, contractAddress: string, ): Promise { const { publicKey: publicKey, privateKey: privateKey } = instance.generateKeypair(); const eip712 = instance.createEIP712(publicKey, contractAddress); - const signature = await signers[user as keyof Signers].signTypedData( - eip712.domain, - { Reencrypt: eip712.types.Reencrypt }, - eip712.message, - ); + const signature = await signer.signTypedData(eip712.domain, { Reencrypt: eip712.types.Reencrypt }, eip712.message); const reencryptedHandle = await instance.reencrypt( handle, @@ -194,7 +172,7 @@ async function reencryptHandle( publicKey, signature.replace("0x", ""), contractAddress, - signers[user as keyof Signers].address, + await signer.getAddress(), ); return reencryptedHandle; diff --git a/test/signers.ts b/test/signers.ts index fb98a77..5fe72ca 100644 --- a/test/signers.ts +++ b/test/signers.ts @@ -9,7 +9,7 @@ export interface Signers { [K in AccountNames]: HardhatEthersSigner; } -let signers: Signers = {} as Signers; +const signers: Signers = {} as Signers; export const initSigners = async (): Promise => { if (Object.entries(signers).length === 0) {