-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
*use react.context to lift engine and utils state.
- Loading branch information
1 parent
80dc948
commit 61fd307
Showing
9 changed files
with
121 additions
and
88 deletions.
There are no files selected for viewing
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,50 @@ | ||
// Initialize the engine and util imports in the SharedResourcesProvider component: | ||
// Note: must import Uitls as a module instead of a component for engine to work (or else you will get 'self' is undefined wasm errors) | ||
'use client' | ||
import React, { createContext, useContext, useState, useEffect } from 'react'; | ||
type Utils = typeof import("./Utils") | ||
type Engine = typeof import("@ezkljs/engine/web/ezkl") | ||
|
||
interface SharedResources { | ||
engine: Engine; | ||
utils: Utils; | ||
} | ||
|
||
const SharedResourcesContext = createContext<SharedResources | null>(null); | ||
export const useSharedResources = (): SharedResources => { | ||
const context = useContext(SharedResourcesContext); | ||
if (!context) { | ||
throw new Error('useSharedResources must be used within a SharedResourcesProvider'); | ||
} | ||
return context; | ||
}; | ||
|
||
interface SharedResourcesProviderProps { | ||
children: React.ReactNode; | ||
} | ||
|
||
export const SharedResourcesProvider: React.FC<SharedResourcesProviderProps> = ({ children }) => { | ||
const [engine, setEngine] = useState<any>(null); // Replace 'any' with the actual type of 'engine' | ||
const [utils, setUtils] = useState<any>(null); // Replace 'any' with the actual type of 'utils' | ||
|
||
useEffect(() => { | ||
async function initializeResources() { | ||
// Initialize the WASM module | ||
const engine = await import("@ezkljs/engine/web/ezkl"); | ||
setEngine(engine) | ||
await engine.default(undefined, new WebAssembly.Memory({ initial: 20, maximum: 4096, shared: true })) | ||
// For human readable wasm debug errors call this function | ||
engine.init_panic_hook() | ||
// Initialize the utils module | ||
const utils = await import("./Utils"); | ||
setUtils(utils) | ||
} | ||
initializeResources(); | ||
}, []); | ||
|
||
return ( | ||
<SharedResourcesContext.Provider value={{ engine, utils }}> | ||
{children} | ||
</SharedResourcesContext.Provider> | ||
); | ||
}; |
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
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,28 @@ | ||
// Example for pages/Page1.js | ||
'use client' | ||
import { | ||
TextInput, | ||
Label, | ||
Button, | ||
Alert, | ||
Spinner as _Spinner, | ||
} from 'flowbite-react' | ||
import React, { useState } from 'react' | ||
// import { formDataSchemaEncrypt, formDataSchemaDecrypt } from './parsers' | ||
import { useSharedResources } from '../EngineContext'; | ||
import { stringify } from "json-bigint"; | ||
|
||
type Utils = typeof import("../Utils") | ||
type Engine = typeof import("@ezkljs/engine/web/ezkl") | ||
|
||
type Cipher = number[][][] | ||
type DecryptedCipher = number[][] | ||
|
||
export default function FeltUtils() { | ||
const { engine, utils } = useSharedResources(); | ||
|
||
return ( | ||
<div className='flex flex-col justify-center items-center h-5/6 pb-20'> | ||
</div> | ||
); | ||
} |
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,6 @@ | ||
import { z } from 'zod' | ||
|
||
const fileSchema = z.custom<File | null>((value) => { | ||
if (value === null) return false | ||
return value instanceof File && value.name.trim() !== '' | ||
}, "File name can't be empty") |
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
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
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
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
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