Skip to content

Commit

Permalink
Merge pull request #266 from privacy-scaling-explorations/refactor/cl…
Browse files Browse the repository at this point in the history
…ient-api-lib

API lib to the client app
  • Loading branch information
cedoor authored Aug 11, 2023
2 parents 4cc1af3 + 5deaba3 commit 63fd92d
Show file tree
Hide file tree
Showing 16 changed files with 162 additions and 59 deletions.
4 changes: 2 additions & 2 deletions apps/api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
"build": "nest build"
},
"dependencies": {
"@bandada/credentials": "0.12.0",
"@bandada/utils": "0.12.0",
"@bandada/credentials": "0.14.0",
"@bandada/utils": "0.14.0",
"@ethersproject/hash": "^5.7.0",
"@nestjs/common": "^9.0.0",
"@nestjs/core": "^9.0.0",
Expand Down
4 changes: 2 additions & 2 deletions apps/client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
"preview": "vite preview"
},
"dependencies": {
"@bandada/api-sdk": "0.12.0",
"@bandada/utils": "0.12.0",
"@bandada/api-sdk": "0.14.0",
"@bandada/utils": "0.14.0",
"@chakra-ui/react": "^2.5.1",
"@chakra-ui/theme-tools": "^2.0.16",
"@emotion/react": "^11.10.6",
Expand Down
62 changes: 35 additions & 27 deletions apps/client/src/pages/home.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { request } from "@bandada/utils"
import {
Button,
Container,
Expand All @@ -19,6 +18,12 @@ import { useCallback, useEffect, useState } from "react"
import { FiGithub } from "react-icons/fi"
import { useSearchParams } from "react-router-dom"
import icon1Image from "../assets/icon1.svg"
import {
addMemberByInviteCode,
getGroup,
getInvite,
isGroupMember
} from "../utils/api"

const injectedConnector = new InjectedConnector({})

Expand Down Expand Up @@ -54,12 +59,9 @@ export default function HomePage(): JSX.Element {
if (account && library) {
setLoading(true)

const invite = await request(
`${import.meta.env.VITE_API_URL}/invites/${inviteCode}`
)
const invite = await getInvite(inviteCode)

if (!invite) {
alert("Some error occurred!")
if (invite === null) {
setLoading(false)
return
}
Expand All @@ -70,33 +72,36 @@ export default function HomePage(): JSX.Element {
const identity = new Identity(await signer.signMessage(message))
const identityCommitment = identity.getCommitment().toString()

const hasJoined = await request(
`${import.meta.env.VITE_API_URL}/groups/${
invite.groupId
}/members/${identityCommitment}`
const hasJoined = await isGroupMember(
invite.groupId,
identityCommitment
)

if (hasJoined === null) {
setLoading(false)
return
}

if (hasJoined) {
alert("You have already joined this group")
setLoading(false)
alert("You have already joined this group")
return
}

await request(
`${import.meta.env.VITE_API_URL}/groups/${
invite.groupId
}/members/${identityCommitment}`,
{
method: "post",
data: {
inviteCode
}
}
const response = await addMemberByInviteCode(
invite.groupId,
identityCommitment,
inviteCode
)

alert("You have joined the group!")
if (response === null) {
setLoading(false)
return
}

setInviteCode("")
setLoading(false)
alert("You have joined the group!")
}
},
[account, library]
Expand All @@ -107,12 +112,15 @@ export default function HomePage(): JSX.Element {
if (account && library) {
setLoading(true)

const { credentials } = await request(
`${import.meta.env.VITE_API_URL}/groups/${groupId}`
)
const group = await getGroup(groupId)

if (group === null) {
setLoading(false)
return
}

const providerName = JSON.parse(credentials)
.id.split("_")[0]
const providerName = group.credentials.id
.split("_")[0]
.toLowerCase()

const signer = library.getSigner(account)
Expand Down
76 changes: 76 additions & 0 deletions apps/client/src/utils/api.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import { ApiSdk, GroupResponse, InviteResponse } from "@bandada/api-sdk"

const api = new ApiSdk(import.meta.env.VITE_API_URL)

export async function getInvite(
inviteCode: string
): Promise<InviteResponse | null> {
try {
return await api.getInvite(inviteCode)
} catch (error: any) {
console.error(error)

if (error.response) {
alert(error.response.statusText)
} else {
alert("Some error occurred!")
}

return null
}
}

export async function getGroup(groupId: string): Promise<GroupResponse | null> {
try {
return await api.getGroup(groupId)
} catch (error: any) {
console.error(error)

if (error.response) {
alert(error.response.statusText)
} else {
alert("Some error occurred!")
}

return null
}
}

export async function isGroupMember(
groupId: string,
memberId: string
): Promise<boolean | null> {
try {
return await api.isGroupMember(groupId, memberId)
} catch (error: any) {
console.error(error)

if (error.response) {
alert(error.response.statusText)
} else {
alert("Some error occurred!")
}

return null
}
}

export async function addMemberByInviteCode(
groupId: string,
memberId: string,
inviteCode: string
): Promise<void | null> {
try {
return await api.addMemberByInviteCode(groupId, memberId, inviteCode)
} catch (error: any) {
console.error(error)

if (error.response) {
alert(error.response.statusText)
} else {
alert("Some error occurred!")
}

return null
}
}
2 changes: 1 addition & 1 deletion apps/contracts/contracts/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@bandada/contracts",
"version": "0.12.0",
"version": "0.14.0",
"description": "Bandada smart contracts to manage off-chain groups and verify their zero-knowledge proofs.",
"license": "MIT",
"files": [
Expand Down
4 changes: 2 additions & 2 deletions apps/dashboard/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
"preview": "vite preview"
},
"dependencies": {
"@bandada/credentials": "0.12.0",
"@bandada/utils": "0.12.0",
"@bandada/credentials": "0.14.0",
"@bandada/utils": "0.14.0",
"@chakra-ui/react": "^2.5.1",
"@chakra-ui/theme-tools": "^2.0.16",
"@emotion/react": "^11.10.6",
Expand Down
1 change: 1 addition & 0 deletions apps/dashboard/src/hooks/use-session-data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export default function useSessionData() {
useEffect(() => {
;(async () => {
if (!(await bandadaAPI.isLoggedIn())) {
session.deleteAdmin()
setAdmin(null)
}
})()
Expand Down
4 changes: 2 additions & 2 deletions libs/api-sdk/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@bandada/api-sdk",
"version": "0.12.0",
"version": "0.14.0",
"description": "A Typescript SDK for the Bandada API.",
"license": "MIT",
"main": "dist/index.node.js",
Expand Down Expand Up @@ -29,7 +29,7 @@
"access": "public"
},
"dependencies": {
"@bandada/utils": "0.12.0"
"@bandada/utils": "0.14.0"
},
"devDependencies": {
"@rollup/plugin-typescript": "^11.0.0",
Expand Down
2 changes: 1 addition & 1 deletion libs/api-sdk/src/apiSdk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export default class ApiSdk {
checkParameter(config, "config", "object")
if (!config["baseURL"]) {
this._config = {
baseUrl: url,
baseURL: url,
...config
}
} else {
Expand Down
16 changes: 13 additions & 3 deletions libs/api-sdk/src/groups.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ const url = "/groups"
export async function getGroups(config: object): Promise<GroupResponse[]> {
const groups = await request(url, config)

groups.map((group: any) => ({
...group,
credentials: JSON.parse(group.credentials)
}))

return groups
}

Expand All @@ -26,6 +31,8 @@ export async function getGroup(

const group = await request(requestUrl, config)

group.credentials = JSON.parse(group.credentials)

return group
}

Expand Down Expand Up @@ -105,12 +112,15 @@ export async function addMemberByInviteCode(
): Promise<void> {
const requestUrl = `${url}/${groupId}/members/${memberId}`

await request(requestUrl, {
const newConfig: any = {
method: "post",
data: {
inviteCode
}
})
},
...config
}

await request(requestUrl, newConfig)
}

/**
Expand Down
2 changes: 2 additions & 0 deletions libs/api-sdk/src/invites.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,7 @@ export async function getInvite(

const invite = await request(requestUrl, config)

invite.group.credentials = JSON.parse(invite.group.credentials)

return invite
}
10 changes: 8 additions & 2 deletions libs/api-sdk/src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ export type GroupResponse = {
fingerprintDuration: number
createdAt: Date
members: string[]
credentials: object
credentials: {
id: string
criteria: Record<string, any>
}
}

type Group = {
Expand All @@ -17,7 +20,10 @@ type Group = {
adminId: string
treeDepth: number
fingerprintDuration: number
credentials: object
credentials: {
id: string
criteria: Record<string, any>
}
apiEnabled: boolean
apiKey: string
createdAt: Date
Expand Down
4 changes: 2 additions & 2 deletions libs/credentials/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@bandada/credentials",
"version": "0.12.0",
"version": "0.14.0",
"description": "Bandada library to validate users' credentials.",
"license": "MIT",
"main": "dist/index.node.js",
Expand Down Expand Up @@ -29,7 +29,7 @@
"access": "public"
},
"dependencies": {
"@bandada/utils": "0.12.0"
"@bandada/utils": "0.14.0"
},
"devDependencies": {
"@rollup/plugin-typescript": "^11.0.0",
Expand Down
4 changes: 2 additions & 2 deletions libs/hardhat/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@bandada/hardhat",
"version": "0.12.0",
"version": "0.14.0",
"description": "A Hardhat plugin which provides tasks to deploy Bandada contracts.",
"license": "MIT",
"main": "dist/index.node.js",
Expand Down Expand Up @@ -39,7 +39,7 @@
"typescript": "^4.9.5"
},
"peerDependencies": {
"@bandada/contracts": "0.12.0",
"@bandada/contracts": "0.14.0",
"@nomiclabs/hardhat-ethers": "^2.0.0",
"@semaphore-protocol/contracts": "3.4.0",
"ethers": "^5.4.7",
Expand Down
2 changes: 1 addition & 1 deletion libs/utils/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@bandada/utils",
"version": "0.12.0",
"version": "0.14.0",
"description": "General Bandada utility functions.",
"license": "MIT",
"main": "dist/index.node.js",
Expand Down
Loading

0 comments on commit 63fd92d

Please sign in to comment.