Skip to content

Commit

Permalink
Merge pull request #102 from zama-ai/prettier-conf
Browse files Browse the repository at this point in the history
chore: update prettier conf and update code style
  • Loading branch information
immortal-tofu authored Jul 28, 2023
2 parents 593f538 + cdbf302 commit 1b6211b
Show file tree
Hide file tree
Showing 21 changed files with 1,268 additions and 3,839 deletions.
3 changes: 0 additions & 3 deletions .prettierrc

This file was deleted.

22 changes: 22 additions & 0 deletions .prettierrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"printWidth": 120,
"plugins": ["@trivago/prettier-plugin-sort-imports", "prettier-plugin-solidity"],
"overrides": [
{
"files": ["*.ts", ".js", ".cjs"],
"options": {
"singleQuote": true,
"importOrder": ["<THIRD_PARTY_MODULES>", "^[./]"],
"importOrderParserPlugins": ["typescript"],
"importOrderSeparation": true,
"importOrderSortSpecifiers": true
}
},
{
"files": "*.sol",
"options": {
"singleQuote": false
}
}
]
}
23 changes: 0 additions & 23 deletions .prettierrc.yml

This file was deleted.

19 changes: 0 additions & 19 deletions .solhint.json

This file was deleted.

16 changes: 3 additions & 13 deletions abstracts/EIP712WithModifier.sol
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,12 @@ import "@openzeppelin/contracts/utils/cryptography/ECDSA.sol";
import "@openzeppelin/contracts/utils/cryptography/EIP712.sol";

abstract contract EIP712WithModifier is EIP712 {
constructor(
string memory name,
string memory version
) EIP712(name, version) {}
constructor(string memory name, string memory version) EIP712(name, version) {}

modifier onlySignedPublicKey(bytes32 publicKey, bytes memory signature) {
bytes32 digest = _hashTypedDataV4(
keccak256(
abi.encode(keccak256("Reencrypt(bytes32 publicKey)"), publicKey)
)
);
bytes32 digest = _hashTypedDataV4(keccak256(abi.encode(keccak256("Reencrypt(bytes32 publicKey)"), publicKey)));
address signer = ECDSA.recover(digest, signature);
require(
signer == msg.sender,
"EIP712 signer and transaction signer do not match"
);
require(signer == msg.sender, "EIP712 signer and transaction signer do not match");
_;
}
}
38 changes: 7 additions & 31 deletions contracts/BlindAuction.sol
Original file line number Diff line number Diff line change
Expand Up @@ -85,23 +85,14 @@ contract BlindAuction is EIP712WithModifier {
if (!TFHE.isInitialized(highestBid)) {
highestBid = currentBid;
} else {
highestBid = TFHE.cmux(
TFHE.lt(highestBid, currentBid),
currentBid,
highestBid
);
highestBid = TFHE.cmux(TFHE.lt(highestBid, currentBid), currentBid, highestBid);
}
}

function getBid(
bytes32 publicKey,
bytes calldata signature
)
public
view
onlySignedPublicKey(publicKey, signature)
returns (bytes memory)
{
) public view onlySignedPublicKey(publicKey, signature) returns (bytes memory) {
return TFHE.reencrypt(bids[msg.sender], publicKey, 0);
}

Expand All @@ -116,22 +107,9 @@ contract BlindAuction is EIP712WithModifier {
function doIHaveHighestBid(
bytes32 publicKey,
bytes calldata signature
)
public
view
onlyAfterEnd
onlySignedPublicKey(publicKey, signature)
returns (bytes memory)
{
if (
TFHE.isInitialized(highestBid) &&
TFHE.isInitialized(bids[msg.sender])
) {
return
TFHE.reencrypt(
TFHE.le(highestBid, bids[msg.sender]),
publicKey
);
) public view onlyAfterEnd onlySignedPublicKey(publicKey, signature) returns (bytes memory) {
if (TFHE.isInitialized(highestBid) && TFHE.isInitialized(bids[msg.sender])) {
return TFHE.reencrypt(TFHE.le(highestBid, bids[msg.sender]), publicKey);
} else {
return TFHE.reencrypt(TFHE.asEuint32(0), publicKey);
}
Expand Down Expand Up @@ -166,14 +144,12 @@ contract BlindAuction is EIP712WithModifier {
}

modifier onlyBeforeEnd() {
if (block.timestamp >= endTime || manuallyStopped == true)
revert TooLate(endTime);
if (block.timestamp >= endTime || manuallyStopped == true) revert TooLate(endTime);
_;
}

modifier onlyAfterEnd() {
if (block.timestamp <= endTime && manuallyStopped == false)
revert TooEarly(endTime);
if (block.timestamp <= endTime && manuallyStopped == false) revert TooEarly(endTime);
_;
}

Expand Down
13 changes: 2 additions & 11 deletions contracts/CMUX.sol
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,7 @@ contract CMUX is EIP712WithModifier {
constructor() EIP712WithModifier("Authorization token", "1") {}

// Set result = if control { ifTrue } else { ifFalse }
function cmux(
bytes calldata controlBytes,
bytes calldata ifTrueBytes,
bytes calldata ifFalseBytes
) public {
function cmux(bytes calldata controlBytes, bytes calldata ifTrueBytes, bytes calldata ifFalseBytes) public {
ebool control = TFHE.asEbool(controlBytes);
euint8 ifTrue = TFHE.asEuint8(ifTrueBytes);
euint8 ifFalse = TFHE.asEuint8(ifFalseBytes);
Expand All @@ -26,12 +22,7 @@ contract CMUX is EIP712WithModifier {
function getResult(
bytes32 publicKey,
bytes calldata signature
)
public
view
onlySignedPublicKey(publicKey, signature)
returns (bytes memory)
{
) public view onlySignedPublicKey(publicKey, signature) returns (bytes memory) {
return TFHE.reencrypt(result, publicKey);
}
}
38 changes: 6 additions & 32 deletions contracts/EncryptedERC20.sol
Original file line number Diff line number Diff line change
Expand Up @@ -48,25 +48,15 @@ contract EncryptedERC20 is EIP712WithModifier {
function getTotalSupply(
bytes32 publicKey,
bytes calldata signature
)
public
view
onlySignedPublicKey(publicKey, signature)
returns (bytes memory)
{
) public view onlySignedPublicKey(publicKey, signature) returns (bytes memory) {
return TFHE.reencrypt(totalSupply, publicKey, 0);
}

// Returns the balance of the caller encrypted under the provided public key.
function balanceOf(
bytes32 publicKey,
bytes calldata signature
)
public
view
onlySignedPublicKey(publicKey, signature)
returns (bytes memory)
{
) public view onlySignedPublicKey(publicKey, signature) returns (bytes memory) {
return TFHE.reencrypt(balances[msg.sender], publicKey, 0);
}

Expand All @@ -82,23 +72,14 @@ contract EncryptedERC20 is EIP712WithModifier {
address spender,
bytes32 publicKey,
bytes calldata signature
)
public
view
onlySignedPublicKey(publicKey, signature)
returns (bytes memory)
{
) public view onlySignedPublicKey(publicKey, signature) returns (bytes memory) {
address owner = msg.sender;

return TFHE.reencrypt(_allowance(owner, spender), publicKey);
}

// Transfers `encryptedAmount` tokens using the caller's allowance.
function transferFrom(
address from,
address to,
bytes calldata encryptedAmount
) public {
function transferFrom(address from, address to, bytes calldata encryptedAmount) public {
transferFrom(from, to, TFHE.asEuint32(encryptedAmount));
}

Expand All @@ -113,22 +94,15 @@ contract EncryptedERC20 is EIP712WithModifier {
allowances[owner][spender] = amount;
}

function _allowance(
address owner,
address spender
) internal view returns (euint32) {
function _allowance(address owner, address spender) internal view returns (euint32) {
if (TFHE.isInitialized(allowances[owner][spender])) {
return allowances[owner][spender];
} else {
return TFHE.asEuint32(0);
}
}

function _updateAllowance(
address owner,
address spender,
euint32 amount
) internal {
function _updateAllowance(address owner, address spender, euint32 amount) internal {
euint32 currentAllowance = _allowance(owner, spender);
TFHE.req(TFHE.le(amount, currentAllowance));
_approve(owner, spender, TFHE.sub(currentAllowance, amount));
Expand Down
51 changes: 26 additions & 25 deletions hardhat.config.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
import "./tasks/accounts";
import "./tasks/mint";
import "./tasks/taskDeploy";
import "@nomicfoundation/hardhat-toolbox";
import { config as dotenvConfig } from "dotenv";
import "hardhat-deploy";
import type { HardhatUserConfig } from "hardhat/config";
import type { NetworkUserConfig } from "hardhat/types";
import { resolve } from "path";
import '@nomicfoundation/hardhat-toolbox';
import { config as dotenvConfig } from 'dotenv';
import 'hardhat-deploy';
import type { HardhatUserConfig } from 'hardhat/config';
import type { NetworkUserConfig } from 'hardhat/types';
import { resolve } from 'path';

const dotenvConfigPath: string = process.env.DOTENV_CONFIG_PATH || "./.env";
import './tasks/accounts';
import './tasks/mint';
import './tasks/taskDeploy';

const dotenvConfigPath: string = process.env.DOTENV_CONFIG_PATH || './.env';
dotenvConfig({ path: resolve(__dirname, dotenvConfigPath) });

// Ensure that we have all the environment variables we need.
const mnemonic: string | undefined = process.env.MNEMONIC;
if (!mnemonic) {
throw new Error("Please set your MNEMONIC in a .env file");
throw new Error('Please set your MNEMONIC in a .env file');
}

const chainIds = {
Expand All @@ -24,8 +25,8 @@ const chainIds = {
function getChainConfig(chain: keyof typeof chainIds): NetworkUserConfig {
let jsonRpcUrl: string;
switch (chain) {
case "zama":
jsonRpcUrl = "https://devnet.zama.ai";
case 'zama':
jsonRpcUrl = 'https://devnet.zama.ai';
break;
}
return {
Expand All @@ -40,32 +41,32 @@ function getChainConfig(chain: keyof typeof chainIds): NetworkUserConfig {
}

const config: HardhatUserConfig = {
defaultNetwork: "zama",
defaultNetwork: 'zama',
namedAccounts: {
deployer: 0,
},
gasReporter: {
currency: "USD",
currency: 'USD',
enabled: process.env.REPORT_GAS ? true : false,
excludeContracts: [],
src: "./contracts",
src: './contracts',
},
networks: {
zama: getChainConfig("zama"),
zama: getChainConfig('zama'),
},
paths: {
artifacts: "./artifacts",
cache: "./cache",
sources: "./contracts",
tests: "./test",
artifacts: './artifacts',
cache: './cache',
sources: './contracts',
tests: './test',
},
solidity: {
version: "0.8.17",
version: '0.8.17',
settings: {
metadata: {
// Not including the metadata hash
// https://github.com/paulrberg/hardhat-template/issues/31
bytecodeHash: "none",
bytecodeHash: 'none',
},
// Disable the optimizer when debugging
// https://hardhat.org/hardhat-network/#solidity-optimizer-support
Expand All @@ -76,8 +77,8 @@ const config: HardhatUserConfig = {
},
},
typechain: {
outDir: "types",
target: "ethers-v6",
outDir: 'types',
target: 'ethers-v6',
},
};

Expand Down
Loading

0 comments on commit 1b6211b

Please sign in to comment.