Step-by-step Guide
Setup
Install
zPass-SDK
usingnpm install zpass-sdk.
As
zPass-SDK
is using wasm for core functionality, install wasm supporting packages such asvite-plugin-wasm
to make sure wasm can be run properly.Create a worker directory in your project.
Create an
AleoWorker.js
file, importcreateAleoWorker()
helper function to help initialize and manage workers.The
createAleoWorker()
takes an argument of an object with 2 values which areURL
and optionalbaseURL
, the url here is referring to the location ofworker.js
file that we are going to define our zPass functions in later. For example, if theworker.js
is located at the same location as theAleoWorker.js
file, the arguments ofcreateAleoWorker()
will be{“worker.js”, import.meta.url}
. Theimport.meta.url
will return the absolute URL of the current module regardless of how the application is deployed or served.
Move on to create a new
worker.js
file to build zPass functions in worker usingzPass-SDK
.In
worker.js
file, 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
initThreadPool
to initialize a thread pool of workers to enable multi-threading and improve performance.Import
ZPassSDK
intoworker.js
, initialize ZPassSDK with userprivateKey
and optionalhost
url to connect to the rpc node of Aleo network.
Import
expose
fromcomlink
to expose functions that are created inworker.js
later.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 thesignCredential
method from ZPassSDK.The
signCredential
method 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.
signCredential
will 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.
After that, we can use the
issueZPass
method from ZPassSDK. Remember to initialize ZPassSDK before using its method.The
issueZPass
method 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
issueZPass
method will returntransactionId
once successfully.In our example, by following the parameters of issue transition in
verify_poseidon2_zpass.aleo
program this will be:
Then return the
txId
back to the main application.To expose the
testZPass
function that we just created, useexpose
fromcomlink
to 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.
Last updated