{"version":3,"file":"w3cCredentialRecord.mjs","names":[],"sources":["../../../../../src/storage/migration/updates/0.4-0.5/w3cCredentialRecord.ts"],"sourcesContent":["import type { BaseAgent } from '../../../../agent/BaseAgent'\nimport { W3cJsonLdVerifiableCredential } from '../../../../modules/vc'\nimport { W3cJsonLdCredentialService } from '../../../../modules/vc/data-integrity/W3cJsonLdCredentialService'\nimport type { W3cCredentialRecord } from '../../../../modules/vc/repository'\nimport { W3cCredentialRepository } from '../../../../modules/vc/repository'\n\n/**\n * Re-saves the w3c credential records to add the new 'types' tag.\n */\nexport async function migrateW3cCredentialRecordToV0_5<Agent extends BaseAgent>(agent: Agent) {\n  agent.config.logger.info('Migration w3c credential records records to storage version 0.5')\n\n  const w3cCredentialRepository = agent.dependencyManager.resolve(W3cCredentialRepository)\n\n  agent.config.logger.debug('Fetching all w3c credential records from storage')\n  const records = await w3cCredentialRepository.getAll(agent.context)\n\n  agent.config.logger.debug(`Found a total of ${records.length} w3c credential records to update.`)\n\n  for (const record of records) {\n    agent.config.logger.debug(\n      `Updating w3c credential record with id ${record.id} to add 'types' tag and fix 'expandedTypes' tag for storage version 0.5`\n    )\n\n    await fixIncorrectExpandedTypesWithAskarStorage(agent, record)\n\n    // Save updated record\n    await w3cCredentialRepository.update(agent.context, record)\n\n    agent.config.logger.debug(`Successfully migrated w3c credential record with id ${record.id} to storage version 0.5`)\n  }\n}\n\n/**\n * Up until 0.5.0 the AskarStorageService contained a bug where a non-computed (so manually set on record) array tag values that contained a : in the value\n * would be incorrectly parsed back from an askar tag to a tag on a record. This would only cause problems for the storage if the record was re-saved and not\n * computed. The following would happen:\n * - Create record with non-computed tag, e.g. expandedTypes that contains a value with a : in it\n * - Save record. The tag is correctly set in Askar as `expandedTypes:https://example.com'\n * - Read record. The tag is correctly read from Askar as `expandedTypes:https://example.com'. However the transformation would result in the tag value on the record being set to `https'.\n * - Save record. The non-computed (important, as otherwise the correct computed value would overwrite the incorrect value before storing) tag value is now set to `https' instead of `https://example.com'\n *\n * This function checks if any of the values for expandedTypes is `https` and if so, re-calculates the correct value and sets it on the record.\n *\n * NOTE: This function needs to resolve the context of a W3cCredentialRecord to be able to correctly calculate the expanded types.\n * To not brick a wallet that has no internet when updating, the storage update will allow the resolving of the expanded types to fail.\n * If this is the case, at a later point the expanded types will need to be recalculated and set on the record.\n *\n * If w3c credential records are never re-saved this shouldn't be a problem though. By default w3c credential records are not re-saved,\n * and so it only applies if you have implemented a custom flow that re-saves w3c credential records (e.g. if you add metadata).\n */\nexport async function fixIncorrectExpandedTypesWithAskarStorage<Agent extends BaseAgent>(\n  agent: Agent,\n  w3cCredentialRecord: W3cCredentialRecord\n) {\n  const firstCredential = w3cCredentialRecord.firstCredential\n  // We don't store the expanded types for JWT credentials (should we? As you can have jwt_vc with json-ld)\n  if (!(firstCredential instanceof W3cJsonLdVerifiableCredential)) return\n\n  const expandedTypes = (w3cCredentialRecord.getTag('expandedTypes') ?? []) as string[]\n\n  // Check if one of the values is `https`\n  const hasInvalidType = expandedTypes.some((type) => type === 'https')\n\n  if (!hasInvalidType) return\n\n  agent.context.config.logger.info(\n    `W3c credential record with id '${w3cCredentialRecord.id}' contains invalid expanded types. Recalculating...`\n  )\n  const w3cJsonLdCredentialService = agent.dependencyManager.resolve(W3cJsonLdCredentialService)\n\n  try {\n    // JsonLd credentials need expanded types to be stored.\n    const newExpandedTypes = await w3cJsonLdCredentialService.getExpandedTypesForCredential(\n      agent.context,\n      firstCredential\n    )\n\n    w3cCredentialRecord.setTag('expandedTypes', newExpandedTypes)\n    agent.context.config.logger.info(\n      `Successfully recalculated expanded types for w3c credential record with id ${w3cCredentialRecord.id} to ${newExpandedTypes} and set it on the record.`\n    )\n  } catch (_error) {\n    agent.context.config.logger.error(\n      `Retrieving expandedTypes fro w3c credential record with id ${w3cCredentialRecord.id} failed. To not brick the wallet, the storage update will not fail. Make sure to recalculate the expanded types at a later point. This is probably due to a missing internet connection. See https://credo.js.org/guides/updating/versions/0.4-to-0.5 for more information.`\n    )\n  }\n}\n"],"mappings":";;;;;;;;;;;;AASA,eAAsB,iCAA0D,OAAc;AAC5F,OAAM,OAAO,OAAO,KAAK,kEAAkE;CAE3F,MAAM,0BAA0B,MAAM,kBAAkB,QAAQ,wBAAwB;AAExF,OAAM,OAAO,OAAO,MAAM,mDAAmD;CAC7E,MAAM,UAAU,MAAM,wBAAwB,OAAO,MAAM,QAAQ;AAEnE,OAAM,OAAO,OAAO,MAAM,oBAAoB,QAAQ,OAAO,oCAAoC;AAEjG,MAAK,MAAM,UAAU,SAAS;AAC5B,QAAM,OAAO,OAAO,MAClB,0CAA0C,OAAO,GAAG,yEACrD;AAED,QAAM,0CAA0C,OAAO,OAAO;AAG9D,QAAM,wBAAwB,OAAO,MAAM,SAAS,OAAO;AAE3D,QAAM,OAAO,OAAO,MAAM,uDAAuD,OAAO,GAAG,yBAAyB;;;;;;;;;;;;;;;;;;;;;AAsBxH,eAAsB,0CACpB,OACA,qBACA;CACA,MAAM,kBAAkB,oBAAoB;AAE5C,KAAI,EAAE,2BAA2B,+BAAgC;AAOjE,KAAI,EALmB,oBAAoB,OAAO,gBAAgB,IAAI,EAAE,EAGnC,MAAM,SAAS,SAAS,QAAQ,CAEhD;AAErB,OAAM,QAAQ,OAAO,OAAO,KAC1B,kCAAkC,oBAAoB,GAAG,qDAC1D;CACD,MAAM,6BAA6B,MAAM,kBAAkB,QAAQ,2BAA2B;AAE9F,KAAI;EAEF,MAAM,mBAAmB,MAAM,2BAA2B,8BACxD,MAAM,SACN,gBACD;AAED,sBAAoB,OAAO,iBAAiB,iBAAiB;AAC7D,QAAM,QAAQ,OAAO,OAAO,KAC1B,8EAA8E,oBAAoB,GAAG,MAAM,iBAAiB,4BAC7H;UACM,QAAQ;AACf,QAAM,QAAQ,OAAO,OAAO,MAC1B,8DAA8D,oBAAoB,GAAG,6QACtF"}