@vlayer/sdk 0.1.0-nightly-20241105-422ee80 → 0.1.0-nightly-20241106-04bef04

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,6 +1,7 @@
1
1
  import { prove } from "../prover";
2
2
  import { createExtensionWebProofProvider } from "../webProof";
3
3
  import { decodeFunctionResult } from "viem";
4
+ import { ZkProvingStatus } from "../../web-proof-commons";
4
5
  function dropEmptyProofFromArgs(args) {
5
6
  if (Array.isArray(args)) {
6
7
  return args.slice(1);
@@ -14,17 +15,27 @@ function generateRandomHash() {
14
15
  }
15
16
  return hash;
16
17
  }
18
+ async function getHash() {
19
+ return Promise.resolve(generateRandomHash());
20
+ }
17
21
  export const createVlayerClient = ({ url = "http://127.0.0.1:3000", webProofProvider = createExtensionWebProofProvider(), } = {
18
22
  url: "http://127.0.0.1:3000",
19
23
  webProofProvider: createExtensionWebProofProvider(),
20
24
  }) => {
21
- console.log("createVlayerClient with", url, webProofProvider);
22
25
  const resultHashMap = new Map();
23
26
  return {
24
- // eslint-disable-next-line @typescript-eslint/require-await
25
27
  prove: async ({ address, functionName, chainId, proverAbi, args }) => {
26
- const result_promise = prove(address, proverAbi, functionName, args, chainId, url);
27
- const hash = generateRandomHash();
28
+ webProofProvider.notifyZkProvingStatus(ZkProvingStatus.Proving);
29
+ const result_promise = prove(address, proverAbi, functionName, args, chainId, url)
30
+ .catch((error) => {
31
+ webProofProvider.notifyZkProvingStatus(ZkProvingStatus.Error);
32
+ throw error;
33
+ })
34
+ .then((result) => {
35
+ webProofProvider.notifyZkProvingStatus(ZkProvingStatus.Done);
36
+ return result;
37
+ });
38
+ const hash = await getHash();
28
39
  resultHashMap.set(hash, [result_promise, proverAbi, functionName]);
29
40
  return { hash };
30
41
  },
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,87 @@
1
+ import { describe, expect, it, vi, beforeEach } from "vitest";
2
+ import { createExtensionWebProofProvider } from "../webProof";
3
+ import { createVlayerClient } from "./client";
4
+ import { ZkProvingStatus } from "src/web-proof-commons";
5
+ import createFetchMock from "vitest-fetch-mock";
6
+ const fetchMocker = createFetchMock(vi);
7
+ fetchMocker.enableMocks();
8
+ beforeEach(() => {
9
+ global.chrome = {
10
+ runtime: {
11
+ sendMessage: vi.fn(),
12
+ connect: vi.fn().mockImplementation(() => {
13
+ return {
14
+ onMessage: {
15
+ addListener: vi.fn(),
16
+ },
17
+ postMessage: vi.fn(),
18
+ };
19
+ }),
20
+ },
21
+ };
22
+ });
23
+ describe("Success zk-proving", () => {
24
+ beforeEach(() => {
25
+ fetchMocker.mockResponseOnce((req) => {
26
+ if (req.url === "http://127.0.0.1:3000/") {
27
+ return {
28
+ body: JSON.stringify({
29
+ result: {
30
+ proof: {},
31
+ },
32
+ }),
33
+ };
34
+ }
35
+ return {};
36
+ });
37
+ });
38
+ it("should send message to extension that zkproving started and then that is done", async () => {
39
+ const webProofProvider = createExtensionWebProofProvider();
40
+ const zkProvingSpy = vi.spyOn(webProofProvider, "notifyZkProvingStatus");
41
+ const vlayer = createVlayerClient({ webProofProvider });
42
+ const { hash } = await vlayer.prove({
43
+ address: `0x${"a".repeat(40)}`,
44
+ functionName: "main",
45
+ proverAbi: [],
46
+ args: [],
47
+ chainId: 42,
48
+ });
49
+ await vlayer.waitForProvingResult({ hash });
50
+ expect(zkProvingSpy).toBeCalledTimes(2);
51
+ expect(zkProvingSpy).toHaveBeenNthCalledWith(1, ZkProvingStatus.Proving);
52
+ expect(zkProvingSpy).toHaveBeenNthCalledWith(2, ZkProvingStatus.Done);
53
+ });
54
+ });
55
+ describe("Failed zk-proving", () => {
56
+ beforeEach(() => {
57
+ fetchMocker.mockResponseOnce((req) => {
58
+ if (req.url === "http://127.0.0.1:3000/") {
59
+ return {
60
+ status: 500,
61
+ };
62
+ }
63
+ return {};
64
+ });
65
+ });
66
+ it("should send message to extension that zkproving started and then that failed ", async () => {
67
+ const webProofProvider = createExtensionWebProofProvider();
68
+ const zkProvingSpy = vi.spyOn(webProofProvider, "notifyZkProvingStatus");
69
+ const vlayer = createVlayerClient({ webProofProvider });
70
+ const { hash } = await vlayer.prove({
71
+ address: `0x${"a".repeat(40)}`,
72
+ functionName: "main",
73
+ proverAbi: [],
74
+ args: [],
75
+ chainId: 42,
76
+ });
77
+ try {
78
+ await vlayer.waitForProvingResult({ hash });
79
+ }
80
+ catch (e) {
81
+ console.log("Error waiting for proving result", e);
82
+ }
83
+ expect(zkProvingSpy).toBeCalledTimes(2);
84
+ expect(zkProvingSpy).toHaveBeenNthCalledWith(1, ZkProvingStatus.Proving);
85
+ expect(zkProvingSpy).toHaveBeenNthCalledWith(2, ZkProvingStatus.Error);
86
+ });
87
+ });
@@ -1,6 +1,6 @@
1
1
  import { Hex, Abi, ContractFunctionName } from "viem";
2
2
  import type { ContractFunctionArgsWithout } from "./viem";
3
- import { Branded, WebProof, WebProofStep } from "../../../web-proof-commons";
3
+ import { Branded, WebProof, WebProofStep, ZkProvingStatus } from "../../../web-proof-commons";
4
4
  export type WebProofSetupInput = {
5
5
  logoUrl: string;
6
6
  steps: WebProofStep[];
@@ -22,6 +22,7 @@ export type GetWebProofArgs<T extends Abi, F extends ContractFunctionName<T>> =
22
22
  } & WebProofSetupInput;
23
23
  export type WebProofProvider = {
24
24
  getWebProof: <T extends Abi, F extends ContractFunctionName<T>>(args: GetWebProofArgs<T, F>) => Promise<WebProof>;
25
+ notifyZkProvingStatus: (status: ZkProvingStatus) => void;
25
26
  };
26
27
  export type WebProofProviderSetup = {
27
28
  notaryUrl?: string;
@@ -16,7 +16,6 @@ export async function v_call(call, context, url = "http://127.0.0.1:3000") {
16
16
  throw new Error(`HTTP error! status: ${response.status}`);
17
17
  }
18
18
  const response_json = await response.json();
19
- //TODO we should launch some schema validation here
20
19
  assertObject(response_json);
21
20
  if ("error" in response_json) {
22
21
  throw new Error(`Error response: ${response_json.error.message || "unknown error"}`);
@@ -1,32 +1,53 @@
1
+ import { ExtensionMessageType, } from "../../../web-proof-commons";
1
2
  // this id is fixed in the extension by the key in manifest.json
2
3
  const EXTENSION_ID = "jbchhcgphfokabmfacnkafoeeeppjmpl";
3
- export const createExtensionWebProofProvider = ({ notaryUrl = "https://notary.pse.dev/v0.1.0-alpha.5/", wsProxyUrl = "wss://notary.pse.dev/proxy", } = {
4
- notaryUrl: "https://notary.pse.dev/v0.1.0-alpha.5/",
5
- wsProxyUrl: "wss://notary.pse.dev/proxy",
6
- }) => {
7
- return {
8
- getWebProof: async function (webProofSetup) {
9
- return new Promise((resolve, reject) => {
10
- chrome.runtime.sendMessage(EXTENSION_ID, {
11
- action: 0 /* ExtensionAction.RequestWebProof */,
12
- payload: {
13
- notaryUrl,
14
- wsProxyUrl,
15
- logoUrl: webProofSetup.logoUrl,
16
- steps: webProofSetup.steps,
17
- },
18
- });
19
- const port = chrome.runtime.connect(EXTENSION_ID);
20
- // TODO: validate message in runtime
21
- port.onMessage.addListener((message) => {
22
- if (message.type === "ProofDone" /* ExtensionMessageType.ProofDone */) {
23
- resolve(message.proof);
24
- }
25
- if (message.type === "ProofError" /* ExtensionMessageType.ProofError */) {
26
- reject(new Error(message.error));
27
- }
28
- });
4
+ class ExtensionWebProofProvider {
5
+ notaryUrl;
6
+ wsProxyUrl;
7
+ port = null;
8
+ constructor(notaryUrl, wsProxyUrl) {
9
+ this.notaryUrl = notaryUrl;
10
+ this.wsProxyUrl = wsProxyUrl;
11
+ }
12
+ notifyZkProvingStatus(status) {
13
+ if (typeof chrome !== "undefined") {
14
+ this.connectToExtension().postMessage({
15
+ action: 1 /* ExtensionAction.NotifyZkProvingStatus */,
16
+ payload: { status },
29
17
  });
30
- },
31
- };
18
+ }
19
+ else {
20
+ console.log("Run out of chrome context");
21
+ }
22
+ }
23
+ connectToExtension() {
24
+ if (!this.port) {
25
+ this.port = chrome.runtime.connect(EXTENSION_ID);
26
+ }
27
+ return this.port;
28
+ }
29
+ async getWebProof(webProofSetup) {
30
+ return new Promise((resolve, reject) => {
31
+ chrome.runtime.sendMessage(EXTENSION_ID, {
32
+ action: 0 /* ExtensionAction.RequestWebProof */,
33
+ payload: {
34
+ notaryUrl: this.notaryUrl,
35
+ wsProxyUrl: this.wsProxyUrl,
36
+ logoUrl: webProofSetup.logoUrl,
37
+ steps: webProofSetup.steps,
38
+ },
39
+ });
40
+ this.connectToExtension().onMessage.addListener((message) => {
41
+ if (message.type === ExtensionMessageType.ProofDone) {
42
+ resolve(message.proof);
43
+ }
44
+ if (message.type === ExtensionMessageType.ProofError) {
45
+ reject(new Error(message.error));
46
+ }
47
+ });
48
+ });
49
+ }
50
+ }
51
+ export const createExtensionWebProofProvider = ({ notaryUrl = "https://notary.pse.dev/v0.1.0-alpha.5/", wsProxyUrl = "wss://notary.pse.dev/proxy", } = {}) => {
52
+ return new ExtensionWebProofProvider(notaryUrl, wsProxyUrl);
32
53
  };
@@ -7,13 +7,24 @@ export declare const EXTENSION_STEP: {
7
7
  };
8
8
  export type ExtensionStep = (typeof EXTENSION_STEP)[keyof typeof EXTENSION_STEP];
9
9
  export declare const enum ExtensionAction {
10
- RequestWebProof = 0
10
+ RequestWebProof = 0,
11
+ NotifyZkProvingStatus = 1
12
+ }
13
+ export declare enum ZkProvingStatus {
14
+ Proving = "proving",
15
+ Done = "done",
16
+ Error = "error"
11
17
  }
12
18
  export type MessageToExtension = {
13
- action: ExtensionAction;
19
+ action: ExtensionAction.RequestWebProof;
14
20
  payload: WebProverSessionConfig;
21
+ } | {
22
+ action: ExtensionAction.NotifyZkProvingStatus;
23
+ payload: {
24
+ status: ZkProvingStatus;
25
+ };
15
26
  };
16
- export declare const enum ExtensionMessageType {
27
+ export declare enum ExtensionMessageType {
17
28
  ProofDone = "ProofDone",
18
29
  ProofError = "ProofError",
19
30
  RedirectBack = "RedirectBack",
@@ -3,3 +3,16 @@ export const EXTENSION_STEP = {
3
3
  startPage: "startPage",
4
4
  notarize: "notarize",
5
5
  };
6
+ export var ZkProvingStatus;
7
+ (function (ZkProvingStatus) {
8
+ ZkProvingStatus["Proving"] = "proving";
9
+ ZkProvingStatus["Done"] = "done";
10
+ ZkProvingStatus["Error"] = "error";
11
+ })(ZkProvingStatus || (ZkProvingStatus = {}));
12
+ export var ExtensionMessageType;
13
+ (function (ExtensionMessageType) {
14
+ ExtensionMessageType["ProofDone"] = "ProofDone";
15
+ ExtensionMessageType["ProofError"] = "ProofError";
16
+ ExtensionMessageType["RedirectBack"] = "RedirectBack";
17
+ ExtensionMessageType["TabOpened"] = "TabOpened";
18
+ })(ExtensionMessageType || (ExtensionMessageType = {}));
package/package.json CHANGED
@@ -11,7 +11,7 @@
11
11
  "types": "./dist/api/webProof/index.d.ts"
12
12
  }
13
13
  },
14
- "version": "0.1.0-nightly-20241105-422ee80",
14
+ "version": "0.1.0-nightly-20241106-04bef04",
15
15
  "scripts": {
16
16
  "build": "bun tsc",
17
17
  "test:unit": "vitest --run",
@@ -22,7 +22,8 @@
22
22
  "@changesets/cli": "^2.27.7",
23
23
  "@types/bun": "latest",
24
24
  "abitype": "^1.0.6",
25
- "vitest": "^2.1.1"
25
+ "vitest": "^2.1.1",
26
+ "vitest-fetch-mock": "^0.4.1"
26
27
  },
27
28
  "peerDependencies": {
28
29
  "typescript": "^5.6.3"