@vlayer/sdk 0.1.0-nightly-20241120-8c3f7ae → 0.1.0-nightly-20241122-7018b34
Sign up to get free protection for your applications and to get access to all the features.
- package/dist/api/lib/client.js +10 -12
- package/dist/api/lib/client.test.js +15 -7
- package/dist/api/lib/errors.d.ts +6 -0
- package/dist/api/lib/errors.js +14 -0
- package/dist/api/lib/types/vlayer.d.ts +1 -0
- package/dist/api/lib/types/webProofProvider.d.ts +5 -1
- package/dist/api/v_call.js +2 -1
- package/dist/api/webProof/providers/extension.js +35 -5
- package/dist/api/webProof/steps/index.d.ts +3 -3
- package/dist/config/createContext.d.ts +2 -2
- package/dist/config/createContext.js +4 -4
- package/dist/config/deploy.js +3 -3
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/dist/web-proof-commons/types/message.d.ts +18 -6
- package/dist/web-proof-commons/types/message.js +1 -0
- package/package.json +1 -1
package/dist/api/lib/client.js
CHANGED
@@ -8,15 +8,9 @@ function dropEmptyProofFromArgs(args) {
|
|
8
8
|
}
|
9
9
|
return [];
|
10
10
|
}
|
11
|
-
function
|
12
|
-
|
13
|
-
|
14
|
-
hash += Math.floor(Math.random() * 16).toString(16);
|
15
|
-
}
|
16
|
-
return hash;
|
17
|
-
}
|
18
|
-
async function getHash() {
|
19
|
-
return Promise.resolve(generateRandomHash());
|
11
|
+
async function getHash(vcall_response) {
|
12
|
+
const result = await vcall_response;
|
13
|
+
return [result.result.hash, result];
|
20
14
|
}
|
21
15
|
export const createVlayerClient = ({ url = "http://127.0.0.1:3000", webProofProvider = createExtensionWebProofProvider(), } = {
|
22
16
|
url: "http://127.0.0.1:3000",
|
@@ -26,7 +20,7 @@ export const createVlayerClient = ({ url = "http://127.0.0.1:3000", webProofProv
|
|
26
20
|
return {
|
27
21
|
prove: async ({ address, functionName, chainId, proverAbi, args }) => {
|
28
22
|
webProofProvider.notifyZkProvingStatus(ZkProvingStatus.Proving);
|
29
|
-
const
|
23
|
+
const response = prove(address, proverAbi, functionName, args, chainId, url)
|
30
24
|
.catch((error) => {
|
31
25
|
webProofProvider.notifyZkProvingStatus(ZkProvingStatus.Error);
|
32
26
|
throw error;
|
@@ -35,8 +29,12 @@ export const createVlayerClient = ({ url = "http://127.0.0.1:3000", webProofProv
|
|
35
29
|
webProofProvider.notifyZkProvingStatus(ZkProvingStatus.Done);
|
36
30
|
return result;
|
37
31
|
});
|
38
|
-
const hash = await getHash();
|
39
|
-
resultHashMap.set(hash, [
|
32
|
+
const [hash, result_promise] = await getHash(response);
|
33
|
+
resultHashMap.set(hash, [
|
34
|
+
Promise.resolve(result_promise),
|
35
|
+
proverAbi,
|
36
|
+
functionName,
|
37
|
+
]);
|
40
38
|
return { hash };
|
41
39
|
},
|
42
40
|
waitForProvingResult: async ({ hash, }) => {
|
@@ -20,6 +20,13 @@ beforeEach(() => {
|
|
20
20
|
},
|
21
21
|
};
|
22
22
|
});
|
23
|
+
function generateRandomHash() {
|
24
|
+
let hash = "0x";
|
25
|
+
for (let i = 0; i < 40; ++i) {
|
26
|
+
hash += Math.floor(Math.random() * 16).toString(16);
|
27
|
+
}
|
28
|
+
return hash;
|
29
|
+
}
|
23
30
|
describe("Success zk-proving", () => {
|
24
31
|
beforeEach(() => {
|
25
32
|
fetchMocker.mockResponseOnce((req) => {
|
@@ -27,6 +34,7 @@ describe("Success zk-proving", () => {
|
|
27
34
|
return {
|
28
35
|
body: JSON.stringify({
|
29
36
|
result: {
|
37
|
+
hash: generateRandomHash(),
|
30
38
|
proof: {},
|
31
39
|
},
|
32
40
|
}),
|
@@ -67,14 +75,14 @@ describe("Failed zk-proving", () => {
|
|
67
75
|
const webProofProvider = createExtensionWebProofProvider();
|
68
76
|
const zkProvingSpy = vi.spyOn(webProofProvider, "notifyZkProvingStatus");
|
69
77
|
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
78
|
try {
|
79
|
+
const hash = await vlayer.prove({
|
80
|
+
address: `0x${"a".repeat(40)}`,
|
81
|
+
functionName: "main",
|
82
|
+
proverAbi: [],
|
83
|
+
args: [],
|
84
|
+
chainId: 42,
|
85
|
+
});
|
78
86
|
await vlayer.waitForProvingResult(hash);
|
79
87
|
}
|
80
88
|
catch (e) {
|
@@ -0,0 +1,14 @@
|
|
1
|
+
export class VersionError extends Error {
|
2
|
+
constructor(message) {
|
3
|
+
super(message);
|
4
|
+
this.name = "VersionError";
|
5
|
+
}
|
6
|
+
}
|
7
|
+
export function parseVCallResponseError({ message, }) {
|
8
|
+
if (message?.startsWith("Unsupported CallGuestID")) {
|
9
|
+
return new VersionError(`${message}
|
10
|
+
vlayer uses the daily release cycle, and SDK version must match the proving server version.
|
11
|
+
Please run "vlayer update" to update the SDK to the latest version.`);
|
12
|
+
}
|
13
|
+
return new Error(`Error response: ${message ?? "unknown error"}`);
|
14
|
+
}
|
@@ -1,6 +1,6 @@
|
|
1
1
|
import { Hex, Abi, ContractFunctionName } from "viem";
|
2
2
|
import type { ContractFunctionArgsWithout } from "./viem.js";
|
3
|
-
import { Branded, WebProof, WebProofStep, ZkProvingStatus } from "../../../web-proof-commons/index.js";
|
3
|
+
import { Branded, ExtensionMessageType, ExtensionMessage, WebProof, WebProofStep, ZkProvingStatus } from "../../../web-proof-commons/index.js";
|
4
4
|
export type WebProofSetupInput = {
|
5
5
|
logoUrl: string;
|
6
6
|
steps: WebProofStep[];
|
@@ -22,7 +22,11 @@ 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
|
+
requestWebProof: <T extends Abi, F extends ContractFunctionName<T>>(args: GetWebProofArgs<T, F>) => void;
|
25
26
|
notifyZkProvingStatus: (status: ZkProvingStatus) => void;
|
27
|
+
addEventListeners: <T extends ExtensionMessageType>(messageType: T, listener: (args: Extract<ExtensionMessage, {
|
28
|
+
type: T;
|
29
|
+
}>) => void) => void;
|
26
30
|
};
|
27
31
|
export type WebProofProviderSetup = {
|
28
32
|
notaryUrl?: string;
|
package/dist/api/v_call.js
CHANGED
@@ -1,3 +1,4 @@
|
|
1
|
+
import { parseVCallResponseError } from "./lib/errors.js";
|
1
2
|
function v_callBody(call, context) {
|
2
3
|
return {
|
3
4
|
method: "v_call",
|
@@ -20,7 +21,7 @@ export async function v_call(call, context, url = "http://127.0.0.1:3000") {
|
|
20
21
|
console.log("response_json", response_json);
|
21
22
|
assertObject(response_json);
|
22
23
|
if ("error" in response_json) {
|
23
|
-
throw
|
24
|
+
throw parseVCallResponseError(response_json.error);
|
24
25
|
}
|
25
26
|
return response_json;
|
26
27
|
}
|
@@ -1,10 +1,10 @@
|
|
1
1
|
import { ExtensionMessageType, } from "../../../web-proof-commons/index.js";
|
2
|
-
// this id is fixed in the extension by the key in manifest.json
|
3
2
|
const EXTENSION_ID = "jbchhcgphfokabmfacnkafoeeeppjmpl";
|
4
3
|
class ExtensionWebProofProvider {
|
5
4
|
notaryUrl;
|
6
5
|
wsProxyUrl;
|
7
6
|
port = null;
|
7
|
+
listeners = {};
|
8
8
|
constructor(notaryUrl, wsProxyUrl) {
|
9
9
|
this.notaryUrl = notaryUrl;
|
10
10
|
this.wsProxyUrl = wsProxyUrl;
|
@@ -12,7 +12,7 @@ class ExtensionWebProofProvider {
|
|
12
12
|
notifyZkProvingStatus(status) {
|
13
13
|
if (typeof chrome !== "undefined") {
|
14
14
|
chrome.runtime.sendMessage(EXTENSION_ID, {
|
15
|
-
action:
|
15
|
+
action: "NotifyZkProvingStatus" /* ExtensionAction.NotifyZkProvingStatus */,
|
16
16
|
payload: { status },
|
17
17
|
});
|
18
18
|
}
|
@@ -20,13 +20,43 @@ class ExtensionWebProofProvider {
|
|
20
20
|
connectToExtension() {
|
21
21
|
if (!this.port) {
|
22
22
|
this.port = chrome.runtime.connect(EXTENSION_ID);
|
23
|
+
this.port.onMessage.addListener((message) => {
|
24
|
+
if (message.type === ExtensionMessageType.ProofDone) {
|
25
|
+
this.listeners[ExtensionMessageType.ProofDone]?.forEach((cb) => {
|
26
|
+
cb(message);
|
27
|
+
});
|
28
|
+
}
|
29
|
+
if (message.type === ExtensionMessageType.ProofError) {
|
30
|
+
this.listeners[ExtensionMessageType.ProofError]?.forEach((cb) => {
|
31
|
+
cb(message);
|
32
|
+
});
|
33
|
+
}
|
34
|
+
});
|
23
35
|
}
|
24
36
|
return this.port;
|
25
37
|
}
|
38
|
+
addEventListeners(messageType, listener) {
|
39
|
+
this.connectToExtension();
|
40
|
+
if (!this.listeners[messageType]) {
|
41
|
+
this.listeners[messageType] = [];
|
42
|
+
}
|
43
|
+
this.listeners[messageType].push(listener);
|
44
|
+
}
|
45
|
+
requestWebProof(webProofSetup) {
|
46
|
+
this.connectToExtension().postMessage({
|
47
|
+
action: "RequestWebProof" /* ExtensionAction.RequestWebProof */,
|
48
|
+
payload: {
|
49
|
+
notaryUrl: this.notaryUrl,
|
50
|
+
wsProxyUrl: this.wsProxyUrl,
|
51
|
+
logoUrl: webProofSetup.logoUrl,
|
52
|
+
steps: webProofSetup.steps,
|
53
|
+
},
|
54
|
+
});
|
55
|
+
}
|
26
56
|
async getWebProof(webProofSetup) {
|
27
57
|
return new Promise((resolve, reject) => {
|
28
58
|
chrome.runtime.sendMessage(EXTENSION_ID, {
|
29
|
-
action:
|
59
|
+
action: "RequestWebProof" /* ExtensionAction.RequestWebProof */,
|
30
60
|
payload: {
|
31
61
|
notaryUrl: this.notaryUrl,
|
32
62
|
wsProxyUrl: this.wsProxyUrl,
|
@@ -36,10 +66,10 @@ class ExtensionWebProofProvider {
|
|
36
66
|
});
|
37
67
|
this.connectToExtension().onMessage.addListener((message) => {
|
38
68
|
if (message.type === ExtensionMessageType.ProofDone) {
|
39
|
-
resolve(message.proof);
|
69
|
+
resolve(message.payload.proof);
|
40
70
|
}
|
41
71
|
if (message.type === ExtensionMessageType.ProofError) {
|
42
|
-
reject(new Error(message.error));
|
72
|
+
reject(new Error(message.payload.error));
|
43
73
|
}
|
44
74
|
});
|
45
75
|
});
|
@@ -2,8 +2,8 @@ import { expectUrl } from "./expectUrl.js";
|
|
2
2
|
import { startPage } from "./startPage.js";
|
3
3
|
import { notarize } from "./notarize.js";
|
4
4
|
declare const steps: {
|
5
|
-
expectUrl: (url: string, label: string) => import("../../../
|
6
|
-
startPage: (url: string, label: string) => import("../../../
|
7
|
-
notarize: (url: string, method: string | undefined, label: string) => import("../../../
|
5
|
+
expectUrl: (url: string, label: string) => import("../../../index.js").WebProofStepExpectUrl;
|
6
|
+
startPage: (url: string, label: string) => import("../../../index.js").WebProofStepStartPage;
|
7
|
+
notarize: (url: string, method: string | undefined, label: string) => import("../../../index.js").WebProofStepNotarize;
|
8
8
|
};
|
9
9
|
export { expectUrl, startPage, notarize, steps };
|
@@ -2,7 +2,7 @@ import { type Chain, type CustomTransport, custom } from "viem";
|
|
2
2
|
import { Config } from "./getConfig.js";
|
3
3
|
export declare const customTransport: typeof custom;
|
4
4
|
export { Chain };
|
5
|
-
export declare const createContext: (config: Config, transport?: CustomTransport) =>
|
5
|
+
export declare const createContext: (config: Config, transport?: CustomTransport) => {
|
6
6
|
chain: Chain;
|
7
7
|
account: {
|
8
8
|
address: import("viem").Address;
|
@@ -4952,4 +4952,4 @@ export declare const createContext: (config: Config, transport?: CustomTransport
|
|
4952
4952
|
chainName: string;
|
4953
4953
|
proverUrl: string;
|
4954
4954
|
privateKey: `0x${string}`;
|
4955
|
-
}
|
4955
|
+
};
|
@@ -1,9 +1,9 @@
|
|
1
1
|
import { createWalletClient, http, publicActions, custom, } from "viem";
|
2
2
|
import { privateKeyToAccount } from "viem/accounts";
|
3
3
|
import { getChainConfirmations } from "./getChainConfirmations.js";
|
4
|
-
|
4
|
+
import * as chains from "viem/chains";
|
5
|
+
const getChainSpecs = (chainName) => {
|
5
6
|
try {
|
6
|
-
const chains = await import("viem/chains");
|
7
7
|
return chains[chainName];
|
8
8
|
}
|
9
9
|
catch {
|
@@ -15,8 +15,8 @@ const createEthClient = (chain, jsonRpcUrl, transport) => createWalletClient({
|
|
15
15
|
chain,
|
16
16
|
transport: transport || http(jsonRpcUrl),
|
17
17
|
}).extend(publicActions);
|
18
|
-
export const createContext =
|
19
|
-
const chain =
|
18
|
+
export const createContext = (config, transport) => {
|
19
|
+
const chain = getChainSpecs(config.chainName);
|
20
20
|
const jsonRpcUrl = config.jsonRpcUrl ?? chain.rpcUrls.default.http[0];
|
21
21
|
return {
|
22
22
|
...config,
|
package/dist/config/deploy.js
CHANGED
@@ -2,7 +2,7 @@ import { getConfig } from "./getConfig.js";
|
|
2
2
|
import { createContext } from "./createContext.js";
|
3
3
|
import { getChainConfirmations } from "./getChainConfirmations.js";
|
4
4
|
export const waitForContractDeploy = async ({ hash, }) => {
|
5
|
-
const { ethClient: client } =
|
5
|
+
const { ethClient: client } = createContext(getConfig());
|
6
6
|
const receipt = await client.waitForTransactionReceipt({
|
7
7
|
hash,
|
8
8
|
confirmations: getChainConfirmations(client.chain?.name),
|
@@ -15,7 +15,7 @@ export const waitForContractDeploy = async ({ hash, }) => {
|
|
15
15
|
return receipt.contractAddress;
|
16
16
|
};
|
17
17
|
export const waitForTransactionReceipt = async ({ hash, }) => {
|
18
|
-
const { ethClient } =
|
18
|
+
const { ethClient } = createContext(getConfig());
|
19
19
|
return ethClient.waitForTransactionReceipt({
|
20
20
|
hash,
|
21
21
|
confirmations: getChainConfirmations(ethClient.chain?.name),
|
@@ -26,7 +26,7 @@ export const waitForTransactionReceipt = async ({ hash, }) => {
|
|
26
26
|
export const deployVlayerContracts = async ({ proverSpec, verifierSpec, proverArgs, verifierArgs, }) => {
|
27
27
|
console.log("Starting contract deployment process...");
|
28
28
|
const config = getConfig();
|
29
|
-
const { chain, ethClient, account } =
|
29
|
+
const { chain, ethClient, account } = createContext(config);
|
30
30
|
console.log("Deploying prover contract...");
|
31
31
|
const proverHash = await ethClient.deployContract({
|
32
32
|
chain,
|
package/dist/index.d.ts
CHANGED
package/dist/index.js
CHANGED
@@ -7,8 +7,8 @@ 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 =
|
11
|
-
NotifyZkProvingStatus =
|
10
|
+
RequestWebProof = "RequestWebProof",
|
11
|
+
NotifyZkProvingStatus = "NotifyZkProvingStatus"
|
12
12
|
}
|
13
13
|
export declare enum ZkProvingStatus {
|
14
14
|
NotStarted = "NotStarted",
|
@@ -29,19 +29,31 @@ export declare enum ExtensionMessageType {
|
|
29
29
|
ProofDone = "ProofDone",
|
30
30
|
ProofError = "ProofError",
|
31
31
|
RedirectBack = "RedirectBack",
|
32
|
-
TabOpened = "TabOpened"
|
32
|
+
TabOpened = "TabOpened",
|
33
|
+
ProofProcessing = "ProofProcessing"
|
33
34
|
}
|
34
35
|
export type ExtensionMessage = {
|
35
36
|
type: ExtensionMessageType.ProofDone;
|
36
|
-
|
37
|
+
payload: {
|
38
|
+
proof: WebProof;
|
39
|
+
};
|
37
40
|
} | {
|
38
41
|
type: ExtensionMessageType.ProofError;
|
39
|
-
|
42
|
+
payload: {
|
43
|
+
error: string;
|
44
|
+
};
|
40
45
|
} | {
|
41
46
|
type: ExtensionMessageType.RedirectBack;
|
42
47
|
} | {
|
43
48
|
type: ExtensionMessageType.TabOpened;
|
44
|
-
|
49
|
+
payload: {
|
50
|
+
tabId: number;
|
51
|
+
};
|
52
|
+
} | {
|
53
|
+
type: ExtensionMessageType.ProofProcessing;
|
54
|
+
payload: {
|
55
|
+
progress?: number;
|
56
|
+
};
|
45
57
|
};
|
46
58
|
export type WebProverSessionConfig = {
|
47
59
|
notaryUrl: string | null;
|
@@ -16,4 +16,5 @@ export var ExtensionMessageType;
|
|
16
16
|
ExtensionMessageType["ProofError"] = "ProofError";
|
17
17
|
ExtensionMessageType["RedirectBack"] = "RedirectBack";
|
18
18
|
ExtensionMessageType["TabOpened"] = "TabOpened";
|
19
|
+
ExtensionMessageType["ProofProcessing"] = "ProofProcessing";
|
19
20
|
})(ExtensionMessageType || (ExtensionMessageType = {}));
|
package/package.json
CHANGED