From 80dc27d5348a89e17018cd095868bcf67c94abb1 Mon Sep 17 00:00:00 2001 From: Matej Rifelj Date: Thu, 20 Jul 2023 02:01:54 +0200 Subject: [PATCH] bugfix: crash importing local VC (#1890) --- packages/gui/src/components/vcs/VCCard.tsx | 44 ++++++++++---------- packages/gui/src/components/vcs/VCDetail.tsx | 2 +- packages/gui/src/components/vcs/VCList.tsx | 4 +- 3 files changed, 25 insertions(+), 25 deletions(-) diff --git a/packages/gui/src/components/vcs/VCCard.tsx b/packages/gui/src/components/vcs/VCCard.tsx index 554db30f41..7c1209e297 100644 --- a/packages/gui/src/components/vcs/VCCard.tsx +++ b/packages/gui/src/components/vcs/VCCard.tsx @@ -1,10 +1,10 @@ import { useGetTimestampForHeightQuery, useRevokeVCMutation, - usePrefs, useLocalStorage, useGetLoggedInFingerprintQuery, useGetTransactionAsyncMutation, + usePrefs, } from '@chia-network/api-react'; import { Truncate, Button, useOpenDialog, AlertDialog, Flex, More, MenuItem } from '@chia-network/core'; import { Burn as BurnIcon } from '@chia-network/icons'; @@ -35,8 +35,8 @@ function RenderProperty(props: RenderPropertyProps) { ); } -export default function VCCard(props: { vcRecord: any; isDetail?: boolean; proofs?: any }) { - const { vcRecord, isDetail, proofs } = props; +export default function VCCard(props: { vcRecord: any; isDetail?: boolean; proofs?: any; isLocal: boolean }) { + const { vcRecord, isDetail, proofs, isLocal } = props; const { data: mintedTimestamp, isLoading: isLoadingMintHeight } = useGetTimestampForHeightQuery({ height: vcRecord?.confirmedAtHeight || 0, }); @@ -44,7 +44,7 @@ export default function VCCard(props: { vcRecord: any; isDetail?: boolean; proof const [revokeVC] = useRevokeVCMutation(); const theme: any = useTheme(); const openDialog = useOpenDialog(); - const [vcTitlesObject] = usePrefs('verifiable-credentials-titles', {}); + const [vcTitlesObject, setVcTitlesObject] = usePrefs('verifiable-credentials-titles', {}); const vcTitle = React.useMemo( () => vcTitlesObject[vcRecord?.vc?.launcherId] || vcTitlesObject[vcRecord?.sha256] || t`Verifiable Credential`, [vcRecord?.vc?.launcherId, vcRecord?.sha256, vcTitlesObject] @@ -55,7 +55,6 @@ export default function VCCard(props: { vcRecord: any; isDetail?: boolean; proof const [pendingRevoke, setPendingRevoke] = useLocalStorage('verifiable-credentials-pending-revoke', {}); const { data: fingerprint } = useGetLoggedInFingerprintQuery(); const [getTransactionAsync] = useGetTransactionAsyncMutation(); - React.useEffect(() => { if (vcRecord?.vc?.launcherId && pendingRevoke[vcRecord.vc.launcherId]) { getTransactionAsync({ transactionId: pendingRevoke[vcRecord.vc.launcherId] }).then((res) => { @@ -84,10 +83,15 @@ export default function VCCard(props: { vcRecord: any; isDetail?: boolean; proof } function renderProperties() { - const didString = vcRecord?.vc?.proofProvider - ? didToDIDId(vcRecord?.vc?.proofProvider) - : vcRecord.credentialSubject?.id || '/'; - + let didString: string = vcRecord.credentialSubject?.id || '/'; + try { + const proofProvider = vcRecord?.vc?.proofProvider; + if (proofProvider) { + didString = didToDIDId(proofProvider); + } + } catch (e) { + /* ignore */ + } const issuanceDate = vcRecord.confirmedAtHeight && !isLoadingMintHeight && mintedTimestamp ? moment(mintedTimestamp.timestamp * 1000).format('LLL') @@ -146,7 +150,7 @@ export default function VCCard(props: { vcRecord: any; isDetail?: boolean; proof Invalid )} - {isDetail && isVCLocal() && vcRecord.format && ( + {isDetail && isLocal && vcRecord.format && ( Standard Version Number}>{vcRecord.format} )} {isDetail && proofs && Object.keys(proofs).length > 0 && ( @@ -174,15 +178,11 @@ export default function VCCard(props: { vcRecord: any; isDetail?: boolean; proof ); } - function isVCLocal() { - return !vcRecord.vc; - } - async function openRevokeVCDialog(type: string) { const confirmedWithFee = await openDialog( Remove Verifiable Credential : Revoke Verifiable Credential } @@ -211,6 +211,10 @@ export default function VCCard(props: { vcRecord: any; isDetail?: boolean; proof delete vcsLocalStorage[fingerprint]; } setVCsLocalStorage(vcsLocalStorage); + /* remove title from prefs */ + const vcTitlesObjectCopy = { ...vcTitlesObject }; + delete vcTitlesObjectCopy[vcRecord.sha256]; + setVcTitlesObject(vcTitlesObjectCopy); } if (type === 'revoke') { /* add revoked flaglocal storage */ @@ -266,7 +270,7 @@ export default function VCCard(props: { vcRecord: any; isDetail?: boolean; proof return ( - {isVCLocal() && ( + {isLocal && ( openRevokeVCDialog('remove')} close> @@ -276,17 +280,13 @@ export default function VCCard(props: { vcRecord: any; isDetail?: boolean; proof )} - {!isVCLocal() && ( + {!isLocal && ( openRevokeVCDialog('revoke')} close> - {isVCLocal() ? ( - Delete Verifiable Credential - ) : ( - Revoke Verifiable Credential - )} + {isLocal ? Delete Verifiable Credential : Revoke Verifiable Credential} )} diff --git a/packages/gui/src/components/vcs/VCDetail.tsx b/packages/gui/src/components/vcs/VCDetail.tsx index 79e0c9cf5c..f423648e94 100644 --- a/packages/gui/src/components/vcs/VCDetail.tsx +++ b/packages/gui/src/components/vcs/VCDetail.tsx @@ -41,7 +41,7 @@ export default function VCDetail() { proofs = localData.proof?.values; } if (isLoading || (!data && !localData)) return null; - return ; + return ; } return ( diff --git a/packages/gui/src/components/vcs/VCList.tsx b/packages/gui/src/components/vcs/VCList.tsx index e06cc4ada8..dba2cbc35a 100644 --- a/packages/gui/src/components/vcs/VCList.tsx +++ b/packages/gui/src/components/vcs/VCList.tsx @@ -132,7 +132,7 @@ export default function VCList() { function renderVCCard(index: number, vcRecord: any) { const proofHash = vcRecord?.vc?.proofHash; const vcProofs = proofHash ? proofs[proofHash] : undefined; - return ; + return ; } const allVCs = React.useMemo(() => { @@ -143,7 +143,7 @@ export default function VCList() { ...record, isValid: !!(proofs[record.vc.proofHash] && Object.keys(proofs[record.vc.proofHash]).length > 0), })) - .concat(VCsLocalStorage[fingerprint]) + .concat((VCsLocalStorage[fingerprint] || []).map((record: any) => ({ ...record, isLocal: true }))) .filter(Boolean); } return [];