Skip to content

Commit

Permalink
Merge pull request #36 from FhenixProtocol/nitrogen
Browse files Browse the repository at this point in the history
Nitrogen docs update
  • Loading branch information
toml01 authored Nov 6, 2024
2 parents 6f9b388 + 231a532 commit 5fcad45
Show file tree
Hide file tree
Showing 91 changed files with 8,612 additions and 211 deletions.
117 changes: 117 additions & 0 deletions docs/devdocs/Encryption and Privacy/Exposed-Encrypted-Variables.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
# 🔎 Encrypted Variables - Preventing Exposure

Ensuring that encrypted data and variables are not leaked is important when working with Fhenix. A common oversight when working with encrypted variables is revealing them to other contracts. Lets take a look at a scenario that leaks encrypted data:

```solidity
contract UserBalanceVulnerable {
mapping(address => euint64) public eUserBalances;
function addBalance(inEuint64 calldata _inBalance) public {
eUserBalances[msg.sender] = eUserBalances[msg.sender].add(
FHE.asEuint64(_inBalance)
);
}
}
```

This seems secure enough and no decrypted data is directly exposed, however the `public` access to `eUserBalances` leaks sensitive data. A malicious contract is able to fetch this data and then decrypt it:

```solidity
contract UserBalanceAttack {
address private vulnerableContract;
function revealUserBalance(address _user) public view returns (uint64) {
return UserBalanceVulnerable(vulnerableContract)
.eUserBalances(_user)
.decrypt();
}
}
```

All contracts on the Fhenix network share an encryption key, therefore an encrypted variable in `ContractA` could be decrypted in `ContractB`.

This is not inherently wrong, and many operations will require encrypted variables to be shared between contracts, but care must be taken to prevent open access to encrypted variables.

### Hardhat Task

The `fhenix-hardhat-plugin` package contains a task that checks your contracts for any exposed encrypted variables. This task is run automatically when your contracts are compiled, but can also be run manually.

#### Task Example

The following contract exposes encrypted variables in 3 ways.

```solidity
pragma solidity >=0.8.13 <0.9.0;
import "@fhenixprotocol/contracts/FHE.sol";
contract ContractWithExposedVariables {
// Example 1
mapping(address => euint8) public eUserBalances;
// Example 2
mapping(address => euint8) private _eUserBalances;
function getUserBalance(address _user) public view returns (euint8) {
return _eUserBalances[_user];
}
// Example 3
struct Player {
address player;
euint8[] eCards;
uint256 chips;
uint256 bet;
}
struct Dealer {
uint256 pot;
euint8[] eFlopCards;
}
struct HoldEmGameState {
Player[] players;
Dealer dealer;
}
HoldEmGameState private gameState;
// Encrypted card values is the Player and Dealer structs are leaked and can be exploited
function getGameState() public view returns (HoldEmGameState memory) {
return gameState;
}
}
```

#### Output

Below is the output of the task when analyzing the above `ContractWithExposedVariables.sol`

<pre>
<b>fhenix-hardhat-plugin:CheckExposedEncryptedVars</b> checking for exposed encrypted variables....

<b style={{color:"orangered"}}>contracts/ContractWithExposedVariables.sol:ContractWithExposedVariables</b>

<b>eUserBalances(address)</b> exposes 1 encrypted variables:
pos-0 - <b style={{color:"orangered"}}>euint8</b>

<b>getUserBalance(address)</b> exposes 1 encrypted variables:
pos-0 - <b style={{color:"orangered"}}>euint8</b>

<b>getGameState()</b> exposes 1 encrypted variables:
pos-0 - struct ContractWithExposedVariables.HoldEmGameState
players - struct ContractWithExposedVariables.Player[]
eCards - <b style={{color:"orangered"}}>euint8[]</b>
dealer - struct ContractWithExposedVariables.Dealer
eFlopCards - <b style={{color:"orangered"}}>euint8[]</b>
</pre>

#### Manual Task Execution

The task can be run manually with the command:

```
npx hardhat task:fhenix:checkExposedEncryptedVars
```

Or as a part of a hardhat compilation:

```
npx hardhat compile
```
17 changes: 12 additions & 5 deletions docs/devdocs/Encryption and Privacy/Permits-Access-Control.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,21 @@ The inclusion of this public key into the permit enables a secure process of dat

#### How to Generate a Permit

Permits are generated using the `getPermit` method in `fhenix.js`. This method requires the following parameters:
Permits are generated using the `generatePermit` method in `fhenix.js`. This method receives the following parameters:

* `contractAddress` (required, string): The address of the contract.
* `provider` (required): An `ethers` (or compatible) object that can sign EIP-712 formatted data. (Note that if you want to unseal data using your wallet's encryption key you can't use "JsonRpcProvider")
* `provider` (optional): An `ethers` (or compatible) object that can sign EIP-712 formatted data. (Note that if you want to unseal data using your wallet's encryption key you can't use "JsonRpcProvider")
* `signer` (optional): Another `ethers` (or compatible) signer if you want to use a different signer than the one in the provider (chain-id requests are still made via the provider)

```javascript
const permit = await getPermit(contractAddress);
const permit = await generatePermit(contractAddress);

// passing a custom signer
let permit = await fhenixjs.generatePermit(
contractAddress,
undefined, // use the internal provider
signer, // created from, e.g. `ethers.getSigners()[0]`
);
```

#### What is a Permission?
Expand Down Expand Up @@ -57,9 +65,8 @@ import { FhenixClient, getPermit } from "fhenixjs";

const provider = new BrowserProvider(window.ethereum);
const client = new FhenixClient({ provider });
const permit = await getPermit(contractAddress, provider);
const permit = await generatePermit(contractAddress, provider);
const permission = client.extractPemitPermissions(permit);
client.storePermit(permit); // Stores a permit for a specific contract address.
const response = await contract.connect(owner).getValue(permission); // Calling "getValue" which is a view function in "contract"
const plaintext = await client.unseal(contractAddress, response);
```
2 changes: 1 addition & 1 deletion docs/devdocs/Examples and References/Templates.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ We compiled a list of a few templates that you can use as a reference to build y

https://github.com/FhenixProtocol/fhenix-hardhat-example

Has a basic contract, some tasks and a simple frontend (TODO: copy over from playground).
Has a basic contract, some tasks and a simple frontend.

### Nuxt 3 + Fhenixjs + Ethers.js + Bootstrap Starter

Expand Down
35 changes: 17 additions & 18 deletions docs/devdocs/Fhenix Testnet/Connecting-To.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,24 @@
sidebar_position: 3
---

# 🔗 Connecting to Fhenix Helium Testnet
# 🔗 Connecting to Fhenix Nitrogen Testnet

Fhenix Helium is the first publicly available FHE-based blockchain, and it is now live! Follow the instructions to connect to Fhenix Helium Testnet.
Fhenix Nitrogen is the first publicly available FHE-based blockchain, and it is now live! Follow the instructions to connect to Fhenix Nitrogen Testnet.

## Configuring MetaMask
## Configuring MetaMask

1. Open MetaMask in your browser and click on the Ethereum network.
2. Click **Add Network.**
3. Click **Add a network manually**.

4. Fill out the network details form. To add a custom network, fill in the following fields:
1. **Network Name: Fhenix Helium**
2. **New RPC URL: https://api.helium.fhenix.zone**
3. **Chain ID:** 8008135
4. **Currency Symbol: tFHE**
5. **Block Explorer URL: https://explorer.helium.fhenix.zone**
1. **Network Name: Fhenix Nitrogen**
2. **New RPC URL: https://api.nitrogen.fhenix.zone**
3. **Chain ID:** 8008148
4. **Currency Symbol: FHE**
5. **Block Explorer URL: https://explorer.nitrogen.fhenix.zone**
5. Once you fill out all the details, click **Save**.
6. Now you are ready to switch to Fhenix Helium Testnet. Tokens are available from the testnet faucet. Start building!

6. Now you are ready to switch to Fhenix Nitrogen Testnet. Tokens are available from the testnet faucet. Start building!

## API endpoints

Expand All @@ -34,31 +33,31 @@ Fhenix Helium is the first publicly available FHE-based blockchain, and it is no
<tbody>
<tr>
<td>JSON-RPC</td>
<td><a href="https://api.helium.fhenix.zone"><strong>https://api.helium.fhenix.zone</strong></a></td>
<td><a href="https://api.nitrogen.fhenix.zone"><strong>https://api.nitrogen.fhenix.zone</strong></a></td>
</tr>
<tr>
<td>Chain ID</td>
<td>8008135</td>
<td>8008148</td>
</tr>
<tr>
<td>Websocket</td>
<td>wss://api.helium.fhenix.zone:8548</td>
<td>wss://api.nitrogen.fhenix.zone:8548</td>
</tr>
</tbody>
</table>

## Explorer

* [https://explorer.helium.fhenix.zone](https://explorer.helium.fhenix.zone)
- [https://explorer.nitrogen.fhenix.zone](https://explorer.nitrogen.fhenix.zone)

## Faucet

To get some test tokens, use the faucet at [https://get-helium.fhenix.zone/](https://get-helium.fhenix.zone/).
To get some test tokens, use the faucet at [https://get-nitrogen.fhenix.zone/](https://get-nitrogen.fhenix.zone/).
You may receive 0.1 tokens once every five minutes. If you need more tokens, please reach out to us on Discord, or bridge some Sepolia!

## Bridge

The Helium testnet is connected to the Sepolia testnet. You can use the bridge to move tokens between the two networks.
If you require more tokens, you can use the bridge to move tokens from Sepolia to Helium.
The Nitrogen testnet is connected to the Sepolia testnet. You can use the bridge to move tokens between the two networks.
If you require more tokens, you can use the bridge to move tokens from Sepolia to Nitrogen.

* [https://bridge.helium.fhenix.zone/](https://bridge.helium.fhenix.zone/)
- [https://bridge.nitrogen.fhenix.zone/](https://bridge.nitrogen.fhenix.zone/)
29 changes: 16 additions & 13 deletions docs/devdocs/Fhenix Testnet/Details/Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,24 @@ title: 🆕 Changelog

Here you can find a list of changes between different versions of the Fhenix Testnet(s) as we evolve and grow.

### Nitrogen

### Helium - Latest Version
TBD

* Added `eaddress` as a native type that can be found in FHE.sol directly
* Added support for large integer sizes: `euint64`, `euint128`, `euint256`. Not all operations are supported for each type
at this time. See [Types and Operators](../../Writing%20Smart%20Contracts/Types-and-Operators.md) for more information.
* Added support for solidity compiler version 0.8.25 and up
* Performance has been greatly increased for all data types
* Performance has been greatly increased for select operations
* All client-side libraries and toolkits have been upgraded and will require an update to version 0.2 to work with Helium - FhenixJS, Remix plugin & hardhat plugins.
* Refactored gas prices for FHE operations to better reflect the operational cost of the operation. See [Gas and Benchmarks](../../Writing%20Smart%20Contracts/Gas-and-Benchmarks.md) for more information.
* Blocks are now posted to the Sepolia Testnet with support for EIP-4844 blobs.
* Refactored gas cost for transaction data to be more in line with Ethereum.
* LocalFhenix - Added support for `Console.log` functionality to allow debug logs during contract execution.
* Many bug fixes and other improvements.
### Helium

- Added `eaddress` as a native type that can be found in FHE.sol directly
- Added support for large integer sizes: `euint64`, `euint128`, `euint256`. Not all operations are supported for each type
at this time. See [Types and Operators](../../Writing%20Smart%20Contracts/Types-and-Operators.md) for more information.
- Added support for solidity compiler version 0.8.25 and up
- Performance has been greatly increased for all data types
- Performance has been greatly increased for select operations
- All client-side libraries and toolkits have been upgraded and will require an update to version 0.2 to work with Helium - FhenixJS, Remix plugin & hardhat plugins.
- Refactored gas prices for FHE operations to better reflect the operational cost of the operation. See [Gas and Benchmarks](../../Writing%20Smart%20Contracts/Gas-and-Benchmarks.md) for more information.
- Blocks are now posted to the Sepolia Testnet with support for EIP-4844 blobs.
- Refactored gas cost for transaction data to be more in line with Ethereum.
- LocalFhenix - Added support for `Console.log` functionality to allow debug logs during contract execution.
- Many bug fixes and other improvements.

### Frontier

Expand Down
2 changes: 1 addition & 1 deletion docs/devdocs/Fhenix Testnet/Details/_category_.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
"position": 100,
"link": {
"type": "generated-index",
"description": "Fhenix Helium Testnet (Testnet v1) Detailed Information"
"description": "Fhenix Nitrogen Testnet (Testnet v1) Detailed Information"
}
}
6 changes: 3 additions & 3 deletions docs/devdocs/Fhenix Testnet/Fhenix-T-FHE.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ FHE makes it possible to write private smart contracts that keep on-chain data e

To read more about different FHE schemes, see our [FHE Overview Section](./FHE-Overview).

## Fhenix Helium Testnet
## Fhenix Nitrogen Testnet

The current Fhenix Helium Testnet is the first public iteration of the Fhenix protocol. It is still an early build, and it has bugs (unfortunately) and many features that are still under development.
The current Fhenix Nitrogen Testnet is the second public iteration of the Fhenix protocol. It is still an early build, and it can potentialy has bugs (unfortunately) and there are still features that are still under development.

There are many challenges ahead and many problems to solve. However, we are excited to be working on this project, because it is potentially an innovative and disruptive technology in the blockchain space.
There are still challenges ahead. However, we are excited to be working on this project, because it is potentially an innovative and disruptive technology in the blockchain space.

What we write here is not set in stone. We are still considering the best way to move forward, and we are excited to have you here with us as we embark on this journey. Please let us know if you have any suggestions, ideas or comments. Feedback is always welcome. We are looking for ways to improve and for people to join us and contribute.
14 changes: 6 additions & 8 deletions docs/devdocs/Fhenix Testnet/Integration.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,19 @@ Are you a developer looking to integrate Fhenix into your project, or support Fh

### APIs, RPCs and general compatibility

Fhenix is based on Arbitrum, with the Helium Testnet based on Arbitrum Nitro version 2.3.4 (ArbOS 20). This means that everything that is natively supported
Fhenix is based on Arbitrum, with the Nitrogen Testnet based on Arbitrum Nitro version 3.2.1 (ArbOS 32). This means that everything that is natively supported
by Arbitrum Nitro is also supported by Fhenix (rpc calls, ABI, etc).

Please refer to the [Arbitrum documentation](https://docs.arbitrum.io/build-decentralized-apps/arbitrum-vs-ethereum/comparison-overview) for more information and specifics.

### EVM Compatibility

Fhenix is fully EVM compatible, up to and including the Cancun Upgrade.
Fhenix is fully EVM compatible, up to and including the Cancun Upgrade.
This means that any contract that runs on Ethereum should run on Fhenix as well. We support Solidity compiler 0.8.26.

### Public Endpoints

We have public endpoints available for the Helium Testnet, which can be used:
We have public endpoints available for the Nitrogen Testnet, which can be used:

<table>
<thead>
Expand All @@ -33,15 +33,15 @@ We have public endpoints available for the Helium Testnet, which can be used:
<tbody>
<tr>
<td>JSON-RPC</td>
<td><a href="https://api.helium.fhenix.zone"><strong>https://api.helium.fhenix.zone</strong></a></td>
<td><a href="https://api.nitrogen.fhenix.zone"><strong>https://api.nitrogen.fhenix.zone</strong></a></td>
</tr>
<tr>
<td>Chain ID</td>
<td>8008135</td>
<td>8008148</td>
</tr>
<tr>
<td>Websocket</td>
<td>wss://api.helium.fhenix.zone:8548</td>
<td>wss://api.nitrogen.fhenix.zone:8548</td>
</tr>
</tbody>
</table>
Expand Down Expand Up @@ -78,5 +78,3 @@ The following contracts are deployed on Ethereum Sepolia and may be used by deve
</td>
</tr>
</table>


4 changes: 2 additions & 2 deletions docs/devdocs/Fhenix Testnet/_category_.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"label": "Fhenix Helium Testnet",
"label": "Fhenix Nitrogen Testnet",
"position": 2,
"link": {
"type": "generated-index",
"description": "Fhenix Helium Testnet Specific Information"
"description": "Fhenix Nitrogen Testnet Specific Information"
}
}
18 changes: 7 additions & 11 deletions docs/devdocs/FhenixJS/Decryption.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,12 @@ This is done in 3 steps: generating a permit, querying the contract and unsealin
#### 1. Creating a Permit

```javascript
import { FhenixClient, getPermit } from 'fhenixjs';
import { FhenixClient, generatePermit } from 'fhenixjs';

const provider = new ethers.JsonRpcProvider('https://api.helium.fhenix.zone/');
const provider = new ethers.JsonRpcProvider('https://api.nitrogen.fhenix.zone/');
const client = new FhenixClient({ provider });


const permit = await getPermit(contractAddress, provider);
client.storePermit(permit);
const permit = await generatePermit(contractAddress, provider);
```

:::tip[Did you know?]
Expand Down Expand Up @@ -74,18 +72,16 @@ Permits are currently limited to support a single contract
#### Putting it all Together

```typescript
import { FhenixClient, getPermit } from 'fhenixjs';
import { FhenixClient, generatePermit } from 'fhenixjs';
import { JsonRpcProvider } from 'ethers';

const provider = new ethers.JsonRpcProvider('https://api.helium.fhenix.zone/');
const provider = new ethers.JsonRpcProvider('https://api.nitrogen.fhenix.zone/');
const client = new FhenixClient({provider});

const permit = await getPermit(contractAddress, provider);
client.storePermit(permit);

const permit = await generatePermit(contractAddress, provider);
const permission = client.extractPermitPermission(permit);
const response = await contract.balanceOf(permission);

const response = await contract.balanceOf(permission);
const plaintext = client.unseal(contractAddress, response);

console.log(`My Balance: ${plaintext}`)
Expand Down
Loading

0 comments on commit 5fcad45

Please sign in to comment.