@pyratzlabs/react-fhevm-utils 0.5.0 → 1.0.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.
@@ -7,6 +7,7 @@ interface ProviderProps {
7
7
  }
8
8
  interface ContextProps {
9
9
  instance?: FhevmInstance;
10
+ config?: FhevmConfig;
10
11
  }
11
12
  export declare const FhevmContext: React.Context<ContextProps>;
12
13
  declare const FhevmProvider: ({ children, config }: ProviderProps) => import("react/jsx-runtime").JSX.Element;
@@ -12,6 +12,7 @@ import { createContext, useContext, useEffect, useState, } from "react";
12
12
  import { getOrCreateFhevmInstance } from "../lib/fhevm";
13
13
  export const FhevmContext = createContext({
14
14
  instance: undefined,
15
+ config: undefined,
15
16
  });
16
17
  const FhevmProvider = ({ children, config }) => {
17
18
  const [instance, setInstance] = useState();
@@ -27,7 +28,7 @@ const FhevmProvider = ({ children, config }) => {
27
28
  useEffect(() => {
28
29
  initFhevm();
29
30
  }, []);
30
- return (_jsx(FhevmContext.Provider, { value: { instance }, children: children }));
31
+ return (_jsx(FhevmContext.Provider, { value: { instance, config }, children: children }));
31
32
  };
32
33
  export const useFhevmContext = () => useContext(FhevmContext);
33
34
  export default FhevmProvider;
@@ -0,0 +1,77 @@
1
+ import { Address } from "viem";
2
+ export interface EncryptValueParams {
3
+ tokenAddress: Address;
4
+ }
5
+ export interface EncryptionResult {
6
+ handles: Uint8Array[];
7
+ inputProof: Uint8Array;
8
+ }
9
+ export declare const useEncryptValue: ({ tokenAddress }: EncryptValueParams) => {
10
+ data: undefined;
11
+ variables: undefined;
12
+ error: null;
13
+ isError: false;
14
+ isIdle: true;
15
+ isPending: false;
16
+ isSuccess: false;
17
+ status: "idle";
18
+ mutate: import("@tanstack/react-query").UseMutateFunction<EncryptionResult, Error, bigint, unknown>;
19
+ reset: () => void;
20
+ context: unknown;
21
+ failureCount: number;
22
+ failureReason: Error | null;
23
+ isPaused: boolean;
24
+ submittedAt: number;
25
+ mutateAsync: import("@tanstack/react-query").UseMutateAsyncFunction<EncryptionResult, Error, bigint, unknown>;
26
+ } | {
27
+ data: undefined;
28
+ variables: bigint;
29
+ error: null;
30
+ isError: false;
31
+ isIdle: false;
32
+ isPending: true;
33
+ isSuccess: false;
34
+ status: "pending";
35
+ mutate: import("@tanstack/react-query").UseMutateFunction<EncryptionResult, Error, bigint, unknown>;
36
+ reset: () => void;
37
+ context: unknown;
38
+ failureCount: number;
39
+ failureReason: Error | null;
40
+ isPaused: boolean;
41
+ submittedAt: number;
42
+ mutateAsync: import("@tanstack/react-query").UseMutateAsyncFunction<EncryptionResult, Error, bigint, unknown>;
43
+ } | {
44
+ data: undefined;
45
+ error: Error;
46
+ variables: bigint;
47
+ isError: true;
48
+ isIdle: false;
49
+ isPending: false;
50
+ isSuccess: false;
51
+ status: "error";
52
+ mutate: import("@tanstack/react-query").UseMutateFunction<EncryptionResult, Error, bigint, unknown>;
53
+ reset: () => void;
54
+ context: unknown;
55
+ failureCount: number;
56
+ failureReason: Error | null;
57
+ isPaused: boolean;
58
+ submittedAt: number;
59
+ mutateAsync: import("@tanstack/react-query").UseMutateAsyncFunction<EncryptionResult, Error, bigint, unknown>;
60
+ } | {
61
+ data: EncryptionResult;
62
+ error: null;
63
+ variables: bigint;
64
+ isError: false;
65
+ isIdle: false;
66
+ isPending: false;
67
+ isSuccess: true;
68
+ status: "success";
69
+ mutate: import("@tanstack/react-query").UseMutateFunction<EncryptionResult, Error, bigint, unknown>;
70
+ reset: () => void;
71
+ context: unknown;
72
+ failureCount: number;
73
+ failureReason: Error | null;
74
+ isPaused: boolean;
75
+ submittedAt: number;
76
+ mutateAsync: import("@tanstack/react-query").UseMutateAsyncFunction<EncryptionResult, Error, bigint, unknown>;
77
+ };
@@ -0,0 +1,50 @@
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 { useEffect, useRef } from "react";
12
+ import { useAccount } from "wagmi";
13
+ import { useFhevmContext } from "../context/FhevmProvider";
14
+ export const useEncryptValue = ({ tokenAddress }) => {
15
+ const { instance, config } = useFhevmContext();
16
+ const { address } = useAccount();
17
+ const workerRef = useRef(null);
18
+ useEffect(() => {
19
+ workerRef.current = new Worker(new URL("../workers/encryption.worker", import.meta.url), { type: "module" });
20
+ return () => {
21
+ var _a;
22
+ (_a = workerRef.current) === null || _a === void 0 ? void 0 : _a.terminate();
23
+ };
24
+ }, []);
25
+ const encryptMutation = useMutation({
26
+ mutationFn: (value) => __awaiter(void 0, void 0, void 0, function* () {
27
+ if (!instance)
28
+ throw new Error("Instance is not defined");
29
+ if (!address)
30
+ throw new Error("User is not connected");
31
+ const encryption = new Promise((resolve, reject) => {
32
+ if (!workerRef.current)
33
+ return reject("Worker not initialized");
34
+ workerRef.current.onmessage = (event) => {
35
+ if (event.data.error) {
36
+ reject(event.data.error);
37
+ }
38
+ else {
39
+ resolve(event.data.result);
40
+ }
41
+ };
42
+ workerRef.current.postMessage(Object.assign({ tokenAddress,
43
+ address,
44
+ value }, config));
45
+ });
46
+ return encryption;
47
+ }),
48
+ });
49
+ return Object.assign({}, encryptMutation);
50
+ };
@@ -0,0 +1,11 @@
1
+ import { Address } from "viem";
2
+ export interface EncryptedTransferProps {
3
+ tokenAddress: Address;
4
+ }
5
+ export declare const useEncryptedTransfer: ({ tokenAddress, }: EncryptedTransferProps) => {
6
+ transfer: (to: Address, handles: Uint8Array[], inputProof: Uint8Array) => void;
7
+ isLoading: boolean;
8
+ isFailed: boolean;
9
+ isSuccess: boolean;
10
+ error: import("@wagmi/core").WriteContractErrorType | null;
11
+ };
@@ -0,0 +1,20 @@
1
+ import { usePerformTransaction } from "./usePerformTransaction";
2
+ import { encryptedABI } from "../types/encryptedABI";
3
+ import { toHexString } from "../utils/string.utils";
4
+ export const useEncryptedTransfer = ({ tokenAddress, }) => {
5
+ const { perform, isLoading, isFailed, isSuccess, error } = usePerformTransaction({
6
+ abi: encryptedABI,
7
+ address: tokenAddress,
8
+ functionName: "transfer",
9
+ });
10
+ const transfer = (to, handles, inputProof) => {
11
+ if (handles.length === 0)
12
+ throw new Error("Bad handles format");
13
+ const encryptedAmount = toHexString(handles[0]);
14
+ const proof = toHexString(inputProof);
15
+ perform({
16
+ args: [to, encryptedAmount, proof],
17
+ });
18
+ };
19
+ return { transfer, isLoading, isFailed, isSuccess, error };
20
+ };
@@ -0,0 +1,3 @@
1
+ export declare const useGetFhevmConfig: () => {
2
+ config: import("..").FhevmConfig | undefined;
3
+ };
@@ -0,0 +1,5 @@
1
+ import { useFhevmContext } from "../context/FhevmProvider";
2
+ export const useGetFhevmConfig = () => {
3
+ const { config } = useFhevmContext();
4
+ return { config };
5
+ };
package/dist/index.d.ts CHANGED
@@ -2,3 +2,6 @@ export { default } from "./context/FhevmProvider";
2
2
  export { useFhevmInstance } from "./hooks/useFhevmInstance";
3
3
  export { useDecryptBalance } from "./hooks/useDecryptBalance";
4
4
  export { useGetEncryptedBalance } from "./hooks/useGetEncryptedBalance";
5
+ export { useEncryptedTransfer } from "./hooks/useEncryptedTransfer";
6
+ export { useEncryptValue } from "./hooks/useEncryptValue";
7
+ export { type FhevmConfig } from "./types/fhevmConfig";
package/dist/index.js CHANGED
@@ -4,3 +4,5 @@ export { default } from "./context/FhevmProvider";
4
4
  export { useFhevmInstance } from "./hooks/useFhevmInstance";
5
5
  export { useDecryptBalance } from "./hooks/useDecryptBalance";
6
6
  export { useGetEncryptedBalance } from "./hooks/useGetEncryptedBalance";
7
+ export { useEncryptedTransfer } from "./hooks/useEncryptedTransfer";
8
+ export { useEncryptValue } from "./hooks/useEncryptValue";
@@ -0,0 +1,2 @@
1
+ import { Address } from "viem";
2
+ export declare const toHexString: (bytes: Uint8Array) => Address;
@@ -0,0 +1,5 @@
1
+ export const toHexString = (bytes) => {
2
+ const hexString = "0x" +
3
+ bytes.reduce((str, byte) => str + byte.toString(16).padStart(2, "0"), "");
4
+ return hexString;
5
+ };
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,35 @@
1
+ /// <reference lib="webworker" />
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ import { getOrCreateFhevmInstance } from "../lib/fhevm";
12
+ const ctx = self;
13
+ ctx.onmessage = (event) => __awaiter(void 0, void 0, void 0, function* () {
14
+ const { tokenAddress, address, value, chainId, networkUrl, network } = event.data;
15
+ const instance = yield getOrCreateFhevmInstance({
16
+ chainId,
17
+ network,
18
+ networkUrl,
19
+ });
20
+ if (!instance || !address) {
21
+ ctx.postMessage({ error: "Instance or address is undefined" });
22
+ return;
23
+ }
24
+ try {
25
+ const result = yield instance
26
+ .createEncryptedInput(tokenAddress, address)
27
+ .add64(value)
28
+ .encrypt();
29
+ ctx.postMessage({ result });
30
+ }
31
+ catch (error) {
32
+ console.error("Worker error", error);
33
+ ctx.postMessage({ error });
34
+ }
35
+ });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pyratzlabs/react-fhevm-utils",
3
- "version": "0.5.0",
3
+ "version": "1.0.0",
4
4
  "description": "React hooks and utilities for Fhevmjs",
5
5
  "main": "dist/index.js",
6
6
  "files": [