@vlayer/sdk 0.1.0-nightly-20241113-0b85bf8 → 0.1.0-nightly-20241114-4867874

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,29 @@
1
+ import { createWalletClient, http, publicActions } from "viem";
2
+ import { privateKeyToAccount } from "viem/accounts";
3
+ import { getChainConfirmations } from "./getChainConfirmations.js";
4
+ const getChainSpecs = async (chainName) => {
5
+ try {
6
+ const chains = await import("viem/chains");
7
+ const chain = chains[chainName];
8
+ return chain;
9
+ }
10
+ catch {
11
+ throw Error(`Cannot import ${chainName} from viem/chains`);
12
+ }
13
+ };
14
+ const createEthClient = (chain, jsonRpcUrl) => createWalletClient({
15
+ chain,
16
+ transport: http(jsonRpcUrl),
17
+ }).extend(publicActions);
18
+ export const createContext = async (config) => {
19
+ const chain = await getChainSpecs(config.chainName);
20
+ const jsonRpcUrl = config.jsonRpcUrl ?? chain.rpcUrls.default.http[0];
21
+ return {
22
+ ...config,
23
+ chain,
24
+ account: privateKeyToAccount(config.privateKey),
25
+ jsonRpcUrl,
26
+ ethClient: createEthClient(chain, jsonRpcUrl),
27
+ confirmations: getChainConfirmations(config.chainName),
28
+ };
29
+ };
@@ -0,0 +1,17 @@
1
+ import { ContractArg, ContractSpec } from "../api/lib/types/ethereum.js";
2
+ import { Address } from "viem";
3
+ export declare const waitForContractDeploy: ({ hash, }: {
4
+ hash: `0x${string}`;
5
+ }) => Promise<Address>;
6
+ export declare const waitForTransactionReceipt: ({ hash, }: {
7
+ hash: `0x${string}`;
8
+ }) => Promise<import("viem").TransactionReceipt>;
9
+ export declare const deployVlayerContracts: ({ proverSpec, verifierSpec, proverArgs, verifierArgs, }: {
10
+ proverSpec: ContractSpec;
11
+ verifierSpec: ContractSpec;
12
+ proverArgs?: ContractArg[];
13
+ verifierArgs?: ContractArg[];
14
+ }) => Promise<{
15
+ prover: `0x${string}`;
16
+ verifier: `0x${string}`;
17
+ }>;
@@ -0,0 +1,54 @@
1
+ import { getConfig } from "./getConfig.js";
2
+ import { createContext } from "./createContext.js";
3
+ import { getChainConfirmations } from "./getChainConfirmations.js";
4
+ export const waitForContractDeploy = async ({ hash, }) => {
5
+ const { ethClient: client } = await createContext(getConfig());
6
+ const receipt = await client.waitForTransactionReceipt({
7
+ hash,
8
+ confirmations: getChainConfirmations(client.chain?.name),
9
+ retryCount: 120,
10
+ retryDelay: 1000,
11
+ });
12
+ if (!receipt.contractAddress || receipt.status !== "success") {
13
+ throw new Error(`Cannot get contract address from receipt: ${receipt.status}`);
14
+ }
15
+ return receipt.contractAddress;
16
+ };
17
+ export const waitForTransactionReceipt = async ({ hash, }) => {
18
+ const { ethClient } = await createContext(getConfig());
19
+ return ethClient.waitForTransactionReceipt({
20
+ hash,
21
+ confirmations: getChainConfirmations(ethClient.chain?.name),
22
+ retryCount: 120,
23
+ retryDelay: 1000,
24
+ });
25
+ };
26
+ export const deployVlayerContracts = async ({ proverSpec, verifierSpec, proverArgs, verifierArgs, }) => {
27
+ console.log("Starting contract deployment process...");
28
+ const config = getConfig();
29
+ const { chain, ethClient, account } = await createContext(config);
30
+ console.log("Deploying prover contract...");
31
+ console.log(proverArgs, verifierArgs, account);
32
+ const proverHash = await ethClient.deployContract({
33
+ chain,
34
+ account,
35
+ args: proverArgs,
36
+ abi: proverSpec.abi,
37
+ bytecode: proverSpec.bytecode.object,
38
+ });
39
+ console.log(proverHash);
40
+ const prover = await waitForContractDeploy({ hash: proverHash });
41
+ console.log(`Prover contract deployed at: ${prover}`);
42
+ console.log("Deploying verifier contract...");
43
+ const verifierHash = await ethClient.deployContract({
44
+ chain,
45
+ account,
46
+ args: prover ? [prover, ...(verifierArgs ?? [])] : verifierArgs,
47
+ abi: verifierSpec.abi,
48
+ bytecode: verifierSpec.bytecode.object,
49
+ });
50
+ const verifier = await waitForContractDeploy({ hash: verifierHash });
51
+ console.log(`Verifier contract deployed at: ${verifier}`);
52
+ console.log("Contract deployment completed successfully");
53
+ return { prover, verifier };
54
+ };
@@ -0,0 +1 @@
1
+ export declare const getChainConfirmations: (chainName?: string) => number;
@@ -0,0 +1,6 @@
1
+ export const getChainConfirmations = (chainName) => {
2
+ if (!chainName || chainName.toLowerCase() === "anvil") {
3
+ return 1;
4
+ }
5
+ return 6;
6
+ };
@@ -0,0 +1,8 @@
1
+ export type Config = {
2
+ chainName: string;
3
+ proverUrl: string;
4
+ jsonRpcUrl: string;
5
+ privateKey: `0x${string}`;
6
+ };
7
+ export declare const toCamelCase: (str: string) => string;
8
+ export declare const getConfig: () => Config;
@@ -0,0 +1,49 @@
1
+ import dotenvflow from "dotenv-flow";
2
+ import chalk from "chalk";
3
+ const ensureEnvVariable = (envVar) => {
4
+ if (!process.env[envVar]) {
5
+ if (envVar === "EXAMPLES_TEST_PRIVATE_KEY") {
6
+ throw new Error(chalk.bgBlue(`${envVar} missing. Add a HEX private key with ETH in .env.local for deploy and verify transactions.`));
7
+ }
8
+ throw new Error(`${envVar} is not set`);
9
+ }
10
+ return process.env[envVar];
11
+ };
12
+ const ensureVlayerEnv = () => {
13
+ try {
14
+ if (!process.env.VLAYER_ENV) {
15
+ throw new Error("VLAYER_ENV is not set. Available options: testnet, dev");
16
+ }
17
+ if (!["testnet", "dev"].includes(process.env.VLAYER_ENV)) {
18
+ throw new Error(`Invalid VLAYER_ENV: ${process.env.VLAYER_ENV}. Available options: testnet, anvil, mainnet`);
19
+ }
20
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
21
+ }
22
+ catch (e) {
23
+ return "dev";
24
+ }
25
+ return process.env.VLAYER_ENV;
26
+ };
27
+ const dotEnvFlowConfig = () => {
28
+ dotenvflow.config({
29
+ node_env: ensureVlayerEnv(),
30
+ });
31
+ };
32
+ export const toCamelCase = (str) => str
33
+ .toLowerCase()
34
+ .replace(/([-_][a-z])/g, (group) => group.toUpperCase().replace("-", "").replace("_", ""));
35
+ const requiredEnvVars = [
36
+ { var: "CHAIN_NAME" },
37
+ { var: "PROVER_URL" },
38
+ { var: "JSON_RPC_URL" },
39
+ { var: "EXAMPLES_TEST_PRIVATE_KEY", to: "privateKey" },
40
+ ];
41
+ export const getConfig = () => {
42
+ dotEnvFlowConfig();
43
+ return requiredEnvVars.reduce((config, envVar) => {
44
+ return {
45
+ ...config,
46
+ [envVar.to ?? toCamelCase(envVar.var)]: ensureEnvVariable(envVar.var),
47
+ };
48
+ }, {});
49
+ };
@@ -0,0 +1,4 @@
1
+ export * from "./getConfig.js";
2
+ export * from "./createContext.js";
3
+ export * from "./deploy.js";
4
+ export * from "./writeEnvVariables.js";
@@ -0,0 +1,4 @@
1
+ export * from "./getConfig.js";
2
+ export * from "./createContext.js";
3
+ export * from "./deploy.js";
4
+ export * from "./writeEnvVariables.js";
@@ -0,0 +1,3 @@
1
+ export declare const writeEnvVariables: (envPath: string, overrides: {
2
+ [key: string]: string;
3
+ }) => Promise<void>;
@@ -0,0 +1,16 @@
1
+ import fs from "fs";
2
+ import dotenv from "dotenv";
3
+ export const writeEnvVariables = async (envPath, overrides) => {
4
+ fs.appendFileSync(envPath, "");
5
+ const envFile = Bun.file(envPath);
6
+ let envContent = await envFile.text();
7
+ if (!envContent) {
8
+ envContent = "";
9
+ }
10
+ const newEnvs = Object.assign(dotenv.parse(envContent), overrides);
11
+ const envLines = Object.entries(newEnvs)
12
+ .map(([key, value]) => `${key}=${value}`)
13
+ .join("\n");
14
+ await Bun.write(envPath, envLines);
15
+ console.log(`Successfully updated the ${envPath} with: `, overrides);
16
+ };
package/package.json CHANGED
@@ -9,9 +9,13 @@
9
9
  "./web_proof": {
10
10
  "import": "./dist/api/webProof/index.js",
11
11
  "types": "./dist/api/webProof/index.d.ts"
12
+ },
13
+ "./config": {
14
+ "import": "./dist/config/index.js",
15
+ "types": "./dist/config/index.d.ts"
12
16
  }
13
17
  },
14
- "version": "0.1.0-nightly-20241113-0b85bf8",
18
+ "version": "0.1.0-nightly-20241114-4867874",
15
19
  "scripts": {
16
20
  "build": "bun tsc && bun tsc-alias",
17
21
  "test:unit": "vitest --run",
@@ -31,6 +35,7 @@
31
35
  "dependencies": {
32
36
  "@vitejs/plugin-react": "^4.3.2",
33
37
  "dns-over-http-resolver": "^3.0.3",
38
+ "dotenv-flow": "^4.1.0",
34
39
  "postal-mime": "^2.3.2",
35
40
  "tsc-alias": "^1.8.10",
36
41
  "viem": "2.21.0",