-
Notifications
You must be signed in to change notification settings - Fork 76
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
1 parent
57f8a2d
commit 05a8005
Showing
25 changed files
with
318 additions
and
161 deletions.
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
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,24 +1,42 @@ | ||
# Table of contents | ||
|
||
* [What is Zama's fhEVM?](README.md) | ||
- [What is Zama's fhEVM?](README.md) | ||
|
||
## Contract | ||
## Zama's fhEVM network | ||
|
||
* [Getting Started](solidity/getting\_started.md) | ||
* [TFHE Library](solidity/library.md) | ||
* [Function specifications](solidity/functions.md) | ||
* [Decryption and control structures](solidity/decryption.md) | ||
- Connect wallet | ||
- [Metamask](getting_started/wallet/metamask.md) | ||
- Faucet | ||
- [Zama Devnet](getting_started/faucet/devnet.md) | ||
- [Local node](getting_started/faucet/local.md) | ||
|
||
## fhevmjs | ||
## Writing contract | ||
|
||
* [Getting Started](sdk/getting\_started.md) | ||
* [Using the CLI](sdk/cli.md) | ||
* [Setup an instance](sdk/instance.md) | ||
* [Inputs](sdk/inputs.md) | ||
* [Reencryption](sdk/reencryption.md) | ||
* [Examples](sdk/examples.md) | ||
- [Getting started](getting_started.md) | ||
- [Remix](solidity/getting_started/remix.md) | ||
- [Hardhat](solidity/getting_started/hardhat.md) | ||
- [Docker](solidity/getting_started/docker.md) | ||
- [TFHE Library](solidity/library.md) | ||
- [Function specifications](solidity/functions.md) | ||
- [Decryption and control structures](solidity/decryption.md) | ||
- [Examples](solidity/examples.md) | ||
|
||
## Client | ||
|
||
- [Getting started](client/getting_started.md) | ||
- [Node](client/getting_started/node.md) | ||
- [Browser](client/getting_started/browser.md) | ||
- [Template](client/getting_started/template.md) | ||
- [CLI](client/getting_started/cli.md) | ||
- [Setup an instance](client/instance.md) | ||
- [Inputs](client/inputs.md) | ||
- [Reencryption](client/reencryption.md) | ||
- [Examples](client/examples.md) | ||
- [Transfer tokens (node)](client/examples/transfererc20.md) | ||
- [Get balance (node)](client/examples/getbalance.md) | ||
|
||
## Resources | ||
|
||
* [Tutorials](resources/tutorials.md) | ||
* [Examples](resources/examples.md) | ||
- [Whitepaper](resources/whitepaper.md) | ||
- [Tutorials](resources/tutorials.md) | ||
- [Repositories](resources/repositories.md) |
File renamed without changes.
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,66 @@ | ||
# Get Balance (Node) | ||
|
||
```javascript | ||
const { createInstance } = require("fhevmjs"); | ||
const { Wallet, JsonRpcProvider, Contract } = require("ethers"); | ||
|
||
const contractInfo = require("./EncryptedERC20.json"); | ||
|
||
const CONTRACT_ADDRESS = "0x309cf2aae85ad8a1db70ca88cfd4225bf17a7482"; | ||
|
||
const provider = new JsonRpcProvider(`https://devnet.zama.ai/`); | ||
|
||
const signer = new Wallet("0x92293977156de6e03b20b26708cb4496b523116190b5c32d77cee8286d0c41f6", provider); | ||
|
||
let _instance; | ||
|
||
const getInstance = async () => { | ||
if (_instance) return _instance; | ||
|
||
// 1. Get chain id | ||
const network = await provider.getNetwork(); | ||
|
||
const chainId = +network.chainId.toString(); | ||
|
||
// Get blockchain public key | ||
const publicKey = await provider.call({ | ||
to: "0x0000000000000000000000000000000000000044", | ||
}); | ||
|
||
// Create instance | ||
_instance = createInstance({ chainId, publicKey }); | ||
return _instance; | ||
}; | ||
|
||
const getBalance = async () => { | ||
// Initialize contract with ethers | ||
const contract = new Contract(CONTRACT_ADDRESS, contractInfo.abi, signer); | ||
|
||
// Get instance to encrypt amount parameter | ||
const instance = await getInstance(); | ||
|
||
// Generate token to decrypt | ||
const generatedToken = instance.generateToken({ | ||
verifyingContract: CONTRACT_ADDRESS, | ||
}); | ||
|
||
// Sign the public key | ||
const signature = await signer.signTypedData( | ||
generatedToken.token.domain, | ||
{ Reencrypt: generatedToken.token.types.Reencrypt }, // Need to remove EIP712Domain from types | ||
generatedToken.token.message, | ||
); | ||
instance.setTokenSignature(CONTRACT_ADDRESS, signature); | ||
|
||
// Call the method | ||
const encryptedBalance = await contract.balanceOf(generatedToken.publicKey, signature); | ||
|
||
// Decrypt the balance | ||
const balance = instance.decrypt(CONTRACT_ADDRESS, encryptedBalance); | ||
return balance; | ||
}; | ||
|
||
getBalance().then((balance) => { | ||
console.log(balance); | ||
}); | ||
``` |
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,46 @@ | ||
# Transfer tokens (Node) | ||
|
||
```javascript | ||
const { createInstance } = require("fhevmjs"); | ||
const { Wallet, JsonRpcProvider, Contract } = require("ethers"); | ||
|
||
const contractInfo = require("./EncryptedERC20.json"); | ||
|
||
const CONTRACT_ADDRESS = "0x309cf2aae85ad8a1db70ca88cfd4225bf17a7482"; | ||
|
||
const provider = new JsonRpcProvider(`https://devnet.zama.ai/`); | ||
|
||
const signer = new Wallet("0x92293977156de6e03b20b26708cb4496b523116190b5c32d77cee8286d0c41f6", provider); | ||
|
||
let _instance; | ||
|
||
const getInstance = async () => { | ||
if (_instance) return _instance; | ||
|
||
// 1. Get chain id | ||
const network = await provider.getNetwork(); | ||
const chainId = +network.chainId.toString(); // Need to be a number | ||
|
||
// Get blockchain public key | ||
const publicKey = await provider.call({ | ||
to: "0x0000000000000000000000000000000000000044", | ||
}); | ||
|
||
// Create instance | ||
_instance = createInstance({ chainId, publicKey }); | ||
return _instance; | ||
}; | ||
|
||
const transfer = async (to, amount) => { | ||
// Initialize contract with ethers | ||
const contract = new Contract(CONTRACT_ADDRESS, contractInfo.abi, signer); | ||
// Get instance to encrypt amount parameter | ||
const instance = await getInstance(); | ||
const encryptedAmount = instance.encrypt32(amount); | ||
|
||
const transaction = await contract["transfer(address,bytes)"](to, encryptedAmount); | ||
return transaction; | ||
}; | ||
|
||
transfer("0xa83a498Eee26f9594E3A784f204e507a5Fae3210", 10); | ||
``` |
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,20 @@ | ||
# Getting Started | ||
|
||
Welcome to the documentation for fhevmjs, a JavaScript library that enables interaction with blockchain using Zama's technology! This comprehensive guide provides developers with detailed information on encryption of data using TFHE (Fully Homomorphic Encryption over the Torus) and generation of EIP-719 tokens for reencrypt data. | ||
|
||
## Installation | ||
|
||
To get started with fhevmjs, you need to install it as a dependency in your JavaScript project. You can do this using npm, Yarn or pnpm. Open your terminal and navigate to your project's directory, then run one of the following commands: | ||
|
||
```bash | ||
# Using npm | ||
npm install fhevmjs | ||
|
||
# Using Yarn | ||
yarn add fhevmjs | ||
|
||
# Using pnpm | ||
pnpm add fhevmjs | ||
``` | ||
|
||
This will download and install the fhevmjs library and its dependencies into your project. |
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,65 @@ | ||
# Browser | ||
|
||
To use the library in your project, you need to load WASM of [TFHE](https://www.npmjs.com/package/tfhe) first with `initFhevm`. | ||
|
||
```javascript | ||
import { BrowserProvider } from "ethers"; | ||
import { initFhevm, createInstance } from "fhevmjs"; | ||
|
||
const createFhevmInstance = async () => { | ||
const provider = new BrowserProvider(window.ethereum); | ||
const network = await provider.getNetwork(); | ||
const chainId = +network.chainId.toString(); | ||
const publicKey = await provider.call({ | ||
from: null, | ||
to: "0x0000000000000000000000000000000000000044", | ||
}); | ||
return createInstance({ chainId, publicKey }); | ||
}; | ||
|
||
const init = async () => { | ||
await initFhevm(); // Load TFHE | ||
return createFhevmInstance(); | ||
}; | ||
|
||
init().then((instance) => { | ||
console.log(instance); | ||
}); | ||
``` | ||
|
||
## Troubleshooting | ||
|
||
### Webpack: "Module not found: Error: Can't resolve 'tfhe_bg.wasm'" | ||
|
||
In the codebase, there is a `new URL('tfhe_bg.wasm')` which triggers a resolve by Webpack. If yo u encounter an issue, you can add a fallback for this file by adding a resolve configuration in y our `webpack.config.js`: | ||
|
||
```javascript | ||
resolve: { | ||
fallback: { | ||
'tfhe_bg.wasm': require.resolve('tfhe/tfhe_bg.wasm'), | ||
}, | ||
}, | ||
``` | ||
|
||
### Issue with importing ESM version | ||
|
||
With a bundler such as Webpack or Rollup, imports will be replaced with the version mentioned in the `"browser"` field of the `package.json`. If you encounter issue with typing, you can use this [tsconfig.json](https://github.com/zama-ai/fhevmjs-react-template/blob/main/tsconfig.json) using TypeScript 5. | ||
|
||
If you encounter any other issue, you can force import of the browser package. | ||
|
||
```javascript | ||
import { initFhevm, createInstance } from "fhevmjs/web"; | ||
``` | ||
|
||
### Use bundled version | ||
|
||
If you have an issue with bundling the library (for example with some SSR framework), you can use the prebundled version available in `fhevmjs/bundle`. Just embed the library with a `<script>` tag and you're good to go. | ||
|
||
```javascript | ||
const start = async () => { | ||
await window.fhevm.initFhevm(); // load wasm needed | ||
const instance = window.fhevm.createInstance({ chainId, publicKey }).then((instance) => { | ||
console.log(instance); | ||
}); | ||
}; | ||
``` |
File renamed without changes.
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,8 @@ | ||
# Node | ||
|
||
```javascript | ||
const { createInstance } = require("fhevmjs"); | ||
createInstance({ chainId, publicKey }).then((instance) => { | ||
console.log(instance); | ||
}); | ||
``` |
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 @@ | ||
# Template | ||
|
||
## React | ||
|
||
You can use [this template](https://github.com/zama-ai/fhevmjs-react-template) to start an application with fhevmjs, using Vite + React + Typescript. |
File renamed without changes.
File renamed without changes.
File renamed without changes.
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,3 @@ | ||
# Zama Devnet | ||
|
||
You get 10 Zama token on [https://faucet.zama.ai/](https://faucet.zama.ai/) |
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,7 @@ | ||
# Local node | ||
|
||
If you need to get coins for a specific wallet, you can use the faucet as follow: | ||
|
||
```bash | ||
docker exec -i fhevm faucet 0xa5e1defb98EFe38EBb2D958CEe052410247F4c80 | ||
``` |
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,36 @@ | ||
# Metamask | ||
|
||
Here are the main steps from the [official guide](https://support.metamask.io/hc/en-us/articles/360043227612-How-to-add-a-custom-network-RPC) provided by Metamask: | ||
|
||
<figure><img src=".gitbook/assets/metamask_add_network.gif" alt=""><figcaption> | ||
1) From the homepage of your wallet, click on the network selector in the top left, and then on 'Add network' | ||
</figcaption></figure> | ||
|
||
<figure><img src=".gitbook/assets/metamask_add_network2.gif" alt=""><figcaption> | ||
2) MetaMask will open in a new tab in fullscreen mode. From here, find and the 'Add network manually' button at the bottom of the network list.</figcaption></figure> | ||
|
||
Add these informations to access to blockchain | ||
{% tabs %} | ||
{% tab title="Zama devnet" %} | ||
|
||
| Fields | Value | | ||
| ----------------------------- | ----------------------------- | | ||
| Network Name | Zama Network | | ||
| New RPC URL | https://devnet.zama.ai | | ||
| Chain ID | 8009 | | ||
| Currency symbol | ZAMA | | ||
| Block explorer URL (Optional) | https://main.explorer.zama.ai | | ||
|
||
{% endtab %} | ||
{% tab title="Local devnet" %} | ||
|
||
| Fields | Value | | ||
| ----------------------------- | ---------------------- | | ||
| Network Name | Zama Local | | ||
| New RPC URL | http://localhost:8545/ | | ||
| Chain ID | 9000 | | ||
| Currency symbol | ZAMA | | ||
| Block explorer URL (Optional) | | | ||
|
||
{% endtab %} | ||
{% endtabs %} |
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,4 @@ | ||
# Repositories | ||
|
||
- [Solidity library](https://github.com/zama-ai/fhevm/) | ||
- [fhEVM on evmos](https://github.com/zama-ai/fhevm-evmos/) |
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,3 @@ | ||
# Whitepaper | ||
|
||
<iframe src="https://raw.githubusercontent.com/zama-ai/fhevm/main/fhevm-whitepaper.pdf" width="100%" height="500px"> </iframe> |
Oops, something went wrong.