-
Notifications
You must be signed in to change notification settings - Fork 87
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: verifyCloudProof function (#240)
* chore: tidy up * feat: verifyCloudProof * feat: nextjs example use verifyCloudProof non-functional until dev portal v2 api is live, but can test by modifying verifyCloudProof to use v1 api and rebuilding * add backend-only check * Update examples/with-next/pages/api/verify.ts Co-authored-by: Miguel Piedrafita <[email protected]> * fix: isBrowser * add endpoint param for verifyCloudProof * update lockfile * don't modify endpoint if passed --------- Co-authored-by: Miguel Piedrafita <[email protected]>
- Loading branch information
Showing
9 changed files
with
126 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { NextApiRequest, NextApiResponse } from 'next' | ||
import { verifyCloudProof, IVerifyResponse } from '@worldcoin/idkit' | ||
|
||
export default async function handler(req: NextApiRequest, res: NextApiResponse) { | ||
if (req.method != 'POST') return res.status(405).json({ message: 'Method Not Allowed' }) | ||
|
||
const { proof, app_id, action, signal } = req.body | ||
const response = (await verifyCloudProof(proof, app_id, action, signal)) as IVerifyResponse | ||
res.status(response.success ? 200 : 400).json(response) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import { hashToField } from './hashing' | ||
import { isBrowser } from 'browser-or-node' | ||
import type { ISuccessResult } from '../types' | ||
|
||
export interface IVerifyResponse { | ||
success: boolean | ||
code?: string | ||
detail?: string | ||
attribute?: string | null | ||
} | ||
|
||
export async function verifyCloudProof( | ||
proof: ISuccessResult, | ||
app_id: `app_${string}`, | ||
action: string, | ||
signal?: string, | ||
endpoint?: URL | string | ||
): Promise<IVerifyResponse> { | ||
if (isBrowser) { | ||
throw new Error('verifyCloudProof can only be used in the backend.') | ||
} | ||
|
||
const response = await fetch(endpoint ?? `https://developer.worldcoin.org/api/v2/verify/${app_id}`, { | ||
method: 'POST', | ||
headers: { | ||
'Content-Type': 'application/json', | ||
}, | ||
body: JSON.stringify({ | ||
...proof, | ||
action, | ||
signal_hash: hashToField(signal ?? '').digest, | ||
}), | ||
}) | ||
|
||
if (response.ok) { | ||
return { success: true } | ||
} else { | ||
return { success: false, ...(await response.json()) } as IVerifyResponse | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.