Skip to content

Commit

Permalink
fix: remove debugging with fhe privKey from tests
Browse files Browse the repository at this point in the history
chore: fixed typo in jsdoc
  • Loading branch information
jatZama committed Aug 30, 2024
1 parent 9aabf5e commit b8ecc8b
Show file tree
Hide file tree
Showing 3 changed files with 192 additions and 46 deletions.
2 changes: 2 additions & 0 deletions test/encryptedERC20/EncryptedERC20.fixture.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { ethers } from "hardhat";

import type { EncryptedERC20 } from "../../types";
import { getSigners } from "../signers";

Expand Down
173 changes: 127 additions & 46 deletions test/encryptedERC20/EncryptedERC20.ts
Original file line number Diff line number Diff line change
@@ -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";

Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
});

Expand Down Expand Up @@ -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);
Expand All @@ -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*/
});
});
63 changes: 63 additions & 0 deletions test/instance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<boolean> => {
if (network.name === "hardhat") {
await awaitCoprocessor();
Expand All @@ -76,6 +85,15 @@ export const decryptBool = async (handle: bigint): Promise<boolean> => {
}
};

/**
* @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<bigint> => {
if (network.name === "hardhat") {
await awaitCoprocessor();
Expand All @@ -85,6 +103,15 @@ export const decrypt4 = async (handle: bigint): Promise<bigint> => {
}
};

/**
* @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<bigint> => {
if (network.name === "hardhat") {
await awaitCoprocessor();
Expand All @@ -94,6 +121,15 @@ export const decrypt8 = async (handle: bigint): Promise<bigint> => {
}
};

/**
* @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<bigint> => {
if (network.name === "hardhat") {
await awaitCoprocessor();
Expand All @@ -103,6 +139,15 @@ export const decrypt16 = async (handle: bigint): Promise<bigint> => {
}
};

/**
* @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<bigint> => {
if (network.name === "hardhat") {
await awaitCoprocessor();
Expand All @@ -112,6 +157,15 @@ export const decrypt32 = async (handle: bigint): Promise<bigint> => {
}
};

/**
* @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<bigint> => {
if (network.name === "hardhat") {
await awaitCoprocessor();
Expand All @@ -121,6 +175,15 @@ export const decrypt64 = async (handle: bigint): Promise<bigint> => {
}
};

/**
* @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<string> => {
if (network.name === "hardhat") {
await awaitCoprocessor();
Expand Down

0 comments on commit b8ecc8b

Please sign in to comment.