@vlayer/sdk 0.1.0-nightly-20241205-223f353 → 0.1.0-nightly-20241209-52a03e4

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,4 +1,4 @@
1
- import { prove } from "../prover.js";
1
+ import { prove, getProofReceipt } from "../prover.js";
2
2
  import { createExtensionWebProofProvider } from "../webProof/index.js";
3
3
  import { decodeFunctionResult, } from "viem";
4
4
  import { ZkProvingStatus } from "../../web-proof-commons/index.js";
@@ -10,7 +10,7 @@ function dropEmptyProofFromArgs(args) {
10
10
  }
11
11
  async function getHash(vcall_response) {
12
12
  const result = await vcall_response;
13
- return [result.result.hash, result];
13
+ return result.result;
14
14
  }
15
15
  export const createVlayerClient = ({ url = "http://127.0.0.1:3000", webProofProvider = createExtensionWebProofProvider(), } = {
16
16
  url: "http://127.0.0.1:3000",
@@ -29,21 +29,17 @@ export const createVlayerClient = ({ url = "http://127.0.0.1:3000", webProofProv
29
29
  webProofProvider.notifyZkProvingStatus(ZkProvingStatus.Done);
30
30
  return result;
31
31
  });
32
- const [hash, result_promise] = await getHash(response);
33
- resultHashMap.set(hash, [
34
- Promise.resolve(result_promise),
35
- proverAbi,
36
- functionName,
37
- ]);
32
+ const hash = await getHash(response);
33
+ resultHashMap.set(hash, [proverAbi, functionName]);
38
34
  return { hash };
39
35
  },
40
36
  waitForProvingResult: async ({ hash, }) => {
37
+ const { result: { proof, evm_call_result }, } = await getProofReceipt({ hash }, url);
41
38
  const savedProvingData = resultHashMap.get(hash);
42
39
  if (!savedProvingData) {
43
40
  throw new Error("No result found for hash " + hash);
44
41
  }
45
- const [result_promise, proverAbi, functionName] = savedProvingData;
46
- const { result: { proof, evm_call_result }, } = await result_promise;
42
+ const [proverAbi, functionName] = savedProvingData;
47
43
  const result = dropEmptyProofFromArgs(decodeFunctionResult({
48
44
  abi: proverAbi,
49
45
  data: evm_call_result,
@@ -1,4 +1,4 @@
1
- import { describe, expect, it, vi, beforeEach } from "vitest";
1
+ import { describe, expect, it, vi, beforeEach, beforeAll, } from "vitest";
2
2
  import { createExtensionWebProofProvider } from "../webProof/index.js";
3
3
  import { createVlayerClient } from "./client.js";
4
4
  import { ZkProvingStatus } from "src/web-proof-commons";
@@ -28,35 +28,46 @@ function generateRandomHash() {
28
28
  return hash;
29
29
  }
30
30
  describe("Success zk-proving", () => {
31
- beforeEach(() => {
32
- fetchMocker.mockResponseOnce((req) => {
33
- if (req.url === "http://127.0.0.1:3000/") {
34
- return {
35
- body: JSON.stringify({
36
- result: {
37
- hash: generateRandomHash(),
38
- proof: {},
39
- },
40
- }),
41
- };
42
- }
43
- return {};
44
- });
45
- });
46
- it("should send message to extension that zkproving started and then that is done", async () => {
31
+ let hash;
32
+ let zkProvingSpy;
33
+ let vlayer;
34
+ beforeAll(() => {
35
+ hash = generateRandomHash();
47
36
  const webProofProvider = createExtensionWebProofProvider();
48
- const zkProvingSpy = vi.spyOn(webProofProvider, "notifyZkProvingStatus");
49
- const vlayer = createVlayerClient({ webProofProvider });
50
- const hash = await vlayer.prove({
37
+ zkProvingSpy = vi.spyOn(webProofProvider, "notifyZkProvingStatus");
38
+ vlayer = createVlayerClient({ webProofProvider });
39
+ });
40
+ it("should send message to extension that zkproving started", async () => {
41
+ fetchMocker.mockResponseOnce(() => {
42
+ return {
43
+ body: JSON.stringify({
44
+ result: hash,
45
+ }),
46
+ };
47
+ });
48
+ const result = await vlayer.prove({
51
49
  address: `0x${"a".repeat(40)}`,
52
50
  functionName: "main",
53
51
  proverAbi: [],
54
52
  args: [],
55
53
  chainId: 42,
56
54
  });
57
- await vlayer.waitForProvingResult(hash);
55
+ expect(result.hash).toBe(hash);
58
56
  expect(zkProvingSpy).toBeCalledTimes(2);
59
57
  expect(zkProvingSpy).toHaveBeenNthCalledWith(1, ZkProvingStatus.Proving);
58
+ });
59
+ it("should send message to extension that zkproving is done", async () => {
60
+ fetchMocker.mockResponseOnce(() => {
61
+ return {
62
+ body: JSON.stringify({
63
+ result: {
64
+ proof: {},
65
+ },
66
+ }),
67
+ };
68
+ });
69
+ await vlayer.waitForProvingResult({ hash });
70
+ expect(zkProvingSpy).toBeCalledTimes(2);
60
71
  expect(zkProvingSpy).toHaveBeenNthCalledWith(2, ZkProvingStatus.Done);
61
72
  });
62
73
  });
@@ -7,7 +7,7 @@ export type ContractSpec = {
7
7
  abi: Abi;
8
8
  bytecode: Bytecode;
9
9
  };
10
- export type ContractArg = number | string | boolean | bigint | number[] | string[] | boolean[] | bigint[] | Address[];
10
+ export type ContractArg = number | string | boolean | bigint | number[] | string[] | boolean[] | bigint[] | Address[] | (string | bigint)[] | (string | bigint)[][];
11
11
  export type EthereumAddress = Branded<Hex, "EthereumAddress">;
12
12
  export type EthereumTxHash = Branded<Hex, "EthereumTxHash">;
13
13
  export declare function assertEthereumAddress(hash: string): asserts hash is EthereumAddress;
@@ -29,14 +29,22 @@ export type Proof = {
29
29
  settleBlockNumber: bigint;
30
30
  };
31
31
  };
32
- export interface VCallResult {
32
+ export type VCallResult = Hex;
33
+ export interface VCallResponse {
34
+ jsonrpc: string;
35
+ result: VCallResult;
36
+ id: number;
37
+ }
38
+ export type VGetProofReceiptParams = {
33
39
  hash: Hex;
40
+ };
41
+ export interface VGetProofReceiptResult {
34
42
  evm_call_result: Hex;
35
43
  proof: Proof;
36
44
  }
37
- export interface VCallResponse {
45
+ export interface VGetProofReceiptResponse {
38
46
  jsonrpc: string;
39
- result: VCallResult;
47
+ result: VGetProofReceiptResult;
40
48
  id: number;
41
49
  }
42
50
  export type VlayerClient = {
@@ -1,5 +1,7 @@
1
1
  import { type Abi, AbiStateMutability, type Address, ContractFunctionArgs, ContractFunctionName } from "viem";
2
+ import { type BrandedHash } from "./lib/types/vlayer.js";
2
3
  export interface ProveOptions {
3
4
  preverifyVersions?: boolean;
4
5
  }
5
6
  export declare function prove<T extends Abi, F extends ContractFunctionName<T>>(prover: Address, abi: T, functionName: F, args: ContractFunctionArgs<T, AbiStateMutability, F>, chainId?: number, gasLimit?: number, url?: string, options?: ProveOptions): Promise<import("./lib/types/vlayer.js").VCallResponse>;
7
+ export declare function getProofReceipt<T extends Abi, F extends ContractFunctionName<T>>(hash: BrandedHash<T, F>, url?: string): Promise<import("./lib/types/vlayer.js").VGetProofReceiptResponse>;
@@ -1,5 +1,6 @@
1
1
  import { encodeFunctionData, } from "viem";
2
2
  import { v_call } from "./v_call.js";
3
+ import { v_getProofReceipt } from "./v_getProofReceipt.js";
3
4
  import { foundry } from "viem/chains";
4
5
  import { v_versions } from "./v_versions.js";
5
6
  import { checkVersionCompatibility } from "./utils/versions.js";
@@ -25,3 +26,9 @@ export async function prove(prover, abi, functionName, args, chainId = foundry.i
25
26
  };
26
27
  return v_call(call, context, url);
27
28
  }
29
+ export async function getProofReceipt(hash, url = "http://127.0.0.1:3000") {
30
+ const params = {
31
+ hash: hash.hash,
32
+ };
33
+ return v_getProofReceipt(params, url);
34
+ }
@@ -0,0 +1,2 @@
1
+ import { VGetProofReceiptParams, VGetProofReceiptResponse } from "./lib/types/vlayer.js";
2
+ export declare function v_getProofReceipt(params: VGetProofReceiptParams, url?: string): Promise<VGetProofReceiptResponse>;
@@ -0,0 +1,32 @@
1
+ import { parseVCallResponseError } from "./lib/errors.js";
2
+ function v_getProofReceiptBody(params) {
3
+ return {
4
+ method: "v_getProofReceipt",
5
+ params: params,
6
+ id: 1,
7
+ jsonrpc: "2.0",
8
+ };
9
+ }
10
+ export async function v_getProofReceipt(params, url = "http://127.0.0.1:3000") {
11
+ const response = await fetch(url, {
12
+ method: "POST",
13
+ body: JSON.stringify(v_getProofReceiptBody(params)),
14
+ headers: { "Content-Type": "application/json" },
15
+ });
16
+ console.log("response", response);
17
+ if (!response.ok) {
18
+ throw new Error(`HTTP error! status: ${response.status}`);
19
+ }
20
+ const response_json = await response.json();
21
+ console.log("response_json", response_json);
22
+ assertObject(response_json);
23
+ if ("error" in response_json) {
24
+ throw parseVCallResponseError(response_json.error);
25
+ }
26
+ return response_json;
27
+ }
28
+ function assertObject(x) {
29
+ if (typeof x !== "object") {
30
+ throw new Error("Expected object");
31
+ }
32
+ }
@@ -31,6 +31,10 @@ class ExtensionWebProofProvider {
31
31
  connectToExtension() {
32
32
  if (!this.port) {
33
33
  this.port = chrome.runtime.connect(EXTENSION_ID);
34
+ this.port.onDisconnect.addListener(() => {
35
+ this.port = null;
36
+ this.connectToExtension();
37
+ });
34
38
  this.port.onMessage.addListener((message) => {
35
39
  if (message.type === ExtensionMessageType.ProofDone) {
36
40
  this.listeners[ExtensionMessageType.ProofDone]?.forEach((cb) => {
@@ -1,7 +1,29 @@
1
1
  import { createExtensionWebProofProvider } from "./extension.js";
2
- import { describe, it, expect } from "vitest";
2
+ import { describe, it, expect, vi } from "vitest";
3
3
  import { expectUrl, startPage, notarize } from "../steps/index.js";
4
4
  import { StepValidationError } from "../../../web-proof-commons/index.js";
5
+ const chrome = {
6
+ runtime: {
7
+ disconnectCallbacks: [],
8
+ connect: vi.fn().mockImplementation(() => ({
9
+ postMessage: vi.fn().mockImplementation(() => { }),
10
+ onMessage: {
11
+ addListener: vi.fn().mockImplementation(() => { }),
12
+ },
13
+ onDisconnect: {
14
+ addListener: vi.fn().mockImplementation((callback) => {
15
+ chrome.runtime.disconnectCallbacks.push(callback);
16
+ }),
17
+ },
18
+ })),
19
+ disconnect: vi.fn().mockImplementation(() => {
20
+ chrome.runtime.disconnectCallbacks.forEach((callback) => {
21
+ callback();
22
+ });
23
+ }),
24
+ },
25
+ };
26
+ vi.stubGlobal("chrome", chrome);
5
27
  const defaults = {
6
28
  logoUrl: "https://example.com/logo.png",
7
29
  proverCallCommitment: {
@@ -60,6 +82,20 @@ describe("ExtensionWebProofProvider", () => {
60
82
  expectUrl(validUrl, label),
61
83
  notarize(validUrl, "GET", label),
62
84
  ],
63
- })).not.toThrow(StepValidationError);
85
+ })).not.toThrow();
86
+ });
87
+ it("should reconnect extension on disconnect", () => {
88
+ const provider = createExtensionWebProofProvider();
89
+ provider.requestWebProof({
90
+ ...defaults,
91
+ steps: [
92
+ startPage(validUrl, label),
93
+ expectUrl(validUrlPattern, label),
94
+ notarize(validUrlPattern, "GET", label),
95
+ ],
96
+ });
97
+ chrome.runtime.connect.mockClear();
98
+ chrome.runtime.disconnect();
99
+ expect(chrome.runtime.connect).toHaveBeenCalled();
64
100
  });
65
101
  });
package/package.json CHANGED
@@ -15,7 +15,7 @@
15
15
  "types": "./dist/config/index.d.ts"
16
16
  }
17
17
  },
18
- "version": "0.1.0-nightly-20241205-223f353",
18
+ "version": "0.1.0-nightly-20241209-52a03e4",
19
19
  "scripts": {
20
20
  "build": "bun tsc && bun tsc-alias",
21
21
  "test:unit": "vitest --run",