Skip to main content
Version: V3

Semaphore data

To fetch on-chain data from the Semaphore.sol contract, you can use the @semaphore-protocol/data library.

There are two ways to do this, using SemaphoreSubgraph or SemaphoreEthers. The SemaphoreSubgraph class uses the Semaphore subgraph, which uses The Graph Protocol under the hood, and the SemaphoreEthers class uses Ethers.

Install library​

npm install @semaphore-protocol/data@^3

Fetch data using SemaphoreSubgraph​

To fetch data using the Semaphore subgraph you can use the SemaphoreSubgraph class from the @semaphore-protocol/data package.

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

const semaphoreSubgraph = new SemaphoreSubgraph()

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

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

Get group Ids​

const groupIds = await semaphoreSubgraph.getGroupIds()

Get groups​

const groups = await semaphoreSubgraph.getGroups()

// or

const groups = await semaphoreSubgraph.getGroups({ members: true, verifiedProofs: true })

Get group​

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

// or

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

Check if an identity commitment is a member of a group​

await semaphoreSubgraph.isGroupMember(
"42",
"16948514235341957898454876473214737047419402240398321289450170535251226167324"
)
info

You can create an off-chain group using the SemaphoreSubgraph class to fetch members like so:

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

const groupId = "3"
const semaphoreSubgraph = new SemaphoreSubgraph("sepolia")
const { members } = await semaphoreSubgraph.getGroup(groupId, { members: true })
const group = new Group(groupId, 20, members)

Fetch data using SemaphoreEthers​

To fetch data using Ethers you can use the SemaphoreEthers class from the @semaphore-protocol/data package.

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

const semaphoreEthers = new SemaphoreEthers()

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

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

Get group Ids​

const groupIds = await semaphoreEthers.getGroupIds()

Get group​

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

Get group admin​

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

Get group members​

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

Get group verified proofs​

const verifiedProofs = await semaphoreEthers.getGroupVerifiedProofs("42")
info

You can create an off-chain group using the SemaphoreEthers class to fetch members like so:

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

const groupId = "3"
const semaphoreEthers = new SemaphoreEthers("sepolia")
const members = await semaphoreEthers.getGroupMembers(groupId)
const group = new Group(groupId, 20, members)