Skip to content

Commit f3c4cf2

Browse files
feat(data): implement Viem lib alternative to SemaphoreEthers (#966)
* feat(data): implement Viem lib alternative to Ethers for @semaphore-protocol/data package class This adds a Viem-based alternative to SemaphoreEthers, allowing developers to choose their preferred Ethereum library. Closes #343 * chore(data): update yarn.lock for viem dependency * chore(data): improve test coverage for SemaphoreViem class * chore(data): improve test coverage for SemaphoreViem class
1 parent 57132a3 commit f3c4cf2

File tree

7 files changed

+1903
-13
lines changed

7 files changed

+1903
-13
lines changed

packages/data/README.md

Lines changed: 58 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,8 @@
4949
</h4>
5050
</div>
5151

52-
| 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. |
53-
| ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
52+
| 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. |
53+
| ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
5454

5555
## 🛠 Install
5656

@@ -187,3 +187,59 @@ const isMember = await semaphoreEthers.isGroupMember(
187187
"16948514235341957898454876473214737047419402240398321289450170535251226167324"
188188
)
189189
```
190+
191+
### Using Viem for Direct Blockchain Interaction
192+
193+
**Initialize a Semaphore Viem instance**
194+
195+
```typescript
196+
import { SemaphoreViem } from "@semaphore-protocol/data"
197+
198+
const semaphoreViem = new SemaphoreViem()
199+
200+
// or:
201+
const semaphoreViemOnSepolia = new SemaphoreViem("sepolia", {
202+
address: "semaphore-address",
203+
startBlock: 0n
204+
})
205+
206+
// or:
207+
const localViemInstance = new SemaphoreViem("http://localhost:8545", {
208+
address: "semaphore-address"
209+
})
210+
```
211+
212+
With your SemaphoreViem instance, you can:
213+
214+
**Fetch Group IDs**
215+
216+
```typescript
217+
const groupIds = await semaphoreViem.getGroupIds()
218+
```
219+
220+
**Fetch Group Details**
221+
222+
```typescript
223+
const group = await semaphoreViem.getGroup("42")
224+
```
225+
226+
**Fetch Group Members**
227+
228+
```typescript
229+
const members = await semaphoreViem.getGroupMembers("42")
230+
```
231+
232+
**Fetch Validated Proofs**
233+
234+
```typescript
235+
const validatedProofs = await semaphoreViem.getGroupValidatedProofs("42")
236+
```
237+
238+
**Check Group Membership**
239+
240+
```typescript
241+
const isMember = await semaphoreViem.isGroupMember(
242+
"42",
243+
"16948514235341957898454876473214737047419402240398321289450170535251226167324"
244+
)
245+
```

packages/data/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
"@semaphore-protocol/utils": "4.10.0",
4141
"@zk-kit/utils": "1.3.0",
4242
"axios": "1.6.6",
43-
"ethers": "6.13.4"
43+
"ethers": "6.13.4",
44+
"viem": "2.23.7"
4445
}
4546
}

packages/data/src/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import SemaphoreEthers from "./ethers"
22
import SemaphoreSubgraph from "./subgraph"
3+
import SemaphoreViem from "./viem"
34

45
export * from "./types"
5-
export { SemaphoreSubgraph, SemaphoreEthers }
6+
export { SemaphoreSubgraph, SemaphoreEthers, SemaphoreViem }

packages/data/src/types/index.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { Chain, Transport } from "viem"
2+
13
export type EthersNetwork =
24
| "mainnet"
35
| "sepolia"
@@ -12,6 +14,8 @@ export type EthersNetwork =
1214
| "linea"
1315
| "linea-sepolia"
1416

17+
export type ViemNetwork = EthersNetwork
18+
1519
export type GroupOptions = {
1620
members?: boolean
1721
validatedProofs?: boolean
@@ -54,3 +58,11 @@ export type EthersOptions = {
5458
applicationId?: string // Pocket
5559
applicationSecret?: string // Pocket
5660
}
61+
62+
export type ViemOptions = {
63+
address?: string
64+
startBlock?: bigint | number
65+
transport?: Transport // Transport from viem
66+
chain?: Chain // Chain from viem
67+
apiKey?: string
68+
}

0 commit comments

Comments
 (0)