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

feat: use CDN #14

Merged
merged 7 commits into from
Dec 16, 2024
Merged
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
6 changes: 5 additions & 1 deletion .eslintrc.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,12 @@ module.exports = {
},
plugins: ['react-refresh'],
rules: {
'react-refresh/only-export-components': ['warn', { allowConstantExport: true }],
'react-refresh/only-export-components': [
'warn',
{ allowConstantExport: true },
],
'@typescript-eslint/no-unsafe-call': 'off',
'@typescript-eslint/no-unsafe-argument': 'off',
'@typescript-eslint/no-explicit-any': 'off',
'@typescript-eslint/no-non-null-assertion': 'off',
'@typescript-eslint/no-unsafe-assignment': 'off',
Expand Down
25 changes: 25 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
name: Test

on:
pull_request:
branches:
- main
push:
branches:
- main
jobs:
build:
runs-on: ubuntu-latest

strategy:
matrix:
node-version: [20.x]
steps:
- uses: actions/checkout@v3
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v3
with:
node-version: ${{ matrix.node-version }}
- run: npm ci
- run: npm run prettier:check
- run: npm run build
4 changes: 1 addition & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,7 @@ Copy `.env.example` to `.env` and update the gateway URL, ACL address, and KMS a
npm run dev
```

The server listens on [http://localhost:4173/](http://localhost:4173/)

Note: HMR is currently broken because Vite does not handle WASM correctly. A workaround has been implemented as a script: running `npm run dev` will execute a build watch alongside Vite preview. If you have a solution for enabling the Vite dev server with HMR, feel free to open a pull request! :)
The server listens on [http://localhost:5173/](http://localhost:5173/)

## Build

Expand Down
4 changes: 4 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
<html lang="en">
<head>
<meta charset="UTF-8" />
<script
src="https://cdn.zama.ai/fhevmjs/0.6.2/fhevmjs.umd.cjs"
type="text/javascript"
></script>
<link rel="icon" type="image/svg+xml" href="/vite.svg" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>fhEVM React demo</title>
Expand Down
15 changes: 8 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"version": "0.0.0",
"type": "module",
"scripts": {
"dev": "node scripts/dev.js",
"dev": "vite dev",
"build": "tsc && vite build",
"lint": "eslint src --ext ts,tsx --report-unused-disable-directives --max-warnings 0",
"preview": "vite preview",
Expand All @@ -26,7 +26,7 @@
"eslint": "^8.57.0",
"eslint-plugin-react-hooks": "^4.6.2",
"eslint-plugin-react-refresh": "^0.4.7",
"fhevmjs": "^0.6.1",
"fhevmjs": "^0.6.2",
"prettier": "^3.4.1",
"typescript": "^5.4.5",
"vite": "^5.4.11",
Expand Down
23 changes: 0 additions & 23 deletions scripts/dev.js

This file was deleted.

18 changes: 15 additions & 3 deletions src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,33 @@ function App() {
const [isInitialized, setIsInitialized] = useState(false);

useEffect(() => {
// Trick to avoid double init with HMR
if (window.fhevmjsInitialized) return;
window.fhevmjsInitialized = true;
init()
.then(() => {
setIsInitialized(true);
})
.catch(() => setIsInitialized(false));
.catch((e) => {
console.log(e);
setIsInitialized(false);
});
}, []);

if (!isInitialized) return null;

return (
<>
<h1>fhevmjs</h1>
<Connect>{(account, provider) => <Devnet account={account} provider={provider} />}</Connect>
<Connect>
{(account, provider) => (
<Devnet account={account} provider={provider} />
)}
</Connect>
<p className="read-the-docs">
<a href="https://docs.zama.ai/fhevm">See the documentation for more information</a>
<a href="https://docs.zama.ai/fhevm">
See the documentation for more information
</a>
</p>
</>
);
Expand Down
8 changes: 6 additions & 2 deletions src/components/Connect/Connect.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ export const Connect: React.FC<{
};

const hasValidNetwork = async (): Promise<boolean> => {
const currentChainId: string = await window.ethereum.request({ method: 'eth_chainId' });
const currentChainId: string = await window.ethereum.request({
method: 'eth_chainId',
});
return AUTHORIZED_CHAIN_ID.includes(currentChainId.toLowerCase());
};

Expand Down Expand Up @@ -124,7 +126,9 @@ export const Connect: React.FC<{
const connectInfos = (
<div className="Connect__info">
{!connected && <button onClick={connect}>Connect your wallet</button>}
{connected && <div className="Connect__account">Connected with {account}</div>}
{connected && (
<div className="Connect__account">Connected with {account}</div>
)}
</div>
);

Expand Down
2 changes: 1 addition & 1 deletion src/components/Devnet/Devnet.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export type DevnetProps = {

const CONTRACT_ADDRESS = '0x309cf2aae85ad8a1db70ca88cfd4225bf17a7482';

export const Devnet = ({ account, provider }: DevnetProps) => {
export const Devnet = ({ account }: DevnetProps) => {
const [handles, setHandles] = useState<Uint8Array[]>([]);
const [encryption, setEncryption] = useState<Uint8Array>();
const [eip712, setEip712] =
Expand Down
6 changes: 2 additions & 4 deletions src/fhevmjs.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { isAddress } from 'ethers';
import { initFhevm, createInstance, FhevmInstance } from 'fhevmjs';
import { initFhevm, createInstance, FhevmInstance } from 'fhevmjs/bundle';
import {
getPublicKey,
getPublicParams,
Expand All @@ -21,9 +21,7 @@ type Keypairs = {
};
};

export const init = async () => {
await initFhevm({ thread: navigator.hardwareConcurrency });
};
export const init = initFhevm;

let instancePromise: Promise<FhevmInstance>;
let instance: FhevmInstance;
Expand Down
2 changes: 2 additions & 0 deletions src/vite-env.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,6 @@ interface Window {
ethereum: import('ethers').Eip1193Provider & {
on: (event: string, cb: (param: any) => any) => void;
};
fhevmjs: import('fhevmjs');
fhevmjsInitialized: boolean;
}
12 changes: 0 additions & 12 deletions vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,14 @@ import { defineConfig } from 'vite';
import react from '@vitejs/plugin-react';
import { nodePolyfills } from 'vite-plugin-node-polyfills';

const workerImportMetaUrlRE =
/\bnew\s+(?:Worker|SharedWorker)\s*\(\s*(new\s+URL\s*\(\s*('[^']+'|"[^"]+"|`[^`]+`)\s*,\s*import\.meta\.url\s*\))/g;

// https://vitejs.dev/config/
export default defineConfig({
assetsInclude: ['**/*.bin'],
plugins: [react(), nodePolyfills()],
server: {
port: 9000,
headers: {
'Cross-Origin-Opener-Policy': 'same-origin',
'Cross-Origin-Embedder-Policy': 'require-corp',
},
},
worker: {
format: 'es',
rollupOptions: {
output: {
entryFileNames: '[name].js',
},
},
},
});
Loading