Skip to content

Commit f2575f2

Browse files
authored
Merge pull request #324 from semaphore-protocol/feat/is-member
New function to check if a member exists Former-commit-id: e783465
2 parents 6580b66 + 3bfc7ce commit f2575f2

File tree

3 files changed

+62
-0
lines changed

3 files changed

+62
-0
lines changed

packages/data/README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,15 @@ const group = await semaphoreSubgraph.getGroup("42")
118118
const { members, verifiedProofs } = semaphoreSubgraph.getGroup("42", { members: true, verifiedProofs: true })
119119
```
120120

121+
\# **isGroupMember**(groupId: _string_, member: _string_): _Promise\<boolean>_
122+
123+
```typescript
124+
await semaphoreSubgraph.isGroupMember(
125+
"42",
126+
"16948514235341957898454876473214737047419402240398321289450170535251226167324"
127+
)
128+
```
129+
121130
\# **new Ethers**(networkOrEthereumURL: Network | string = "goerli", options: EthersOptions = {}): _SemaphoreEthers_
122131

123132
```typescript

packages/data/src/subgraph.test.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -338,4 +338,31 @@ describe("SemaphoreSubgraph", () => {
338338
})
339339
})
340340
})
341+
342+
describe("# isGroupMember", () => {
343+
it("Should throw an error if the member parameter type is wrong", async () => {
344+
const fun = () => semaphore.isGroupMember("1", 1 as any)
345+
346+
await expect(fun).rejects.toThrow("Parameter 'member' is not a string")
347+
})
348+
349+
it("Should return true if a group member exist", async () => {
350+
requestMocked.mockImplementationOnce(() =>
351+
Promise.resolve({
352+
groups: [
353+
{
354+
id: "1"
355+
}
356+
]
357+
})
358+
)
359+
360+
const expectedValue = await semaphore.isGroupMember(
361+
"1",
362+
"19759682999141591121829027463339362582441675980174830329241909768001406603165"
363+
)
364+
365+
expect(expectedValue).toBeTruthy()
366+
})
367+
})
341368
})

packages/data/src/subgraph.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,4 +195,30 @@ export default class SemaphoreSubgraph {
195195

196196
return groups[0]
197197
}
198+
199+
/**
200+
* Returns true if a member is part of group, and false otherwise.
201+
* @param groupId Group id
202+
* @param member Group member.
203+
* @returns True if the member is part of the group, false otherwise.
204+
*/
205+
async isGroupMember(groupId: string, member: string): Promise<boolean> {
206+
checkParameter(groupId, "groupId", "string")
207+
checkParameter(member, "member", "string")
208+
209+
const config: AxiosRequestConfig = {
210+
method: "post",
211+
data: JSON.stringify({
212+
query: `{
213+
groups(where: { id: "${groupId}", members_: { identityCommitment: "${member}" } }) {
214+
id
215+
}
216+
}`
217+
})
218+
}
219+
220+
const { groups } = await request(this._url, config)
221+
222+
return groups.length !== 0
223+
}
198224
}

0 commit comments

Comments
 (0)