@vlayer/react 0.1.0-nightly-20250212-df2f5a0 → 0.1.0-nightly-20250214-11fe464
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.
- package/dist/context.d.ts +20 -0
- package/dist/context.d.ts.map +1 -0
- package/dist/context.js +51 -0
- package/dist/context.js.map +1 -0
- package/dist/context.test.d.ts +2 -0
- package/dist/context.test.d.ts.map +1 -0
- package/dist/context.test.js +75 -0
- package/dist/context.test.js.map +1 -0
- package/dist/defaults.d.ts +20 -0
- package/dist/defaults.d.ts.map +1 -0
- package/dist/defaults.js +21 -0
- package/dist/defaults.js.map +1 -0
- package/dist/index.d.ts +7 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +7 -0
- package/dist/index.js.map +1 -0
- package/dist/types.d.ts +35 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +18 -0
- package/dist/types.js.map +1 -0
- package/dist/useCallProver/useCallProver.d.ts +19 -0
- package/dist/useCallProver/useCallProver.d.ts.map +1 -0
- package/dist/useCallProver/useCallProver.js +49 -0
- package/dist/useCallProver/useCallProver.js.map +1 -0
- package/dist/useWaitForProvingResult/useWaitForProvingResult.d.ts +18 -0
- package/dist/useWaitForProvingResult/useWaitForProvingResult.d.ts.map +1 -0
- package/dist/useWaitForProvingResult/useWaitForProvingResult.js +41 -0
- package/dist/useWaitForProvingResult/useWaitForProvingResult.js.map +1 -0
- package/dist/useWebproof/extension.mock.d.ts +25 -0
- package/dist/useWebproof/extension.mock.d.ts.map +1 -0
- package/dist/useWebproof/extension.mock.js +65 -0
- package/dist/useWebproof/extension.mock.js.map +1 -0
- package/dist/useWebproof/useWebProof.d.ts +16 -0
- package/dist/useWebproof/useWebProof.d.ts.map +1 -0
- package/dist/useWebproof/useWebProof.js +37 -0
- package/dist/useWebproof/useWebProof.js.map +1 -0
- package/dist/useWebproof/useWebProof.test.d.ts +2 -0
- package/dist/useWebproof/useWebProof.test.d.ts.map +1 -0
- package/dist/useWebproof/useWebProof.test.js +119 -0
- package/dist/useWebproof/useWebProof.test.js.map +1 -0
- package/package.json +5 -2
- package/bun.lockb +0 -0
- package/eslint.config.ts +0 -36
- package/src/context.test.tsx +0 -105
- package/src/context.tsx +0 -78
- package/src/defaults.ts +0 -22
- package/src/index.ts +0 -7
- package/src/interface.test.ts +0 -12
- package/src/types.ts +0 -47
- package/src/useCallProver/useCallProver.test.ts +0 -126
- package/src/useCallProver/useCallProver.ts +0 -60
- package/src/useWaitForProvingResult/useWaitForProvingResult.test.ts +0 -80
- package/src/useWaitForProvingResult/useWaitForProvingResult.ts +0 -48
- package/src/useWebproof/extension.mock.ts +0 -103
- package/src/useWebproof/useWebProof.test.tsx +0 -142
- package/src/useWebproof/useWebProof.ts +0 -52
- package/tsconfig.base.json +0 -26
- package/tsconfig.build.json +0 -12
- package/tsconfig.json +0 -6
- package/vitest.config.ts +0 -8
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"useWebProof.test.d.ts","sourceRoot":"","sources":["../../src/useWebproof/useWebProof.test.tsx"],"names":[],"mappings":""}
|
@@ -0,0 +1,119 @@
|
|
1
|
+
import { jsx as _jsx } from "react/jsx-runtime";
|
2
|
+
import { act, renderHook } from "@testing-library/react";
|
3
|
+
import { it, expect, describe, vi, beforeEach } from "vitest";
|
4
|
+
import { useWebProof } from "./useWebProof.js";
|
5
|
+
import { ProofProvider } from "../context.js";
|
6
|
+
import { MockExtensionWebProofProvider } from "./extension.mock.js";
|
7
|
+
import { WebProofRequestStatus } from "../types.js";
|
8
|
+
const mocks = vi.hoisted(() => ({
|
9
|
+
createExtensionWebProofProvider: vi.fn(),
|
10
|
+
}));
|
11
|
+
vi.mock("@vlayer/sdk/web_proof", () => ({
|
12
|
+
createExtensionWebProofProvider: mocks.createExtensionWebProofProvider,
|
13
|
+
}));
|
14
|
+
const stubWebProofRequest = {
|
15
|
+
proverCallCommitment: {
|
16
|
+
address: "0x",
|
17
|
+
proverAbi: [
|
18
|
+
{
|
19
|
+
type: "function",
|
20
|
+
name: "mint",
|
21
|
+
inputs: [],
|
22
|
+
outputs: [],
|
23
|
+
stateMutability: "nonpayable",
|
24
|
+
},
|
25
|
+
],
|
26
|
+
chainId: 1,
|
27
|
+
functionName: "mint",
|
28
|
+
commitmentArgs: [],
|
29
|
+
},
|
30
|
+
logoUrl: "http://twitterswap.com/logo.png",
|
31
|
+
steps: [],
|
32
|
+
};
|
33
|
+
describe("useWebproof", () => {
|
34
|
+
beforeEach(() => {
|
35
|
+
vi.useFakeTimers();
|
36
|
+
});
|
37
|
+
const VlayerMockContext = ({ children }) => (_jsx(ProofProvider, { children: children }));
|
38
|
+
const renderWebproofHook = () => renderHook(() => useWebProof(stubWebProofRequest), {
|
39
|
+
wrapper: VlayerMockContext,
|
40
|
+
});
|
41
|
+
const setupMockProvider = (options = {}) => {
|
42
|
+
mocks.createExtensionWebProofProvider.mockImplementation(() => new MockExtensionWebProofProvider(options));
|
43
|
+
vi.runAllTimers();
|
44
|
+
};
|
45
|
+
it("should throw error if called outside of ProofProvider", () => {
|
46
|
+
expect(() => renderHook(() => useWebProof(stubWebProofRequest))).toThrow("useProofContext must be used within a ProofProvider");
|
47
|
+
});
|
48
|
+
it("should initialize with the correct state", () => {
|
49
|
+
setupMockProvider();
|
50
|
+
const { result } = renderWebproofHook();
|
51
|
+
expect(result.current).toEqual({
|
52
|
+
webProof: null,
|
53
|
+
status: WebProofRequestStatus.idle,
|
54
|
+
isIdle: true,
|
55
|
+
isError: false,
|
56
|
+
isSuccess: false,
|
57
|
+
isPending: false,
|
58
|
+
error: null,
|
59
|
+
requestWebProof: expect.any(Function),
|
60
|
+
});
|
61
|
+
});
|
62
|
+
it("should request the web proof", () => {
|
63
|
+
const { result } = renderWebproofHook();
|
64
|
+
act(() => result.current.requestWebProof());
|
65
|
+
expect(result.current).toEqual({
|
66
|
+
webProof: null,
|
67
|
+
status: WebProofRequestStatus.pending,
|
68
|
+
isIdle: false,
|
69
|
+
isError: false,
|
70
|
+
isSuccess: false,
|
71
|
+
isPending: true,
|
72
|
+
error: null,
|
73
|
+
requestWebProof: expect.any(Function),
|
74
|
+
});
|
75
|
+
});
|
76
|
+
it("should set the web proof when the request is successful", () => {
|
77
|
+
setupMockProvider({
|
78
|
+
shouldSucceed: true,
|
79
|
+
mockProof: { mock: "proof" },
|
80
|
+
});
|
81
|
+
const { result, rerender } = renderWebproofHook();
|
82
|
+
act(() => result.current.requestWebProof());
|
83
|
+
vi.advanceTimersByTime(1000);
|
84
|
+
rerender();
|
85
|
+
expect(result.current).toMatchObject({
|
86
|
+
webProof: {
|
87
|
+
webProofJson: JSON.stringify({ presentationJson: { mock: "proof" } }),
|
88
|
+
},
|
89
|
+
status: WebProofRequestStatus.success,
|
90
|
+
isIdle: false,
|
91
|
+
isPending: false,
|
92
|
+
isError: false,
|
93
|
+
isSuccess: true,
|
94
|
+
error: null,
|
95
|
+
requestWebProof: expect.any(Function),
|
96
|
+
});
|
97
|
+
});
|
98
|
+
it("should set the error when the request fails", () => {
|
99
|
+
setupMockProvider({
|
100
|
+
shouldSucceed: false,
|
101
|
+
mockError: "Mock error occurred",
|
102
|
+
});
|
103
|
+
const { result, rerender } = renderWebproofHook();
|
104
|
+
act(() => result.current.requestWebProof());
|
105
|
+
vi.advanceTimersByTime(1000);
|
106
|
+
rerender();
|
107
|
+
expect(result.current).toEqual({
|
108
|
+
webProof: null,
|
109
|
+
status: WebProofRequestStatus.error,
|
110
|
+
isIdle: false,
|
111
|
+
isPending: false,
|
112
|
+
isError: true,
|
113
|
+
isSuccess: false,
|
114
|
+
error: new Error("Mock error occurred"),
|
115
|
+
requestWebProof: expect.any(Function),
|
116
|
+
});
|
117
|
+
});
|
118
|
+
});
|
119
|
+
//# sourceMappingURL=useWebProof.test.js.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"useWebProof.test.js","sourceRoot":"","sources":["../../src/useWebproof/useWebProof.test.tsx"],"names":[],"mappings":";AAAA,OAAO,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,GAAG,EAAE;QACrD,iBAAiB,CAAC;YAChB,aAAa,EAAE,KAAK;YACpB,SAAS,EAAE,qBAAqB;SACjC,CAAC,CAAC;QACH,MAAM,EAAE,MAAM,EAAE,QAAQ,EAAE,GAAG,kBAAkB,EAAE,CAAC;QAElD,GAAG,CAAC,GAAG,EAAE,CAAC,MAAM,CAAC,OAAO,CAAC,eAAe,EAAE,CAAC,CAAC;QAC5C,EAAE,CAAC,mBAAmB,CAAC,IAAI,CAAC,CAAC;QAC7B,QAAQ,EAAE,CAAC;QAEX,MAAM,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC,OAAO,CAAC;YAC7B,QAAQ,EAAE,IAAI;YACd,MAAM,EAAE,qBAAqB,CAAC,KAAK;YACnC,MAAM,EAAE,KAAK;YACb,SAAS,EAAE,KAAK;YAChB,OAAO,EAAE,IAAI;YACb,SAAS,EAAE,KAAK;YAChB,KAAK,EAAE,IAAI,KAAK,CAAC,qBAAqB,CAAC;YACvC,eAAe,EAAE,MAAM,CAAC,GAAG,CAAC,QAAQ,CAAe;SACpD,CAAC,CAAC;IACL,CAAC,CAAC,CAAC;AACL,CAAC,CAAC,CAAC"}
|
package/package.json
CHANGED
@@ -1,8 +1,8 @@
|
|
1
1
|
{
|
2
2
|
"name": "@vlayer/react",
|
3
|
-
"version": "0.1.0-nightly-
|
3
|
+
"version": "0.1.0-nightly-20250214-11fe464",
|
4
4
|
"exports": {
|
5
|
-
".": "./
|
5
|
+
".": "./dist/index.js"
|
6
6
|
},
|
7
7
|
"type": "module",
|
8
8
|
"devDependencies": {
|
@@ -21,6 +21,9 @@
|
|
21
21
|
"viem": "2.x",
|
22
22
|
"@vlayer/sdk": "^0.1.0"
|
23
23
|
},
|
24
|
+
"files": [
|
25
|
+
"dist"
|
26
|
+
],
|
24
27
|
"scripts": {
|
25
28
|
"build": "bun tsc --project tsconfig.build.json && bun tsc-alias",
|
26
29
|
"test:unit": "vitest --run"
|
package/bun.lockb
DELETED
Binary file
|
package/eslint.config.ts
DELETED
@@ -1,36 +0,0 @@
|
|
1
|
-
import js from "@eslint/js";
|
2
|
-
import ts from "typescript-eslint";
|
3
|
-
import prettierRecommended from "eslint-plugin-prettier/recommended";
|
4
|
-
import globals from "globals";
|
5
|
-
import eslintPluginReact from "eslint-plugin-react";
|
6
|
-
import reactHooks from "eslint-plugin-react-hooks";
|
7
|
-
|
8
|
-
export default [
|
9
|
-
js.configs.recommended,
|
10
|
-
...ts.configs.recommended,
|
11
|
-
prettierRecommended,
|
12
|
-
eslintPluginReact.configs.recommended,
|
13
|
-
reactHooks.configs.recommended,
|
14
|
-
{
|
15
|
-
settings: {
|
16
|
-
react: {
|
17
|
-
version: "detect"
|
18
|
-
}
|
19
|
-
},
|
20
|
-
rules: {
|
21
|
-
"no-unused-vars": "warn",
|
22
|
-
"no-undef": "warn",
|
23
|
-
},
|
24
|
-
languageOptions: {
|
25
|
-
globals: {
|
26
|
-
...globals.browser,
|
27
|
-
Bun: false,
|
28
|
-
},
|
29
|
-
parserOptions: {
|
30
|
-
ecmaFeatures: {
|
31
|
-
jsx: true
|
32
|
-
}
|
33
|
-
}
|
34
|
-
},
|
35
|
-
},
|
36
|
-
];
|
package/src/context.test.tsx
DELETED
@@ -1,105 +0,0 @@
|
|
1
|
-
import { describe, it, expect } from "vitest";
|
2
|
-
import { renderHook } from "@testing-library/react";
|
3
|
-
import {
|
4
|
-
ProofProvider,
|
5
|
-
useProofContext,
|
6
|
-
useProverContext,
|
7
|
-
useWebProofContext,
|
8
|
-
} from "./context";
|
9
|
-
import type { PropsWithChildren } from "react";
|
10
|
-
import { DEFAULT_CONFIG, DEFAULT_CONFIG_ENV } from "./defaults";
|
11
|
-
import { ProofEnv } from "./types";
|
12
|
-
|
13
|
-
describe("Context Providers and Hooks", () => {
|
14
|
-
describe("useProofContext", () => {
|
15
|
-
it("should throw error when used outside ProofProvider", () => {
|
16
|
-
expect(() => {
|
17
|
-
renderHook(() => useProofContext());
|
18
|
-
}).toThrow("useProofContext must be used within a ProofProvider");
|
19
|
-
});
|
20
|
-
|
21
|
-
it("should provide both WebProof and Prover contexts", () => {
|
22
|
-
const config = {
|
23
|
-
proverUrl: "test-prover-url",
|
24
|
-
notaryUrl: "test-notary-url",
|
25
|
-
wsProxyUrl: "test-ws-url",
|
26
|
-
};
|
27
|
-
|
28
|
-
const wrapper = ({ children }: PropsWithChildren) => (
|
29
|
-
<ProofProvider config={config}>{children}</ProofProvider>
|
30
|
-
);
|
31
|
-
|
32
|
-
const { result } = renderHook(() => useProofContext(), { wrapper });
|
33
|
-
expect(result.current.webProofProvider).toBeDefined();
|
34
|
-
expect(result.current.vlayerClient).toBeDefined();
|
35
|
-
expect(result.current.config).toEqual(config);
|
36
|
-
});
|
37
|
-
|
38
|
-
it("should use default config when env is provided", () => {
|
39
|
-
const config = {
|
40
|
-
env: ProofEnv.TESTNET,
|
41
|
-
wsProxyUrl: "test-ws-url",
|
42
|
-
};
|
43
|
-
|
44
|
-
const wrapper = ({ children }: PropsWithChildren) => (
|
45
|
-
<ProofProvider config={config}>{children}</ProofProvider>
|
46
|
-
);
|
47
|
-
|
48
|
-
const { result } = renderHook(() => useProofContext(), { wrapper });
|
49
|
-
expect(result.current.config).toEqual({
|
50
|
-
...DEFAULT_CONFIG[ProofEnv.TESTNET],
|
51
|
-
...{ wsProxyUrl: config.wsProxyUrl },
|
52
|
-
});
|
53
|
-
});
|
54
|
-
|
55
|
-
it("should use proper default config when env is not provided", () => {
|
56
|
-
const config = {
|
57
|
-
proverUrl: "custom-url",
|
58
|
-
};
|
59
|
-
|
60
|
-
const wrapper = ({ children }: PropsWithChildren) => (
|
61
|
-
<ProofProvider config={config}>{children}</ProofProvider>
|
62
|
-
);
|
63
|
-
|
64
|
-
const { result } = renderHook(() => useProofContext(), { wrapper });
|
65
|
-
expect(result.current.config).toEqual({
|
66
|
-
...DEFAULT_CONFIG[DEFAULT_CONFIG_ENV],
|
67
|
-
...config,
|
68
|
-
});
|
69
|
-
});
|
70
|
-
});
|
71
|
-
|
72
|
-
describe("useWebProofContext", () => {
|
73
|
-
it("should throw error when used outside WebProofProvider", () => {
|
74
|
-
expect(() => {
|
75
|
-
renderHook(() => useWebProofContext());
|
76
|
-
}).toThrow("useWebProofContext must be used within a WebProofProvider");
|
77
|
-
});
|
78
|
-
|
79
|
-
it("should return webProofContext when used within Provider", () => {
|
80
|
-
const wrapper = ({ children }: PropsWithChildren) => (
|
81
|
-
<ProofProvider>{children}</ProofProvider>
|
82
|
-
);
|
83
|
-
|
84
|
-
const { result } = renderHook(() => useWebProofContext(), { wrapper });
|
85
|
-
expect(result.current.webProofProvider).toBeDefined();
|
86
|
-
});
|
87
|
-
});
|
88
|
-
|
89
|
-
describe("useProverContext", () => {
|
90
|
-
it("should throw error when used outside ProverProvider", () => {
|
91
|
-
expect(() => {
|
92
|
-
renderHook(() => useProverContext());
|
93
|
-
}).toThrow("useProverContext must be used within a ProverProvider");
|
94
|
-
});
|
95
|
-
|
96
|
-
it("should return proverContext when used within Provider", () => {
|
97
|
-
const wrapper = ({ children }: PropsWithChildren) => (
|
98
|
-
<ProofProvider>{children}</ProofProvider>
|
99
|
-
);
|
100
|
-
|
101
|
-
const { result } = renderHook(() => useProverContext(), { wrapper });
|
102
|
-
expect(result.current.vlayerClient).toBeDefined();
|
103
|
-
});
|
104
|
-
});
|
105
|
-
});
|
package/src/context.tsx
DELETED
@@ -1,78 +0,0 @@
|
|
1
|
-
import { createVlayerClient } from "@vlayer/sdk";
|
2
|
-
import { createContext, useContext, type PropsWithChildren } from "react";
|
3
|
-
import { createExtensionWebProofProvider } from "@vlayer/sdk/web_proof";
|
4
|
-
import {
|
5
|
-
type ProofContextType,
|
6
|
-
type ProofConfig,
|
7
|
-
type WebProofContextType,
|
8
|
-
type ProverContextType,
|
9
|
-
} from "./types";
|
10
|
-
import { DEFAULT_CONFIG, DEFAULT_CONFIG_ENV } from "./defaults";
|
11
|
-
|
12
|
-
export const ProofContext = createContext<ProofContextType | null>(null);
|
13
|
-
export const WebProofContext = createContext<WebProofContextType | null>(null);
|
14
|
-
export const ProverContext = createContext<ProverContextType | null>(null);
|
15
|
-
|
16
|
-
export const ProofProvider = ({
|
17
|
-
config,
|
18
|
-
children,
|
19
|
-
}: PropsWithChildren<{
|
20
|
-
config?: Partial<ProofConfig>;
|
21
|
-
}>) => {
|
22
|
-
const { proverUrl, notaryUrl, wsProxyUrl } = {
|
23
|
-
...DEFAULT_CONFIG[config?.env ?? DEFAULT_CONFIG_ENV],
|
24
|
-
...config,
|
25
|
-
};
|
26
|
-
|
27
|
-
const webProofProvider = createExtensionWebProofProvider({
|
28
|
-
notaryUrl: notaryUrl,
|
29
|
-
wsProxyUrl: wsProxyUrl,
|
30
|
-
});
|
31
|
-
|
32
|
-
const vlayerClient = createVlayerClient({
|
33
|
-
url: proverUrl,
|
34
|
-
webProofProvider,
|
35
|
-
});
|
36
|
-
|
37
|
-
return (
|
38
|
-
<WebProofContext.Provider
|
39
|
-
value={{ webProofProvider, config: { notaryUrl, wsProxyUrl } }}
|
40
|
-
>
|
41
|
-
<ProverContext.Provider value={{ vlayerClient, config: { proverUrl } }}>
|
42
|
-
{children}
|
43
|
-
</ProverContext.Provider>
|
44
|
-
</WebProofContext.Provider>
|
45
|
-
);
|
46
|
-
};
|
47
|
-
|
48
|
-
export const useProofContext = () => {
|
49
|
-
const webProofContext = useContext(WebProofContext);
|
50
|
-
const proverContext = useContext(ProverContext);
|
51
|
-
|
52
|
-
if (!webProofContext || !proverContext) {
|
53
|
-
throw new Error("useProofContext must be used within a ProofProvider");
|
54
|
-
}
|
55
|
-
return {
|
56
|
-
...webProofContext,
|
57
|
-
...proverContext,
|
58
|
-
config: { ...webProofContext.config, ...proverContext.config },
|
59
|
-
};
|
60
|
-
};
|
61
|
-
|
62
|
-
export const useWebProofContext = () => {
|
63
|
-
const webProofContext = useContext(WebProofContext);
|
64
|
-
if (!webProofContext) {
|
65
|
-
throw new Error(
|
66
|
-
"useWebProofContext must be used within a WebProofProvider",
|
67
|
-
);
|
68
|
-
}
|
69
|
-
return webProofContext;
|
70
|
-
};
|
71
|
-
|
72
|
-
export const useProverContext = () => {
|
73
|
-
const proverContext = useContext(ProverContext);
|
74
|
-
if (!proverContext) {
|
75
|
-
throw new Error("useProverContext must be used within a ProverProvider");
|
76
|
-
}
|
77
|
-
return proverContext;
|
78
|
-
};
|
package/src/defaults.ts
DELETED
@@ -1,22 +0,0 @@
|
|
1
|
-
import { ProofEnv } from "./types";
|
2
|
-
|
3
|
-
export const DEFAULT_CONFIG = {
|
4
|
-
[ProofEnv.DEV]: {
|
5
|
-
proverUrl: "http://localhost:3000",
|
6
|
-
notaryUrl: "http://localhost:7047",
|
7
|
-
wsProxyUrl: "ws://localhost:55688",
|
8
|
-
},
|
9
|
-
//for now we use the same urls for testnet and prod
|
10
|
-
[ProofEnv.TESTNET]: {
|
11
|
-
proverUrl: "https://test-prover.vlayer.xyz",
|
12
|
-
notaryUrl: "https://test-notary.vlayer.xyz",
|
13
|
-
wsProxyUrl: "wss://test-wsproxy.vlayer.xyz",
|
14
|
-
},
|
15
|
-
[ProofEnv.PROD]: {
|
16
|
-
proverUrl: "https://test-prover.vlayer.xyz",
|
17
|
-
notaryUrl: "https://test-notary.vlayer.xyz",
|
18
|
-
wsProxyUrl: "wss://test-wsproxy.vlayer.xyz",
|
19
|
-
},
|
20
|
-
};
|
21
|
-
|
22
|
-
export const DEFAULT_CONFIG_ENV = ProofEnv.TESTNET;
|
package/src/index.ts
DELETED
@@ -1,7 +0,0 @@
|
|
1
|
-
import { useWebProof } from "./useWebproof/useWebProof";
|
2
|
-
import { ProofProvider } from "./context";
|
3
|
-
import { useCallProver } from "./useCallProver/useCallProver";
|
4
|
-
import { useWaitForProvingResult } from "./useWaitForProvingResult/useWaitForProvingResult";
|
5
|
-
|
6
|
-
export { ProofProvider, useWebProof, useCallProver, useWaitForProvingResult };
|
7
|
-
export * from "./types";
|
package/src/interface.test.ts
DELETED
@@ -1,12 +0,0 @@
|
|
1
|
-
import { describe, it, expect } from "vitest";
|
2
|
-
import * as hooks from "./index";
|
3
|
-
|
4
|
-
describe("interface", () => {
|
5
|
-
it("should export all must have hooks", () => {
|
6
|
-
expect(Object.keys(hooks)).toEqual(
|
7
|
-
expect.arrayContaining([
|
8
|
-
"useWebProof", // counterpart of webProofProvider.getWebProof https://book.vlayer.xyz/javascript/web-proofs.html
|
9
|
-
]),
|
10
|
-
);
|
11
|
-
});
|
12
|
-
});
|
package/src/types.ts
DELETED
@@ -1,47 +0,0 @@
|
|
1
|
-
import { type VlayerClient, type WebProofProvider } from "@vlayer/sdk";
|
2
|
-
import { expectUrl, notarize, startPage } from "@vlayer/sdk/web_proof";
|
3
|
-
import { type WebProofRequestInput, type GetWebProofArgs } from "@vlayer/sdk";
|
4
|
-
|
5
|
-
export type ProofContextType = {
|
6
|
-
vlayerClient: VlayerClient;
|
7
|
-
webProofProvider: WebProofProvider;
|
8
|
-
config: ProofConfig;
|
9
|
-
};
|
10
|
-
|
11
|
-
export type WebProofContextType = {
|
12
|
-
webProofProvider: WebProofProvider;
|
13
|
-
config: Pick<ProofConfig, "notaryUrl" | "wsProxyUrl">;
|
14
|
-
};
|
15
|
-
|
16
|
-
export type ProverContextType = {
|
17
|
-
vlayerClient: VlayerClient;
|
18
|
-
config: Pick<ProofConfig, "proverUrl">;
|
19
|
-
};
|
20
|
-
|
21
|
-
export enum ProofEnv {
|
22
|
-
DEV = "dev",
|
23
|
-
TESTNET = "testnet",
|
24
|
-
PROD = "prod",
|
25
|
-
}
|
26
|
-
|
27
|
-
export type ProofConfig = {
|
28
|
-
proverUrl: string;
|
29
|
-
notaryUrl: string;
|
30
|
-
wsProxyUrl: string;
|
31
|
-
env?: ProofEnv;
|
32
|
-
};
|
33
|
-
|
34
|
-
export enum WebProofRequestStatus {
|
35
|
-
idle = "idle",
|
36
|
-
pending = "pending",
|
37
|
-
error = "error",
|
38
|
-
success = "success",
|
39
|
-
}
|
40
|
-
|
41
|
-
export {
|
42
|
-
expectUrl,
|
43
|
-
notarize,
|
44
|
-
startPage,
|
45
|
-
type WebProofRequestInput,
|
46
|
-
type GetWebProofArgs,
|
47
|
-
};
|
@@ -1,126 +0,0 @@
|
|
1
|
-
import { renderHook, act } from "@testing-library/react";
|
2
|
-
import { describe, test, expect, vi, beforeEach } from "vitest";
|
3
|
-
import { useCallProver, ProverStatus } from "./useCallProver";
|
4
|
-
import type { Abi } from "viem";
|
5
|
-
import type { ProveArgs } from "@vlayer/sdk";
|
6
|
-
|
7
|
-
const mockVlayerClient = vi.hoisted(() => ({
|
8
|
-
prove: vi.fn(),
|
9
|
-
}));
|
10
|
-
|
11
|
-
const mockChainId = 1;
|
12
|
-
const anotherChainId = 2;
|
13
|
-
|
14
|
-
describe("useCallProver", () => {
|
15
|
-
beforeEach(() => {
|
16
|
-
vi.mock("../context", () => ({
|
17
|
-
useProofContext: () => ({
|
18
|
-
vlayerClient: mockVlayerClient,
|
19
|
-
}),
|
20
|
-
}));
|
21
|
-
|
22
|
-
vi.mock("wagmi", () => ({
|
23
|
-
useChainId: () => mockChainId,
|
24
|
-
}));
|
25
|
-
|
26
|
-
mockVlayerClient.prove.mockReset();
|
27
|
-
});
|
28
|
-
|
29
|
-
test("initial state", () => {
|
30
|
-
const { result } = renderHook(() =>
|
31
|
-
useCallProver({
|
32
|
-
address: "0x456",
|
33
|
-
proverAbi: [] as Abi,
|
34
|
-
functionName: "test",
|
35
|
-
}),
|
36
|
-
);
|
37
|
-
|
38
|
-
expect(result.current).toMatchObject({
|
39
|
-
status: ProverStatus.Idle,
|
40
|
-
error: null,
|
41
|
-
data: null,
|
42
|
-
isIdle: true,
|
43
|
-
isPending: false,
|
44
|
-
isReady: false,
|
45
|
-
isError: false,
|
46
|
-
});
|
47
|
-
});
|
48
|
-
|
49
|
-
test("success", async () => {
|
50
|
-
const mockHash = "0x123";
|
51
|
-
mockVlayerClient.prove.mockResolvedValueOnce({ hash: mockHash });
|
52
|
-
|
53
|
-
const { result } = renderHook(() =>
|
54
|
-
useCallProver({
|
55
|
-
address: "0x456",
|
56
|
-
proverAbi: [] as Abi,
|
57
|
-
functionName: "test",
|
58
|
-
}),
|
59
|
-
);
|
60
|
-
|
61
|
-
await act(async () => {
|
62
|
-
await result.current.callProver([]);
|
63
|
-
});
|
64
|
-
|
65
|
-
expect(result.current).toMatchObject({
|
66
|
-
status: ProverStatus.Ready,
|
67
|
-
data: { hash: mockHash },
|
68
|
-
isReady: true,
|
69
|
-
error: null,
|
70
|
-
isError: false,
|
71
|
-
});
|
72
|
-
});
|
73
|
-
|
74
|
-
test("error on prove failure", async () => {
|
75
|
-
const mockError = new Error("Proving failed");
|
76
|
-
mockVlayerClient.prove.mockRejectedValueOnce(mockError);
|
77
|
-
|
78
|
-
const { result } = renderHook(() =>
|
79
|
-
useCallProver({
|
80
|
-
address: "0x456",
|
81
|
-
proverAbi: {} as Abi,
|
82
|
-
functionName: "test",
|
83
|
-
}),
|
84
|
-
);
|
85
|
-
|
86
|
-
await act(async () => {
|
87
|
-
await result.current.callProver([]);
|
88
|
-
});
|
89
|
-
|
90
|
-
expect(result.current).toMatchObject({
|
91
|
-
status: ProverStatus.Error,
|
92
|
-
error: mockError,
|
93
|
-
isError: true,
|
94
|
-
});
|
95
|
-
});
|
96
|
-
|
97
|
-
test("success with another chainId", async () => {
|
98
|
-
const { result } = renderHook(() =>
|
99
|
-
useCallProver({
|
100
|
-
address: "0x456",
|
101
|
-
proverAbi: [] as Abi,
|
102
|
-
functionName: "test",
|
103
|
-
}),
|
104
|
-
);
|
105
|
-
|
106
|
-
vi.mock("wagmi", () => ({
|
107
|
-
useChainId: () => anotherChainId,
|
108
|
-
}));
|
109
|
-
|
110
|
-
const proverCallArgs: ProveArgs<Abi, string> = {
|
111
|
-
address: "0x456",
|
112
|
-
proverAbi: [] as Abi,
|
113
|
-
functionName: "test",
|
114
|
-
args: [],
|
115
|
-
};
|
116
|
-
|
117
|
-
await act(async () => {
|
118
|
-
await result.current.callProver([]);
|
119
|
-
});
|
120
|
-
|
121
|
-
expect(mockVlayerClient.prove).toHaveBeenCalledWith({
|
122
|
-
...proverCallArgs,
|
123
|
-
chainId: anotherChainId,
|
124
|
-
});
|
125
|
-
});
|
126
|
-
});
|
@@ -1,60 +0,0 @@
|
|
1
|
-
import { useState } from "react";
|
2
|
-
import {
|
3
|
-
type Abi,
|
4
|
-
type AbiStateMutability,
|
5
|
-
type ContractFunctionArgs,
|
6
|
-
type ContractFunctionName,
|
7
|
-
} from "viem";
|
8
|
-
import { type BrandedHash, type ProveArgs } from "@vlayer/sdk";
|
9
|
-
import { useProofContext } from "../context";
|
10
|
-
import { useChainId } from "wagmi";
|
11
|
-
|
12
|
-
export enum ProverStatus {
|
13
|
-
Idle = "Idle",
|
14
|
-
Pending = "Pending",
|
15
|
-
Ready = "Ready",
|
16
|
-
Error = "Error",
|
17
|
-
}
|
18
|
-
|
19
|
-
export const useCallProver = (
|
20
|
-
proveArgs: Omit<ProveArgs<Abi, ContractFunctionName<Abi>>, "args">,
|
21
|
-
) => {
|
22
|
-
// read vlayer client from context
|
23
|
-
const { vlayerClient } = useProofContext();
|
24
|
-
// read chainId from wagmi
|
25
|
-
const chainId = useChainId();
|
26
|
-
|
27
|
-
// state
|
28
|
-
const [status, setStatus] = useState<ProverStatus>(ProverStatus.Idle);
|
29
|
-
const [error, setError] = useState<Error | null>(null);
|
30
|
-
const [hash, setHash] = useState<BrandedHash<Abi, string> | null>(null);
|
31
|
-
|
32
|
-
const callProver = async (
|
33
|
-
args: ContractFunctionArgs<Abi, AbiStateMutability, string>,
|
34
|
-
) => {
|
35
|
-
setStatus(ProverStatus.Pending);
|
36
|
-
try {
|
37
|
-
const hash = await vlayerClient.prove({
|
38
|
-
...proveArgs,
|
39
|
-
args,
|
40
|
-
chainId,
|
41
|
-
});
|
42
|
-
setHash(hash);
|
43
|
-
setStatus(ProverStatus.Ready);
|
44
|
-
} catch (e) {
|
45
|
-
setError(e as Error);
|
46
|
-
setStatus(ProverStatus.Error);
|
47
|
-
}
|
48
|
-
};
|
49
|
-
|
50
|
-
return {
|
51
|
-
callProver,
|
52
|
-
status,
|
53
|
-
error,
|
54
|
-
data: hash,
|
55
|
-
isIdle: status === ProverStatus.Idle,
|
56
|
-
isPending: status === ProverStatus.Pending,
|
57
|
-
isReady: status === ProverStatus.Ready,
|
58
|
-
isError: status === ProverStatus.Error,
|
59
|
-
};
|
60
|
-
};
|