Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions src/utils/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,3 +92,50 @@
}
return keyTypeInfo
}

type Namespaces = Record<string, any>

export function processIsoImages(namespaces: Namespaces): Namespaces {
const IMAGE_FIELDS = ['portrait', 'enrolment_portrait_image']

for (const [nsKey, nsValue] of Object.entries(namespaces)) {
if (!nsKey.includes('org.iso')) continue

for (const field of IMAGE_FIELDS) {
const value = nsValue[field]

if (value && typeof value === 'string') {
nsValue[field] = safeBase64DataUrlToUint8Array(value)
}
}
}

return namespaces
}

function safeBase64DataUrlToUint8Array(dataUrl: string): Uint8Array | string {
try {
if (typeof dataUrl !== 'string') return dataUrl

// Must contain base64 data
if (!dataUrl.includes('base64,')) return dataUrl

const parts = dataUrl.split(',')
if (parts.length < 2) return dataUrl

const base64 = parts[1]

// Node.js safe decode (will throw if invalid)
const buffer = Buffer.from(base64, 'base64')

// Extra validation: ensure it decoded something meaningful
if (!buffer || buffer.length === 0) {
return dataUrl
}

return new Uint8Array(buffer)
} catch (err) {
// fallback → keep original string
return dataUrl
}

Check warning on line 140 in src/utils/helpers.ts

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Handle this exception or don't catch it at all.

See more on https://sonarcloud.io/project/issues?id=credebl_afj-controller&issues=AZ01b9z4mIEHLU8AKTst&open=AZ01b9z4mIEHLU8AKTst&pullRequest=355
}
11 changes: 10 additions & 1 deletion src/utils/oid4vc-agent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { ClaimFormat, X509ModuleConfig } from '@credo-ts/core'
import { OpenId4VciCredentialFormatProfile } from '@credo-ts/openid4vc'

import { SignerMethod } from '../enums/enum'
import { processIsoImages } from './helpers'

export function getMixedCredentialRequestToCredentialMapper(): OpenId4VciCredentialRequestToCredentialMapper {
return async ({
Expand Down Expand Up @@ -103,14 +104,22 @@ export function getMixedCredentialRequestToCredentialMapper(): OpenId4VciCredent
const parsedCertificate = X509Service.parseCertificate(agentContext, {
encodedCertificate: issuerx509certificate[0],
})
console.log(`\n credential validityInfo for mdoc: ${JSON.stringify(credential.payload.validityInfo)} \n`)
parsedCertificate.publicJwk.keyId = credential.signerOptions.keyId
const updatedNamespaces = processIsoImages(credential.payload.namespaces)
credential.payload.namespaces = updatedNamespaces
return {
type: 'credentials',
format: ClaimFormat.MsoMdoc,
credentials: holderBinding.keys.map((holderBindingDetails) => ({
issuerCertificate: parsedCertificate,
holderKey: holderBindingDetails.jwk,
...credential.payload,
validityInfo: {
...credential.payload.validityInfo,
validFrom: new Date(credential.payload.validityInfo.validFrom),
validUntil: new Date(credential.payload.validityInfo.validUntil),
},
docType: credentialConfiguration.doctype,
})),
} satisfies OpenId4VciSignMdocCredentials
Expand Down Expand Up @@ -171,7 +180,7 @@ export async function getTrustedCerts() {
}
const data = await response.json()
return data as string[]
} catch (error) {
} catch (error) {
// eslint-disable-next-line no-console
console.error('Error fetching data:', error)
return []
Expand Down
Loading