{"version":3,"file":"W3cV2CredentialRecord.mjs","names":[],"sources":["../../../../src/modules/vc/repository/W3cV2CredentialRecord.ts"],"sourcesContent":["import { BaseRecord, type Tags, type TagsBase } from '../../../storage/BaseRecord'\nimport type { NonEmptyArray } from '../../../types'\nimport { asArray, JsonTransformer } from '../../../utils'\nimport { CredentialMultiInstanceState } from '../../../utils/credentialUseTypes'\nimport type { Constructable } from '../../../utils/mixins'\nimport { uuid } from '../../../utils/uuid'\nimport { W3cV2JwtVerifiableCredential } from '../jwt-vc'\nimport { ClaimFormat, type W3cV2VerifiableCredential } from '../models'\nimport { W3cV2SdJwtVerifiableCredential } from '../sd-jwt-vc'\n\nexport interface W3cV2CredentialRecordOptions {\n  id?: string\n  createdAt?: Date\n\n  credentialInstances: W3cV2CredentialRecordInstances\n}\n\nexport type W3cV2CredentialRecordInstances = NonEmptyArray<{\n  /**\n   * NOTE: String for now, might change in the future if we add other types.\n   */\n  credential: string\n\n  // no kmsKeyId for w3c credential record, since all credentials are bound to a DID\n}>\n\nexport type DefaultW3cV2CredentialTags = {\n  issuerId: string\n  subjectIds: Array<string>\n  schemaIds: Array<string>\n  contexts: Array<string>\n  givenId?: string\n  claimFormat: W3cV2VerifiableCredential['claimFormat']\n\n  types: Array<string>\n  algs?: Array<string>\n\n  /**\n   * @since 0.6 - tag was not defined before 0.6\n   */\n  multiInstanceState?: CredentialMultiInstanceState\n}\n\nexport class W3cV2CredentialRecord extends BaseRecord<DefaultW3cV2CredentialTags> {\n  public static readonly type = 'W3cV2CredentialRecord'\n  public readonly type = W3cV2CredentialRecord.type\n\n  public credentialInstances!: W3cV2CredentialRecordInstances\n\n  /**\n   * Tracks the state of credential instances on this record.\n   *\n   * NOTE: This defaults to `CredentialMultiInstanceState.SingleInstanceUsed` for records that\n   * don't have a value set from before 0.6. We assume the credential has already been used.\n   */\n  public multiInstanceState = CredentialMultiInstanceState.SingleInstanceUsed\n\n  public constructor(props: W3cV2CredentialRecordOptions) {\n    super()\n    if (props) {\n      this.id = props.id ?? uuid()\n      this.createdAt = props.createdAt ?? new Date()\n      this.credentialInstances = props.credentialInstances\n\n      // Set multiInstanceState based on the number of initial instances. We\n      // assume the instance is unused when the record is created.\n      this.multiInstanceState =\n        this.credentialInstances.length === 1\n          ? CredentialMultiInstanceState.SingleInstanceUnused\n          : CredentialMultiInstanceState.MultiInstanceFirstUnused\n    }\n  }\n\n  /**\n   * Only here for class transformation. If credential is set we transform\n   * it to the new credentialInstances array format\n   */\n  private set credential(credential: W3cV2VerifiableCredential) {\n    this.credentialInstances = [\n      {\n        // NOTE: we have to type the `set` method the same as the `get`. Previously\n        // we had a transformer that would transform. Now the set is private and will\n        // only be called by class transformer, and is the raw type.\n        credential: credential as unknown as string,\n      },\n    ]\n  }\n\n  public get firstCredential(): W3cV2VerifiableCredential {\n    const credential = this.credentialInstances[0].credential\n    return credential.includes('~')\n      ? W3cV2SdJwtVerifiableCredential.fromCompact(credential)\n      : W3cV2JwtVerifiableCredential.fromCompact(credential)\n  }\n\n  public static fromCredential(credential: W3cV2VerifiableCredential) {\n    return new W3cV2CredentialRecord({\n      credentialInstances: [\n        {\n          credential: credential.encoded as string,\n        },\n      ],\n    })\n  }\n\n  public getTags(): Tags<DefaultW3cV2CredentialTags, TagsBase> {\n    const credential = this.firstCredential\n    const resolvedCredential = credential.resolvedCredential\n\n    // Contexts are usually strings, but can sometimes be objects. We're unable to use objects as tags,\n    // so we filter out the objects before setting the tags.\n    const stringContexts = resolvedCredential.contexts.filter((ctx): ctx is string => typeof ctx === 'string')\n\n    const tags: Tags<DefaultW3cV2CredentialTags, TagsBase> = {\n      ...this._tags,\n      issuerId: resolvedCredential.issuerId,\n      subjectIds: resolvedCredential.credentialSubjectIds,\n      schemaIds: resolvedCredential.credentialSchemaIds,\n      contexts: stringContexts,\n      givenId: resolvedCredential.id,\n      claimFormat: credential.claimFormat,\n      types: asArray(resolvedCredential.type),\n      multiInstanceState: this.multiInstanceState,\n    }\n\n    if (credential.claimFormat === ClaimFormat.JwtW3cVc) {\n      tags.algs = [credential.jwt.header.alg]\n    } else if (credential.claimFormat === ClaimFormat.SdJwtW3cVc) {\n      tags.algs = [credential.sdJwt.header.alg]\n    }\n\n    return tags\n  }\n\n  /**\n   * This overwrites the default `clone` method for records, as the W3cV3CredentialRecord\n   * has issues with the default clone method due to the way the JWT and SD-JWT\n   * records are implemented. This is a temporary way to make sure the clone still works, but ideally\n   * we find an alternative.\n   */\n  public clone(): this {\n    return JsonTransformer.fromJSON(JsonTransformer.toJSON(this), this.constructor as Constructable<this>)\n  }\n\n  /**\n   * encoded is convenience method added to all credential records\n   */\n  public get encoded() {\n    return this.credentialInstances[0].credential\n  }\n}\n"],"mappings":";;;;;;;;;;;;;;;;AA2CA,IAAa,wBAAb,MAAa,8BAA8B,WAAuC;CAchF,AAAO,YAAY,OAAqC;AACtD,SAAO;OAbO,OAAO,sBAAsB;OAUtC,qBAAqB,6BAA6B;AAIvD,MAAI,OAAO;AACT,QAAK,KAAK,MAAM,MAAM,MAAM;AAC5B,QAAK,YAAY,MAAM,6BAAa,IAAI,MAAM;AAC9C,QAAK,sBAAsB,MAAM;AAIjC,QAAK,qBACH,KAAK,oBAAoB,WAAW,IAChC,6BAA6B,uBAC7B,6BAA6B;;;;;;;CAQvC,IAAY,WAAW,YAAuC;AAC5D,OAAK,sBAAsB,CACzB,EAIc,YACb,CACF;;CAGH,IAAW,kBAA6C;EACtD,MAAM,aAAa,KAAK,oBAAoB,GAAG;AAC/C,SAAO,WAAW,SAAS,IAAI,GAC3B,+BAA+B,YAAY,WAAW,GACtD,6BAA6B,YAAY,WAAW;;CAG1D,OAAc,eAAe,YAAuC;AAClE,SAAO,IAAI,sBAAsB,EAC/B,qBAAqB,CACnB,EACE,YAAY,WAAW,SACxB,CACF,EACF,CAAC;;CAGJ,AAAO,UAAsD;EAC3D,MAAM,aAAa,KAAK;EACxB,MAAM,qBAAqB,WAAW;EAItC,MAAM,iBAAiB,mBAAmB,SAAS,QAAQ,QAAuB,O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