Skip to content

Commit

Permalink
define response for app requests wip
Browse files Browse the repository at this point in the history
  • Loading branch information
lukachi committed Mar 26, 2024
1 parent 44e9379 commit ed925cd
Show file tree
Hide file tree
Showing 9 changed files with 177 additions and 212 deletions.
23 changes: 0 additions & 23 deletions src/api/modules/verify/consts/index.ts

This file was deleted.

3 changes: 2 additions & 1 deletion src/api/modules/verify/enums/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export enum ClaimTypes {
AuthClaim = 'AuthClaim',
Registration = 'registration',
Voting = 'voting',
}

export enum VotingTypes {
Expand Down
116 changes: 46 additions & 70 deletions src/api/modules/verify/helpers/zkp.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,9 @@
import { config } from '@config'
import { Poseidon } from '@iden3/js-crypto'
import { BytesLike, utils } from 'ethers'
import { groth16 } from 'snarkjs'
import { v4 as uuidv4 } from 'uuid'

import {
AppRequestOpts,
CLAIM_TYPES_MAP_ON_CHAIN,
ProofRequestResponse,
} from '@/api/modules/verify'
import { config } from '@/config'

import { ClaimTypes, ClaimTypesMapOnChain, ProofRequestResponse } from '@/api/modules/verify'
import { VerifierHelper } from '@/types/contracts/Voting'

export type SecretPair = {
Expand Down Expand Up @@ -112,30 +107,7 @@ function swap(arr: unknown[], i: number, j: number) {
arr[j] = temp
}

export const createRequestOnChain = (
reason: string,
message: string,
sender: string,
callbackUrl: string,
) => {
const uuid = uuidv4()

return {
id: uuid,
thid: uuid,
from: sender,
typ: 'application/iden3comm-plain-json',
type: 'https://iden3-communication.io/authorization/1.0/request',
body: {
reason: reason,
message: message,
callbackUrl: callbackUrl,
scope: [],
},
}
}

export const createRequest = async (opts: AppRequestOpts) => {
export const buildAppRequest = async <T extends ClaimTypes>(opts: ClaimTypesMapOnChain[T]) => {
// const { data } = await api.get<{
// verification_id: string
// jwt: string
Expand All @@ -146,31 +118,20 @@ export const createRequest = async (opts: AppRequestOpts) => {
jwt: 'qwerty',
}

const request = createRequestOnChain(
opts.reason,
opts.message,
opts.sender,
`${config.API_URL}/integrations/verify-proxy/v1/public/verify/callback/${data.verification_id}`,
)

return {
verificationId: data.verification_id,
request: {
...request,
id: data.verification_id,
thid: data.verification_id,
body: {
...request.body,
scope: [CLAIM_TYPES_MAP_ON_CHAIN[opts.claimType]],
},
...opts,
callbackUrl: `${config.API_URL}/integrations/verify-proxy/v1/public/verify/callback/${data.verification_id}`,
},
jwtToken: data.jwt,
}
}

export const getRequestResponse = (
export const subscribeToAppRequestResponse = <T extends ClaimTypes>(
verificationId: string,
jwtToken: string,
callback: (res: ProofRequestResponse) => void,
callback: (res: ProofRequestResponse[T]) => void,
): (() => void) => {
// Create WebSocket connection.
// const socket = new WebSocket(
Expand Down Expand Up @@ -199,31 +160,46 @@ export const getRequestResponse = (
//
// return socket.close

// TODO: remove mocked data
setTimeout(() => {
callback({
// eslint-disable-next-line max-len
jwz: `eyJhbGciOiJncm90aDE2IiwiY2lyY3VpdElkIjoiYXV0aFYyIiwiY3JpdCI6WyJjaXJjdWl0SWQiXSwidHlwIjoiSldaIn0.bXltZXNzYWdl.eyJwcm9vZiI6eyJwaV9hIjpbIjU4OTA4MTc2NDc0NDY4MzQ2MDU3NjU3NzA0NTExMDIyMDg4NjMyMDkxNDgwMTE5NDgzNjA0MDQ3NDU0ODA2NzE1NDM2MjU5MTkwNDIiLCI2OTY1MzI0OTI3MDYzMDQxOTU2NTIwODg5ODU1MDcxNjU1OTg5Mzg4NzQyODM1ODgzOTI1NjU4MDI1NDE0MjM4OTQ2OTkxNjE1ODMwIiwiMSJdLCJwaV9iIjpbWyIxNjgwMjkyNTc5OTM3NjI4MDExOTc1MTk2MTk1MDEzNjQ5NjkyMjMyOTU1NDI5Mjc0Nzc5OTE1NDI2MDQwMzMwNTM0Njc1NDU1Mzk5NCIsIjIwNzkzNDcyNDAwMzczNDkzMjIyNzAyNDY4NDcxMjQzNzcwMzk3NzY1MzY0OTc3NDA0NDQwNTQ2Mzc0MTkxNjU2OTM0NDE3Mjg1MDQxIl0sWyI2MTI1MjcxNjYyOTI4NDUzMjQ5NDgyMjc5MjQ2ODA2NTIxNTE2MzU5NDQwMTcxMDM1MzgxMzU4OTI3MjI4Njc2NTQxNTc0NTg5MDkxIiwiNTY4MDc3OTcxNTc0MjMyMjI0ODQyOTM0NDc1ODA5NDk0MzMyMzE1OTIzOTQzNjkyNzI3MjM3NDEwOTkxMzYzOTAyMjM2NDMyMjYwNiJdLFsiMSIsIjAiXV0sInBpX2MiOlsiOTQ4MTkzNTE5MTMwNTA0OTM5MTA3MjkxMDkxNzE2ODQzNzA0OTI4MjQyMzc3NDQ5MDM4NzMwNDU3NzM3MTI4Mjc0Mjc1NTc3ODYwOCIsIjEyMDMxNDE1NjE1ODExNTEzNzc2OTcwMDYwOTgzMDk2NTMxNzcwMTcwNDAxMjkzODEwMTQwMDY2ODM1NzkyMjk4NTcwNDQxMzcyMTg3IiwiMSJdLCJwcm90b2NvbCI6Imdyb3RoMTYiLCJjdXJ2ZSI6ImJuMTI4In0sInB1Yl9zaWduYWxzIjpbIjIzMTQ4OTM2NDY2MzM0MzUwNzQ0NTQ4NzkwMDEyMjk0NDg5MzY1MjA3NDQwNzU0NTA5OTg4OTg2Njg0Nzk3NzA4MzcwMDUxMDczIiwiNjExMDUxNzc2ODI0OTU1OTIzODE5MzQ3NzQzNTQ1NDc5MjAyNDczMjE3Mzg2NTQ4ODkwMDI3MDg0OTYyNDMyODY1MDc2NTY5MTQ5NCIsIjEyNDM5MDQ3MTE0Mjk5NjE4NTg3NzQyMjA2NDc2MTA3MjQyNzM3OTg5MTg0NTc5OTE0ODYwMzE1NjcyNDQxMDA3NjcyNTkyMzk3NDciXX0`,
statesMerkleData: {
issuerId: 'mockedIssuerId',
state: {
hash: '',
createdAtTimestamp: '1234123',
index: '',
lastUpdateOperationIndex: '',
},
merkleProof: [],
// TODO: get data from callback url and parse jwz token
// if (!proofResponse?.jwz) {
// throw new Error('Invalid proof data')
// }
//
// const jwzToken = await Token.parse(proofResponse?.jwz)
//
// const zkProofPayload = JSON.parse(jwzToken.getPayload())
//
// const zkpProof = zkProofPayload.body.scope[0] as ZKProof

const mockedData: ProofRequestResponse[ClaimTypes.Registration] = {
type: ClaimTypes.Registration,
data: {
proveIdentityParams: {
issuingAuthority: '123',
documentNullifier: '123',
commitment: '0x123',
},
updateStateDetails: {
stateRootHash: '',
gistRootDataStruct: {
root: '',
createdAtTimestamp: '',
registerProofParams: {
a: ['123', '123'],
b: [
['123', '123'],
['123', '123'],
],
c: ['123', '123'],
inputs: ['123', '123', '123'],
statesMerkleData: {
merkleProof: ['0x123'],
createdAtTimestamp: '123',
issuerState: '123',
issuerId: '123',
},
proof: '',
},
documentNullifier: '',
})
},
}

// TODO: remove mocked data
setTimeout(() => {
callback(mockedData as ProofRequestResponse[T])
}, 5_000)

return () => {}
Expand Down
1 change: 0 additions & 1 deletion src/api/modules/verify/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
export * from './consts'
export * from './enums'
export * from './helpers'
export * from './types'
78 changes: 56 additions & 22 deletions src/api/modules/verify/types/zkp.ts
Original file line number Diff line number Diff line change
@@ -1,31 +1,65 @@
import { ClaimTypes } from '@/api/modules/verify'
import { ClaimTypes } from '@/api/modules/verify/enums'

export type ProofRequestResponse = {
updateStateDetails: {
stateRootHash: string
gistRootDataStruct: {
root: string | number
createdAtTimestamp: string | number
[ClaimTypes.Registration]: {
type: ClaimTypes.Registration
data: {
proveIdentityParams: {
issuingAuthority: string
documentNullifier: string
commitment: string
}
registerProofParams: {
a: string[]
b: [[string, string], [string, string]]
c: [string, string]
inputs: string[]
statesMerkleData: {
merkleProof: string[]
createdAtTimestamp: string
issuerState: string
issuerId: string
}
}
}
proof: string
}
jwz: string
documentNullifier: string
statesMerkleData?: {
issuerId: string
state: {
index: string
hash: string
createdAtTimestamp: string
lastUpdateOperationIndex: string
[ClaimTypes.Voting]: {
type: ClaimTypes.Voting
data: {
proveIdentityParams: {
issuingAuthority: string
documentNullifier: string
commitment: string
}
registerProofParams: {
a: string[]
b: [[string, string], [string, string]]
c: [string, string]
inputs: string[]
statesMerkleData: {
merkleProof: string
createdAtTimestamp: string
issuerState: string
issuerId: string
}
}
}
merkleProof: string[]
}
}

export type AppRequestOpts = {
claimType: ClaimTypes
reason: string
message: string
sender: string
export type ClaimTypesMapOnChain = {
[ClaimTypes.Registration]: {
type: ClaimTypes.Registration
data: {
metadata_url: string
// callback: string
}
}
[ClaimTypes.Voting]: {
type: ClaimTypes.Voting
data: {
metadata_url: string
// callback: string
}
}
}
39 changes: 13 additions & 26 deletions src/pages/Votings/hooks/app-request.ts
Original file line number Diff line number Diff line change
@@ -1,46 +1,33 @@
import { useCallback, useState } from 'react'

import {
AppRequestOpts,
createRequest,
generateSecrets,
getCommitment,
getRequestResponse,
buildAppRequest,
ClaimTypes,
ClaimTypesMapOnChain,
ProofRequestResponse,
SecretPair,
subscribeToAppRequestResponse,
} from '@/api/modules/verify'

export const useAppRequest = (
reqOpts: AppRequestOpts,
export const useAppRequest = <T extends ClaimTypes>(
reqOpts: ClaimTypesMapOnChain[T],
): {
request: string
cancelSubscription: () => void

start: (
onSuccess: (proofResponse: ProofRequestResponse, secrets: SecretPair) => Promise<void>,
) => Promise<void>
start: (onSuccess: (response: ProofRequestResponse[T]) => Promise<void>) => Promise<void>
} => {
const [request, setRequest] = useState('')
const [cancelSubscription, setCancelSubscription] = useState<() => void>(() => {})

const start = useCallback(
async (
onSuccess: (proofResponse: ProofRequestResponse, secrets: SecretPair) => Promise<void>,
) => {
// TODO: get secrets from app?
const secrets = generateSecrets()
async (onSuccess: (response: ProofRequestResponse[T]) => Promise<void>) => {
const { request, verificationId, jwtToken } = await buildAppRequest(reqOpts)

// TODO: ?
const commitment = getCommitment(secrets)

// TODO: upd params
const { request, jwtToken } = await createRequest(reqOpts)

const cancelSubscription = getRequestResponse(
request.id,
const cancelSubscription = subscribeToAppRequestResponse(
verificationId,
jwtToken,
(res: ProofRequestResponse) => {
onSuccess(res, secrets)
response => {
onSuccess(response as ProofRequestResponse[T])
},
)

Expand Down
6 changes: 2 additions & 4 deletions src/pages/Votings/hooks/app-voting-details.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,13 +99,11 @@ export const useAppVotingDetails = (pairIdOrInstance: string | AppVoting) => {
])

const getIsUserRegistered = useCallback(
async (proofResponse: ProofRequestResponse) => {
async (documentNullifier: string) => {
if (!provider?.rawProvider) throw new TypeError('Provider is not connected')

if (!appVoting) throw new TypeError('Voting is not found')

if (!proofResponse?.documentNullifier) return false

const registrationInstance = VotingRegistration__factory.connect(
appVoting.registration.contract_address,
provider.rawProvider as unknown as providers.JsonRpcProvider,
Expand All @@ -118,7 +116,7 @@ export const useAppVotingDetails = (pairIdOrInstance: string | AppVoting) => {
provider.rawProvider as unknown as providers.JsonRpcProvider,
)

return registerVerifierInstance.isIdentityRegistered(proofResponse.documentNullifier)
return registerVerifierInstance.isIdentityRegistered(documentNullifier)
},
[appVoting, provider?.rawProvider],
)
Expand Down
Loading

0 comments on commit ed925cd

Please sign in to comment.