Files
semaphore/packages/data
Vivian Plasencia d4fb23a7af chore: update roadmap website and license year (#1060)
* chore(website): update roadmap

* chore(website): update license year 2024 -> 2025

re #947
2025-10-23 00:39:16 +02:00
..
2025-10-13 10:31:58 +02:00

Semaphore data

A library for querying Semaphore smart contract.

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

This library provides tools for querying and interacting with the Semaphore.sol smart contract. It supports the Semaphore subgraph and direct Ethereum network connections via Ethers or Viem. Designed for use in both Node.js and browser environments, it facilitates the management of group data and verification processes within the Semaphore protocol.

🛠 Install

npm or yarn

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

npm i @semaphore-protocol/data

or yarn:

yarn add @semaphore-protocol/data

📜 Usage

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

Creating and Managing Subgraphs

Initialize a Semaphore Subgraph instance

import { SemaphoreSubgraph } from "@semaphore-protocol/data"

const semaphoreSubgraph = new SemaphoreSubgraph()

// or:
const semaphoreSubgraphOnArbitrum = new SemaphoreSubgraph("arbitrum")

// or:
const customSubgraph = new SemaphoreSubgraph(
    "https://api.studio.thegraph.com/query/14377/<your-subgraph>/<your-version>"
)

With your SemaphoreSubgraph, you can:

Query Group IDs

const groupIds = await semaphoreSubgraph.getGroupIds()

Query Group Details

const group = await semaphoreSubgraph.getGroup("42")
const { members, verifiedProofs } = await semaphoreSubgraph.getGroup("42", { members: true, verifiedProofs: true })

Query Group Members

const members = await semaphoreSubgraph.getGroupMembers("42")

Query Verified Proofs

const verifiedProofs = await semaphoreSubgraph.getGroupVerifiedProofs("42")

Check Group Membership

const isMember = await semaphoreSubgraph.isGroupMember(
    "42",
    "16948514235341957898454876473214737047419402240398321289450170535251226167324"
)

Using Ethers for Direct Blockchain Interaction

Initialize a Semaphore Ethers instance

import { SemaphoreEthers } from "@semaphore-protocol/data"

const semaphoreEthers = new SemaphoreEthers()

// or:
const semaphoreEthersOnHomestead = new SemaphoreEthers("homestead", {
    address: "semaphore-address",
    startBlock: 0
})

// or:
const localEthersInstance = new SemaphoreEthers("http://localhost:8545", {
    address: "semaphore-address"
})

With your SemaphoreEthers instance, you can:

Fetch Group IDs

const groupIds = await semaphoreEthers.getGroupIds()

Fetch Group Details

const group = await semaphoreEthers.getGroup("42")

Fetch Group Admin

const admin = await semaphoreEthers.getGroupAdmin("42")

Fetch Group Members

const members = await semaphoreEthers.getGroupMembers("42")

Fetch Validated Proofs

const verifiedProofs = await semaphoreEthers.getGroupValidatedProofs("42")

Check Group Membership

const isMember = await semaphoreEthers.isGroupMember(
    "42",
    "16948514235341957898454876473214737047419402240398321289450170535251226167324"
)

Using Viem for Direct Blockchain Interaction

Initialize a Semaphore Viem instance

import { SemaphoreViem } from "@semaphore-protocol/data"

const semaphoreViem = new SemaphoreViem()

// or:
const semaphoreViemOnSepolia = new SemaphoreViem("sepolia", {
    address: "semaphore-address",
    startBlock: 0n
})

// or:
const localViemInstance = new SemaphoreViem("http://localhost:8545", {
    address: "semaphore-address"
})

With your SemaphoreViem instance, you can:

Fetch Group IDs

const groupIds = await semaphoreViem.getGroupIds()

Fetch Group Details

const group = await semaphoreViem.getGroup("42")

Fetch Group Members

const members = await semaphoreViem.getGroupMembers("42")

Fetch Validated Proofs

const validatedProofs = await semaphoreViem.getGroupValidatedProofs("42")

Check Group Membership

const isMember = await semaphoreViem.isGroupMember(
    "42",
    "16948514235341957898454876473214737047419402240398321289450170535251226167324"
)