forked from pot4e/FHE-contract-example
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
23 changed files
with
512 additions
and
7,865 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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="" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
``` |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
})() |
Oops, something went wrong.