@unirep/contracts
TypeScript icon, indicating that this package has built-in type declarations

2.0.0-beta-2 • Public • Published

Unirep contracts package

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

Github license NPM version Downloads Linter eslint Code style prettier Coverage report


🛠 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

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

yarn contracts compile

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/deploy'

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

yarn contracts deploy

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/utils'
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 Identity()
const tx = await unirepContract.userSignUp(id.commitment)

// 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.epochKey,
    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 stateTree = epkProof.stateTree

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
)

Install

Version

2.0.0-beta-2

License

MIT

Unpacked Size

1.12 MB

Total Files

156

Last publish

Collaborators

  • vivianjeng
  • jchancehud