Unirep
  • 👏Welcome
  • 🧩Introduction
  • 🎮Getting Started
    • Install & build 🛠
    • Start with cli commands 🔌
      • 0. Install and build
      • 1. Spin up the testing chain
      • 2. Deploy Unirep contract
      • 3. User generates semaphore identity
      • 4. User signs up
      • 5. Attester signs up
      • 6. User generates epoch key and epoch key proof
      • 7. Attesters/Users verify epoch key proof
      • 8. Submit epoch key proof to Unirep smart contract
      • 9. Attester attest to epoch key
      • 10. Epoch transition
      • 11. User state transition
      • 12. User generates reputation proof
      • 13. Attesters/ Users verify the reputation proof
      • 14. User generates sign up proof
      • 15. Attesters/ Users verify the sign up proof
    • Start with Typescript 📠
      • 0. Install packages
      • 1. deploy
      • 2. User signs up
      • 3. Attester signs up
      • 4. Epoch key proof
      • 5. Attest
      • 6. Epoch transition
      • 7. User state transition
      • 8. Reputation proof
    • Computation happens off-chain â„šī¸
  • â˜€ī¸Protocol
    • Glossary
      • Users and Attesters
      • Epoch
      • Epoch Key
      • Reputation
      • Trees
      • Nullifiers
      • Epoch Transition
      • User State Transition
    • Circuits
      • Epoch Key Proof
      • Reputation Proof
      • User Sign Up Proof
      • User State Transition Proof
    • Contract
      • Sign up
      • Attestations
      • Epoch transition
      • User state transition
      • Verify proofs
  • 🌈Package usage
    • @unirep/crypto
    • @unirep/circuits
    • @unirep/contracts
    • @unirep/core
    • @unirep/subgraph
    • cli
      • Deploy Unirep Contract
      • User Identity
      • User Sign Up
      • Epoch Key And Proof
      • Attestation
      • Epoch transition
      • User state transition
      • Reputation Proof
      • Airdrop Reputation
      • Spend Reputation
  • đŸŒģApplications
    • Unirep Social
Powered by GitBook
On this page
  • 🛠 Install
  • 👩đŸģâ€âš•ī¸ Haven't deployed a contract yet?
  • 🙆đŸģâ€â™€ī¸ Unirep contract has been compiled
  • 🧑đŸģ‍đŸ’ģ Call Unirep contract with ethers
  • 🙋đŸģâ€â™‚ī¸ Call Unirep contract in DApps
  • 📚 Other usages

Was this helpful?

Edit on GitHub
  1. Package usage

@unirep/contracts

Client library for contracts related functions which are used in UniRep protocol.

Previous@unirep/circuitsNext@unirep/core

Last updated 2 years ago

Was this helpful?

🛠 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

🍀 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

🌈
PSE server
Code style prettier
Downloads
Github license
Linter eslint
NPM version