From e15c2104f72c02f571ae755dc3f205394e6b1d5b Mon Sep 17 00:00:00 2001 From: orhoj Date: Tue, 29 Aug 2023 13:16:18 +0200 Subject: [PATCH 1/2] Add credential metadata fallback --- packages/browser-wallet/CHANGELOG.md | 6 ++++ .../AddWeb3IdCredential.tsx | 1 + .../VerifiableCredentialHooks.tsx | 28 +++++++++++++------ .../src/shared/storage/types.ts | 5 +++- .../src/shared/utils/log-helpers.ts | 4 +++ 5 files changed, 35 insertions(+), 9 deletions(-) diff --git a/packages/browser-wallet/CHANGELOG.md b/packages/browser-wallet/CHANGELOG.md index 26d19c00..7219ed77 100644 --- a/packages/browser-wallet/CHANGELOG.md +++ b/packages/browser-wallet/CHANGELOG.md @@ -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 diff --git a/packages/browser-wallet/src/popup/pages/AddWeb3IdCredential/AddWeb3IdCredential.tsx b/packages/browser-wallet/src/popup/pages/AddWeb3IdCredential/AddWeb3IdCredential.tsx index 818c85ae..1db85b8c 100644 --- a/packages/browser-wallet/src/popup/pages/AddWeb3IdCredential/AddWeb3IdCredential.tsx +++ b/packages/browser-wallet/src/popup/pages/AddWeb3IdCredential/AddWeb3IdCredential.tsx @@ -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) { diff --git a/packages/browser-wallet/src/popup/pages/VerifiableCredential/VerifiableCredentialHooks.tsx b/packages/browser-wallet/src/popup/pages/VerifiableCredential/VerifiableCredentialHooks.tsx index 882ea356..c104d44a 100644 --- a/packages/browser-wallet/src/popup/pages/VerifiableCredential/VerifiableCredentialHooks.tsx +++ b/packages/browser-wallet/src/popup/pages/VerifiableCredential/VerifiableCredentialHooks.tsx @@ -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. @@ -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; diff --git a/packages/browser-wallet/src/shared/storage/types.ts b/packages/browser-wallet/src/shared/storage/types.ts index 1ef3c8c5..10241c4e 100644 --- a/packages/browser-wallet/src/shared/storage/types.ts +++ b/packages/browser-wallet/src/shared/storage/types.ts @@ -287,8 +287,11 @@ export interface VerifiableCredential extends APIVerifiableCredential { // Secrets signature: string; randomness: Record; - /** 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 { diff --git a/packages/browser-wallet/src/shared/utils/log-helpers.ts b/packages/browser-wallet/src/shared/utils/log-helpers.ts index 312889d0..1e3f0a23 100644 --- a/packages/browser-wallet/src/shared/utils/log-helpers.ts +++ b/packages/browser-wallet/src/shared/utils/log-helpers.ts @@ -66,3 +66,7 @@ export async function logError(error: unknown) { logErrorMessage(String(error)); } } + +export async function logWarningMessage(message: string) { + log(message, LoggingLevel.WARN); +} From f974f6dd384ed25c563c3f99c0f221790d69b54a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakob=20=C3=98rh=C3=B8j?= <57264157+orhoj@users.noreply.github.com> Date: Tue, 29 Aug 2023 16:26:56 +0200 Subject: [PATCH 2/2] Update packages/browser-wallet/src/popup/pages/VerifiableCredential/VerifiableCredentialHooks.tsx MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Søren Hjort <87635671+shjortConcordium@users.noreply.github.com> --- .../pages/VerifiableCredential/VerifiableCredentialHooks.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/browser-wallet/src/popup/pages/VerifiableCredential/VerifiableCredentialHooks.tsx b/packages/browser-wallet/src/popup/pages/VerifiableCredential/VerifiableCredentialHooks.tsx index c104d44a..20aad2a8 100644 --- a/packages/browser-wallet/src/popup/pages/VerifiableCredential/VerifiableCredentialHooks.tsx +++ b/packages/browser-wallet/src/popup/pages/VerifiableCredential/VerifiableCredentialHooks.tsx @@ -128,7 +128,7 @@ export function useCredentialMetadata(credential?: VerifiableCredential) { } // 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` + // 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) {