# Get started with UniRep Build your own application with UniRep is easy. Here are the guides for you to follow: - Getting started with [create-unirep-app](https://developer.unirep.io/docs/getting-started/create-unirep-app) - Getting started with [typescript/javascript](https://developer.unirep.io/docs/getting-started/ts-js) ## Need idea? - Get some inspiration for what you can you build: - [What I can build with UniRep?](https://developer.unirep.io/docs/next/what-can-i-build) - [Applications built by UniRep core contributors](https://developer.unirep.io/docs/next/welcome#-design--deploy-a-customized-attester) ## Questions? We are at discord! https://discord.gg/VzMMDJmYc5 # Code examples ## Installation ```shell yarn add @unirep/core ``` ## deploy ```typescript import { ethers } from 'ethers' import { deployUnirep } from '@unirep/contracts/deploy' // connect to a wallet const privateKey = 'YOUR/PRIVATE/KEY' const provider = 'YOUR/ETH/PROVIDER' const deployer = new ethers.Wallet(privateKey, provider) // deploy unirep contract const unirepContract = await deployUnirep(deployer) ``` ## connect deployed unirep contract ```typescript import { getUnirepContract } from '@unirep/contracts' const address = 'UNIREP/ADDRESS' const provider = 'YOUR/ETH/PROVIDER' const unirep = getUnirepContract(address, provider) ``` ## attester sign up with wallet ```typescript // attester wallet const attester = new ethers.Wallet(privateKey, provider) // define epoch length const epochLength = 300 // 300 seconds // send transaction const tx = await unirepContract .connect(attester) .attesterSignUp(epochLength) await tx.wait() ``` ## user sign up **User** ```typescript import { UserState } from '@unirep/core' import { defaultProver } from '@unirep/circuits/provers/defaultProver' import { Identity } from "@semaphore-protocol/identity" // semaphore Identity const identity = new Identity() // generate user state const userState = new UserState({ prover: defaultProver, // a circuit prover unirepAddress: unirepContract.address, provider: provider, // an ethers.js provider id: identity }) // start and sync await userState.start() await userState.waitForSync() // generate signup proof const { proof, publicSignals } = await userState.genUserSignUpProof() // stop sync userState.stop() ``` **Attester** ```typescript // attester sends the tx const tx = await unirepContract .connect(attester) .userSignUp(publicSignals, proof) await tx.wait() ``` > user can check if he signs up successfully > **User** > ```typescript > await userState.waitForSync() > console.log(await userState.hasSignedUp()) // true > ``` ## attest **User** ```typescript! import { genEpochKey } from '@unirep/utils' // get epoch from contract const epoch = await unirepContract.attesterCurrentEpoch(attester.address) // define nonce const nonce = 0 // it could be 0 to (NUM_EPOCH_KEY_NONCE - 1) per user // generate an epoch key const epochKey = genEpochKey( identity.secret, BigInt(attester.address), epoch, nonce ) ``` **Attester** ```typescript! // attester sends the tx const fieldIndex = 0 // the data field that the attester chooses to change const change = 5 // the amount of the change // know more about data // https://developer.unirep.io/docs/protocol/data const tx = await unirepContract .connect(attester) .attest(epochKey, epoch, fieldIndex, change) await tx.wait() ``` ## user state transition when epoch ends (e.g. 300 seconds finish) **User** ```typescript! // calling this to make sure the state is updated await userState.waitForSync() // generate the user state transition proof const { proof, publicSignals } = await userState.genUserStateTransitionProof() userState.stop() ``` ```typescript! // sends the tx const key = 'RELAYER/PRIVATE/KEY' const signer = new ethers.Wallet(key, provider) // it doesn't need to be the attester const tx = await unirepContract .connect(signer) .userStateTransition(publicSignals, proof) await tx.wait() ``` ## Prove data ```typescript // calling this to make sure the state is updated await userState.waitForSync() // the data user wants to prove // If the user has 5, then he can choose to prove it is more than 3 // see: https://developer.unirep.io/docs/circuits-api/reputation-proof const proof = await userState.genProveReputationProof({ minRep: 3 }) // check if proof is valid console.log(await proof.verify()) // true ```