Skip to content

Latest commit

 

History

History
202 lines (144 loc) · 5.9 KB

README.md

File metadata and controls

202 lines (144 loc) · 5.9 KB

Semaphore group

A library to create and manage Semaphore groups.

NPM license NPM version Downloads Documentation typedoc Linter eslint Code style prettier

This library is an abstraction of the LeanIMT data structure (part of @zk-kit/imt). The main goal is to make it easier to create offchain groups, which are also used to generate Semaphore proofs. Semaphore groups are actually Merkle trees, and the group members are tree leaves.

🛠 Install

npm or yarn

Install the @semaphore-protocol/group package with npm:

npm i @semaphore-protocol/group

or yarn:

yarn add @semaphore-protocol/group

📜 Usage

For more information on the functions provided by @semaphore-protocol/group, please refer to the TypeDoc documentation.

# new Group(members: BigNumberish[] = []): Group

import { Group } from "@semaphore-protocol/group"
import { Identity } from "@semaphore-protocol/identity"

const group1 = new Group()

const identity1 = new Identity()
const identity2 = new Identity()

const group2 = new Group([identity1.commitment, identity2.commitment])

# addMember(member: BigNumberish)

import { Group } from "@semaphore-protocol/group"
import { Identity } from "@semaphore-protocol/identity"

const group = new Group()

const { commitment } = new Identity()

group.addMember(commitment)

// 12989101133047504182892154686643420754368236204022364847543591045056549053997n
console.log(group.members[0])

# addMembers(members: BigNumberish[])

import { Group } from "@semaphore-protocol/group"
import { Identity } from "@semaphore-protocol/identity"

const group = new Group()

const identity1 = new Identity()
const identity2 = new Identity()

group.addMembers([identity1.commitment, identity2.commitment])

# updateMember(index: number, member: BigNumberish)

import { Group } from "@semaphore-protocol/group"

const group = new Group([1n, 3n])

group.updateMember(0, 2)

console.log(group.members[0]) // "2n"

# removeMember(index: number)

import { Group } from "@semaphore-protocol/group"

const group = new Group([1n, 3n])

group.removeMember(0)

console.log(group.members[0]) // 0n

# indexOf(member: BigNumberish): number

import { Group } from "@semaphore-protocol/group"

const group = new Group([1n])

const index = group.indexOf(1)

console.log(index) // 0

# generateMerkleProof(index: number): MerkleProof

import { Group } from "@semaphore-protocol/group"

const group = new Group([1n, 3n])

const proof = group.generateMerkleProof(0)

console.log(proof)
/*
{
    index: 0,
    leaf: '1',
    root: '21106761926285267690763443010820487107972411248208546226053195422384279971821',
    siblings: [ '3' ]
}
*/

# export(): string

import { Group } from "@semaphore-protocol/group"

const group = new Group([1n, 2n, 3n])

const exportedGroup = group.export()

console.log(exportedGroup)
/*
[["1","2","3"],["7853200120776062878684798364095072458815029376092732009249414926327459813530","3"],["13816780880028945690020260331303642730075999758909899334839547418969502592169"]]
*/

# import(exportedGroup: string): Group

import { Group } from "@semaphore-protocol/group"

const group1 = new Group([1n, 2n, 3n])

const exportedGroup = group.export()

const group2 = Group.import(exportedGroup)

assert(group1.root === group2.root)