@pyratzlabs/react-fhevm-utils 1.1.1 → 2.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.
@@ -1,6 +1,6 @@
1
1
  import React, { ReactNode } from "react";
2
- import { FhevmInstance } from "fhevmjs";
3
2
  import { FhevmConfig } from "../types/fhevmConfig";
3
+ import { FhevmInstance } from "@zama-fhe/relayer-sdk/web";
4
4
  interface ProviderProps {
5
5
  children: ReactNode;
6
6
  config?: FhevmConfig;
@@ -1,8 +1,5 @@
1
- import { Address } from "viem";
2
- export interface DecryptBalanceParams {
3
- tokenAddress: Address;
4
- }
5
- export declare const useDecryptBalance: ({ tokenAddress }: DecryptBalanceParams) => {
1
+ import { HandleContractPair } from "@zama-fhe/relayer-sdk/web";
2
+ export declare const useDecryptHandles: () => {
6
3
  data: undefined;
7
4
  variables: undefined;
8
5
  error: null;
@@ -11,63 +8,63 @@ export declare const useDecryptBalance: ({ tokenAddress }: DecryptBalanceParams)
11
8
  isPending: false;
12
9
  isSuccess: false;
13
10
  status: "idle";
14
- mutate: import("@tanstack/react-query").UseMutateFunction<bigint, Error, bigint, unknown>;
11
+ mutate: import("@tanstack/react-query").UseMutateFunction<import("@zama-fhe/relayer-sdk/web").DecryptedResults, Error, HandleContractPair[], unknown>;
15
12
  reset: () => void;
16
13
  context: unknown;
17
14
  failureCount: number;
18
15
  failureReason: Error | null;
19
16
  isPaused: boolean;
20
17
  submittedAt: number;
21
- mutateAsync: import("@tanstack/react-query").UseMutateAsyncFunction<bigint, Error, bigint, unknown>;
18
+ mutateAsync: import("@tanstack/react-query").UseMutateAsyncFunction<import("@zama-fhe/relayer-sdk/web").DecryptedResults, Error, HandleContractPair[], unknown>;
22
19
  } | {
23
20
  data: undefined;
24
- variables: bigint;
21
+ variables: HandleContractPair[];
25
22
  error: null;
26
23
  isError: false;
27
24
  isIdle: false;
28
25
  isPending: true;
29
26
  isSuccess: false;
30
27
  status: "pending";
31
- mutate: import("@tanstack/react-query").UseMutateFunction<bigint, Error, bigint, unknown>;
28
+ mutate: import("@tanstack/react-query").UseMutateFunction<import("@zama-fhe/relayer-sdk/web").DecryptedResults, Error, HandleContractPair[], unknown>;
32
29
  reset: () => void;
33
30
  context: unknown;
34
31
  failureCount: number;
35
32
  failureReason: Error | null;
36
33
  isPaused: boolean;
37
34
  submittedAt: number;
38
- mutateAsync: import("@tanstack/react-query").UseMutateAsyncFunction<bigint, Error, bigint, unknown>;
35
+ mutateAsync: import("@tanstack/react-query").UseMutateAsyncFunction<import("@zama-fhe/relayer-sdk/web").DecryptedResults, Error, HandleContractPair[], unknown>;
39
36
  } | {
40
37
  data: undefined;
41
38
  error: Error;
42
- variables: bigint;
39
+ variables: HandleContractPair[];
43
40
  isError: true;
44
41
  isIdle: false;
45
42
  isPending: false;
46
43
  isSuccess: false;
47
44
  status: "error";
48
- mutate: import("@tanstack/react-query").UseMutateFunction<bigint, Error, bigint, unknown>;
45
+ mutate: import("@tanstack/react-query").UseMutateFunction<import("@zama-fhe/relayer-sdk/web").DecryptedResults, Error, HandleContractPair[], unknown>;
49
46
  reset: () => void;
50
47
  context: unknown;
51
48
  failureCount: number;
52
49
  failureReason: Error | null;
53
50
  isPaused: boolean;
54
51
  submittedAt: number;
55
- mutateAsync: import("@tanstack/react-query").UseMutateAsyncFunction<bigint, Error, bigint, unknown>;
52
+ mutateAsync: import("@tanstack/react-query").UseMutateAsyncFunction<import("@zama-fhe/relayer-sdk/web").DecryptedResults, Error, HandleContractPair[], unknown>;
56
53
  } | {
57
- data: bigint;
54
+ data: import("@zama-fhe/relayer-sdk/web").DecryptedResults;
58
55
  error: null;
59
- variables: bigint;
56
+ variables: HandleContractPair[];
60
57
  isError: false;
61
58
  isIdle: false;
62
59
  isPending: false;
63
60
  isSuccess: true;
64
61
  status: "success";
65
- mutate: import("@tanstack/react-query").UseMutateFunction<bigint, Error, bigint, unknown>;
62
+ mutate: import("@tanstack/react-query").UseMutateFunction<import("@zama-fhe/relayer-sdk/web").DecryptedResults, Error, HandleContractPair[], unknown>;
66
63
  reset: () => void;
67
64
  context: unknown;
68
65
  failureCount: number;
69
66
  failureReason: Error | null;
70
67
  isPaused: boolean;
71
68
  submittedAt: number;
72
- mutateAsync: import("@tanstack/react-query").UseMutateAsyncFunction<bigint, Error, bigint, unknown>;
69
+ mutateAsync: import("@tanstack/react-query").UseMutateAsyncFunction<import("@zama-fhe/relayer-sdk/web").DecryptedResults, Error, HandleContractPair[], unknown>;
73
70
  };
@@ -10,25 +10,30 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
10
10
  import { useMutation } from "@tanstack/react-query";
11
11
  import { useAccount, useSignTypedData } from "wagmi";
12
12
  import { useFhevmContext } from "../context/FhevmProvider";
13
- export const useDecryptBalance = ({ tokenAddress }) => {
13
+ export const useDecryptHandles = () => {
14
14
  const { instance } = useFhevmContext();
15
15
  const { address } = useAccount();
16
16
  const { signTypedDataAsync } = useSignTypedData();
17
17
  const decryptMutation = useMutation({
18
- mutationFn: (encryptedBalance) => __awaiter(void 0, void 0, void 0, function* () {
18
+ mutationFn: (handles) => __awaiter(void 0, void 0, void 0, function* () {
19
19
  if (!instance)
20
20
  throw new Error("Instance is not defined");
21
21
  if (!address)
22
22
  throw new Error("User wallet is not connected");
23
+ const startTimeStamp = Math.floor(Date.now() / 1000).toString();
24
+ const durationDays = "1";
23
25
  const { publicKey, privateKey } = instance.generateKeypair();
24
- const eip712 = instance.createEIP712(publicKey, tokenAddress);
26
+ const contractAddresses = handles.map((handle) => handle.contractAddress);
27
+ const eip712 = instance.createEIP712(publicKey, contractAddresses, startTimeStamp, durationDays);
25
28
  const signature = yield signTypedDataAsync({
26
29
  domain: eip712.domain,
27
- types: { Reencrypt: eip712.types.Reencrypt },
28
- primaryType: "Reencrypt",
30
+ types: {
31
+ UserDecryptRequestVerification: eip712.types.UserDecryptRequestVerification,
32
+ },
33
+ primaryType: "UserDecryptRequestVerification",
29
34
  message: eip712.message,
30
35
  });
31
- const decryptedBalance = yield instance.reencrypt(encryptedBalance, privateKey, publicKey, signature.replace("0x", ""), tokenAddress, address);
36
+ const decryptedBalance = yield instance.userDecrypt(handles, privateKey, publicKey, signature.replace("0x", ""), contractAddresses, address, startTimeStamp, durationDays);
32
37
  return decryptedBalance;
33
38
  }),
34
39
  });
@@ -15,14 +15,20 @@ export declare const useEncryptValue: ({ tokenAddress }: EncryptValueParams) =>
15
15
  isPending: false;
16
16
  isSuccess: false;
17
17
  status: "idle";
18
- mutate: import("@tanstack/react-query").UseMutateFunction<EncryptionResult, Error, bigint, unknown>;
18
+ mutate: import("@tanstack/react-query").UseMutateFunction<{
19
+ handles: Uint8Array[];
20
+ inputProof: Uint8Array;
21
+ }, Error, bigint, unknown>;
19
22
  reset: () => void;
20
23
  context: unknown;
21
24
  failureCount: number;
22
25
  failureReason: Error | null;
23
26
  isPaused: boolean;
24
27
  submittedAt: number;
25
- mutateAsync: import("@tanstack/react-query").UseMutateAsyncFunction<EncryptionResult, Error, bigint, unknown>;
28
+ mutateAsync: import("@tanstack/react-query").UseMutateAsyncFunction<{
29
+ handles: Uint8Array[];
30
+ inputProof: Uint8Array;
31
+ }, Error, bigint, unknown>;
26
32
  } | {
27
33
  data: undefined;
28
34
  variables: bigint;
@@ -32,14 +38,20 @@ export declare const useEncryptValue: ({ tokenAddress }: EncryptValueParams) =>
32
38
  isPending: true;
33
39
  isSuccess: false;
34
40
  status: "pending";
35
- mutate: import("@tanstack/react-query").UseMutateFunction<EncryptionResult, Error, bigint, unknown>;
41
+ mutate: import("@tanstack/react-query").UseMutateFunction<{
42
+ handles: Uint8Array[];
43
+ inputProof: Uint8Array;
44
+ }, Error, bigint, unknown>;
36
45
  reset: () => void;
37
46
  context: unknown;
38
47
  failureCount: number;
39
48
  failureReason: Error | null;
40
49
  isPaused: boolean;
41
50
  submittedAt: number;
42
- mutateAsync: import("@tanstack/react-query").UseMutateAsyncFunction<EncryptionResult, Error, bigint, unknown>;
51
+ mutateAsync: import("@tanstack/react-query").UseMutateAsyncFunction<{
52
+ handles: Uint8Array[];
53
+ inputProof: Uint8Array;
54
+ }, Error, bigint, unknown>;
43
55
  } | {
44
56
  data: undefined;
45
57
  error: Error;
@@ -49,16 +61,25 @@ export declare const useEncryptValue: ({ tokenAddress }: EncryptValueParams) =>
49
61
  isPending: false;
50
62
  isSuccess: false;
51
63
  status: "error";
52
- mutate: import("@tanstack/react-query").UseMutateFunction<EncryptionResult, Error, bigint, unknown>;
64
+ mutate: import("@tanstack/react-query").UseMutateFunction<{
65
+ handles: Uint8Array[];
66
+ inputProof: Uint8Array;
67
+ }, Error, bigint, unknown>;
53
68
  reset: () => void;
54
69
  context: unknown;
55
70
  failureCount: number;
56
71
  failureReason: Error | null;
57
72
  isPaused: boolean;
58
73
  submittedAt: number;
59
- mutateAsync: import("@tanstack/react-query").UseMutateAsyncFunction<EncryptionResult, Error, bigint, unknown>;
74
+ mutateAsync: import("@tanstack/react-query").UseMutateAsyncFunction<{
75
+ handles: Uint8Array[];
76
+ inputProof: Uint8Array;
77
+ }, Error, bigint, unknown>;
60
78
  } | {
61
- data: EncryptionResult;
79
+ data: {
80
+ handles: Uint8Array[];
81
+ inputProof: Uint8Array;
82
+ };
62
83
  error: null;
63
84
  variables: bigint;
64
85
  isError: false;
@@ -66,12 +87,18 @@ export declare const useEncryptValue: ({ tokenAddress }: EncryptValueParams) =>
66
87
  isPending: false;
67
88
  isSuccess: true;
68
89
  status: "success";
69
- mutate: import("@tanstack/react-query").UseMutateFunction<EncryptionResult, Error, bigint, unknown>;
90
+ mutate: import("@tanstack/react-query").UseMutateFunction<{
91
+ handles: Uint8Array[];
92
+ inputProof: Uint8Array;
93
+ }, Error, bigint, unknown>;
70
94
  reset: () => void;
71
95
  context: unknown;
72
96
  failureCount: number;
73
97
  failureReason: Error | null;
74
98
  isPaused: boolean;
75
99
  submittedAt: number;
76
- mutateAsync: import("@tanstack/react-query").UseMutateAsyncFunction<EncryptionResult, Error, bigint, unknown>;
100
+ mutateAsync: import("@tanstack/react-query").UseMutateAsyncFunction<{
101
+ handles: Uint8Array[];
102
+ inputProof: Uint8Array;
103
+ }, Error, bigint, unknown>;
77
104
  };
@@ -8,42 +8,22 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
8
8
  });
9
9
  };
10
10
  import { useMutation } from "@tanstack/react-query";
11
- import { useEffect, useRef } from "react";
12
11
  import { useAccount } from "wagmi";
13
12
  import { useFhevmContext } from "../context/FhevmProvider";
13
+ import { encryptInt64 } from "../workers/encryption.worker";
14
14
  export const useEncryptValue = ({ tokenAddress }) => {
15
15
  const { instance, config } = useFhevmContext();
16
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
17
  const encryptMutation = useMutation({
26
18
  mutationFn: (value) => __awaiter(void 0, void 0, void 0, function* () {
27
19
  if (!instance)
28
20
  throw new Error("Instance is not defined");
29
21
  if (!address)
30
22
  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;
23
+ return yield encryptInt64(Object.assign({ tokenAddress,
24
+ address,
25
+ value,
26
+ instance }, config));
47
27
  }),
48
28
  });
49
29
  return Object.assign({}, encryptMutation);
@@ -1,3 +1,3 @@
1
1
  export declare const useFhevmInstance: () => {
2
- instance: import("fhevmjs").FhevmInstance | undefined;
2
+ instance: import("@zama-fhe/relayer-sdk/web").FhevmInstance | undefined;
3
3
  };
@@ -1,7 +1,8 @@
1
1
  import { Address } from "viem";
2
2
  export interface GetEncryptedBalanceParams {
3
3
  tokenAddress: Address;
4
+ userAddress?: Address;
4
5
  }
5
- export declare const useGetEncryptedBalance: ({ tokenAddress, }: GetEncryptedBalanceParams) => {
6
+ export declare const useGetEncryptedBalance: ({ tokenAddress, userAddress, }: GetEncryptedBalanceParams) => {
6
7
  encryptedBalance: bigint | undefined;
7
8
  };
@@ -1,12 +1,12 @@
1
1
  import { useAccount, useReadContract } from "wagmi";
2
2
  import { encryptedABI } from "../types/encryptedABI";
3
- export const useGetEncryptedBalance = ({ tokenAddress, }) => {
3
+ export const useGetEncryptedBalance = ({ tokenAddress, userAddress, }) => {
4
4
  const { address } = useAccount();
5
5
  const { data } = useReadContract({
6
6
  address: tokenAddress,
7
7
  abi: encryptedABI,
8
8
  functionName: "balanceOf",
9
- args: [address],
9
+ args: [userAddress !== null && userAddress !== void 0 ? userAddress : address],
10
10
  query: {
11
11
  enabled: !!address,
12
12
  refetchInterval: 5000,
package/dist/index.d.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  export { default } from "./context/FhevmProvider";
2
2
  export { useFhevmInstance } from "./hooks/useFhevmInstance";
3
- export { useDecryptBalance } from "./hooks/useDecryptBalance";
3
+ export { useDecryptHandles } from "./hooks/useDecryptHandles";
4
4
  export { useGetEncryptedBalance } from "./hooks/useGetEncryptedBalance";
5
5
  export { useEncryptedTransfer } from "./hooks/useEncryptedTransfer";
6
6
  export { useEncryptValue } from "./hooks/useEncryptValue";
package/dist/index.js CHANGED
@@ -2,7 +2,7 @@
2
2
  export { default } from "./context/FhevmProvider";
3
3
  // hooks
4
4
  export { useFhevmInstance } from "./hooks/useFhevmInstance";
5
- export { useDecryptBalance } from "./hooks/useDecryptBalance";
5
+ export { useDecryptHandles } from "./hooks/useDecryptHandles";
6
6
  export { useGetEncryptedBalance } from "./hooks/useGetEncryptedBalance";
7
7
  export { useEncryptedTransfer } from "./hooks/useEncryptedTransfer";
8
8
  export { useEncryptValue } from "./hooks/useEncryptValue";
@@ -1,5 +1,14 @@
1
- import { FhevmInstance } from "fhevmjs";
2
1
  import { FhevmConfig } from "../types/fhevmConfig";
2
+ import { createInstance, FhevmInstance, SepoliaConfig, initSDK } from "@zama-fhe/relayer-sdk/web";
3
+ declare global {
4
+ interface Window {
5
+ relayerSDK: {
6
+ initSDK: typeof initSDK;
7
+ createInstance: typeof createInstance;
8
+ SepoliaConfig: typeof SepoliaConfig;
9
+ };
10
+ }
11
+ }
3
12
  export declare const createFhevmInstance: (config?: FhevmConfig) => Promise<FhevmInstance>;
4
13
  export declare const getOrCreateFhevmInstance: (config?: FhevmConfig) => Promise<FhevmInstance>;
5
14
  export declare const getInstance: () => FhevmInstance | null;
package/dist/lib/fhevm.js CHANGED
@@ -7,23 +7,17 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
7
7
  step((generator = generator.apply(thisArg, _arguments || [])).next());
8
8
  });
9
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;
10
+ import { createInstance, SepoliaConfig, initSDK, } from "@zama-fhe/relayer-sdk/web";
14
11
  let fhevmInstance = null;
15
12
  export const createFhevmInstance = (config) => __awaiter(void 0, void 0, void 0, function* () {
16
- var _a, _b;
17
13
  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
- });
14
+ yield initSDK();
15
+ if (config) {
16
+ fhevmInstance = yield createInstance(Object.assign({}, SepoliaConfig));
17
+ }
18
+ else {
19
+ fhevmInstance = yield createInstance(SepoliaConfig);
20
+ }
27
21
  }
28
22
  return fhevmInstance;
29
23
  });
@@ -1 +1,12 @@
1
+ import { FhevmInstance } from "@zama-fhe/relayer-sdk/web";
2
+ interface EventData {
3
+ tokenAddress: string;
4
+ address?: string;
5
+ value: bigint;
6
+ instance: FhevmInstance;
7
+ }
8
+ export declare const encryptInt64: (data: EventData) => Promise<{
9
+ handles: Uint8Array[];
10
+ inputProof: Uint8Array;
11
+ }>;
1
12
  export {};
@@ -8,28 +8,13 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
8
8
  step((generator = generator.apply(thisArg, _arguments || [])).next());
9
9
  });
10
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
- });
11
+ export const encryptInt64 = (data) => __awaiter(void 0, void 0, void 0, function* () {
12
+ const { tokenAddress, address, value, instance } = data;
20
13
  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
- .add256(value)
28
- .encrypt();
29
- ctx.postMessage({ result });
30
- }
31
- catch (error) {
32
- console.error("Worker error", error);
33
- ctx.postMessage({ error });
14
+ throw new Error("Instance or address is undefined");
34
15
  }
16
+ return yield instance
17
+ .createEncryptedInput(tokenAddress, address)
18
+ .add64(value)
19
+ .encrypt();
35
20
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pyratzlabs/react-fhevm-utils",
3
- "version": "1.1.1",
3
+ "version": "2.0.0",
4
4
  "description": "React hooks and utilities for Fhevmjs",
5
5
  "main": "dist/index.js",
6
6
  "files": [
@@ -31,7 +31,7 @@
31
31
  },
32
32
  "dependencies": {
33
33
  "@tanstack/react-query": "^5.71.5",
34
- "fhevmjs": "^0.6.2",
34
+ "@zama-fhe/relayer-sdk": "^0.1.2",
35
35
  "react": "^19.1.0",
36
36
  "viem": "^2.25.0",
37
37
  "wagmi": "^2.14.16"