-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* add gen proof modal
- Loading branch information
Showing
4 changed files
with
291 additions
and
207 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
142 changes: 142 additions & 0 deletions
142
src/pages/Credentials/pages/CredentialsId/components/ProofGenerationModal.tsx
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,142 @@ | ||
import { Button, Dialog, DialogProps, Divider, Typography, useTheme } from '@mui/material' | ||
import { Stack } from '@mui/system' | ||
import { CircuitId, W3CCredential, ZKProof } from '@rarimo/rarime-connector' | ||
import { useCallback, useMemo, useState } from 'react' | ||
|
||
import { zkpSnap } from '@/api/clients' | ||
import { Icons } from '@/enums' | ||
import { ErrorHandler } from '@/helpers' | ||
import { useCopyToClipboard } from '@/hooks' | ||
import { UiDialogActions, UiDialogContent, UiDialogTitle, UiIcon } from '@/ui' | ||
|
||
interface Props extends DialogProps { | ||
vc: W3CCredential | ||
} | ||
|
||
export default function ProofGenerationModal({ vc, ...rest }: Props) { | ||
const { isCopied, copy } = useCopyToClipboard() | ||
|
||
const [isPending, setIsPending] = useState(false) | ||
const { palette, spacing } = useTheme() | ||
|
||
const [generatedProof, setGeneratedProof] = useState<ZKProof>() | ||
|
||
const createProofRequest = useMemo(() => { | ||
return { | ||
circuitId: CircuitId.AtomicQueryMTPV2OnChain, | ||
accountAddress: vc.credentialSubject.address as string, | ||
issuerDid: vc.issuer, | ||
|
||
// TODO: get proof query based on the VC | ||
query: { | ||
allowedIssuers: ['*'], | ||
credentialSubject: { | ||
isNatural: { | ||
$eq: 1, | ||
}, | ||
}, | ||
type: vc.type, | ||
}, | ||
} | ||
}, [vc.credentialSubject.address, vc.issuer, vc.type]) | ||
|
||
const generateProof = useCallback(async () => { | ||
setIsPending(true) | ||
|
||
try { | ||
const proof = await zkpSnap.createProof({ | ||
// FIXME: some DOM elements or large functions is in _prototype, | ||
// so metamask API can't serialize it | ||
...JSON.parse(JSON.stringify(createProofRequest)), | ||
}) | ||
|
||
setGeneratedProof(proof.zkpProof) | ||
} catch (error) { | ||
ErrorHandler.process(error) | ||
} | ||
|
||
setIsPending(false) | ||
}, [createProofRequest]) | ||
|
||
const generationDetails = useMemo(() => { | ||
return [ | ||
{ | ||
text: 'Query', | ||
value: ( | ||
<Typography | ||
component='pre' | ||
variant='body4' | ||
bgcolor={palette.action.active} | ||
p={2} | ||
borderRadius={1} | ||
sx={{ overflowX: 'auto' }} | ||
> | ||
{JSON.stringify(createProofRequest, null, 2)} | ||
</Typography> | ||
), | ||
}, | ||
...(generatedProof | ||
? [ | ||
{ | ||
text: 'Proof', | ||
value: ( | ||
<Stack spacing={2}> | ||
<Typography | ||
component='pre' | ||
variant='body4' | ||
bgcolor={palette.action.active} | ||
p={2} | ||
borderRadius={1} | ||
sx={{ overflowX: 'auto' }} | ||
> | ||
{JSON.stringify(generatedProof, null, 2)} | ||
</Typography> | ||
|
||
<Button | ||
fullWidth | ||
startIcon={<UiIcon name={isCopied ? Icons.Check : Icons.CopySimple} size={5} />} | ||
onClick={() => copy(JSON.stringify(vc, null, 2))} | ||
> | ||
{isCopied ? 'Copied' : 'Copy Proof'} | ||
</Button> | ||
</Stack> | ||
), | ||
}, | ||
] | ||
: []), | ||
] | ||
}, [palette.action.active, createProofRequest, generatedProof, isCopied, copy, vc]) | ||
|
||
return ( | ||
<Dialog {...rest} PaperProps={{ sx: { maxWidth: spacing(160) } }}> | ||
<UiDialogTitle onClose={rest.onClose}>Generate Proof</UiDialogTitle> | ||
<UiDialogContent> | ||
<Stack spacing={2}> | ||
{generationDetails.map(({ text, value }, index) => ( | ||
<Stack key={index} spacing={2}> | ||
<Stack spacing={1}> | ||
<Typography variant='body4' color={palette.text.secondary}> | ||
{text} | ||
</Typography> | ||
{value} | ||
</Stack> | ||
{index < generationDetails.length - 1 && <Divider />} | ||
</Stack> | ||
))} | ||
</Stack> | ||
</UiDialogContent> | ||
<UiDialogActions> | ||
<Button | ||
fullWidth | ||
startIcon={ | ||
<UiIcon name={isPending ? Icons.Lock : Icons.ArrowCounterClockwise} size={5} /> | ||
} | ||
onClick={generateProof} | ||
disabled={isPending} | ||
> | ||
{isPending ? 'Generating...' : 'Generate Proof'} | ||
</Button> | ||
</UiDialogActions> | ||
</Dialog> | ||
) | ||
} |
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
Oops, something went wrong.