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

2.0.1 • Public • Published

UniRep utils package

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

Github license NPM version Downloads Linter eslint Code style prettier


💡 About UniRep

UniRep is a private and non-repudiable data system. Users can receive attestations from attesters, and voluntarily prove facts about their data without revealing the data itself. Moreover, users cannot refuse to receive attestations from an attester.

📘 Documentation

Read the medium article to know more about the concept of UniRep protocol. For more information, refer to the documentation

🛠 Install

npm or yarn

Install the @unirep/utils package with npm:

npm i @unirep/utils

or yarn:

yarn add @unirep/utils

📔 Usage

IncrementalMerkleTree

Create a IncrementalMerkleTree

import { IncrementalMerkleTree } from '@unirep/utils'

const depth = 4
// initialize incremental merkle tree with depth 4
const tree = new IncrementalMerkleTree(depth)

Get tree root

const root = tree.root

Insert leaf

const leaf = 1
tree.insert(leaf)

Generate merkle proof

const index = 0
const proof = tree.createProof(index)

Verify merkle proof

const isValid = tree.verifyProof(proof)

Crypto utils

genRandomSalt

import { genRandomSalt } from '@unirep/utils'

// generate random bigint
const salt = genRandomSalt()

genEpochKey

import { Identity } from '@semaphore-protocol/identity'
import { genEpochKey } from '@unirep/utils'

const id = new Identity()
const attesterId = '0x1234'
const epoch = 0
const nonce = 0
const chainId = 1
const epochKey = genEpochKey(
    id.secret,
    attesterId,
    epoch,
    nonce,
    chainId
)

genIdentityHash

import { Identity } from '@semaphore-protocol/identity'
import { genIdentityHash } from '@unirep/utils'

const id = new Identity()
const attesterId = '0x1234'
const epoch = 0
const chainId = 1
const idHash = genIdentityHash(
    id.secret,
    attesterId,
    epoch,
    chainId
)

genStateTreeLeaf

import { Identity } from '@semaphore-protocol/identity'
import { genStateTreeLeaf } from '@unirep/utils'

const id = new Identity()
const attesterId = '0x1234'
const epoch = 0
const FIELD_COUNT = 6
const data = Array(FIELD_COUNT).fill(0)
const chainId = 1
const leaf = genStateTreeLeaf(
    id.secret,
    attesterId,
    epoch,
    data,
    chainId
)

genEpochTreeLeaf

import { genEpochTreeLeaf } from '@unirep/utils'

const epochKey = '0x3456'
const FIELD_COUNT = 6
const data = Array(FIELD_COUNT).fill(0)
const leaf = genEpochTreeLeaf(
    epochKey,
    data
)

stringifyBigInts/unstringifyBigInts

import { stringifyBigInts, unstringifyBigInts } from '@unirep/utils'

const values = {
    input1: genRandomSalt(),
    input2: genRandomSalt(),
    input3: genRandomSalt(),
}
// stringify bigint elements with stringifyBigInts function
const stringifiedValues = stringifyBigInts(values)
// it can be recoverd by unstringifyBigInts function
const unstringifiedValues = unstringifyBigInts(stringifiedValues)

🙌🏻 Join our community

  • Discord server:
  • Twitter account:
  • Telegram group:

Privacy & Scaling Explorations

This project is supported by Privacy & Scaling Explorations and the Ethereum Foundation. See more projects on: https://pse.dev/.

Package Sidebar

Install

npm i @unirep/utils

Weekly Downloads

31

Version

2.0.1

License

MIT

Unpacked Size

26.3 kB

Total Files

10

Last publish

Collaborators

  • vivianjeng
  • jchancehud