Step-by-step Guide
Setup
Install
zPass-SDKusingnpm install zpass-sdk.As
zPass-SDKis using wasm for core functionality, install wasm supporting packages such asvite-plugin-wasmto make sure wasm can be run properly.Create a worker directory in your project.
Create an
AleoWorker.jsfile, importcreateAleoWorker()helper function to help initialize and manage workers.The
createAleoWorker()takes an argument of an object with 2 values which areURLand optionalbaseURL, the url here is referring to the location ofworker.jsfile that we are going to define our zPass functions in later. For example, if theworker.jsis located at the same location as theAleoWorker.jsfile, the arguments ofcreateAleoWorker()will be{“worker.js”, import.meta.url}. Theimport.meta.urlwill return the absolute URL of the current module regardless of how the application is deployed or served.
import { createAleoWorker } from "zpass-sdk";
const AleoWorker = () => {
return createAleoWorker({
url: "worker.js",
baseUrl: import.meta.url,
});
};
export { AleoWorker };Move on to create a new
worker.jsfile to build zPass functions in worker usingzPass-SDK.In
worker.jsfile, import and callinitThreadPool()to initialize a thread pool of Workers. This enables multi-threading, which significantly improves performance.Then proceed to create own zPass functions using
zPass-SDK, these functions will be later exposed to the main threat usingexpose()fromcomlink. Theexpose()method takes an argument of an object with all the functions defined inworkers.js.
Using zPass
(Optional) Import
initThreadPoolto initialize a thread pool of workers to enable multi-threading and improve performance.Import
ZPassSDKintoworker.js, initialize ZPassSDK with userprivateKeyand optionalhosturl to connect to the rpc node of Aleo network.
const sdk = new ZPassSDK({
privateKey: privateKey,
host: host
});Import
exposefromcomlinkto expose functions that are created inworker.jslater.To start using zPass on-chain, a zPass program must be deployed onto the network or use any zPass program that has been deployed. Check out this guide on how to write a zPass program to prove certain credentials requirements.
Once a zPass program is deployed on-chain, the first thing to do is to get the issuer to sign the credentials defined in the zPass program so that the users can issue themselves an on-chain zPass later.
For example, first create a function called
testZPass()and in the scope of it, let’s use thesignCredentialmethod from ZPassSDK.The
signCredentialmethod takes an argument as an object with fields:data: A credential object with properties and respective values same as defined in the deployed zPass program. Note that the name of each property must match the name of the field in the credential struct from the zPass program. The value must be in string type and appended with Aleo type suffix such as u32 in order to be able to parse correctly.hashType: Pick supported hash algorithm to use for hashing the data, type HashAlgorithm can be imported from zpass-sdk.privateKey?: An optional private key to switch using if the issuer account private key is not used during initialization of ZPassSDK.
signCredentialwill return an object that contains signature and hash in string type, signature will be required as one of the inputs later during proveOffChain, proveOnChain or issueZPass.
const { signature } = await sdk.signCredential({
data: {
issuer: "aleo1rhgdu77hgyqd3xjj8ucu3jj9r2krwz6mnzyd80gncr5fxcwlh5rsvzp9px",
subject: "aleo172s23z4lw3z3ruwc92dgukq8s0v3249jg28zsldq6a0adpw7c5gqfnkla2",
dob: "19990301u32",
nationality: "2148979field",
expiry: "20250301u32",
salt: "231scalar"
},
hashType: HashAlgorithm.POSEIDON2
}); After that, we can use the
issueZPassmethod from ZPassSDK. Remember to initialize ZPassSDK before using its method.The
issueZPassmethod takes an argument as an object with fields:programName: The name of the program to call in string type, which is the name of a zPass program that has been deployed on-chain with the suffix of .aleo.functionName: The name of the calling function in string type.privateFee: To spend fee from private records or not, either true or false.inputs: An array of strings which are the inputs to the program function.fee: Fee to spend in microcredit, in number type.feeRecord?: Optional fee records to pass for program execution if privateFee is true.
The
issueZPassmethod will returntransactionIdonce successfully.In our example, by following the parameters of issue transition in
verify_poseidon2_zpass.aleoprogram this will be:
const txId = await sdk.issueZPass({
programName: "verify_poseidon2_zpass.aleo",
functionName: "issue",
privateFee: false,
inputs: [
signature,
`{
issuer: aleo1rhgdu77hgyqd3xjj8ucu3jj9r2krwz6mnzyd80gncr5fxcwlh5rsvzp9px,
subject: aleo172s23z4lw3z3ruwc92dgukq8s0v3249jg28zsldq6a0adpw7c5gqfnkla2,
dob: 19990301u32,
nationality: 2148979field,
expiry: 20250301u32
}`,
`{
salt: 231scalar
}`
],
fee: 3000,
});
Then return the
txIdback to the main application.To expose the
testZPassfunction that we just created, useexposefromcomlinkto expose it as a worker method likeexpose({ testZPass }); .
Test running ZPass worker method
To try to run the
testZpass()worker method that we just created, simply importAleoWorker()fromAleoWorker.js, create an instant usingAleoWorker()then calltestZPass()method from that instant.To test it in the local development network, check out how to set it up here.
import { AleoWorker } from "./workers/AleoWorker.js";
const aleoWorker = AleoWorker();
const result = await aleoWorker.testZPass({
privateKey: privateKey,
host: hosturl
})Last updated