@vlayer/sdk 0.1.0-nightly-20241009-3c85ef5 → 0.1.0-nightly-20241010-e69694a

Sign up to get free protection for your applications and to get access to all the features.
@@ -1 +1,2 @@
1
1
  export * from "./message";
2
+ export * from "./utils";
@@ -1,7 +1,23 @@
1
+ import type { Branded } from "./utils.ts";
2
+
3
+ export const EXTENSION_STEP = {
4
+ expectUrl: "expectUrl",
5
+ startPage: "startPage",
6
+ notarize: "notarize",
7
+ } as const;
8
+
9
+ export type ExtensionStep =
10
+ (typeof EXTENSION_STEP)[keyof typeof EXTENSION_STEP];
11
+
1
12
  export const enum ExtensionAction {
2
13
  RequestWebProof,
3
14
  }
4
15
 
16
+ export type MessageToExtension = {
17
+ action: ExtensionAction;
18
+ payload: WebProverSessionConfig;
19
+ };
20
+
5
21
  export const enum ExtensionMessageType {
6
22
  ProofDone = "ProofDone",
7
23
  ProofError = "ProofError",
@@ -17,4 +33,39 @@ export type ExtensionMessage =
17
33
  export type WebProverSessionConfig = {
18
34
  notaryUrl: string;
19
35
  wsProxyUrl: string;
36
+ logoUrl: string;
37
+ steps: WebProofStep[];
20
38
  };
39
+
40
+ export type WebProofStep =
41
+ | WebProofStepNotarize
42
+ | WebProofStepExpectUrl
43
+ | WebProofStepStartPage;
44
+
45
+ export type WebProofStepNotarize = Branded<
46
+ {
47
+ url: string;
48
+ method: string;
49
+ label: string;
50
+ step: typeof EXTENSION_STEP.notarize;
51
+ },
52
+ "notarize"
53
+ >;
54
+
55
+ export type WebProofStepExpectUrl = Branded<
56
+ {
57
+ url: string;
58
+ label: string;
59
+ step: typeof EXTENSION_STEP.expectUrl;
60
+ },
61
+ "expectUrl"
62
+ >;
63
+
64
+ export type WebProofStepStartPage = Branded<
65
+ {
66
+ url: string;
67
+ label: string;
68
+ step: typeof EXTENSION_STEP.startPage;
69
+ },
70
+ "startPage"
71
+ >;
@@ -0,0 +1,12 @@
1
+ declare const __brand: unique symbol;
2
+ type Brand<B> = { [__brand]: B };
3
+ export type Branded<T, B> = T & Brand<B>;
4
+
5
+ export function isDefined<T>(
6
+ value: T | undefined,
7
+ message: string = "Value is undefined",
8
+ ): asserts value is T {
9
+ if (value === undefined) {
10
+ throw new Error(message);
11
+ }
12
+ }
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "@vlayer/sdk",
3
3
  "type": "module",
4
4
  "module": "src/index.ts",
5
- "version": "0.1.0-nightly-20241009-3c85ef5",
5
+ "version": "0.1.0-nightly-20241010-e69694a",
6
6
  "types": "src/index.ts",
7
7
  "scripts": {
8
8
  "build": "npm run gen:types",
@@ -21,7 +21,7 @@ const rpcUrls: Map<number, HttpTransport> = new Map([[foundry.id, http()]]);
21
21
 
22
22
  export const chainIds = [foundry.id];
23
23
 
24
- export function client(
24
+ export function createAnvilClient(
25
25
  chainId: number = foundry.id,
26
26
  ): ReturnType<typeof walletActions> & PublicClient {
27
27
  const transport = rpcUrls.get(chainId);
@@ -43,7 +43,7 @@ export async function deployContract(
43
43
  args: ContractArg[] = [],
44
44
  chainId: number = foundry.id,
45
45
  ): Promise<Address> {
46
- const ethClient = client(chainId);
46
+ const ethClient = createAnvilClient(chainId);
47
47
 
48
48
  const [deployer] = await ethClient.getAddresses();
49
49
 
@@ -113,7 +113,7 @@ export async function call<
113
113
  args?: ContractFunctionArgs<T, "pure" | "view", F>,
114
114
  chainId: number = foundry.id,
115
115
  ) {
116
- const ethClient = client(chainId);
116
+ const ethClient = createAnvilClient(chainId);
117
117
 
118
118
  return ethClient.readContract({
119
119
  abi,
@@ -134,7 +134,7 @@ export async function writeContract<
134
134
  sender?: Address,
135
135
  chainId: number = foundry.id,
136
136
  ) {
137
- const ethClient = client(chainId);
137
+ const ethClient = createAnvilClient(chainId);
138
138
  const selectedSender = sender || (await ethClient.getAddresses())[0];
139
139
 
140
140
  const txHash = await ethClient.writeContract({
@@ -160,4 +160,4 @@ export const getTestAccount = () => privateKeyToAccount(generatePrivateKey());
160
160
 
161
161
  export const getTestAddresses = (
162
162
  chainId: number = foundry.id,
163
- ): Promise<Address[]> => client(chainId).getAddresses();
163
+ ): Promise<Address[]> => createAnvilClient(chainId).getAddresses();
@@ -1,8 +1,25 @@
1
- import { VlayerClient } from "types/vlayer";
1
+ import { VCallResponse, VlayerClient } from "types/vlayer";
2
2
  import { WebProofProvider } from "types/webProofProvider";
3
3
 
4
4
  import { prove } from "../prover";
5
5
  import { createExtensionWebProofProvider } from "../webProof";
6
+ import { type Abi, decodeFunctionResult } from "viem";
7
+
8
+ function dropProofFromArgs(args: unknown) {
9
+ if (Array.isArray(args)) {
10
+ return args.slice(1);
11
+ }
12
+ return [];
13
+ }
14
+
15
+ function generateRandomHash() {
16
+ let hash = "0x";
17
+ for (let i = 0; i < 40; ++i) {
18
+ hash += Math.floor(Math.random() * 16).toString(16);
19
+ }
20
+ return hash;
21
+ }
22
+
6
23
  export const createVlayerClient = (
7
24
  {
8
25
  url = "http://127.0.0.1:3000",
@@ -17,9 +34,43 @@ export const createVlayerClient = (
17
34
  ): VlayerClient => {
18
35
  // TODO : implement high level api
19
36
  console.log("createVlayerClient with", url, webProofProvider);
37
+ const resultHashMap = new Map<
38
+ string,
39
+ [Promise<VCallResponse>, Abi, string]
40
+ >();
41
+
20
42
  return {
21
43
  prove: async ({ address, functionName, chainId, proverAbi, args }) => {
22
- return prove(address, proverAbi, functionName, args, chainId, url);
44
+ const result_promise = prove(
45
+ address,
46
+ proverAbi,
47
+ functionName,
48
+ args,
49
+ chainId,
50
+ url,
51
+ );
52
+ const hash = generateRandomHash();
53
+ resultHashMap.set(hash, [result_promise, proverAbi, functionName]);
54
+ return { hash };
55
+ },
56
+ waitForProvingResult: async ({ hash }) => {
57
+ const savedProvingData = resultHashMap.get(hash);
58
+ if (!savedProvingData) {
59
+ throw new Error("No result found for hash " + hash);
60
+ }
61
+ const {
62
+ result: { proof, evm_call_result },
63
+ } = await savedProvingData[0];
64
+
65
+ const result = dropProofFromArgs(
66
+ decodeFunctionResult({
67
+ abi: savedProvingData[1] as Abi,
68
+ data: evm_call_result,
69
+ functionName: savedProvingData[2] as string,
70
+ }),
71
+ );
72
+
73
+ return { proof, result };
23
74
  },
24
75
  };
25
76
  };
@@ -1,5 +1,5 @@
1
1
  import { Abi, Address, Hex } from "viem";
2
- import { Branded } from "./utils";
2
+ import { Branded } from "@vlayer/web-proof-commons";
3
3
  export type Bytecode = {
4
4
  object: Hex;
5
5
  };
@@ -1,5 +1,4 @@
1
1
  export * from "./ethereum";
2
2
  export * from "./webProof";
3
- export * from "./utils";
4
3
  export * from "./vlayer";
5
4
  export * from "./webProofProvider";
@@ -66,7 +66,12 @@ export type VlayerClient = {
66
66
  F extends ContractFunctionName<T>,
67
67
  >(
68
68
  args: VlayerClientProveArgs<T, F>,
69
- ) => void;
69
+ ) => Promise<{ hash: string }>;
70
+ waitForProvingResult: ({
71
+ hash,
72
+ }: {
73
+ hash: string;
74
+ }) => Promise<{ proof: Proof; result: unknown[] }>;
70
75
  };
71
76
 
72
77
  export type VlayerClientProveArgs<
@@ -1,44 +1,11 @@
1
1
  import { WebProof } from "types/webProof.ts";
2
2
  import { AbiFunction, Hex, Abi, ContractFunctionName } from "viem";
3
- import { Branded } from "types/utils.ts";
4
3
  import type { ContractFunctionArgsWithout } from "./viem";
5
-
6
- export const EXTENSION_STEP = {
7
- expectUrl: "expectUrl",
8
- startPage: "startPage",
9
- notarize: "notarize",
10
- } as const;
11
-
12
- export type ExtensionStep =
13
- (typeof EXTENSION_STEP)[keyof typeof EXTENSION_STEP];
14
-
15
- export type WebProofStepNotarize = Branded<
16
- {
17
- url: string;
18
- method: string;
19
- label: string;
20
- step: typeof EXTENSION_STEP.notarize;
21
- },
22
- "notarize"
23
- >;
24
-
25
- export type WebProofStepExpectUrl = Branded<
26
- {
27
- url: string;
28
- label: string;
29
- step: typeof EXTENSION_STEP.expectUrl;
30
- },
31
- "expectUrl"
32
- >;
33
-
34
- export type WebProofStepStartPage = Branded<
35
- {
36
- url: string;
37
- label: string;
38
- step: typeof EXTENSION_STEP.startPage;
39
- },
40
- "startPage"
41
- >;
4
+ import {
5
+ Branded,
6
+ WebProofStepExpectUrl,
7
+ WebProofStepStartPage,
8
+ } from "@vlayer/web-proof-commons";
42
9
 
43
10
  export type WebProofSetupInput = {
44
11
  logoUrl: string;
@@ -62,6 +29,7 @@ export type ProverCallCommitment<
62
29
  commitmentArgs: ContractFunctionArgsWithout<T, F, { name: "webProof" }>;
63
30
  chainId: number;
64
31
  };
32
+
65
33
  export type GetWebProofArgs<
66
34
  T extends readonly [AbiFunction, ...Abi[number][]],
67
35
  F extends ContractFunctionName<T>,
package/src/api/prover.ts CHANGED
@@ -5,19 +5,13 @@ import {
5
5
  type Address,
6
6
  ContractFunctionArgs,
7
7
  ContractFunctionName,
8
- decodeFunctionResult,
9
8
  encodeFunctionData,
10
9
  } from "viem";
11
10
 
12
11
  import { type CallContext, type CallParams } from "types/vlayer";
13
12
  import { v_call } from "./v_call";
14
- import { ContractSpec } from "types/ethereum";
15
13
  import { foundry } from "viem/chains";
16
14
 
17
- export async function getContractSpec(file: string): Promise<ContractSpec> {
18
- return Bun.file(file).json();
19
- }
20
-
21
15
  export async function prove<
22
16
  T extends readonly [AbiFunction, ...Abi[number][]],
23
17
  F extends ContractFunctionName<T>,
@@ -40,16 +34,5 @@ export async function prove<
40
34
  chain_id: chainId,
41
35
  };
42
36
 
43
- const {
44
- result: { proof, evm_call_result },
45
- } = await v_call(call, context, url);
46
-
47
- const [, ...result] = decodeFunctionResult({
48
- abi: abi as Abi,
49
- data: evm_call_result,
50
- functionName: functionName as string,
51
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
52
- }) as any[];
53
-
54
- return { proof, result };
37
+ return v_call(call, context, url);
55
38
  }
@@ -6,8 +6,9 @@ import {
6
6
 
7
7
  import {
8
8
  ExtensionAction,
9
- ExtensionMessage,
9
+ type ExtensionMessage,
10
10
  ExtensionMessageType,
11
+ type MessageToExtension,
11
12
  } from "@vlayer/web-proof-commons";
12
13
 
13
14
  import { WebProof } from "../../lib/types/webProof";
@@ -20,7 +21,10 @@ import { WebProof } from "../../lib/types/webProof";
20
21
 
21
22
  declare const chrome: {
22
23
  runtime: {
23
- sendMessage: (extensionId: string | undefined, message: unknown) => void;
24
+ sendMessage: (
25
+ extensionId: string | undefined,
26
+ message: MessageToExtension,
27
+ ) => void;
24
28
  connect: (extensionId: string) => {
25
29
  onMessage: {
26
30
  addListener: (message: unknown) => void;
@@ -1,7 +1,7 @@
1
1
  import {
2
2
  EXTENSION_STEP,
3
3
  WebProofStepExpectUrl,
4
- } from "../../../api/lib/types/webProofProvider";
4
+ } from "@vlayer/web-proof-commons";
5
5
 
6
6
  export const expectUrl = (url: string, label: string) => {
7
7
  return {
@@ -1,7 +1,7 @@
1
1
  import {
2
2
  EXTENSION_STEP,
3
3
  WebProofStepNotarize,
4
- } from "../../../api/lib/types/webProofProvider";
4
+ } from "@vlayer/web-proof-commons";
5
5
 
6
6
  export const notarize = (
7
7
  url: string,
@@ -1,7 +1,7 @@
1
1
  import {
2
2
  EXTENSION_STEP,
3
3
  WebProofStepStartPage,
4
- } from "../../../api/lib/types/webProofProvider";
4
+ } from "@vlayer/web-proof-commons";
5
5
 
6
6
  export const startPage = (url: string, label: string) => {
7
7
  return {
package/src/index.ts CHANGED
@@ -1,10 +1,4 @@
1
- export { v_call } from "./api/v_call";
2
- export type { CallParams, CallContext } from "types/vlayer";
3
- export type { ContractSpec } from "types/ethereum";
4
-
5
- export { getContractSpec } from "./api/prover";
6
1
  export * as testHelpers from "./api/helpers";
7
- export { client as createTestClient } from "./api/helpers";
8
2
  export { preverifyEmail } from "./api/email/preverify.ts";
9
3
  export { createVlayerClient } from "./api/lib/client.ts";
10
4
 
@@ -1,3 +0,0 @@
1
- declare const __brand: unique symbol;
2
- type Brand<B> = { [__brand]: B };
3
- export type Branded<T, B> = T & Brand<B>;