Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
90 changes: 90 additions & 0 deletions docs/tutorials-examples/AnonSuggestionBox.md
Original file line number Diff line number Diff line change
@@ -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.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we use the word 'secrecy' instead of 'privacy' here

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And 'hypothetical' instead of 'practical'


## **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]
122 changes: 122 additions & 0 deletions docs/tutorials-examples/EncryptedVoting.md
Original file line number Diff line number Diff line change
@@ -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: [https://github.com/ten-protocol/sample-applications/tree/main/Encrypted%20DAO%20Voting]

## **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.
112 changes: 112 additions & 0 deletions docs/tutorials-examples/SealedBidAuctions.md
Original file line number Diff line number Diff line change
@@ -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.

:::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) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since we're doing extra computation here when we're the highest bidder, would it be possible to work out when you're the highest bidder? Just keep bidding until you notice gas increases

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe this calculation could be saved until the end and performed by whoever closes the bidding?

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: [https://github.com/ten-protocol/sample-applications/tree/main/Sealed%20Bid%20Auctions]

## **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.