Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bugfix: crash importing local VC #1890

Merged
merged 3 commits into from
Jul 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
44 changes: 22 additions & 22 deletions packages/gui/src/components/vcs/VCCard.tsx
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -35,16 +35,16 @@ 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,
});
const navigate = useNavigate();
const [revokeVC] = useRevokeVCMutation();
const theme: any = useTheme();
const openDialog = useOpenDialog();
const [vcTitlesObject] = usePrefs<any>('verifiable-credentials-titles', {});
const [vcTitlesObject, setVcTitlesObject] = usePrefs<any>('verifiable-credentials-titles', {});
const vcTitle = React.useMemo(
() => vcTitlesObject[vcRecord?.vc?.launcherId] || vcTitlesObject[vcRecord?.sha256] || t`Verifiable Credential`,
[vcRecord?.vc?.launcherId, vcRecord?.sha256, vcTitlesObject]
Expand All @@ -55,7 +55,6 @@ export default function VCCard(props: { vcRecord: any; isDetail?: boolean; proof
const [pendingRevoke, setPendingRevoke] = useLocalStorage<any>('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) => {
Expand Down Expand Up @@ -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 || '/';
paninaro marked this conversation as resolved.
Show resolved Hide resolved
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')
Expand Down Expand Up @@ -146,7 +150,7 @@ export default function VCCard(props: { vcRecord: any; isDetail?: boolean; proof
<Trans>Invalid</Trans>
)}
</RenderProperty>
{isDetail && isVCLocal() && vcRecord.format && (
{isDetail && isLocal && vcRecord.format && (
<RenderProperty label={<Trans>Standard Version Number</Trans>}>{vcRecord.format}</RenderProperty>
)}
{isDetail && proofs && Object.keys(proofs).length > 0 && (
Expand Down Expand Up @@ -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(
<VCRevokeDialog
vcTitle={vcTitle}
isLocal={!vcRecord.vc}
isLocal={isLocal}
title={
type === 'remove' ? <Trans>Remove Verifiable Credential</Trans> : <Trans>Revoke Verifiable Credential</Trans>
}
Expand Down Expand Up @@ -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 */
Expand Down Expand Up @@ -266,7 +270,7 @@ export default function VCCard(props: { vcRecord: any; isDetail?: boolean; proof
return (
<Flex sx={{ marginBottom: '10px', padding: '8px' }}>
<More>
{isVCLocal() && (
{isLocal && (
<MenuItem onClick={() => openRevokeVCDialog('remove')} close>
<ListItemIcon>
<DeleteIcon />
Expand All @@ -276,17 +280,13 @@ export default function VCCard(props: { vcRecord: any; isDetail?: boolean; proof
</Typography>
</MenuItem>
)}
{!isVCLocal() && (
{!isLocal && (
<MenuItem onClick={() => openRevokeVCDialog('revoke')} close>
<ListItemIcon>
<BurnIcon />
</ListItemIcon>
<Typography variant="inherit" noWrap>
{isVCLocal() ? (
<Trans>Delete Verifiable Credential</Trans>
) : (
<Trans>Revoke Verifiable Credential</Trans>
)}
{isLocal ? <Trans>Delete Verifiable Credential</Trans> : <Trans>Revoke Verifiable Credential</Trans>}
</Typography>
</MenuItem>
)}
Expand Down
2 changes: 1 addition & 1 deletion packages/gui/src/components/vcs/VCDetail.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export default function VCDetail() {
proofs = localData.proof?.values;
}
if (isLoading || (!data && !localData)) return null;
return <VCCard isDetail vcRecord={data || localData} proofs={proofs} />;
return <VCCard isDetail vcRecord={data || localData} proofs={proofs} isLocal={!!localData} />;
}
return (
<Box sx={{ padding: '25px' }}>
Expand Down
4 changes: 2 additions & 2 deletions packages/gui/src/components/vcs/VCList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 <VCCard vcRecord={vcRecord} proofs={vcProofs} />;
return <VCCard vcRecord={vcRecord} proofs={vcProofs} isLocal={!!vcRecord.isLocal} />;
}

const allVCs = React.useMemo(() => {
Expand All @@ -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 [];
Expand Down