@pyratzlabs/react-fhevm-utils 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,6 @@
1
+ export declare const config: {
2
+ ACL_ADDRESS: string;
3
+ KMS_ADDRESS: string;
4
+ GATEWAY_URL: string;
5
+ RPC_URL: string;
6
+ };
package/dist/config.js ADDED
@@ -0,0 +1,6 @@
1
+ export const config = {
2
+ ACL_ADDRESS: "0xFee8407e2f5e3Ee68ad77cAE98c434e637f516e5",
3
+ KMS_ADDRESS: "0x9D6891A6240D6130c54ae243d8005063D05fE14b",
4
+ GATEWAY_URL: "https://gateway.sepolia.zama.ai/",
5
+ RPC_URL: "https://eth-sepolia.g.alchemy.com/v2/z7ESc-2NGy8mOHByrhAENi2XbZHbW6dl",
6
+ };
@@ -0,0 +1,14 @@
1
+ import React, { ReactNode } from "react";
2
+ import { FhevmInstance } from "fhevmjs";
3
+ import { FhevmConfig } from "../types/fhevmConfig";
4
+ interface ProviderProps {
5
+ children: ReactNode;
6
+ config?: FhevmConfig;
7
+ }
8
+ interface ContextProps {
9
+ instance?: FhevmInstance;
10
+ }
11
+ export declare const FhevmContext: React.Context<ContextProps>;
12
+ declare const FhevmProvider: ({ children, config }: ProviderProps) => import("react/jsx-runtime").JSX.Element;
13
+ export declare const useFhevmContext: () => ContextProps;
14
+ export default FhevmProvider;
@@ -0,0 +1,33 @@
1
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
2
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
3
+ return new (P || (P = Promise))(function (resolve, reject) {
4
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
5
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
6
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
7
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
8
+ });
9
+ };
10
+ import { jsx as _jsx } from "react/jsx-runtime";
11
+ import { createContext, useContext, useEffect, useState, } from "react";
12
+ import { getOrCreateFhevmInstance } from "../lib/fhevm";
13
+ export const FhevmContext = createContext({
14
+ instance: undefined,
15
+ });
16
+ const FhevmProvider = ({ children, config }) => {
17
+ const [instance, setInstance] = useState();
18
+ const initFhevm = () => __awaiter(void 0, void 0, void 0, function* () {
19
+ try {
20
+ const instance = yield getOrCreateFhevmInstance(config);
21
+ setInstance(instance);
22
+ }
23
+ catch (error) {
24
+ throw new Error(`Failed to initialize FHEVM: ${error}`);
25
+ }
26
+ });
27
+ useEffect(() => {
28
+ initFhevm();
29
+ }, []);
30
+ return (_jsx(FhevmContext.Provider, { value: { instance }, children: children }));
31
+ };
32
+ export const useFhevmContext = () => useContext(FhevmContext);
33
+ export default FhevmProvider;
@@ -0,0 +1,73 @@
1
+ import { Address } from "viem";
2
+ export interface DecryptBalanceParams {
3
+ tokenAddress: Address;
4
+ }
5
+ export declare const useDecryptBalance: ({ tokenAddress }: DecryptBalanceParams) => {
6
+ data: undefined;
7
+ variables: undefined;
8
+ error: null;
9
+ isError: false;
10
+ isIdle: true;
11
+ isPending: false;
12
+ isSuccess: false;
13
+ status: "idle";
14
+ mutate: import("@tanstack/react-query").UseMutateFunction<number, Error, bigint, unknown>;
15
+ reset: () => void;
16
+ context: unknown;
17
+ failureCount: number;
18
+ failureReason: Error | null;
19
+ isPaused: boolean;
20
+ submittedAt: number;
21
+ mutateAsync: import("@tanstack/react-query").UseMutateAsyncFunction<number, Error, bigint, unknown>;
22
+ } | {
23
+ data: undefined;
24
+ variables: bigint;
25
+ error: null;
26
+ isError: false;
27
+ isIdle: false;
28
+ isPending: true;
29
+ isSuccess: false;
30
+ status: "pending";
31
+ mutate: import("@tanstack/react-query").UseMutateFunction<number, Error, bigint, unknown>;
32
+ reset: () => void;
33
+ context: unknown;
34
+ failureCount: number;
35
+ failureReason: Error | null;
36
+ isPaused: boolean;
37
+ submittedAt: number;
38
+ mutateAsync: import("@tanstack/react-query").UseMutateAsyncFunction<number, Error, bigint, unknown>;
39
+ } | {
40
+ data: undefined;
41
+ error: Error;
42
+ variables: bigint;
43
+ isError: true;
44
+ isIdle: false;
45
+ isPending: false;
46
+ isSuccess: false;
47
+ status: "error";
48
+ mutate: import("@tanstack/react-query").UseMutateFunction<number, Error, bigint, unknown>;
49
+ reset: () => void;
50
+ context: unknown;
51
+ failureCount: number;
52
+ failureReason: Error | null;
53
+ isPaused: boolean;
54
+ submittedAt: number;
55
+ mutateAsync: import("@tanstack/react-query").UseMutateAsyncFunction<number, Error, bigint, unknown>;
56
+ } | {
57
+ data: number;
58
+ error: null;
59
+ variables: bigint;
60
+ isError: false;
61
+ isIdle: false;
62
+ isPending: false;
63
+ isSuccess: true;
64
+ status: "success";
65
+ mutate: import("@tanstack/react-query").UseMutateFunction<number, Error, bigint, unknown>;
66
+ reset: () => void;
67
+ context: unknown;
68
+ failureCount: number;
69
+ failureReason: Error | null;
70
+ isPaused: boolean;
71
+ submittedAt: number;
72
+ mutateAsync: import("@tanstack/react-query").UseMutateAsyncFunction<number, Error, bigint, unknown>;
73
+ };
@@ -0,0 +1,36 @@
1
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
2
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
3
+ return new (P || (P = Promise))(function (resolve, reject) {
4
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
5
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
6
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
7
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
8
+ });
9
+ };
10
+ import { useMutation } from "@tanstack/react-query";
11
+ import { useAccount, useSignTypedData } from "wagmi";
12
+ import { useFhevmContext } from "../context/FhevmProvider";
13
+ export const useDecryptBalance = ({ tokenAddress }) => {
14
+ const { instance } = useFhevmContext();
15
+ const { address } = useAccount();
16
+ const { signTypedDataAsync } = useSignTypedData();
17
+ const decryptMutation = useMutation({
18
+ mutationFn: (encryptedBalance) => __awaiter(void 0, void 0, void 0, function* () {
19
+ if (!instance)
20
+ throw new Error("Instance is not defined");
21
+ if (!address)
22
+ throw new Error("User wallet is not connected");
23
+ const { publicKey, privateKey } = instance.generateKeypair();
24
+ const eip712 = instance.createEIP712(publicKey, tokenAddress);
25
+ const signature = yield signTypedDataAsync({
26
+ domain: eip712.domain,
27
+ types: { Reencrypt: eip712.types.Reencrypt },
28
+ primaryType: "Reencrypt",
29
+ message: eip712.message,
30
+ });
31
+ const decryptedBalance = yield instance.reencrypt(encryptedBalance, privateKey, publicKey, signature.replace("0x", ""), tokenAddress, address);
32
+ return Number(decryptedBalance);
33
+ }),
34
+ });
35
+ return Object.assign({}, decryptMutation);
36
+ };
@@ -0,0 +1,3 @@
1
+ export declare const useFhevmInstance: () => {
2
+ instance: import("fhevmjs").FhevmInstance | undefined;
3
+ };
@@ -0,0 +1,5 @@
1
+ import { useFhevmContext } from "../context/FhevmProvider";
2
+ export const useFhevmInstance = () => {
3
+ const { instance } = useFhevmContext();
4
+ return { instance };
5
+ };
@@ -0,0 +1,3 @@
1
+ export { default } from "./context/FhevmProvider";
2
+ export { useFhevmInstance } from "./hooks/useFhevmInstance";
3
+ export { useDecryptBalance } from "./hooks/useDecryptBalance";
package/dist/index.js ADDED
@@ -0,0 +1,5 @@
1
+ // context
2
+ export { default } from "./context/FhevmProvider";
3
+ // hooks
4
+ export { useFhevmInstance } from "./hooks/useFhevmInstance";
5
+ export { useDecryptBalance } from "./hooks/useDecryptBalance";
@@ -0,0 +1,5 @@
1
+ import { FhevmInstance } from "fhevmjs";
2
+ import { FhevmConfig } from "../types/fhevmConfig";
3
+ export declare const createFhevmInstance: (config?: FhevmConfig) => Promise<FhevmInstance>;
4
+ export declare const getOrCreateFhevmInstance: (config?: FhevmConfig) => Promise<FhevmInstance>;
5
+ export declare const getInstance: () => FhevmInstance | null;
@@ -0,0 +1,38 @@
1
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
2
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
3
+ return new (P || (P = Promise))(function (resolve, reject) {
4
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
5
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
6
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
7
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
8
+ });
9
+ };
10
+ import { createInstance, initFhevm } from "fhevmjs";
11
+ import { config } from "../config";
12
+ import { sepolia } from "viem/chains";
13
+ const { ACL_ADDRESS, KMS_ADDRESS, GATEWAY_URL, RPC_URL } = config;
14
+ let fhevmInstance = null;
15
+ export const createFhevmInstance = (config) => __awaiter(void 0, void 0, void 0, function* () {
16
+ var _a, _b;
17
+ if (!fhevmInstance) {
18
+ yield initFhevm();
19
+ fhevmInstance = yield createInstance({
20
+ chainId: (_a = config === null || config === void 0 ? void 0 : config.chainId) !== null && _a !== void 0 ? _a : sepolia.id,
21
+ networkUrl: (_b = config === null || config === void 0 ? void 0 : config.networkUrl) !== null && _b !== void 0 ? _b : RPC_URL,
22
+ network: config === null || config === void 0 ? void 0 : config.network,
23
+ gatewayUrl: GATEWAY_URL,
24
+ aclContractAddress: ACL_ADDRESS,
25
+ kmsContractAddress: KMS_ADDRESS,
26
+ });
27
+ }
28
+ return fhevmInstance;
29
+ });
30
+ export const getOrCreateFhevmInstance = (config) => __awaiter(void 0, void 0, void 0, function* () {
31
+ if (!fhevmInstance) {
32
+ fhevmInstance = yield createFhevmInstance(config);
33
+ }
34
+ return fhevmInstance;
35
+ });
36
+ export const getInstance = () => {
37
+ return fhevmInstance;
38
+ };
@@ -0,0 +1,6 @@
1
+ import { EIP1193Provider } from "viem";
2
+ export interface FhevmConfig {
3
+ chainId?: number;
4
+ network?: EIP1193Provider;
5
+ networkUrl?: string;
6
+ }
@@ -0,0 +1 @@
1
+ export {};
package/package.json ADDED
@@ -0,0 +1,39 @@
1
+ {
2
+ "name": "@pyratzlabs/react-fhevm-utils",
3
+ "version": "0.4.0",
4
+ "description": "React hooks and utilities for Fhevmjs",
5
+ "main": "dist/index.js",
6
+ "files": [
7
+ "dist"
8
+ ],
9
+ "exports": {
10
+ ".": {
11
+ "types": "./dist/index.d.ts",
12
+ "default": "./dist/index.js"
13
+ }
14
+ },
15
+ "scripts": {
16
+ "build": "tsc",
17
+ "test": "npm run test",
18
+ "clean": "rm -rf dist",
19
+ "preversion": "npm run build",
20
+ "version": "npm publish --access=public"
21
+ },
22
+ "keywords": [
23
+ "fhevm"
24
+ ],
25
+ "author": "Pyratzlabs",
26
+ "license": "MIT",
27
+ "devDependencies": {
28
+ "@types/react": "^19.1.0",
29
+ "ts-node": "^10.9.2",
30
+ "typescript": "^5.8.2"
31
+ },
32
+ "dependencies": {
33
+ "@tanstack/react-query": "^5.71.5",
34
+ "fhevmjs": "^0.6.2",
35
+ "react": "^19.1.0",
36
+ "viem": "^2.25.0",
37
+ "wagmi": "^2.14.16"
38
+ }
39
+ }