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

Add credential metadata fallback #365

Merged
merged 2 commits into from
Aug 29, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions packages/browser-wallet/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog

## Unreleased

### Fixed

- An issue where changing the credential metadata URL to an invalid URL, or a URL that does not contain a credential metadata file, would result in an empty screen.

## 1.1.3

### Changed
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,7 @@ export default function AddWeb3IdCredential({ onAllow, onReject }: Props) {
credentialSubject,
id: credentialId,
index,
metadataUrl: metadataUrl.url,
};
await setWeb3IdCredentials([...web3IdCredentials.value, fullCredential]);
if (metadata) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import {
import { AsyncWrapper } from '@popup/store/utils';
import { ConcordiumGRPCClient } from '@concordium/web-sdk';
import { useTranslation } from 'react-i18next';
import { logError } from '@shared/utils/log-helpers';
import { logError, logWarningMessage } from '@shared/utils/log-helpers';

/**
* Retrieve the on-chain credential status for a verifiable credential in a CIS-4 credential registry contract.
Expand Down Expand Up @@ -110,24 +110,36 @@ export function useCredentialMetadata(credential?: VerifiableCredential) {
if (storedMetadata.loading) {
return;
}
let url;

let url: string | undefined;
if (credentialEntry) {
url = credentialEntry.credentialInfo.metadataUrl.url;
} else if (!tempMetadata.loading && credential) {
url = tempMetadata.value[credential.id];
}
if (!url) {
if (url === undefined || credential === undefined) {
return;
}

const storedCredentialMetadata = storedMetadata.value[url];
if (!storedCredentialMetadata) {
if (storedCredentialMetadata) {
setMetadata(storedCredentialMetadata);
return;
}

// The URL we got does not have a corresponding entry in our local storage.
// In this case we fallback to using the known "good" value so that we can
// still get metadata for this credential.
const fallbackCredentialMetadata = storedMetadata.value[credential.metadataUrl];
if (!fallbackCredentialMetadata) {
throw new Error(
`Attempted to find credential metadata for credentialId: ${
credentialEntry?.credentialInfo.credentialHolderId || credential?.id
} but none was found!`
`Attempted to find credential metadata for credentialId: ${credential.id} at URL ${credential.metadataUrl} but none was found!`
);
}
setMetadata(storedCredentialMetadata);
logWarningMessage(
`Using fallback credential metadata for credential ${credential.id}. The credential entry metadata URL is [${credentialEntry?.credentialInfo.metadataUrl.url}]`
);
setMetadata(fallbackCredentialMetadata);
}, [storedMetadata.loading, tempMetadata.loading, credentialEntry, credential?.id]);

return metadata;
Expand Down
5 changes: 4 additions & 1 deletion packages/browser-wallet/src/shared/storage/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -287,8 +287,11 @@ export interface VerifiableCredential extends APIVerifiableCredential {
// Secrets
signature: string;
randomness: Record<string, string>;
/** index used to derive keys for credential */
// Index used to derive keys for credential
index: number;
// The original metadataUrl received when first adding the credential
// TODO: The URL should be updated when there are valid updates to the metadata.
metadataUrl: string;
}

interface CredentialSchemaProperty {
Expand Down
4 changes: 4 additions & 0 deletions packages/browser-wallet/src/shared/utils/log-helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,3 +66,7 @@ export async function logError(error: unknown) {
logErrorMessage(String(error));
}
}

export async function logWarningMessage(message: string) {
log(message, LoggingLevel.WARN);
}
Loading