Skip to content

feat(data): implement Viem lib alternative to SemaphoreEthers #966

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 58 additions & 2 deletions packages/data/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@
</h4>
</div>

| This library provides tools for querying and interacting with the [`Semaphore.sol`](https://github.com/semaphore-protocol/semaphore/blob/main/packages/contracts/contracts/Semaphore.sol) smart contract. It supports both the Semaphore subgraph and direct Ethereum network connections via Ethers. Designed for use in both Node.js and browser environments, it facilitates the management of group data and verification processes within the Semaphore protocol. |
| ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| This library provides tools for querying and interacting with the [`Semaphore.sol`](https://github.com/semaphore-protocol/semaphore/blob/main/packages/contracts/contracts/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

Expand Down Expand Up @@ -187,3 +187,59 @@ const isMember = await semaphoreEthers.isGroupMember(
"16948514235341957898454876473214737047419402240398321289450170535251226167324"
)
```

### Using Viem for Direct Blockchain Interaction

**Initialize a Semaphore Viem instance**

```typescript
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**

```typescript
const groupIds = await semaphoreViem.getGroupIds()
```

**Fetch Group Details**

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

**Fetch Group Members**

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

**Fetch Validated Proofs**

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

**Check Group Membership**

```typescript
const isMember = await semaphoreViem.isGroupMember(
"42",
"16948514235341957898454876473214737047419402240398321289450170535251226167324"
)
```
3 changes: 2 additions & 1 deletion packages/data/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
"@semaphore-protocol/utils": "4.9.1",
"@zk-kit/utils": "1.3.0",
"axios": "1.6.6",
"ethers": "6.13.4"
"ethers": "6.13.4",
"viem": "2.23.7"
}
}
3 changes: 2 additions & 1 deletion packages/data/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import SemaphoreEthers from "./ethers"
import SemaphoreSubgraph from "./subgraph"
import SemaphoreViem from "./viem"

export * from "./types"
export { SemaphoreSubgraph, SemaphoreEthers }
export { SemaphoreSubgraph, SemaphoreEthers, SemaphoreViem }
12 changes: 12 additions & 0 deletions packages/data/src/types/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { Chain, Transport } from "viem"

export type EthersNetwork =
| "mainnet"
| "sepolia"
Expand All @@ -12,6 +14,8 @@ export type EthersNetwork =
| "linea"
| "linea-sepolia"

export type ViemNetwork = EthersNetwork

export type GroupOptions = {
members?: boolean
validatedProofs?: boolean
Expand Down Expand Up @@ -54,3 +58,11 @@ export type EthersOptions = {
applicationId?: string // Pocket
applicationSecret?: string // Pocket
}

export type ViemOptions = {
address?: string
startBlock?: bigint | number
transport?: Transport // Transport from viem
chain?: Chain // Chain from viem
apiKey?: string
}
Loading