đ Install
npm or yarn
Install the @unirep/contracts
package with npm:
npm i @unirep/contracts
or yarn:
yarn add @unirep/contracts
đŠđģââī¸ Haven't deployed a contract yet?
Get circuit keys from one of the following methods
đ Solution 1. Download circuit keys from server
(TODO) Get circuits files from PSE server
đ Solution 2. Build circuits locally
git clone https://github.com/Unirep/Unirep.git && \
cd Unirep/ && \
yarn install && \
yarn build
By default, The zksnarkBuild
directory will be found in ./packages/circuits
Compile contracts from the keys
Step 1. Set the zksnarkBuild
path in buildVerifier.ts
Step 2. Run compile command
By default, The artifacts
directory will be found in ./packages/contracts/build
đđģââī¸ Unirep contract has been compiled
Deploy Unirep contract
Deploy Unirep smart contract with default config:
import ethers from 'ethers'
import { deployUnirep, Unirep } from '@unirep/contracts'
const privateKey = 'YOUR/PRIVATE/KEY'
const provider = 'YOUR/ETH/PROVIDER'
const deployer = new ethers.Wallet(privateKey, provider);
const unirepContract: Unirep = await deployUnirep(deployer)
(TODO) A deploy
script
Get unirep contract with address
import ethers from 'ethers'
import { getUnirepContract, Unirep } from '@unirep/contracts'
const address = '0x....'
const provider = 'YOUR/ETH/PROVIDER'
const unirepContract: Unirep = getUnirepContract(address, provider)
đ§đģâđģ Call Unirep contract with ethers
import { ethers } from 'ethers'
import { ZkIdentity } from '@unirep/crypto'
import { getUnirepContract, Unirep } from '@unirep/contracts'
const address = '0x....'
const privateKey = 'YOUR/PRIVATE/KEY'
const provider = 'YOUR/ETH/PROVIDER'
// connect a signer
const signer = new ethers.Wallet(privateKey, provider)
const unirepContract: Unirep = getUnirepContract(address, signer)
// user sign up
const id = new ZkIdentity()
const tx = await unirepContract.userSignUp(id.genIdentityCommitment())
// attester sign up
const tx = await unirepContract.attesterSignUp()
đđģââī¸ Call Unirep contract in DApps
đ¸ Please copy verifiers/*.sol
files to node_modules/@unirep/contracts/verifiers/
directories.
cp -rf ../Unirep/packages/contracts/contracts/verifiers/* ./node_modules/@unirep/contracts/verifiers
(TODO) Find a better way to do this.
import { Unirep } from '@unirep/contracts/Unirep.sol';
contract YourContract {
Unirep public unirep;
uint256 internal _attesterId;
// Initialize contract with a deployed
constructor(Unirep _unirepContract) {
// Set the unirep contract address
unirep = _unirepContract;
}
// Relay Users sign up in Unirep
function signUp(uint256 idCommitment) external {
unirep.userSignUp(idCommitment);
}
// Sign up this contract as an attester
function signUpContract() external {
unirep.attesterSignUp();
_attesterId = unirep.attesters(address(this));
}
// Users submit their epoch key proof to Unirep contract
// And get attestation from the contract
function submitEpochKeyProof(Unirep.EpochKeyProof memory input)
external
payable
{
// Step 1. submit epoch key proof
unirep.submitEpochKeyProof(input);
// Step 2. get proof index
bytes32 proofNullifier = unirep.hashEpochKeyProof(input);
uint256 proofIndex = unirep.getProofIndex(proofNullifier);
// Step 3. init attestation
// create an attestation which sends 5 positive Rep to the epochKey
Unirep.Attestation memory attestation;
attestation.attesterId = _attesterId;
attestation.posRep = 5;
// Step 4. send attestation
unirep.submitAttestation{ value: unirep.attestingFee() }(
attestation,
input.epochKey,
proofIndex,
0 // if no reputation spent required
);
}
}
đ Other usages
Proofs
An example of epoch key proof 1. Generate an epoch key proof structure
import { Circuit } from '@unirep/circuits'
import { EpochKeyProof } from '@unirep/contracts'
// see @unirep/circuits to know how to generate a prover and circuitInputs
const prover = {
...
}
const circuitInputs = {
...
}
const { publicSignals, proof } = await prover.genProofAndPublicSignals(
Circuit.verifyEpochKey,
circuitInputs
)
const epkProof = new EpochKeyProof(
publicSignals,
proof,
prover
)
2. Get data from epoch key proof structure
const epoch = epkProof.epoch
const epochKey = epkProof.epochKey
const globalStateTree = epkProof.globalStateTree
3. Verify the epoch key proof
const isValid = await epkProof.verify()
4. Compute keccak256 hash of the proof
const hash = epkProof.hash()
5. The proof structure can help with formatting the proof on chain
const tx = await unirepContract.submitEpochKeyProof(
epkProof.publicSignals,
epkProof.proof
)
Attestation
An example of constructing an Attestation object
import { Attestation } from '@unirep/contracts'
const attestation = new Attestation(
attesterID,
positiveReputation,
negativeReputation,
graffiti,
signUpFlag
)
const hash = attestation.hash()
Event/ Attestation event
The event enum is used to help with determining the type of the event, which are as the same definition in IUnirep.sol