import { RevocationStatusList } from './revocationStatusList.js'; import { StatusListCredentialResponse } from '../resolvers/statusListCredentialFetcher.js'; import { TokenStatusListDecoder } from './tokenStatusListDecoder.js'; import { CredentialStatusListDecoder } from './statusListCredentialDecoder.js'; import { parseStatusReferenceType, StatusListType, } from './statusListReference.js'; import { decodeJwt, decodeProtectedHeader } from 'jose'; export interface StatusListDecoder { getEncodedList( data: string | StatusListCredentialResponse, ): RevocationStatusList; } export class StatusListDecoderFactory { static getDecoder(statusReferenceType: StatusListType): StatusListDecoder { if (statusReferenceType === StatusListType.TokenStatusList) { return new TokenStatusListDecoder(); } if ( (statusReferenceType as string).startsWith( StatusListType.BitstringStatusList, ) || (statusReferenceType as string).startsWith( StatusListType.StatusList2021, ) || (statusReferenceType as string).startsWith( StatusListType.RevocationList2021, ) ) { return new CredentialStatusListDecoder(); } throw new Error( `Unsupported status reference type: ${statusReferenceType}`, ); } static getDecoderFromData( data: string | StatusListCredentialResponse, ): StatusListDecoder { if (typeof data === 'string') { const header = decodeProtectedHeader(data); if (header.typ === 'statuslist+jwt') { return StatusListDecoderFactory.getDecoder( StatusListType.TokenStatusList, ); } const payload = decodeJwt(data); const type = (payload['vc'] as any)?.credentialSubject?.type; return StatusListDecoderFactory.getDecoder( parseStatusReferenceType(type), ); } return StatusListDecoderFactory.getDecoder( parseStatusReferenceType(data.credentialSubject?.type), ); } }