Skip to content

Commit

Permalink
update fhe function
Browse files Browse the repository at this point in the history
  • Loading branch information
lyhv committed May 22, 2024
1 parent 343b0c9 commit 49c3984
Show file tree
Hide file tree
Showing 23 changed files with 512 additions and 7,865 deletions.
5 changes: 5 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Make sure to rename this file to .env before adding your private keys!!!
PRIVATE_KEY_1=''
PRIVATE_KEY_2=''
# Add more if your project requires more private keys
INFURA_API_KEY=""
34 changes: 19 additions & 15 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
# directories
.coverage_artifacts
.coverage_cache
.coverage_contracts
artifacts
build
cache
coverage
dist
node_modules
.env
types

# Hardhat files
/cache
/artifacts

# TypeChain files
/typechain
/typechain-types

# solidity-coverage files
/coverage
/coverage.json

# Hardhat Ignition default folder for deployments against a local node
ignition/deployments/chain-31337
# files
*.env
*.log
.DS_Store
.pnp.*
coverage.json
package-lock.json
pnpm-lock.yaml
yarn.lock
2 changes: 1 addition & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"solidity.compileUsingRemoteVersion": "v0.8.24+commit.e11b9ed9"
"solidity.compileUsingRemoteVersion": "v0.8.22+commit.4fc1097e"
}
51 changes: 50 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,50 @@
# FHE-contract-example
# FHE-contract-example
### Pre Requisites

Install [pnpm](https://pnpm.io/installation)

Before being able to run any command, you need to create a `.env` file and set a BIP-39 compatible mnemonic as an
environment variable. If you don't already have a mnemonic, you can use this [website](https://iancoleman.io/bip39/) to
generate one. You can run the following command to use the example .env:

```sh
cp .env.example .env
```
Then, proceed with installing dependencies:

```sh
pnpm install
```

### Compile

Compile the smart contracts with Hardhat:

```sh
npx hardhat compile --network inco
```

### TypeChain

Compile the smart contracts and generate TypeChain bindings:

```sh
pnpm typechain
```
(For more control over the deployment process, you can rewrite the deployment script (deploy.ts) and use the command
`npx hardhat run scripts/deploy.ts --network inco` to deploy your contracts.)
### Test

Run the tests with Hardhat:

```sh
npx hardhat test --network inco
```

### Deploy

Deploy the ERC20 to Inco Gentry Testnet Network:

```sh
npx hardhat run deploy/BlindAuction.ts --network inco
```
82 changes: 0 additions & 82 deletions contracts/Bidding.sol

This file was deleted.

130 changes: 130 additions & 0 deletions contracts/BlindAuction.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.20;
import "@openzeppelin/contracts/token/ERC721/IERC721.sol";
import "fhevm/lib/TFHE.sol";
import "fhevm/abstracts/EIP712WithModifier.sol";

contract BlindAuction is EIP712WithModifier {
// Contract owner
address public owner;

struct BidData {
uint256 amount;
uint256 timstamp;
}
// NFT tokenIds for Bid
euint32[] public tokenIds;
// nftId => (bidder => amount)
mapping(euint32 => mapping(address => BidData)) public bidsByTokenId;
// nfId => highestBidder
mapping(euint32 => address) public highestBidder;
// nftId => amount
mapping(euint32 => uint256) public highestBid;

uint public endTime;

bool public manuallyStopped = false;

// The function has been called too early.
// Try again at `time`.
error TooEarly(uint time);
// The function has been called too late.
// It cannot be called after `time`.
error TooLate(uint time);

event Winner(address who);

constructor(
uint[] memory _tokenIds,
uint _biddingTime
) EIP712WithModifier("Authorization token", "1") {
tokenIds = new euint32[](_tokenIds.length);
for (uint i = 0; i < _tokenIds.length; i++) {
tokenIds[i] = TFHE.asEuint32(_tokenIds[i]);
}
owner = msg.sender;
endTime = block.timestamp + _biddingTime;
}

modifier onlyOwner() {
require(msg.sender == owner, "Only owner can call this function");
_;
}
modifier onlyBeforeEnd() {
if (block.timestamp >= endTime || manuallyStopped == true)
revert TooLate(endTime);
_;
}

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

function bid(
bytes calldata encryptedTokenId
) external payable onlyBeforeEnd {
require(msg.value != 0, "Bid amount must be greater than 0");
euint32 tokenId = TFHE.asEuint32(encryptedTokenId);
require(TFHE.decrypt(checkValidTokenId(tokenId)), "Invalid token id");
BidData memory existingBid = bidsByTokenId[tokenId][msg.sender];
// Add bid
existingBid.amount = existingBid.amount + msg.value;
existingBid.timstamp = block.timestamp;
bidsByTokenId[tokenId][msg.sender] = existingBid;
// Update highest bidder
if (highestBid[tokenId] < bidsByTokenId[tokenId][msg.sender].amount) {
highestBid[tokenId] = bidsByTokenId[tokenId][msg.sender].amount;
highestBidder[tokenId] = msg.sender;
}
// Mint proofNFT.mintProof(msg.sender, tokenId, msg.value);
}

function stopBidding() external onlyOwner onlyBeforeEnd {
manuallyStopped = true;
}

function checkValidTokenId(euint32 tokenId) private view returns (ebool) {
ebool isValidTokenId = TFHE.asEbool(false);
for (uint i = 0; i < tokenIds.length; i++) {
if (TFHE.decrypt(TFHE.eq(tokenIds[i], tokenId))) {
isValidTokenId = TFHE.asEbool(true);
break;
}
}
return isValidTokenId;
}

function getBid(
bytes calldata encryptedTokenId,
bytes32 publicKey,
bytes calldata signature
)
public
view
onlySignedPublicKey(publicKey, signature)
returns (BidData memory)
{
euint32 tokenId = TFHE.asEuint32(encryptedTokenId);
return bidsByTokenId[tokenId][msg.sender];
}

function getBids(
bytes32 publicKey,
bytes calldata signature
)
public
view
onlySignedPublicKey(publicKey, signature)
returns (bytes[] memory, BidData[] memory)
{
bytes[] memory _tokenIds = new bytes[](tokenIds.length);
BidData[] memory bids = new BidData[](tokenIds.length);
for (uint i = 0; i < tokenIds.length; i++) {
_tokenIds[i] = TFHE.reencrypt(tokenIds[i], publicKey, 0);
bids[i] = bidsByTokenId[tokenIds[i]][msg.sender];
}
return (_tokenIds, bids);
}
}
34 changes: 0 additions & 34 deletions contracts/Lock.sol

This file was deleted.

2 changes: 1 addition & 1 deletion contracts/ProofOfBidERC721.sol
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.24;
pragma solidity ^0.8.20;

// Uncomment this line to use console.log
// import "hardhat/console.sol";
Expand Down
11 changes: 11 additions & 0 deletions deploy/BlindAuction.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import hre, { ethers } from "hardhat";
import { TOKEN_IDS, BLIND_TIME } from "../test/config";

(async () => {
const [owner] = await hre.ethers.getSigners();

const contractFactory = await ethers.getContractFactory("BlindAuction");
const contract = await contractFactory.connect(owner).deploy(TOKEN_IDS, BLIND_TIME);
await contract.waitForDeployment();
console.log("BlindAuction deployed to: ", await contract.getAddress());
})()
Loading

0 comments on commit 49c3984

Please sign in to comment.