From 48ff3fa48a04eeb872ff8ead77b893020b20303a Mon Sep 17 00:00:00 2001 From: Jobin Babu Ayathil Date: Thu, 5 Sep 2024 02:26:42 +0530 Subject: [PATCH 1/8] Create SealedBidAuctions.md --- docs/tutorials-examples/SealedBidAuctions.md | 112 +++++++++++++++++++ 1 file changed, 112 insertions(+) create mode 100644 docs/tutorials-examples/SealedBidAuctions.md diff --git a/docs/tutorials-examples/SealedBidAuctions.md b/docs/tutorials-examples/SealedBidAuctions.md new file mode 100644 index 00000000..9ed74bd0 --- /dev/null +++ b/docs/tutorials-examples/SealedBidAuctions.md @@ -0,0 +1,112 @@ +--- +sidebar_position: 2 +--- +# Sealed Bid Auction + +Sealed Bid Auction demonstrates TEN's encryption capabilities in a practical scenario. Bidders submit encrypted bids, ensuring true privacy until the auction ends. This showcases how TEN solves the challenge of maintaining bid secrecy in transparent blockchain environments. + +## **Auction Mechanics** + +- Bidders submit encrypted bids during the auction period. +- Each bid is kept secret from other participants and even the auctioneer. +- The highest bid and bidder remain hidden until the auction concludes. +- After the auction ends, the highest bid and winner are revealed only to the auctioneer through an event. +- Losing bidders can withdraw their bids. + +## **Participating in the Auction** + +1. **Ensure Gateway Authentication:** Verify your account is authenticated through the [TEN Gateway](https://testnet.ten.xyz/). + +2. **Visit the Auction Website:** Navigate to the auction's web interface. + +3. **Place Your Bid:** Enter your bid amount and submit. The transaction will be encrypted. + +4. **Wait for Auction End:** Once the auction period is over, the winner will be determined. + +5. **Check Results or Withdraw:** If you win, you'll be notified. If not, you can withdraw your bid. + +:::tip +You can get free testnet tokens from the [TEN Faucet](/docs/getting-started/for-users/get-tokens). +::: + +## **Building the Sealed Bid Auction** + +This guide will walk you through creating a Sealed Bid Auction on TEN, using private shared states. + +1. **Contract Setup:** + +```solidity +// SPDX-License-Identifier: MIT +pragma solidity ^0.8.18; + +contract SealedBidAuction { + address public auctioneer; + uint public auctionEndTime; + address private highestBidder; + uint private highestBid; + + event AuctionEnded(address winner, uint winningBid); + + constructor(uint _duration) { + auctioneer = msg.sender; + auctionEndTime = block.timestamp + _duration; + } +} +``` + +2. **Secure Bid Storage:** TEN provides true on-chain encryption for private state variables. Even using `getStorageAt`, these variables cannot be accessed. + +```solidity +mapping(address => uint) private bids; +``` + +This ensures that bids are stored securely and are inaccessible to anyone outside the contract. + +3. **Placing Bids:** The bidding function allows users to submit their bids securely. + +```solidity +function placeBid() public payable { + require(block.timestamp < auctionEndTime, "Auction ended"); + bids[msg.sender] += msg.value; + + if (msg.value > highestBid) { + highestBidder = msg.sender; + highestBid = msg.value; + } +} +``` + +On TEN, the `msg.value` in transactions is encrypted, ensuring bid amounts remain private. Moreover, `highestBidder` and `highestBid` are private variables, keeping the current auction status secret. + +4. **Ending the Auction:** Only the auctioneer can end the auction and reveal the winner. + +```solidity +function endAuction() external { + require(msg.sender == auctioneer, "Only auctioneer"); + require(block.timestamp >= auctionEndTime, "Auction not yet ended"); + + emit AuctionEnded(highestBidder, highestBid); + + // Transfer highest bid to auctioneer + payable(auctioneer).transfer(highestBid); +} +``` + +The `AuctionEnded` event reveals the winner and winning bid only at the end of the auction, maintaining privacy throughout the bidding process. + +5. **Withdrawing Bids:** Losing bidders can withdraw their bids after the auction ends. + +```solidity +function withdrawBid() external { + require(block.timestamp >= auctionEndTime, "Auction not yet ended"); + require(msg.sender != highestBidder, "Winner cannot withdraw"); + + uint amount = bids[msg.sender]; + bids[msg.sender] = 0; + payable(msg.sender).transfer(amount); +} +``` + +By leveraging TEN's privacy features, this Sealed Bid Auction ensures true bid secrecy until the auction concludes. The highest bid and bidder remain hidden throughout the auction, only being revealed through an event when the auction ends. This provides a fair, tamper-resistant, and truly private bidding process. + +Find the complete contract & frontend here: [link] From 66d7fd7266cc4a2a180883b8152bf503dfe1e678 Mon Sep 17 00:00:00 2001 From: Jobin Babu Ayathil Date: Thu, 5 Sep 2024 02:32:27 +0530 Subject: [PATCH 2/8] Add Full Contract Code Link --- docs/tutorials-examples/SealedBidAuctions.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/tutorials-examples/SealedBidAuctions.md b/docs/tutorials-examples/SealedBidAuctions.md index 9ed74bd0..9147e55b 100644 --- a/docs/tutorials-examples/SealedBidAuctions.md +++ b/docs/tutorials-examples/SealedBidAuctions.md @@ -109,4 +109,4 @@ function withdrawBid() external { By leveraging TEN's privacy features, this Sealed Bid Auction ensures true bid secrecy until the auction concludes. The highest bid and bidder remain hidden throughout the auction, only being revealed through an event when the auction ends. This provides a fair, tamper-resistant, and truly private bidding process. -Find the complete contract & frontend here: [link] +Find the complete contract & frontend here: [https://github.com/ten-protocol/sample-applications/tree/main/Sealed%20Bid%20Auctions] From 7e5e025547ac9457145db9ed1f75e56752205ebe Mon Sep 17 00:00:00 2001 From: Jobin Babu Ayathil Date: Thu, 5 Sep 2024 03:09:25 +0530 Subject: [PATCH 3/8] Create Anon Suggestion Box Tutorial --- docs/tutorials-examples/AnonSuggestionBox.md | 90 ++++++++++++++++++++ 1 file changed, 90 insertions(+) create mode 100644 docs/tutorials-examples/AnonSuggestionBox.md diff --git a/docs/tutorials-examples/AnonSuggestionBox.md b/docs/tutorials-examples/AnonSuggestionBox.md new file mode 100644 index 00000000..6e5c1204 --- /dev/null +++ b/docs/tutorials-examples/AnonSuggestionBox.md @@ -0,0 +1,90 @@ +--- +sidebar_position: 3 +--- +# Anonymous Suggestion Box + +The Anon Suggestion Box demonstrates TEN's encryption capabilities in a practical scenario. Users submit encrypted suggestions, ensuring true privacy and anonymity. This showcases how TEN solves the challenge of maintaining confidentiality in transparent blockchain environments. + +## **Suggestion Box Mechanics** + +- Users submit encrypted suggestions at any time. +- Each suggestion is kept secret from other participants. +- Only the authorized user (here contract owner) can read the suggestions. +- The number of suggestions is public, but their content remains private. +- Suggestions can be cleared by the authorized user if needed. + +:::tip +You can get free testnet tokens from the [TEN Faucet](/docs/getting-started/for-users/get-tokens). +::: + +## **Building the Anon Suggestion Box** + +This guide will walk you through creating an anonymous suggestion box on TEN, using private shared states. + +1. **Contract Setup:** + +```solidity +// SPDX-License-Identifier: MIT +pragma solidity ^0.8.18; + +contract AnonSuggestionBox { + address private owner; + bytes[] private suggestions; + + event NewSuggestion(uint256 indexed suggestionId); + + constructor() { + owner = msg.sender; + } +} +``` + +2. **Secure Suggestion Storage:** TEN provides true on-chain encryption for private state variables. Even using `getStorageAt`, these variables cannot be accessed. + +```solidity +bytes[] private suggestions; +``` + +This ensures that suggestions are stored securely and are inaccessible to anyone outside the contract. + +3. **Submitting Suggestions:** The submission function allows users to submit their suggestions securely. + +```solidity +function submitSuggestion(bytes calldata encryptedSuggestion) external { + suggestions.push(encryptedSuggestion); + emit NewSuggestion(suggestions.length - 1); +} +``` + +On TEN, the `encryptedSuggestion` in transactions is encrypted, ensuring suggestion content remains private. The `suggestions` array is a private variable, keeping all submissions secret. + +4. **Reading Suggestions:** Only the owner can read the suggestions. + +```solidity +function readAllSuggestions() external view onlyOwner returns (bytes[] memory) { + return suggestions; +} +``` + +This function allows only the authorized user to retrieve all suggestions. + +5. **Clearing Suggestions:** The authorized user can clear all suggestions if needed. + +```solidity +function clearSuggestions() external onlyOwner { + delete suggestions; +} +``` + +6. **Access Control:** + +```solidity +modifier onlyOwner() { + require(msg.sender == owner, "Only the owner can perform this action"); + _; +} +``` + +By leveraging TEN's encryption features, this Anon Suggestion Box ensures true suggestion secrecy. The content of suggestions remains hidden from everyone except the authorized user, providing a secure and truly private feedback mechanism. + +Find the complete contract & frontend here: [https://github.com/ten-protocol/sample-applications/tree/main/Anon%20Suggestion%20Box] From 513e0127f6d49979cdaf127a57c0f0457a1817b6 Mon Sep 17 00:00:00 2001 From: Jobin Babu Ayathil Date: Thu, 5 Sep 2024 03:12:27 +0530 Subject: [PATCH 4/8] Update SealedBidAuctions.md --- docs/tutorials-examples/SealedBidAuctions.md | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/docs/tutorials-examples/SealedBidAuctions.md b/docs/tutorials-examples/SealedBidAuctions.md index 9147e55b..6f023d26 100644 --- a/docs/tutorials-examples/SealedBidAuctions.md +++ b/docs/tutorials-examples/SealedBidAuctions.md @@ -5,14 +5,6 @@ sidebar_position: 2 Sealed Bid Auction demonstrates TEN's encryption capabilities in a practical scenario. Bidders submit encrypted bids, ensuring true privacy until the auction ends. This showcases how TEN solves the challenge of maintaining bid secrecy in transparent blockchain environments. -## **Auction Mechanics** - -- Bidders submit encrypted bids during the auction period. -- Each bid is kept secret from other participants and even the auctioneer. -- The highest bid and bidder remain hidden until the auction concludes. -- After the auction ends, the highest bid and winner are revealed only to the auctioneer through an event. -- Losing bidders can withdraw their bids. - ## **Participating in the Auction** 1. **Ensure Gateway Authentication:** Verify your account is authenticated through the [TEN Gateway](https://testnet.ten.xyz/). @@ -110,3 +102,11 @@ function withdrawBid() external { By leveraging TEN's privacy features, this Sealed Bid Auction ensures true bid secrecy until the auction concludes. The highest bid and bidder remain hidden throughout the auction, only being revealed through an event when the auction ends. This provides a fair, tamper-resistant, and truly private bidding process. Find the complete contract & frontend here: [https://github.com/ten-protocol/sample-applications/tree/main/Sealed%20Bid%20Auctions] + +## **Auction Mechanics** + +- Bidders submit encrypted bids during the auction period. +- Each bid is kept secret from other participants and even the auctioneer. +- The highest bid and bidder remain hidden until the auction concludes. +- After the auction ends, the highest bid and winner are revealed only to the auctioneer through an event. +- Losing bidders can withdraw their bids. From 0cd1744c0612ad269b05e0fbc4484542aa7b7cb1 Mon Sep 17 00:00:00 2001 From: Jobin Babu Ayathil Date: Thu, 5 Sep 2024 03:13:09 +0530 Subject: [PATCH 5/8] Update SealedBidAuctions.md --- docs/tutorials-examples/SealedBidAuctions.md | 32 ++++++++++---------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/docs/tutorials-examples/SealedBidAuctions.md b/docs/tutorials-examples/SealedBidAuctions.md index 6f023d26..a5de4736 100644 --- a/docs/tutorials-examples/SealedBidAuctions.md +++ b/docs/tutorials-examples/SealedBidAuctions.md @@ -5,17 +5,13 @@ sidebar_position: 2 Sealed Bid Auction demonstrates TEN's encryption capabilities in a practical scenario. Bidders submit encrypted bids, ensuring true privacy until the auction ends. This showcases how TEN solves the challenge of maintaining bid secrecy in transparent blockchain environments. -## **Participating in the Auction** - -1. **Ensure Gateway Authentication:** Verify your account is authenticated through the [TEN Gateway](https://testnet.ten.xyz/). - -2. **Visit the Auction Website:** Navigate to the auction's web interface. - -3. **Place Your Bid:** Enter your bid amount and submit. The transaction will be encrypted. - -4. **Wait for Auction End:** Once the auction period is over, the winner will be determined. +## **Auction Mechanics** -5. **Check Results or Withdraw:** If you win, you'll be notified. If not, you can withdraw your bid. +- Bidders submit encrypted bids during the auction period. +- Each bid is kept secret from other participants and even the auctioneer. +- The highest bid and bidder remain hidden until the auction concludes. +- After the auction ends, the highest bid and winner are revealed only to the auctioneer through an event. +- Losing bidders can withdraw their bids. :::tip You can get free testnet tokens from the [TEN Faucet](/docs/getting-started/for-users/get-tokens). @@ -103,10 +99,14 @@ By leveraging TEN's privacy features, this Sealed Bid Auction ensures true bid s Find the complete contract & frontend here: [https://github.com/ten-protocol/sample-applications/tree/main/Sealed%20Bid%20Auctions] -## **Auction Mechanics** +## **Participating in the Auction** -- Bidders submit encrypted bids during the auction period. -- Each bid is kept secret from other participants and even the auctioneer. -- The highest bid and bidder remain hidden until the auction concludes. -- After the auction ends, the highest bid and winner are revealed only to the auctioneer through an event. -- Losing bidders can withdraw their bids. +1. **Ensure Gateway Authentication:** Verify your account is authenticated through the [TEN Gateway](https://testnet.ten.xyz/). + +2. **Visit the Auction Website:** Navigate to the auction's web interface. + +3. **Place Your Bid:** Enter your bid amount and submit. The transaction will be encrypted. + +4. **Wait for Auction End:** Once the auction period is over, the winner will be determined. + +5. **Check Results or Withdraw:** If you win, you'll be notified. If not, you can withdraw your bid. From 92109fde2ff9394aa7c13ba50a2ba01c7d0af781 Mon Sep 17 00:00:00 2001 From: Jobin Babu Ayathil Date: Thu, 5 Sep 2024 03:34:22 +0530 Subject: [PATCH 6/8] Create EncryptedVoting.sol --- docs/tutorials-examples/EncryptedVoting.sol | 1 + 1 file changed, 1 insertion(+) create mode 100644 docs/tutorials-examples/EncryptedVoting.sol diff --git a/docs/tutorials-examples/EncryptedVoting.sol b/docs/tutorials-examples/EncryptedVoting.sol new file mode 100644 index 00000000..8b137891 --- /dev/null +++ b/docs/tutorials-examples/EncryptedVoting.sol @@ -0,0 +1 @@ + From 6fe04564894e69886527976c3d80c91141bdff69 Mon Sep 17 00:00:00 2001 From: Jobin Babu Ayathil Date: Thu, 5 Sep 2024 03:38:16 +0530 Subject: [PATCH 7/8] Add encryptedVoting contract --- docs/tutorials-examples/EncryptedVoting.md | 122 ++++++++++++++++++++ docs/tutorials-examples/EncryptedVoting.sol | 1 - 2 files changed, 122 insertions(+), 1 deletion(-) create mode 100644 docs/tutorials-examples/EncryptedVoting.md delete mode 100644 docs/tutorials-examples/EncryptedVoting.sol diff --git a/docs/tutorials-examples/EncryptedVoting.md b/docs/tutorials-examples/EncryptedVoting.md new file mode 100644 index 00000000..bbe6c9e2 --- /dev/null +++ b/docs/tutorials-examples/EncryptedVoting.md @@ -0,0 +1,122 @@ +--- +sidebar_position: 4 +--- +# Encrypted DAO Voting + +Encrypted DAO Voting demonstrates TEN's encryption capabilities in a critical governance scenario. DAO members can cast encrypted votes, ensuring true privacy throughout the voting process. This showcases how TEN solves the challenge of maintaining vote secrecy in transparent blockchain environments, preventing vote buying and coercion. + +## **Voting Mechanics** + +- DAO members submit encrypted votes (upvote or downvote) during the voting period. +- Each vote is kept secret from other members. +- Members can verify they've voted without revealing their choice. +- The voting results (total upvotes and downvotes) remain hidden until the voting period concludes. +- After voting ends, only the final tally is revealed, not individual votes. + +:::tip +You can get free testnet tokens from the [TEN Faucet](/docs/getting-started/for-users/get-tokens). +::: + +## **Building the Encrypted DAO Voting System** + +This guide will walk you through creating a Encrypted DAO Voting system on TEN, using private shared states. + +1. **Contract Setup:** + +```solidity +// SPDX-License-Identifier: MIT +pragma solidity ^0.8.18; + +contract encryptedVoting { + address public admin; + uint public votingEndTime; + + // These variables are private and encrypted by TEN's L2 protocol + uint private upvotes; + uint private downvotes; + mapping(address => bool) private hasVoted; + + event VoteCast(address indexed voter); + event VotingEnded(uint upvotes, uint downvotes); + + constructor(uint _durationInMinutes) { + admin = msg.sender; + votingEndTime = block.timestamp + (_durationInMinutes * 1 minutes); + } +} +``` + +2. **Secure Vote Storage:** TEN provides true on-chain encryption for private state variables. Even using `getStorageAt`, these variables cannot be accessed. + +```solidity +uint private upvotes; +uint private downvotes; +mapping(address => bool) private hasVoted; +``` + +This ensures that votes are stored securely and are inaccessible to anyone outside the contract. + +3. **Casting Votes:** The voting function allows members to submit their votes securely. + +```solidity +function vote(bool isUpvote) external votingOpen { + require(!hasVoted[msg.sender], "Already voted"); + + if (isUpvote) { + upvotes++; + } else { + downvotes++; + } + hasVoted[msg.sender] = true; + + emit VoteCast(msg.sender); +} +``` + +On TEN, the `isUpvote` parameter in transactions is encrypted, ensuring vote choices remain private. The `upvotes`, `downvotes`, and `hasVoted` variables are private, keeping the voting status secret. + +4. **Verifying Votes:** Members can verify each other's vote was counted without revealing their choice. + +```solidity +function hasUserVoted(address user) external view returns (bool) { + return hasVoted[user]; +} +``` + +This function allows anyone to check if a specific address has voted, maintaining vote privacy while ensuring transparency. + +5. **Ending the Vote:** Only the admin can end the voting and reveal the results. + +```solidity +function endVoting() external onlyAdmin { + require(block.timestamp >= votingEndTime, "Voting period not yet over"); + emit VotingEnded(upvotes, downvotes); +} +``` + +The `VotingEnded` event reveals only the total upvotes and downvotes, maintaining privacy of individual votes. + +By leveraging TEN's encryption features, this Encrypted DAO Voting system ensures: + +- **Vote Secrecy:** Individual votes remain hidden throughout the process. +- **Verifiable Participation:** Members can prove they voted without revealing their choice. +- **Tamper-Resistant Tallying:** Vote counts are securely updated and stored. +- **Delayed Result Revelation:** Final tallies are only revealed when voting ends. + +This provides a fair, tamper-resistant, and truly private voting mechanism for DAOs, which is not possible on traditional transparent blockchains. + +Find the complete contract & frontend here: [GitHub link to be added] + +## **Participating in DAO Voting** + +1. **Ensure Gateway Authentication:** Verify your account is authenticated through the [TEN Gateway](https://testnet.ten.xyz/). + +2. **Visit the DAO Voting Website:** Navigate to the DAO's voting interface. + +3. **Cast Your Vote:** Choose to upvote or downvote and submit. The transaction will be encrypted. + +4. **Verify Your Vote:** Use the `hasUserVoted` function to confirm your vote was recorded without revealing your choice. + +5. **Wait for Voting End:** Once the voting period is over, results will be tallied. + +6. **Check Results:** View the final tally of upvotes and downvotes without compromising individual vote privacy. diff --git a/docs/tutorials-examples/EncryptedVoting.sol b/docs/tutorials-examples/EncryptedVoting.sol deleted file mode 100644 index 8b137891..00000000 --- a/docs/tutorials-examples/EncryptedVoting.sol +++ /dev/null @@ -1 +0,0 @@ - From e09a76f40713af5c2e000d244c8adba23003e2bd Mon Sep 17 00:00:00 2001 From: Jobin Babu Ayathil Date: Thu, 5 Sep 2024 03:40:52 +0530 Subject: [PATCH 8/8] Update Full Contract Code Link --- docs/tutorials-examples/EncryptedVoting.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/tutorials-examples/EncryptedVoting.md b/docs/tutorials-examples/EncryptedVoting.md index bbe6c9e2..cd8ec2dc 100644 --- a/docs/tutorials-examples/EncryptedVoting.md +++ b/docs/tutorials-examples/EncryptedVoting.md @@ -105,7 +105,7 @@ By leveraging TEN's encryption features, this Encrypted DAO Voting system ensure This provides a fair, tamper-resistant, and truly private voting mechanism for DAOs, which is not possible on traditional transparent blockchains. -Find the complete contract & frontend here: [GitHub link to be added] +Find the complete contract & frontend here: [https://github.com/ten-protocol/sample-applications/tree/main/Encrypted%20DAO%20Voting] ## **Participating in DAO Voting**