Skip to content

Commit

Permalink
Merge pull request #38 from zama-ai/fhevm-0-5-9
Browse files Browse the repository at this point in the history
fix: compatibility with modified .env
  • Loading branch information
jatZama authored Sep 26, 2024
2 parents 71432b5 + 8f252f6 commit 79f7d5c
Show file tree
Hide file tree
Showing 11 changed files with 129 additions and 48 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ abi/
# files
*.env
*.log
.env.docker
.DS_Store
.pnp.*
coverage.json
Expand Down
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,12 @@ generate-fhe-keys:

run-full:
$(MAKE) generate-fhe-keys
@docker compose -f docker-compose/docker-compose-full.yml up --detach
@docker compose --env-file .env.docker -f docker-compose/docker-compose-full.yml up --detach
@echo 'sleep a little to let the docker start up'
sleep 5

stop-full:
@docker compose -f docker-compose/docker-compose-full.yml down
@docker compose --env-file .env.docker -f docker-compose/docker-compose-full.yml down


clean:
Expand Down
6 changes: 3 additions & 3 deletions docker-compose/docker-compose-full.yml
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,12 @@ services:
- GATEWAY__ETHEREUM__WSS_URL=ws://fhevm-validator:8546
- GATEWAY__ETHEREUM__HTTP_URL=http://fhevm-validator:8545
- GATEWAY__ETHEREUM__FHE_LIB_ADDRESS=000000000000000000000000000000000000005d
- GATEWAY__ETHEREUM__ORACLE_PREDEPLOY_ADDRESS=c8c9303Cd7F337fab769686B593B87DC3403E0ce
- GATEWAY__ETHEREUM__ORACLE_PREDEPLOY_ADDRESS=${GATEWAY_CONTRACT_PREDEPLOY_ADDRESS}
- GATEWAY__KMS__ADDRESS=http://kms-validator:9090
- GATEWAY__KMS__KEY_ID=408d8cbaa51dece7f782fe04ba0b1c1d017b1088
- GATEWAY__STORAGE__URL=http://gateway-store:8088
- ASC_CONN__BLOCKCHAIN__ADDRESSES=http://kms-validator:9090
- GATEWAY__ETHEREUM__RELAYER_KEY=7ec931411ad75a7c201469a385d6f18a325d4923f9f213bd882bbea87e160b67
- GATEWAY__ETHEREUM__RELAYER_KEY=${PRIVATE_KEY_GATEWAY_RELAYER}
- RUST_BACKTRACE=1
depends_on:
fhevm-validator:
Expand All @@ -80,7 +80,7 @@ services:

fhevm-validator:
environment:
- TFHE_EXECUTOR_CONTRACT_ADDRESS=0x05fD9B5EFE0a996095f42Ed7e77c390810CF660c
- TFHE_EXECUTOR_CONTRACT_ADDRESS=${TFHE_EXECUTOR_CONTRACT_ADDRESS}
image: ghcr.io/zama-ai/ethermint-dev-node:v0.5.0-2
ports:
- "26656-26657:26656-26657"
Expand Down
11 changes: 6 additions & 5 deletions hardhat.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,12 @@ task("test", async (taskArgs, hre, runSuper) => {
// Run modified test task
if (hre.network.name === "hardhat") {
// in fhevm mode all this block is done when launching the node via `pnmp fhevm:start`
const privKeyDeployer = process.env.PRIVATE_KEY_GATEWAY_DEPLOYER;
await hre.run("task:computePredeployAddress", { privateKey: privKeyDeployer });
await hre.run("task:computeACLAddress");
await hre.run("task:computeTFHEExecutorAddress");
await hre.run("task:computeKMSVerifierAddress");

await hre.run("compile:specific", { contract: "contracts" });
const sourceDir = path.resolve(__dirname, "node_modules/fhevm/");
const destinationDir = path.resolve(__dirname, "fhevmTemp/");
Expand All @@ -103,11 +109,6 @@ task("test", async (taskArgs, hre, runSuper) => {
await hre.network.provider.send("hardhat_setCode", [targetAddress, bytecode]);
console.log(`Code of Mocked Pre-compile set at address: ${targetAddress}`);

const privKeyDeployer = process.env.PRIVATE_KEY_GATEWAY_DEPLOYER;
await hre.run("task:computePredeployAddress", { privateKey: privKeyDeployer });
await hre.run("task:computeACLAddress");
await hre.run("task:computeTFHEExecutorAddress");
await hre.run("task:computeKMSVerifierAddress");
await hre.run("task:deployACL");
await hre.run("task:deployTFHEExecutor");
await hre.run("task:deployKMSVerifier");
Expand Down
21 changes: 8 additions & 13 deletions launch-fhevm.sh
Original file line number Diff line number Diff line change
@@ -1,28 +1,23 @@
#!/bin/bash

# Assumes the following:
# 1. A local and **fresh** fhEVM node is already running.
# 2. All test addresses are funded (e.g. via the fund_test_addresses.sh script).
npx hardhat clean
# 1. Predeploys addresses have been precomputed via the precomputeAddresses.sh script.
# 2. A local and **fresh** fhEVM node is already running.
# 3. All test addresses are funded (e.g. via the fund_test_addresses.sh script).

npx hardhat compile:specific --contract contracts

mkdir -p fhevmTemp
cp -L -r node_modules/fhevm fhevmTemp/
npx hardhat compile:specific --contract fhevmTemp/fhevm/lib
npx hardhat compile:specific --contract fhevmTemp/fhevm/gateway
mkdir -p abi
cp artifacts/fhevmTemp/fhevm/lib/TFHEExecutor.sol/TFHEExecutor.json abi/TFHEExecutor.json

PRIVATE_KEY_GATEWAY_DEPLOYER=$(grep PRIVATE_KEY_GATEWAY_DEPLOYER .env | cut -d '"' -f 2)
npx hardhat task:computePredeployAddress --private-key "$PRIVATE_KEY_GATEWAY_DEPLOYER"

npx hardhat compile:specific --contract contracts

npx hardhat task:computeACLAddress
npx hardhat task:computeTFHEExecutorAddress
npx hardhat task:computeKMSVerifierAddress
npx hardhat task:deployACL
npx hardhat task:deployTFHEExecutor
npx hardhat task:deployKMSVerifier

rm -rf fhevmTemp
npx hardhat task:launchFhevm --skip-get-coin true

npx hardhat task:launchFhevm --skip-get-coin true
rm -rf fhevmTemp
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
"eslint": "^9.9.0",
"eslint-config-prettier": "^8.5.0",
"ethers": "^6.8.0",
"fhevm": "^0.5.8",
"fhevm": "^0.5.9",
"fhevmjs": "^0.5.2",
"fs-extra": "^10.1.0",
"globals": "^15.9.0",
Expand Down Expand Up @@ -91,7 +91,7 @@
"task:getEthereumAddress": "hardhat task:getEthereumAddress",
"task:deployERC20": "hardhat task:deployERC20",
"task:accounts": "hardhat task:accounts",
"fhevm:start": "make run-full && ./scripts/fund_test_addresses_docker.sh && ./launch-fhevm.sh",
"fhevm:start": "./precomputeAddresses.sh && make run-full && ./scripts/fund_test_addresses_docker.sh && ./launch-fhevm.sh",
"fhevm:stop": "make clean",
"fhevm:restart": "fhevm:stop && fhevm:start",
"fhevm:faucet": "npm run fhevm:faucet:alice && sleep 5 && npm run fhevm:faucet:bob && sleep 5 && npm run fhevm:faucet:carol && sleep 5 && npm run fhevm:faucet:dave && sleep 5 && npm run fhevm:faucet:eve",
Expand Down
10 changes: 5 additions & 5 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

39 changes: 39 additions & 0 deletions precomputeAddresses.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#!/bin/bash

npx hardhat clean
PRIVATE_KEY_GATEWAY_DEPLOYER=$(grep PRIVATE_KEY_GATEWAY_DEPLOYER .env | cut -d '"' -f 2)
npx hardhat task:computeACLAddress --network hardhat
npx hardhat task:computeTFHEExecutorAddress --network hardhat
npx hardhat task:computeKMSVerifierAddress --network hardhat
npx hardhat task:computePredeployAddress --private-key "$PRIVATE_KEY_GATEWAY_DEPLOYER" --network hardhat

# Paths to input files
ENV_EXEC="node_modules/fhevm/lib/.env.exec"
ENV_GATEWAY="node_modules/fhevm/gateway/.env.gateway"
ENV_FILE=".env"

# Path to output file
ENV_DOCKER=".env.docker"

# Check if input files exist
for file in "$ENV_EXEC" "$ENV_GATEWAY" "$ENV_FILE"; do
if [ ! -f "$file" ]; then
echo "Error: $file does not exist."
exit 1
fi
done

# Extract values
TFHE_EXECUTOR_CONTRACT_ADDRESS=$(grep "^TFHE_EXECUTOR_CONTRACT_ADDRESS=" "$ENV_EXEC" | cut -d'=' -f2)
GATEWAY_CONTRACT_PREDEPLOY_ADDRESS=$(grep "^GATEWAY_CONTRACT_PREDEPLOY_ADDRESS=" "$ENV_GATEWAY" | cut -d'=' -f2)
GATEWAY_CONTRACT_PREDEPLOY_ADDRESS_NO0X=${GATEWAY_CONTRACT_PREDEPLOY_ADDRESS#0x}
PRIVATE_KEY_GATEWAY_RELAYER=$(grep PRIVATE_KEY_GATEWAY_RELAYER .env | cut -d '"' -f 2)

# Write to .env.docker
cat << EOF > "$ENV_DOCKER"
TFHE_EXECUTOR_CONTRACT_ADDRESS=$TFHE_EXECUTOR_CONTRACT_ADDRESS
PRIVATE_KEY_GATEWAY_RELAYER=$PRIVATE_KEY_GATEWAY_RELAYER
GATEWAY_CONTRACT_PREDEPLOY_ADDRESS=$GATEWAY_CONTRACT_PREDEPLOY_ADDRESS_NO0X
EOF

echo "Successfully created $ENV_DOCKER with the aggregated environment variables."
71 changes: 58 additions & 13 deletions scripts/fund_test_addresses_docker.sh
Original file line number Diff line number Diff line change
@@ -1,15 +1,60 @@
#!/usr/bin/env bash

docker exec -i zama-dev-fhevm-validator-1 faucet a5e1defb98efe38ebb2d958cee052410247f4c80
sleep 8
docker exec -i zama-dev-fhevm-validator-1 faucet fCefe53c7012a075b8a711df391100d9c431c468
sleep 8
docker exec -i zama-dev-fhevm-validator-1 faucet a44366bAA26296c1409AD1e284264212029F02f1
sleep 8
docker exec -i zama-dev-fhevm-validator-1 faucet c1d91b49A1B3D1324E93F86778C44a03f1063f1b
sleep 8
docker exec -i zama-dev-fhevm-validator-1 faucet 305F1F471e9baCFF2b3549F9601f9A4BEafc94e1
sleep 8
docker exec -i zama-dev-fhevm-validator-1 faucet c45994e4098271c3140117ebD5c74C70dd56D9cd
sleep 8
docker exec -i zama-dev-fhevm-validator-1 faucet 97F272ccfef4026A1F3f0e0E879d514627B84E69
# Read MNEMONIC from .env file, remove 'export' and quotes
MNEMONIC=$(grep MNEMONIC .env | cut -d '"' -f 2)
PRIVATE_KEY_GATEWAY_DEPLOYER=$(grep PRIVATE_KEY_GATEWAY_DEPLOYER .env | cut -d '"' -f 2)
PRIVATE_KEY_GATEWAY_OWNER=$(grep PRIVATE_KEY_GATEWAY_DEPLOYER .env | cut -d '"' -f 2)
PRIVATE_KEY_GATEWAY_RELAYER=$(grep PRIVATE_KEY_GATEWAY_DEPLOYER .env | cut -d '"' -f 2)

# Verify that global envs are set
if [ -z "$MNEMONIC" ]; then
echo "Error: MNEMONIC is not set."
exit 1
fi
if [ -z "$PRIVATE_KEY_GATEWAY_DEPLOYER" ]; then
echo "Error: PRIVATE_KEY_GATEWAY_DEPLOYER is not set."
exit 1
fi
if [ -z "$PRIVATE_KEY_GATEWAY_OWNER" ]; then
echo "Error: PRIVATE_KEY_GATEWAY_OWNER is not set."
exit 1
fi
if [ -z "$PRIVATE_KEY_GATEWAY_RELAYER" ]; then
echo "Error: PRIVATE_KEY_GATEWAY_RELAYER is not set."
exit 1
fi

# Compute addresses using ethers.js v6 - signers[0] to signers[4] are Alice, Bob, Carol, David and Eve. signers[9] is the fhevm deployer.
addresses=$(node -e "
const { ethers } = require('ethers');
const derivationPath = \"m/44'/60'/0'/0\";
const mnemonicInstance = ethers.Mnemonic.fromPhrase('$MNEMONIC');
const hdNode = ethers.HDNodeWallet.fromMnemonic(mnemonicInstance, derivationPath);
const indices = [0, 1, 2, 3, 4, 9];
for (const i of indices) {
const childNode = hdNode.derivePath(\`\${i}\`);
console.log(childNode.address);
}
const deployerAddress = new ethers.Wallet('$PRIVATE_KEY_GATEWAY_DEPLOYER').address;
console.log(deployerAddress);
const ownerAddress = new ethers.Wallet('$PRIVATE_KEY_GATEWAY_OWNER').address;
console.log(ownerAddress);
const relayerAddress = new ethers.Wallet('$PRIVATE_KEY_GATEWAY_RELAYER').address;
console.log(relayerAddress);
" 2>/dev/null)

# Check if addresses were generated successfully
if [ -z "$addresses" ]; then
echo "Error: Failed to generate addresses."
exit 1
fi

# Convert the addresses string into an array
IFS=$'\n' read -rd '' -a addressArray <<<"$addresses"

# Loop through each address, strip '0x', and run the Docker command
for addr in "${addressArray[@]}"; do
addr_no0x=${addr#0x}
docker exec -i zama-dev-fhevm-validator-1 faucet "$addr_no0x"
sleep 8
done
8 changes: 4 additions & 4 deletions tasks/taskTFHE.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,15 +66,15 @@ address constant fhevmCoprocessorAdd = ${execAddress};\n`;

task("task:computeKMSVerifierAddress").setAction(async function (taskArguments: TaskArguments, { ethers }) {
const deployer = (await ethers.getSigners())[9].address;
const kmsVerfierAddress = ethers.getCreateAddress({
const kmsVerifierAddress = ethers.getCreateAddress({
from: deployer,
nonce: 2, // using nonce of 2 for the Kms Verifier contract
});
const envFilePath = path.join(__dirname, "../node_modules/fhevm/lib/.env.kmsverifier");
const content = `KMS_VERIFIER_CONTRACT_ADDRESS=${kmsVerfierAddress}\n`;
const content = `KMS_VERIFIER_CONTRACT_ADDRESS=${kmsVerifierAddress}\n`;
try {
fs.writeFileSync(envFilePath, content, { flag: "w" });
console.log(`KMS Verifier address ${kmsVerfierAddress} written successfully!`);
console.log(`KMS Verifier address ${kmsVerifierAddress} written successfully!`);
} catch (err) {
console.error("Failed to write KMS Verifier address:", err);
}
Expand All @@ -83,7 +83,7 @@ task("task:computeKMSVerifierAddress").setAction(async function (taskArguments:
pragma solidity ^0.8.24;
address constant KMS_VERIFIER_CONTRACT_ADDRESS = ${kmsVerfierAddress};\n`;
address constant KMS_VERIFIER_CONTRACT_ADDRESS = ${kmsVerifierAddress};\n`;

try {
fs.writeFileSync("node_modules/fhevm/lib/KMSVerifierAddress.sol", solidityTemplate, {
Expand Down
2 changes: 1 addition & 1 deletion test/encryptedERC20/EncryptedERC20.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ describe("EncryptedERC20", function () {
this.instances = await createInstances(this.signers);
});

it("should mint the contract", async function () {
it.only("should mint the contract", async function () {
const transaction = await this.erc20.mint(1000);
await transaction.wait();

Expand Down

0 comments on commit 79f7d5c

Please sign in to comment.