import { RevocationStatusListFactory } from './revocationStatusListFactory.js'; import { RevocationStatusList } from './revocationStatusList.js'; import { StatusListDecoder } from './statusListDecoderFactory.js'; import { StatusListCredentialResponse } from '../resolvers/statusListCredentialFetcher.js'; import { decodeJwt } from 'jose'; export type StatusListCredentialSubject = { id?: string; type: string; encodedList: string; statusPurpose?: string; statusListIndex?: string; statusListCredential?: string; }; export class CredentialStatusListDecoder implements StatusListDecoder { getEncodedList( data: string | StatusListCredentialResponse, ): RevocationStatusList { const credentialSubject = this.extractCredentialSubject(data); if (typeof credentialSubject.encodedList !== 'string') { throw new Error('Invalid or missing encodedList'); } if (typeof credentialSubject.type !== 'string') { throw new Error('Invalid or missing type'); } return RevocationStatusListFactory.fromEncoded( credentialSubject.encodedList, credentialSubject.type, ); } private extractCredentialSubject( data: string | StatusListCredentialResponse, ): StatusListCredentialSubject { if (typeof data === 'string') { const payload = decodeJwt(data); if (!payload['vc']) { throw new Error( 'Expected a JWT with a vc payload for CredentialStatus types', ); } const vc = payload['vc'] as StatusListCredentialResponse; if (!vc.credentialSubject) { throw new Error('Missing credentialSubject in vc payload'); } return vc.credentialSubject as StatusListCredentialSubject; } if (!data.credentialSubject) { throw new Error('Missing credentialSubject'); } return data.credentialSubject as StatusListCredentialSubject; } }