Skip to main content
Version: 1.0.1

@unirep/contracts

Github license NPM version Downloads Linter eslint Code style prettier

🛠 Install

npm or yarn

Install the @unirep/contracts package with npm:

npm i @unirep/contracts

or yarn:

yarn add @unirep/contracts

Deploy Unirep contract

Deploy Unirep smart contract with default config:

import { ethers } from 'ethers'
import { deployUnirep } from '@unirep/contracts/deploy'
import { 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)

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(uint256)'](id.genIdentityCommitment())

// attester sign up
const tx = await unirepContract.attesterSignUp()

🙋🏻‍♂️ Call Unirep contract in DApps

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));
}

// And get attestation from the contract
function submitAttestation(
uint256[] memory publicSignals,
uint256[8] memory proof
) external payable {
// Step 1. verify epoch key proof
bool valid = unirep.verifyEpochKeyValidity(publicSignals, proof);
require(valid);

// Step 2. init attestation
// create an attestation which sends 5 positive Rep to the epochKey
Unirep.Attestation memory attestation;
attestation.attesterId = _attesterId;
attestation.posRep = 5;

// Step 3. send attestation
unirep.submitAttestation{ value: unirep.attestingFee() }(
attestation,
publicSignals[0] // epoch key
);
}
}

📚 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()

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