Skip to content

Commit a7dfbc1

Browse files
committed
feat: tranform mdoc claims to json
1 parent 6541485 commit a7dfbc1

File tree

1 file changed

+28
-1
lines changed

1 file changed

+28
-1
lines changed

packages/core/src/modules/dcql/DcqlService.ts

+28-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import type { AgentContext } from '../../agent'
33
import { DcqlCredential, DcqlMdocCredential, DcqlQuery, DcqlSdJwtVcCredential } from 'dcql'
44
import { injectable } from 'tsyringe'
55

6+
import { JsonValue } from '../../types'
67
import { Mdoc, MdocApi, MdocDeviceResponse, MdocOpenId4VpSessionTranscriptOptions, MdocRecord } from '../mdoc'
78
import { SdJwtVcApi, SdJwtVcRecord, SdJwtVcService } from '../sd-jwt-vc'
89
import { buildDisclosureFrameForPayload } from '../sd-jwt-vc/disclosureFrame'
@@ -17,6 +18,19 @@ import {
1718
} from './models'
1819
import { dcqlGetPresentationsToCreate as getDcqlVcPresentationsToCreate } from './utils'
1920

21+
interface HasToJson {
22+
toJson(): JsonValue
23+
}
24+
25+
function isToJsonable(value: unknown): value is HasToJson {
26+
return (
27+
value !== null &&
28+
typeof value === 'object' &&
29+
'toJson' in value &&
30+
typeof (value as HasToJson).toJson === 'function'
31+
)
32+
}
33+
2034
/**
2135
* @todo create a public api for using dif presentation exchange
2236
*/
@@ -99,10 +113,23 @@ export class DcqlService {
99113

100114
const dcqlCredentials: DcqlCredential[] = credentialRecords.map((record) => {
101115
if (record.type === 'MdocRecord') {
116+
const transformValue = (value: unknown): unknown => {
117+
if (typeof value !== 'function' && typeof value !== 'object') return value
118+
return isToJsonable(value) ? value.toJson() : 'unknown json representation'
119+
}
120+
121+
const mdoc = Mdoc.fromBase64Url(record.base64Url)
122+
123+
const namespaces = Object.fromEntries(
124+
Object.entries(mdoc.issuerSignedNamespaces).map(([key, namespace]) => [
125+
key,
126+
Object.fromEntries(Object.entries(namespace).map(([k, v]) => [k, transformValue(v)])),
127+
])
128+
)
102129
return {
103130
credential_format: 'mso_mdoc',
104131
doctype: record.getTags().docType,
105-
namespaces: Mdoc.fromBase64Url(record.base64Url).issuerSignedNamespaces,
132+
namespaces,
106133
} satisfies DcqlMdocCredential
107134
} else if (record.type === 'SdJwtVcRecord') {
108135
return {

0 commit comments

Comments
 (0)