Skip to content

Commit

Permalink
Merge pull request #362 from privacy-scaling-explorations/feat/285tre…
Browse files Browse the repository at this point in the history
…eRoot

feat: returning group/groups fingerprint in APIs/SDK response
  • Loading branch information
aguzmant103 authored Jan 26, 2024
2 parents cf4abdf + 15a4b5e commit e4f2dd2
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 13 deletions.
2 changes: 2 additions & 0 deletions apps/api/src/app/groups/docSchemas/group.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ export class Group {
@ApiProperty()
treeDepth: number
@ApiProperty()
fingerprint: string
@ApiProperty()
fingerprintDuration: number
@ApiProperty()
createdAt: Date
Expand Down
13 changes: 12 additions & 1 deletion apps/api/src/app/groups/groups.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,18 +45,24 @@ export class GroupsController {
@ApiCreatedResponse({ type: Group, isArray: true })
async getGroups(@Query("adminId") adminId: string) {
const groups = await this.groupsService.getGroups({ adminId })
const groupIds = groups.map((group) => group.id)
const fingerprints = await this.groupsService.getFingerprints(groupIds)

return groups.map((g) => mapGroupToResponseDTO(g))
return groups.map((group, index) =>
mapGroupToResponseDTO(group, fingerprints[index])
)
}

@Get(":group")
@ApiOperation({ description: "Returns a specific group." })
@ApiCreatedResponse({ type: Group })
async getGroup(@Param("group") groupId: string, @Req() req: Request) {
const group = await this.groupsService.getGroup(groupId)
const fingerprint = await this.groupsService.getFingerprint(groupId)

return mapGroupToResponseDTO(
group,
fingerprint,
req.session.adminId === group.adminId
)
}
Expand All @@ -69,9 +75,11 @@ export class GroupsController {
dto,
req.session.adminId
)
const fingerprint = await this.groupsService.getFingerprint(group.id)

return mapGroupToResponseDTO(
group,
fingerprint,
req.session.adminId === group.adminId
)
}
Expand All @@ -97,8 +105,11 @@ export class GroupsController {
req.session.adminId
)

const fingerprint = await this.groupsService.getFingerprint(groupId)

return mapGroupToResponseDTO(
group,
fingerprint,
req.session.adminId === group.adminId
)
}
Expand Down
51 changes: 39 additions & 12 deletions apps/api/src/app/groups/groups.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ export class GroupsService {

/**
* Join the group by redeeming invite code.
* @param groupId Group name.
* @param groupId Group id.
* @param memberId Member's identity commitment.
* @param dto Parameters used to add a group member.
* @returns Group data with added member.
Expand All @@ -248,7 +248,7 @@ export class GroupsService {

/**
* Add a member to the group manually as an admin.
* @param groupId ID of the group
* @param groupId Group id.
* @param memberIds Member's identity to be added
* @param adminId id of the admin making the request
* @returns Group
Expand All @@ -263,7 +263,7 @@ export class GroupsService {

/**
* Add members to the group manually as an admin.
* @param groupId ID of the group
* @param groupId Group id.
* @param memberIds Array of member IDs to be added
* @param adminId id of the admin making the request
* @returns Group
Expand Down Expand Up @@ -294,7 +294,7 @@ export class GroupsService {

/**
* Add a member to the group using API Key.
* @param groupId ID of the group
* @param groupId Group id.
* @param memberIds Member's identity to be added
* @param apiKey API key for the group
* @returns Group
Expand All @@ -309,7 +309,7 @@ export class GroupsService {

/**
* Add members to the group using API Key.
* @param groupId ID of the group
* @param groupId Group id.
* @param memberIds Array of member IDs to be added
* @param apiKey API key for the group
* @returns Group
Expand Down Expand Up @@ -340,7 +340,7 @@ export class GroupsService {

/**
* Add a member to the group.
* @param groupId ID of the group
* @param groupId Group id.
* @param memberId ID of the member to be added
* @returns Group
*/
Expand Down Expand Up @@ -387,7 +387,7 @@ export class GroupsService {

/**
* Add multiple members to the group.
* @param groupId ID of the group
* @param groupId Group id.
* @param memberIds Array of member IDs to be added
* @returns Group
*/
Expand Down Expand Up @@ -439,7 +439,7 @@ export class GroupsService {

/**
* Delete a member from a group.
* @param groupId Group name.
* @param groupId Group id.
* @param memberId Member's identity commitment.
* @param adminId Group admin id.
* @returns Group data with removed member.
Expand All @@ -454,7 +454,7 @@ export class GroupsService {

/**
* Delete members from a group.
* @param groupId Group name.
* @param groupId Group id.
* @param memberIds Array of member's identity commitments.
* @param adminId Group admin id.
* @returns Group data with removed member.
Expand Down Expand Up @@ -517,7 +517,7 @@ export class GroupsService {

/**
* Delete a member from group using API Key
* @param groupId Group name.
* @param groupId Group id.
* @param memberId Member's identity commitment.
* @returns Group data with removed member.
*/
Expand All @@ -531,7 +531,7 @@ export class GroupsService {

/**
* Delete a member from group using API Key
* @param groupId Group name.
* @param groupId Group id.
* @param memberIds Array of members' identity commitment.
* @returns Group data with removed member.
*/
Expand Down Expand Up @@ -612,7 +612,7 @@ export class GroupsService {

/**
* Returns a specific group.
* @param groupId Group name.
* @param groupId Group id.
* @returns Specific group.
*/
async getGroup(groupId: string): Promise<Group> {
Expand Down Expand Up @@ -661,6 +661,33 @@ export class GroupsService {
return cachedGroup.generateMerkleProof(memberIndex)
}

/**
* Returns a group fingerprint.
* @param groupId Group id.
* @returns Group fingerprint.
*/
async getFingerprint(groupId: string): Promise<string> {
return (await this.getFingerprints([groupId]))[0]
}

/**
* Returns a list of fingerprints from a list of groups.
* @param groupIds Array of group ids.
* @returns Fingerprints from groups.
*/
async getFingerprints(groupIds: string[]): Promise<string[]> {
const fingerprints: string[] = []

for (const groupId of groupIds) {
const cachedGroup = this.cachedGroups.get(groupId)
if (cachedGroup) {
fingerprints.push(cachedGroup.root.toString())
}
}

return fingerprints
}

private async _updateFingerprintDuration(
groupId: string,
duration: number
Expand Down
7 changes: 7 additions & 0 deletions apps/api/src/app/groups/groups.utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,18 @@ describe("Groups utils", () => {
it("Should map the group data with api keys if specified", async () => {
const { apiKey, apiEnabled } = mapGroupToResponseDTO(
{ apiEnabled: true, apiKey: "123" } as any,
"12345",
true
)

expect(apiEnabled).toBeTruthy()
expect(apiKey).toBe("123")
})

it("Should map the fingerprint correctly", async () => {
const { fingerprint } = mapGroupToResponseDTO({} as any, "12345")

expect(fingerprint).toBe("12345")
})
})
})
2 changes: 2 additions & 0 deletions apps/api/src/app/groups/groups.utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Group } from "./entities/group.entity"

export function mapGroupToResponseDTO(
group: Group,
fingerprint: string = "",
includeAPIKey: boolean = false
) {
const dto = {
Expand All @@ -10,6 +11,7 @@ export function mapGroupToResponseDTO(
description: group.description,
admin: group.adminId,
treeDepth: group.treeDepth,
fingerprint,
fingerprintDuration: group.fingerprintDuration,
createdAt: group.createdAt,
members: (group.members || []).map((m) => m.id),
Expand Down
1 change: 1 addition & 0 deletions libs/api-sdk/src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ export type GroupResponse = {
description: string
admin: string
treeDepth: number
fingerprint: string
fingerprintDuration: number
createdAt: Date
members: string[]
Expand Down

0 comments on commit e4f2dd2

Please sign in to comment.