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
  • 📔 Usage

Was this helpful?

Edit on GitHub
  1. Package usage

@unirep/core

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

Previous@unirep/contractsNext@unirep/subgraph

Last updated 2 years ago

Was this helpful?

🛠 Install

npm or yarn

Install the @unirep/core package with npm:

npm i @unirep/core

or yarn:

yarn add @unirep/core

📔 Usage

Synchronizer ⏲

Construct a synchronizer

import { Synchronizer, schema } from '@unirep/core'
import { getUnirepContract, Unirep } from '@unirep/contracts'
import { DB, SQLiteConnector } from 'anondb/node'

// connect a unirep contract with the address and a provider
const unirepContract: Unirep = getUnirepContract(address, provider)
// initialize a database
const db: DB = await SQLiteConnector.create(schema, ':memory:')

// 1. initialize a synchronizer
const synchronizer = new Synchronizer(db, provider, unirepContract)
// 2. start listening to unriep contract events
await synchronizer.start()
// 3. wait until the latest block is processed
await synchronizer.waitForSync()

Example: use the synchronizer to generate unirep state

const epoch = 1
const globalStateTree = await synchronizer.genGSTree(epoch)

UserState 👤

Construct a user state

import { ZkIdentity } from '@unirep/crypto'
import { Synchronizer, schema } from '@unirep/core'
import { getUnirepContract, Unirep } from '@unirep/contracts'
import { DB, SQLiteConnector } from 'anondb/node'

// random generate a user identity
const identity = new ZkIdentity()
// connect a unirep contract with the address and a provider
const unirepContract: Unirep = getUnirepContract(address, provider)
// initialize a database
const db: DB = await SQLiteConnector.create(schema, ':memory:')

// 1. initialize a user state object
const userState = new UserState(
    db,
    provider,
    unirepContract,
    identity
)
// 2. start listening to unriep contract events
await userState.start()
// 3. wait until the latest block is processed
await userState.waitForSync()

Example: use the user state to generate proofs

const nonce = 1
const epochKeyProof = await userState.genVerifyEpochKeyProof(nonce)

// 1. submit the epoch key proof to smart contract
const tx = await unirepContract.submitEpochKeyProof(
    epochKeyProof.publicSignals,
    epochKeyProof.proof
)

// 2. get the index of the epoch key proof
const proofHash = epochKeyProof.hash()
const index = await unirepContract.getProofIndex(proofHash)

// Then the attester can call `submitAttestation` on Unirep contract
// to send attestation to the epoch key with a proof index

Utils đŸ§ŗ

Example: Compute an epoch key

import { ZkIdentity } from '@unirep/crypto'
import { genEpochKey } from '@unirep/core'

const identity = new ZkIdentity()
const epoch = 1
const nonce = 0
const epochTreeDepth = 64

const epk = genEpochKey(
    identity.identityNullifier, 
    epoch, 
    nonce, 
    epochTreeDepth
)
🌈
NPM version
Downloads
Github license
Linter eslint
Code style prettier