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 bundle #6

Merged
merged 3 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
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
5 changes: 3 additions & 2 deletions index.html
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
<!doctype html>
<!DOCTYPE html>
<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>Vite + Vue + TS</title>
<title>fhEVM Vue demo</title>
</head>
<body>
<div id="app"></div>
Expand Down
50 changes: 33 additions & 17 deletions package-lock.json

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

11 changes: 7 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,22 @@
"version": "0.0.0",
"type": "module",
"scripts": {
"dev": "node scripts/dev.js",
"dev": "vite",
"build": "vue-tsc && vite build",
"preview": "vite preview"
"preview": "vite preview",
"prettier": "prettier --write \"**/*.{js,json,md,sol,ts,yml}\"",
"prettier:check": "prettier --check \"**/*.{js,json,md,sol,ts,yml}\""
},
"dependencies": {
"ethers": "^6.13.4",
"fhevmjs": "^0.6.0",
"fhevmjs": "^0.6.2",
"idb": "^8.0.0",
"typescript": "5.6.2",
"vue": "^3.5.13"
},
"devDependencies": {
"@vitejs/plugin-vue": "^5.2.1",
"typescript": "^5.7.2",
"prettier": "^3.4.2",
"vite": "^5.4.11",
"vite-plugin-node-polyfills": "^0.22.0",
"vue-tsc": "^2.1.10"
Expand Down
14 changes: 7 additions & 7 deletions scripts/dev.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
import { build, preview } from 'vite';
import { build, preview } from "vite";

(async () => {
try {
await preview({
BASE_URL: '/',
MODE: 'production',
BASE_URL: "/",
MODE: "production",
DEV: false,
PROD: true,
});
console.log('Preview server is running...');
console.log('Listening on http://localhost:4173\n');
console.log("Preview server is running...");
console.log("Listening on http://localhost:4173\n");

const watcher = await build({
build: {
watch: {},
},
});
console.log('Build in watch mode is running...');
console.log("Build in watch mode is running...");
} catch (err) {
console.error('Error:', err);
console.error("Error:", err);
}
})();
2 changes: 0 additions & 2 deletions src/components/Connect.vue
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ const refreshNetwork = async () => {
loading.value = true;
await createFhevmInstance();
loading.value = false;
} else {
setValidNetwork(false);
}
};

Expand Down
7 changes: 3 additions & 4 deletions src/components/Devnet.vue
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,12 @@ import { getInstance } from '../fhevmjs';

const toHexString = (bytes: Uint8Array) => bytes.reduce((str, byte) => str + byte.toString(16).padStart(2, '0'), '');

const handles = ref(null);
const inputProof = ref(null);
const handles = ref<Uint8Array[] | null>(null);
const inputProof = ref<Uint8Array | null>(null);

const instance = getInstance();

const encrypt = async (val: number) => {
const now = Date.now();
const encrypt = async () => {
const enc = await instance
.createEncryptedInput(
getAddress('0x309cf2aae85ad8a1db70ca88cfd4225bf17a7456'),
Expand Down
42 changes: 24 additions & 18 deletions src/fhevmStorage.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { openDB, DBSchema, IDBPDatabase } from 'idb';
import { openDB, DBSchema, IDBPDatabase } from "idb";

interface PublicParamsDB extends DBSchema {
publicKeyStore: {
Expand All @@ -17,32 +17,36 @@ interface PublicParamsDB extends DBSchema {
};
}

const dbPromise: Promise<IDBPDatabase<PublicParamsDB>> = openDB<PublicParamsDB>('fhevm', 1, {
upgrade(db) {
if (!db.objectStoreNames.contains('paramsStore')) {
db.createObjectStore('paramsStore', { keyPath: 'acl' });
}
if (!db.objectStoreNames.contains('publicKeyStore')) {
db.createObjectStore('publicKeyStore', { keyPath: 'acl' });
}
const dbPromise: Promise<IDBPDatabase<PublicParamsDB>> = openDB<PublicParamsDB>(
"fhevm",
1,
{
upgrade(db) {
if (!db.objectStoreNames.contains("paramsStore")) {
db.createObjectStore("paramsStore", { keyPath: "acl" });
}
if (!db.objectStoreNames.contains("publicKeyStore")) {
db.createObjectStore("publicKeyStore", { keyPath: "acl" });
}
},
},
});
);

export async function storePublicParams(
acl: string,
value: { publicParamsId: string; publicParams: Uint8Array }
value: { publicParamsId: string; publicParams: Uint8Array },
): Promise<void> {
const db = await dbPromise;
await db.put('paramsStore', { acl, value });
await db.put("paramsStore", { acl, value });
console.log(`Stored public params for: ${acl}`);
}

export async function getPublicParams(
acl: string
acl: string,
): Promise<{ publicParamsId: string; publicParams: Uint8Array } | null> {
const db = await dbPromise;
try {
const result = await db.get('paramsStore', acl);
const result = await db.get("paramsStore", acl);
return result ? result.value : null;
} catch (e) {
return null;
Expand All @@ -51,17 +55,19 @@ export async function getPublicParams(

export async function storePublicKey(
acl: string,
value: { publicKeyId: string; publicKey: Uint8Array }
value: { publicKeyId: string; publicKey: Uint8Array },
): Promise<void> {
const db = await dbPromise;
await db.put('publicKeyStore', { acl, value });
await db.put("publicKeyStore", { acl, value });
console.log(`Stored public key for: ${acl}`);
}

export async function getPublicKey(acl: string): Promise<{ publicKeyId: string; publicKey: Uint8Array } | null> {
export async function getPublicKey(
acl: string,
): Promise<{ publicKeyId: string; publicKey: Uint8Array } | null> {
const db = await dbPromise;
try {
const result = await db.get('publicKeyStore', acl);
const result = await db.get("publicKeyStore", acl);
return result ? result.value : null;
} catch (e) {
return null;
Expand Down
32 changes: 22 additions & 10 deletions src/fhevmjs.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import { isAddress } from 'ethers';
import { initFhevm, createInstance, FhevmInstance } from 'fhevmjs';
import { getPublicKey, getPublicParams, storePublicKey, storePublicParams } from './fhevmStorage';
import { isAddress } from "ethers";
import { initFhevm, createInstance, FhevmInstance } from "fhevmjs/bundle";
import {
getPublicKey,
getPublicParams,
storePublicKey,
storePublicParams,
} from "./fhevmStorage";

const ACL_ADDRESS: string = import.meta.env.VITE_ACL_ADDRESS;

Expand All @@ -16,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 All @@ -33,7 +36,7 @@ export const createFhevmInstance = async () => {
const storedPublicParams = await getPublicParams(ACL_ADDRESS);
const publicParams = storedPublicParams
? {
'2048': storedPublicParams,
"2048": storedPublicParams,
}
: null;
instancePromise = createInstance({
Expand All @@ -56,14 +59,23 @@ export const createFhevmInstance = async () => {
}
};

export const setKeypair = (contractAddress: string, userAddress: string, keypair: Keypair) => {
export const setKeypair = (
contractAddress: string,
userAddress: string,
keypair: Keypair,
) => {
if (!isAddress(contractAddress) || !isAddress(userAddress)) return;
keypairs[userAddress][contractAddress] = keypair;
};

export const getKeypair = (contractAddress: string, userAddress: string): Keypair | null => {
export const getKeypair = (
contractAddress: string,
userAddress: string,
): Keypair | null => {
if (!isAddress(contractAddress) || !isAddress(userAddress)) return null;
return keypairs[userAddress] ? keypairs[userAddress][contractAddress] || null : null;
return keypairs[userAddress]
? keypairs[userAddress][contractAddress] || null
: null;
};

export const getInstance = (): FhevmInstance => {
Expand Down
8 changes: 4 additions & 4 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { createApp } from 'vue'
import './style.css'
import App from './App.vue'
import { createApp } from "vue";
import "./style.css";
import App from "./App.vue";

createApp(App).mount('#app')
createApp(App).mount("#app");
Loading
Loading