Skip to content

Commit

Permalink
make blockNumber and commitmentId optional params on ImageVerificatio…
Browse files Browse the repository at this point in the history
…nResponse
  • Loading branch information
forgetso committed Apr 26, 2024
1 parent 5a6bb9d commit ee301eb
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 17 deletions.
14 changes: 10 additions & 4 deletions demos/provider-mock/src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,13 @@
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
import { ApiParams, ApiPaths, ImageVerificationResponse, VerifySolutionBody } from '@prosopo/types'
import {
ApiParams,
ApiPaths,
ImageVerificationResponse,
VerificationResponse,
VerifySolutionBody,
} from '@prosopo/types'
import { ProsopoApiError } from '@prosopo/common'
import { VerifySolutionBodyType } from '@prosopo/types'
import express, { Router } from 'express'
Expand Down Expand Up @@ -63,11 +69,11 @@ export function prosopoRouter(): Router {
}
return res.json(response)
}

return res.json({
const verificationResponse: VerificationResponse = {
[ApiParams.status]: req.t(statusMessage),
[ApiParams.verified]: false,
})
}
return res.json(verificationResponse)
} catch (err) {
return next(new ProsopoApiError('API.UNKNOWN', { context: { error: err, errorCode: 500 } }))
}
Expand Down
2 changes: 1 addition & 1 deletion packages/procaptcha/src/modules/Manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ export function Manager(
undefined,
configOptional.challengeValidLength
)
if (verifyDappUserResponse.verified) {
if (verifyDappUserResponse.verified && verifyDappUserResponse.commitmentId) {
updateState({ isHuman: true, loading: false })
const output: ProcaptchaOutput = {
[ApiParams.providerUrl]: procaptchaStorage.providerUrl,
Expand Down
15 changes: 6 additions & 9 deletions packages/provider/src/api/captcha.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,27 +142,24 @@ export function prosopoRouter(env: ProviderEnvironment): Router {
return next(new ProsopoApiError('CAPTCHA.PARSE_ERROR', { context: { errorCode: 400, error: err } }))
}
try {
const failedVerificationResponse: VerificationResponse = {
[ApiParams.status]: req.t('API.USER_NOT_VERIFIED'),
[ApiParams.verified]: false,
}
const solution = await (parsed.commitmentId
? tasks.getDappUserCommitmentById(parsed.commitmentId)
: tasks.getDappUserCommitmentByAccount(parsed.user))

if (!solution) {
return res.json({
[ApiParams.status]: req.t('API.USER_NOT_VERIFIED'),
[ApiParams.verified]: false,
})
return res.json(failedVerificationResponse)
}

if (parsed.maxVerifiedTime) {
const currentBlockNumber = await getCurrentBlockNumber(tasks.contract.api)
const blockTimeMs = getBlockTimeMs(tasks.contract.api)
const timeSinceCompletion = (currentBlockNumber - solution.completedAt) * blockTimeMs
const verificationResponse: VerificationResponse = {
[ApiParams.status]: req.t('API.USER_NOT_VERIFIED'),
[ApiParams.verified]: false,
}
if (timeSinceCompletion > parsed.maxVerifiedTime) {
return res.json(verificationResponse)
return res.json(failedVerificationResponse)
}
}

Expand Down
6 changes: 5 additions & 1 deletion packages/server/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,7 @@ export class ProsopoServer {
commitmentId?: string,
maxVerifiedTime = DEFAULT_MAX_VERIFIED_TIME_CACHED
) {
let verifyRecency = false
this.logger.info('Verifying with provider.')
const providerApi = await this.getProviderApi(providerUrl)
if (challenge) {
Expand All @@ -207,7 +208,10 @@ export class ProsopoServer {
return result.verified
}
const result = await providerApi.verifyDappUser(dapp, user, blockNumber, commitmentId, maxVerifiedTime)
const verifyRecency = await this.verifyRecency(result.blockNumber, maxVerifiedTime)

if (result.blockNumber) {
verifyRecency = await this.verifyRecency(result.blockNumber, maxVerifiedTime)
}
return result.verified && verifyRecency
}

Expand Down
4 changes: 2 additions & 2 deletions packages/types/src/provider/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,9 +128,9 @@ export interface VerificationResponse {
}

export interface ImageVerificationResponse extends VerificationResponse {
[ApiParams.commitmentId]: Hash
[ApiParams.commitmentId]?: Hash
// The block at which the captcha was requested
[ApiParams.blockNumber]: number
[ApiParams.blockNumber]?: number
}

export interface GetPowCaptchaResponse {
Expand Down

0 comments on commit ee301eb

Please sign in to comment.