From 6fcd666754534f1d8f923188ba382dfaa3c9a694 Mon Sep 17 00:00:00 2001 From: "Jason C. Leach" Date: Tue, 12 Nov 2024 14:18:38 -0800 Subject: [PATCH] fix: check proof state before fetch (#1313) Signed-off-by: Jason C. Leach --- packages/legacy/core/App/utils/helpers.ts | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/packages/legacy/core/App/utils/helpers.ts b/packages/legacy/core/App/utils/helpers.ts index 246feb36ba..237bd8726e 100644 --- a/packages/legacy/core/App/utils/helpers.ts +++ b/packages/legacy/core/App/utils/helpers.ts @@ -693,12 +693,20 @@ export const retrieveCredentialsForProof = async ( t: TFunction<'translation', undefined>, groupByReferent?: boolean ) => { + // This is the only valid state to retrieve credentials for a proof + // `getCredentialsForRequest` will fail otherwise. + if (proof.state !== ProofState.RequestReceived) { + return + } + try { const format = await agent.proofs.getFormatData(proof.id) const hasPresentationExchange = format.request?.presentationExchange !== undefined const hasAnonCreds = format.request?.anoncreds !== undefined const hasIndy = format.request?.indy !== undefined - const credentialsPromise = agent.proofs.getCredentialsForRequest({ + + // Will fail if credential not in state 'request-received' + const credentialsAsPromise = agent.proofs.getCredentialsForRequest({ proofRecordId: proof.id, proofFormats: { // FIXME: Credo will try to use the format, even if the value is undefined (but the key is present) @@ -728,7 +736,9 @@ export const retrieveCredentialsForProof = async ( ...(hasPresentationExchange ? { presentationExchange: {} } : {}), }, }) - const credentialsWithRevokedPromise = agent.proofs.getCredentialsForRequest({ + + // Will fail if credential not in state 'request-received' + const credentialsWithRevokedAsPromise = agent.proofs.getCredentialsForRequest({ proofRecordId: proof.id, proofFormats: { // FIXME: Credo will try to use the format, even if the value is undefined (but the key is present) @@ -758,9 +768,14 @@ export const retrieveCredentialsForProof = async ( ...(hasPresentationExchange ? { presentationExchange: {} } : {}), }, }) - const [credentials, credentialsWithRevoked] = await Promise.all([credentialsPromise, credentialsWithRevokedPromise]) - // in the case where there are only revoked credentials to satisfy a proof, include them for errors on the proof screen, otherwise leave them out + const [credentials, credentialsWithRevoked] = await Promise.all([ + credentialsAsPromise, + credentialsWithRevokedAsPromise, + ]) + + // In the case where there are only revoked credentials to satisfy a proof, + // include them for errors on the proof screen, otherwise leave them out const addRevokedCredsIfNeeded = (proofFormat: 'indy' | 'anoncreds', proofItem: 'attributes' | 'predicates') => { if (credentials.proofFormats[proofFormat] && credentialsWithRevoked.proofFormats[proofFormat]) { Object.keys(credentials.proofFormats[proofFormat]?.[proofItem] ?? {}).forEach((key) => {