Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
126 changes: 73 additions & 53 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,65 +89,85 @@ import {
ProvingParams,
WithSignature,
ZkWasmUtil,
ZkWasmServiceHelper
ZkWasmServiceHelper,
ProofSubmitMode,
InputContextType,
WithCustomInputContextType,
} from "zkwasm-service-helper";

const endpoint = ""https://rpc.zkwasmhub.com:8090";
const image_md5 = "xxxx";
const public_inputs = "0x22:i64 0x21:i64";
const private_inputs = "";
const user_addr = "0xaaaaaa";

let helper = new ZkWasmServiceHelper(endpoint, "", "");
let pb_inputs: Array<string> = helper.parseProvingTaskInput(public_inputs);
let priv_inputs: Array<string> = helper.parseProvingTaskInput(private_inputs);

// Use the helper Enum type to determine the proof submit mode
let proofSubmitMode = ProofSubmitMode.Auto ? ProofSubmitMode.Auto : ProofSubmitMode.Manual;

let info: ProvingParams = {
user_address: user_addr.toLowerCase(),
md5: image_md5,
public_inputs: pb_inputs,
private_inputs: priv_inputs,
// Whether the proof will be batched and verified through the auto submit service or manually submitted.
// If the field is not specified, the default value will be ProofSubmitMode.Manual and the proof will not be batched.
proof_submit_mode: proofSubmitMode,
};

// Context type for proof task. If none provided, will default to InputContextType.ImageCurrent in the server and use the image's current context
let selectedInputContextType = InputContextType.ImageCurrent;

// For Custom Context, upload a binary file first containing the context.
if (selectedInputContextType === InputContextType.Custom) {
async function AddNewProofTask() {
const endpoint = "https://rpc.zkwasmhub.com:8090";
const image_md5 = "xxxxx";
const public_inputs = "0x22:i64 0x21:i64";
const private_inputs = "";
const user_addr = "";
const privateKey = "";

let helper = new ZkWasmServiceHelper(endpoint, "", "");
let pb_inputs: Array<string> = ZkWasmUtil.validateInputs(public_inputs);
let priv_inputs: Array<string> = ZkWasmUtil.validateInputs(private_inputs);

// Use the helper Enum type to determine the proof submit mode
let proofSubmitMode = ProofSubmitMode.Auto
? ProofSubmitMode.Auto
: ProofSubmitMode.Manual;

let info: ProvingParams = {
user_address: user_addr.toLowerCase(),
md5: image_md5,
public_inputs: pb_inputs,
private_inputs: priv_inputs,
// Whether the proof will be batched and verified through the auto submit service or manually submitted.
// If the field is not specified, the default value will be ProofSubmitMode.Manual and the proof will not be batched.
proof_submit_mode: proofSubmitMode,
};

let contextFile = await ZkWasmUtil.loadContexFileAsBytes("<YourFilePath>");
// Context type for proof task. If none provided, will default to InputContextType.ImageCurrent in the server and use the image's current context
let selectedInputContextType = InputContextType.Custom;

// For Custom Context, upload a binary file first containing the context.
if (selectedInputContextType === InputContextType.Custom) {
// Load bytes into Tempfile/Buffer type - Server side example with NodeJS
const contextBytes = new Uint8Array(64);
contextBytes.fill(1);
ZkWasmUtil.validateContextBytes(contextBytes);
let bytesFile = await ZkWasmUtil.bytesToTempFile(contextBytes);
let context_info: WithCustomInputContextType = {
input_context: bytesFile,
input_context_md5: ZkWasmUtil.convertToMd5(contextBytes),
input_context_type: selectedInputContextType,
};

let context_info: WithCustomInputContextType = {
input_context: contextFile,
input_context_md5: ZkWasmUtil.convertToMd5(contextFile),
input_context_type: selectedInputContextType,
// // LOADING AS FILE DIRECTLY - Browser side example
// let contextFile = await ZkWasmUtil.loadContexFileAsBytes("<YourFilePath>");

// let context_info: WithCustomInputContextType = {
// input_context: contextFile,
// input_context_md5: ZkWasmUtil.convertToMd5(contextFile),
// input_context_type: selectedInputContextType,
// };
info = { ...info, ...context_info };
} else {
info = { ...info, input_context_type: selectedInputContextType };
}
let msgString = ZkWasmUtil.createProvingSignMessage(info);

let signature: string;
try {
signature = await ZkWasmUtil.signMessage(msgString, privateKey);
} catch (e: unknown) {
console.log("error signing message", e);
return;
}

let task: WithSignature<ProvingParams> = {
...info,
signature: signature,
};
info = { ...info, ...context_info };
} else {
info = { ...info, input_context_type: selectedInputContextType };
}
let msgString = ZkWasmUtil.createProvingSignMessage(info);

let signature: string;
try {
signature = await ZkWasmUtil.signMessage(msgString, priv);
} catch (e: unknown) {
console.log("error signing message", e);
return;
}

let task: WithSignature<ProvingParams> = {
...info,
signature: signature,
};

let response = await helper.addProvingTask(task);
let response = await helper.addProvingTask(task);
console.log(response);
}
```

### Verifying a proof
Expand Down
3 changes: 3 additions & 0 deletions dist/cjs/helper/util.d.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/// <reference types="node" />
import BN from "bn.js";
import { AddImageParams, ProvingParams, DeployParams, ResetImageParams, ModifyImageParams, VerifyProofParams, LogQuery, ContextHexString, VerifyBatchProofParams } from "../interface/interface.js";
import { Contract, Signer } from "ethers";
Expand Down Expand Up @@ -98,6 +99,8 @@ export declare class ZkWasmUtil {
static loadContextFileFromPath(filePath: string): Promise<ContextHexString>;
static loadContexFileAsBytes(filePath: string): Promise<Uint8Array>;
static browserLoadContextFileAsBytes(file: File): Promise<Uint8Array>;
static bytesToTempFile(data: Uint8Array): Promise<Buffer>;
static bytesToFile(data: Uint8Array): Promise<File>;
static MAX_CONTEXT_SIZE: number;
static validateContextBytes(data: Uint8Array): boolean;
}
52 changes: 52 additions & 0 deletions dist/cjs/helper/util.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,27 @@
"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
Expand Down Expand Up @@ -353,6 +376,35 @@ class ZkWasmUtil {
});
});
}
// Convert Bytes into a temporary file (Buffer for nodejs), as it will be submitted through multipart to a server, file does not need to be saved
static bytesToTempFile(data) {
return __awaiter(this, void 0, void 0, function* () {
this.validateContextBytes(data);
if (typeof window === "undefined") {
// Node.js environment
const { Buffer } = yield Promise.resolve().then(() => __importStar(require("buffer")));
const buffer = Buffer.from(data);
return buffer;
}
else {
// Browser environment
throw new Error("File creation in the browser is not supported by this function.");
}
});
}
static bytesToFile(data) {
return __awaiter(this, void 0, void 0, function* () {
this.validateContextBytes(data);
if (typeof window === "undefined") {
throw new Error("File creation in NodeJS env is not supported by this function.");
}
else {
// Browser environment
const blob = new Blob([data]);
return new File([blob], "context.bin");
}
});
}
// Validate bytes are a multiple of 8 bytes (64 bits) and length less than 4KB
static validateContextBytes(data) {
if (data.length > this.MAX_CONTEXT_SIZE) {
Expand Down
3 changes: 3 additions & 0 deletions dist/mjs/helper/util.d.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/// <reference types="node" />
import BN from "bn.js";
import { AddImageParams, ProvingParams, DeployParams, ResetImageParams, ModifyImageParams, VerifyProofParams, LogQuery, ContextHexString, VerifyBatchProofParams } from "../interface/interface.js";
import { Contract, Signer } from "ethers";
Expand Down Expand Up @@ -98,6 +99,8 @@ export declare class ZkWasmUtil {
static loadContextFileFromPath(filePath: string): Promise<ContextHexString>;
static loadContexFileAsBytes(filePath: string): Promise<Uint8Array>;
static browserLoadContextFileAsBytes(file: File): Promise<Uint8Array>;
static bytesToTempFile(data: Uint8Array): Promise<Buffer>;
static bytesToFile(data: Uint8Array): Promise<File>;
static MAX_CONTEXT_SIZE: number;
static validateContextBytes(data: Uint8Array): boolean;
}
25 changes: 25 additions & 0 deletions dist/mjs/helper/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -447,6 +447,31 @@ export class ZkWasmUtil {
reader.readAsArrayBuffer(file);
});
}
// Convert Bytes into a temporary file (Buffer for nodejs), as it will be submitted through multipart to a server, file does not need to be saved
static async bytesToTempFile(data) {
this.validateContextBytes(data);
if (typeof window === "undefined") {
// Node.js environment
const { Buffer } = await import("buffer");
const buffer = Buffer.from(data);
return buffer;
}
else {
// Browser environment
throw new Error("File creation in the browser is not supported by this function.");
}
}
static async bytesToFile(data) {
this.validateContextBytes(data);
if (typeof window === "undefined") {
throw new Error("File creation in NodeJS env is not supported by this function.");
}
else {
// Browser environment
const blob = new Blob([data]);
return new File([blob], "context.bin");
}
}
static MAX_CONTEXT_SIZE = 4096;
// Validate bytes are a multiple of 8 bytes (64 bits) and length less than 4KB
static validateContextBytes(data) {
Expand Down
31 changes: 31 additions & 0 deletions src/helper/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -567,6 +567,37 @@ export class ZkWasmUtil {
});
}

// Convert Bytes into a temporary file (Buffer for nodejs), as it will be submitted through multipart to a server, file does not need to be saved
static async bytesToTempFile(data: Uint8Array): Promise<Buffer> {
this.validateContextBytes(data);

if (typeof window === "undefined") {
// Node.js environment
const { Buffer } = await import("buffer");
const buffer = Buffer.from(data);
return buffer;
} else {
// Browser environment
throw new Error(
"File creation in the browser is not supported by this function."
);
}
}

static async bytesToFile(data: Uint8Array): Promise<File> {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add a comment to say this is for browser env?

this.validateContextBytes(data);

if (typeof window === "undefined") {
throw new Error(
"File creation in NodeJS env is not supported by this function."
);
} else {
// Browser environment
const blob = new Blob([data]);
return new File([blob], "context.bin");
}
}

static MAX_CONTEXT_SIZE = 4096;

// Validate bytes are a multiple of 8 bytes (64 bits) and length less than 4KB
Expand Down