🤖Build
ZKSAFE Password Docking
Preparations
Required Node.js v16,install snarkjs
install ethers, you need to know how to use ethers, all the code examples bellow assumed you know how to use ethers
Note: The test environment is hardhat. ethers is used slightly differently than the formal environment. The following code is based on the test environment
We suggested don't enter password outside ZKPass and ZKSAFE, to prevent password leakage. ZKPass (short of ZKSAFE Password) contracts are open to partner contracts, such as ZKSAFE
resetPassword() reset password
Initializing password and changing password are the same interface. Let's start with the util function getProof()
that all ZK use
Util Function
For the convenience, we wrote a util function getProof()
, wraps all of our ZK algorithms. Note that circuit.wasm
, circuit_final.zkey
, verification_key.json
are fixed values that can be found in ZK source code
getProof()
is the ZK Circuit in the diagram
getProof()
has 4 params:
pwd: your password, string type
address: your wallet address, string type
nonce: obtain your nonce value from ZKPass contarct, string type
datahash: the hash of the data you would like to sign, string type
Return all data related to ZK algorithm:
proof: proof of ZK-SNARK, array of 8 uint256
pwdhash: pwdhash needed in ZKPass contract, uint256 type
address: address from params, string type
expiration: password signing expiration seconds, int type
chainId: chain id, int type
nonce: nonce from params, string type
datahash: datahash from params, string type
fullhash: dosen’t need to upload to contract, 254 bits
allhash: hash of all above, uint256 type
Initialize Password
resetPassword()
has 7 params:
proof1: proof generated by the old password, array of 8 uint256
expiration1: old password signing expiry seconds, uint256 type
allhash1: allhash generated by the old password, uint256 type
proof2: proof generated by the new password, array of 8 uint 256
pwdhash2: pwdhash of the new password generated by ZK, uint256
expiration2: new password signing expiry seconds, uint256 type
allhash2: allhash generated by the new password, uint256 type
Since there’s no old password for initial password, the first 3 parameters related to the old password are not required in the contract. However, they were all required to the contract (parameter as 0) or take proof2 of the new password as proof1 (as in the example)
Upon success, the password for the caller's address (msg.sender) is pwd
Reset Password
Still resetPassword()
function, old password is required for resetting password, so the first 3 params were generated by the old password
Upon success, the password for the caller's address (msg.sender) is newpwd
, and the oldpwd
is invalid
verify() verify password
Password can be verified off chain by obtaining pwdhash
, or onchain with the partner contract. The partner contract calls ZKPAss.verify()
, if the password is incorrect, it throws an error. If no errors, the password is correct, and the signature is valid
Unsuggested to enter passwords outside ZKPass and ZKSAFE, to prevent password leakage. Partners can use ZKPass for on-chain verification
verify()
has 5 params:
user: the password owner, address type
proof: from getProof(), array of 8 uint256
datahash: the data what user signing, this is the hash of the data, uint256 type
expiration: from getProof(), uint256 type
allhash:from getProof(),uint256 type
The contract will use the user's pwdhash to verify the password and convert the datahash to 254 bits fullhash... In summary, the getProof() tool will process all ZK validation parameters
ZKSAFE as a partner contract to call ZKPass
In this example, user wants to withdaw the token from ZKSAFE, so the tokenAddr
and token amount
needs to be signed with password
ZKSAFE off-chain code
datahash
is defined by the partner, uint256 type, which is usually a hash value. There are exceptions, such as address for the signed code which is uint160 type, it fits datahash
without Keccak256
The datahash
calculated off chain should be consistent with the one in the partner contract
Last updated