Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Docs/use bundle fhevmjs #659

Merged
merged 14 commits into from
Dec 16, 2024
Merged
Show file tree
Hide file tree
Changes from 10 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
Binary file modified docs/.gitbook/assets/useGateway.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
5 changes: 3 additions & 2 deletions docs/fundamentals/decryption/reencryption.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,15 @@ Here, `balanceOf` allows retrieval of the user’s encrypted balance stored on t

## Step 2: re-encrypt the ciphertext

Re-encryption is performed client-side using the `fhevmjs` library. Below is an example of how to implement this in a dApp:
Re-encryption is performed client-side using the `fhevmjs` library. [Refer to the guide](../../guides/frontend/webapp.md) to learn how to include `fhevmjs` in your project.
Below is an example of how to implement reencryption in a dApp:

```ts
import { createInstances } from "../instance";
import { getSigners, initSigners } from "../signers";
import abi from "./abi.json";
import { Contract, BrowserProvider } from "ethers";
import { createInstance } from "fhevmjs";
import { createInstance } from "fhevmjs/bundle";

const CONTRACT_ADDRESS = "";

Expand Down
12 changes: 6 additions & 6 deletions docs/getting_started/ethereum.md
Original file line number Diff line number Diff line change
Expand Up @@ -123,10 +123,10 @@ Save this in your `.env` file:

| Contract/Service | Address/Value |
| ---------------------- | ------------------------------------------ |
| TFHE_EXECUTOR_CONTRACT | 0x199fB61DFdfE46f9F90C9773769c28D9623Bb90e |
| ACL_CONTRACT | 0x9479B455904dCccCf8Bc4f7dF8e9A1105cBa2A8e |
| PAYMENT_CONTRACT | 0x25FE5d92Ae6f89AF37D177cF818bF27EDFe37F7c |
| KMS_VERIFIER_CONTRACT | 0x904Af2B61068f686838bD6257E385C2cE7a09195 |
| GATEWAY_CONTRACT | 0x7455c89669cdE1f7Cb6D026DFB87263422D821ca |
| PUBLIC_KEY_ID | 55729ddea48547ea837137d122e1c90043e94c41 |
| TFHE_EXECUTOR_CONTRACT | 0x687408aB54661ba0b4aeF3a44156c616c6955E07 |
| ACL_CONTRACT | 0xFee8407e2f5e3Ee68ad77cAE98c434e637f516e5 |
| PAYMENT_CONTRACT | 0xFb03BE574d14C256D56F09a198B586bdfc0A9de2 |
| KMS_VERIFIER_CONTRACT | 0x9D6891A6240D6130c54ae243d8005063D05fE14b |
| GATEWAY_CONTRACT | 0x33347831500F1e73f0ccCBb95c9f86B94d7b1123 |
| PUBLIC_KEY_ID | 0301c5dd3e2702992b7c12930b7d4defeaaa52cf |
| GATEWAY_URL | `https://gateway.sepolia.zama.ai/` |
32 changes: 0 additions & 32 deletions docs/getting_started/first_smart_contract.md
Original file line number Diff line number Diff line change
Expand Up @@ -164,38 +164,6 @@ The test file demonstrates key concepts for testing fhEVM smart contracts:
- Handle asynchronous operations properly with async/await
- Set up proper encryption instances for testing encrypted values

### No constant nor immutable encrypted state variables

Never use encrypted types for constant or immutable state variables, even if they should actually stay constants, or else any transaction involving those will fail. This is because ciphertexts should always be stored in the privileged storage of the contract (see paragraph 4.4 of [whitepaper](../../fhevm-whitepaper.pdf)) while constant and immutable variables are just appended to the bytecode of the deployed contract at construction time.

❌ So, even if `a` and `b` should never change after construction, the following example :

```solidity
contract C {
euint32 internal constant a = TFHE.asEuint32(42);
euint32 internal immutable b;

constructor(uint32 _b) {
b = TFHE.asEuint32(_b);
TFHE.allowThis(b);
}
}
```

✅ Should be replaced by this snippet:

```solidity
contract C {
euint32 internal a = TFHE.asEuint32(42);
euint32 internal b;

constructor(uint32 _b) {
b = TFHE.asEuint32(_b);
TFHE.allowThis(b);
}
}
```

## **Next steps**

Congratulations! You’ve configured and written your first confidential smart contract. Here are some ideas to expand your knowledge:
Expand Down
4 changes: 1 addition & 3 deletions docs/getting_started/write_contract/remix.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,7 @@ This document provides guidance on using the new Zama plugin for the [the offici
After connecting to the Zama Plugin, follow the steps to configure it:

1. Click on the plugin button located on the left of the screen
2. Add a Gateway URL to be able to request reencryption of ciphertexts, as shown in the picture below.

The default recommended Gateway URL is: `https://gateway.devnet.zama.ai`.
2. Use the Zama Sepolia settings or use a custom gateway and contracts settings.

<figure><img src="../../.gitbook/assets/useGateway.png" alt="How to install Remix IDE plugin" width="300"><figcaption></figcaption></figure>

Expand Down
4 changes: 2 additions & 2 deletions docs/guides/frontend/node.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ const { createInstance } = require("fhevmjs");
const createFhevmInstance = async () => {
return createInstance({
chainId: 11155111, // Sepolia chain ID
networkUrl: "https://rpc.sepolia.org/", // Sepolia RPC URL
gatewayUrl: "https://gateway.zama.ai",
networkUrl: "https://eth-sepolia.public.blastapi.io", // Sepolia RPC URL
gatewayUrl: "https://gateway.sepolia.zama.ai",
});
};
createFhevmInstance().then((instance) => {
Expand Down
56 changes: 48 additions & 8 deletions docs/guides/frontend/webapp.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,43 @@ You can also use [this template](https://github.com/zama-ai/fhevmjs-next-templat

## Using directly the library

### Step 1: Install the library
### Step 1: Setup the library

`fhevmjs` consists of multiple files, including WASM files and WebWorkers, which can make packaging these components correctly in your setup cumbersome. To simplify this process, especially if you're developing a dApp with server-side rendering (SSR), we recommend using our CDN.

#### Using UMD CDN

Include this line at the top of your project.

```html
<script src="https://cdn.zama.ai/fhevmjs/0.6.2/fhevmjs.umd.cjs" type="text/javascript"></script>
```

In your project, you can use the bundle import if you install `fhevmjs` package:

```javascript
import { initFhevm, createInstance } from "fhevmjs/bundle";
```

#### Using ESM CDN

If you prefer You can also use the `fhevmjs` as a ES module:

```html
<script type="module">
import { initFhevm, createInstance } from "https://cdn.zama.ai/fhevmjs/0.6.2/fhevmjs.js";

await initFhevm();
const instance = await createInstance({
network: window.ethereum,
kmsContractAddress: "0x904Af2B61068f686838bD6257E385C2cE7a09195",
aclContractAddress: "0x9479B455904dCccCf8Bc4f7dF8e9A1105cBa2A8e",
gatewayUrl: "https://gateway.sepolia.zama.ai",
});
</script>
```

#### Using npm package

Install the `fhevmjs` library to your project:

Expand All @@ -35,14 +71,18 @@ yarn add fhevmjs
pnpm add fhevmjs
```

### Step 2: Initialize your project

`fhevmjs` uses ESM format. You need to set the [type to "module" in your package.json](https://nodejs.org/api/packages.html#type). If your node project use `"type": "commonjs"` or no type, you can force the loading of the web version by using `import { createInstance } from 'fhevmjs/web';`

```javascript
import { initFhevm, createInstance } from "fhevmjs";
```

### Step 2: Initialize your project

To use the library in your project, you need to load the WASM of [TFHE](https://www.npmjs.com/package/tfhe) first with `initFhevm`.

```javascript
import { initFhevm } from "fhevmjs";
import { initFhevm } from "fhevmjs/bundle";

const init = async () => {
await initFhevm(); // Load needed WASM
Expand All @@ -61,15 +101,15 @@ Once the WASM is loaded, you can now create an instance. An instance receives an
- `coprocessorUrl` (optional): the URL of the coprocessor

```javascript
import { initFhevm, createInstance } from "fhevmjs";
import { initFhevm, createInstance } from "fhevmjs/bundle";

const init = async () => {
await initFhevm(); // Load TFHE
return createInstance({
kmsContractAddress: "0x208De73316E44722e16f6dDFF40881A3e4F86104",
aclContractAddress: "0xc9990FEfE0c27D31D0C2aa36196b085c0c4d456c",
kmsContractAddress: "0x904Af2B61068f686838bD6257E385C2cE7a09195",
aclContractAddress: "0x9479B455904dCccCf8Bc4f7dF8e9A1105cBa2A8e",
network: window.ethereum,
gatewayUrl: "https://gateway.zama.ai/",
gatewayUrl: "https://gateway.sepolia.zama.ai/",
});
};

Expand Down
8 changes: 4 additions & 4 deletions docs/guides/frontend/webpack.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,17 +54,17 @@ resolve: {

**Cause:** The library may not bundle correctly with certain frameworks, leading to errors during the build or runtime process.

**Possible solutions:** Use the prebundled version available in `fhevmjs/bundle`. Embed the library with a `<script>` tag and initialize it as shown below:
**Possible solutions:** Use the [prebundled version available](./webapp.md) with `fhevmjs/bundle`. Embed the library with a `<script>` tag and initialize it as shown below:

```javascript
const start = async () => {
await window.fhevm.initFhevm(); // load wasm needed
const instance = window.fhevm
.createInstance({
kmsContractAddress: "0x208De73316E44722e16f6dDFF40881A3e4F86104",
aclContractAddress: "0xc9990FEfE0c27D31D0C2aa36196b085c0c4d456c",
kmsContractAddress: "0x904Af2B61068f686838bD6257E385C2cE7a09195",
aclContractAddress: "0x9479B455904dCccCf8Bc4f7dF8e9A1105cBa2A8e",
network: window.ethereum,
gatewayUrl: "https://gateway.zama.ai/",
gatewayUrl: "https://gateway.sepolia.zama.ai/",
})
.then((instance) => {
console.log(instance);
Expand Down
37 changes: 17 additions & 20 deletions docs/references/fhevmjs.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,14 @@ This document provides an overview of the `fhevmjs` library, detailing its initi
If you are using `fhevmjs` in a web application, you need to initialize it before creating an instance. To do this, you should call `initFhevm` and wait for the promise to resolve.

```javascript
import { FhevmInstance, createInstance as createFhevmInstance } from "fhevmjs/node";
import { FhevmInstance, createInstance } from "fhevmjs/node";

initFhevm().then(() => {
const instance = await createFhevmInstance({
networkUrl: (network.config as NetworkConfig & { url: string }).url,
gatewayUrl: parsedEnv.GATEWAY_URL,
aclContractAddress: parsedEnv.ACL_CONTRACT_ADDRESS,
kmsContractAddress: parsedEnv.KMS_VERIFIER_CONTRACT_ADDRESS,
publicKeyId: parsedEnv.PUBLIC_KEY_ID,
const instance = await createInstance({
network: window.ethereum,
gatewayUrl: "https://gateway.sepolia.zama.ai",
kmsContractAddress: "0x904Af2B61068f686838bD6257E385C2cE7a09195",
aclContractAddress: "0x9479B455904dCccCf8Bc4f7dF8e9A1105cBa2A8e",
});
});
```
Expand All @@ -37,26 +36,24 @@ This function returns an instance of fhevmjs, which accepts an object containing
```javascript
import { createInstance } from "fhevmjs";

const instance = await createFhevmInstance({
networkUrl: (network.config as NetworkConfig & { url: string }).url,
gatewayUrl: parsedEnv.GATEWAY_URL,
aclContractAddress: parsedEnv.ACL_CONTRACT_ADDRESS,
kmsContractAddress: parsedEnv.KMS_VERIFIER_CONTRACT_ADDRESS,
publicKeyId: parsedEnv.PUBLIC_KEY_ID,
});
const instance = await createInstance({
networkUrl: "https://eth-sepolia.public.blastapi.io",
gatewayUrl: "https://gateway.sepolia.zama.ai",
kmsContractAddress: "0x904Af2B61068f686838bD6257E385C2cE7a09195",
aclContractAddress: "0x9479B455904dCccCf8Bc4f7dF8e9A1105cBa2A8e",
});
```

Using `window.ethereum` object:

```javascript
import { FhevmInstance, createInstance as createFhevmInstance } from "fhevmjs/node";
import { FhevmInstance, createInstance } from "fhevmjs/bundle";

const instance = await createFhevmInstance({
const instance = await createInstance({
network: window.ethereum,
gatewayUrl: parsedEnv.GATEWAY_URL,
aclContractAddress: parsedEnv.ACL_CONTRACT_ADDRESS,
kmsContractAddress: parsedEnv.KMS_VERIFIER_CONTRACT_ADDRESS,
publicKeyId: parsedEnv.PUBLIC_KEY_ID,
gatewayUrl: "https://gateway.sepolia.zama.ai",
kmsContractAddress: "0x904Af2B61068f686838bD6257E385C2cE7a09195",
aclContractAddress: "0x9479B455904dCccCf8Bc4f7dF8e9A1105cBa2A8e",
});
```

Expand Down
Loading