@@ -26,87 +26,62 @@ struct UnsignedMdocVPTokenBuilder: UnsignedVPTokenBuilder {
2626 }
2727
2828
29- func build( credentialInputDescriptorMappings: inout [ CredentialInputDescriptorMapping ] ) async throws -> ( vpTokenSigningPayload: Any ? , unsignedVPTokens: [ UnsignedVPToken ] ) {
30- var docTypeToDeviceAuthenticationBytes : [ String : String ] = [ : ]
29+ func build( credentialInputDescriptorMappings: inout [ CredentialInputDescriptorMapping ] ) async throws -> ( vpTokenSigningPayload: VPTokenSigningPayload , unsignedVPTokens: [ UnsignedVPToken ] ) {
30+ var uuidToDeviceAuthenticationBytes : [ String : String ] = [ : ]
3131 var unsignedVPTokens : [ UnsignedVPToken ] = [ ]
32-
32+
3333 let sessionTranscript = try getSessionTranscript ( )
3434 let deviceNamespacesBytes = getDeviceNamespacesBytes ( )
35-
35+
36+ var existingDocTypes : Set < String > = [ ]
37+
3638 for index in 0 ..< credentialInputDescriptorMappings. count {
3739 let credentialInputDescriptorMapping = credentialInputDescriptorMappings [ index]
38- let ( docType , deviceAuthentication , unsignedVPToken) = try await buildPayloadAndUnsignedVPToken (
40+ let ( _ , _ , unsignedVPToken) = try await buildPayloadAndUnsignedVPToken (
3941 with: credentialInputDescriptorMapping. credential,
4042 sessionTranscript: sessionTranscript,
4143 deviceNamespacesBytes: deviceNamespacesBytes,
42- docTypeToDeviceAuthenticationBytes: docTypeToDeviceAuthenticationBytes,
43- updateIdentifier: { docTypeString in
44- credentialInputDescriptorMappings [ index] . identifier = docTypeString
45- }
44+ uuidToDeviceAuthenticationBytes: & uuidToDeviceAuthenticationBytes,
45+ updateIdentifier: { identifier in
46+ credentialInputDescriptorMappings [ index] . identifier = identifier
47+ } ,
48+ existingDocTypes: & existingDocTypes
4649 )
4750
48- docTypeToDeviceAuthenticationBytes [ docType] = deviceAuthentication
4951 unsignedVPTokens. append ( unsignedVPToken)
5052 }
51-
52-
53- unsignedVPTokens = [ ]
54- for docType in docTypeToDeviceAuthenticationBytes. keys. sorted ( ) {
55- let mapping = credentialInputDescriptorMappings. first ( where: { $0. identifier == docType } ) !
56- let mdocCredential = mapping. credential. value as! String
57- let ( keyRef, alg) = try resolveMdocKeyAndAlg ( mdocCredential)
58- unsignedVPTokens. append ( UnsignedVPToken (
59- format: . mso_mdoc,
60- holderKeyReference: keyRef,
61- signatureAlgorithm: alg,
62- dataToSign: Data ( ( docTypeToDeviceAuthenticationBytes [ docType] !) . utf8)
63- ) )
64- }
65-
53+
6654 return (
67- vpTokenSigningPayload: docTypeToDeviceAuthenticationBytes ,
55+ vpTokenSigningPayload: uuidToDeviceAuthenticationBytes ,
6856 unsignedVPTokens: unsignedVPTokens
6957 )
7058 }
7159
72- func build( credentialToCredentialQueryIdMappings: inout [ CredentialToCredentialQueryIdMapping ] ) async throws -> ( vpTokenSigningPayload: Any ? , unsignedVPTokens: [ UnsignedVPToken ] ) {
73- var docTypeToDeviceAuthenticationBytes : [ String : String ] = [ : ]
60+ func build( credentialToCredentialQueryIdMappings: inout [ CredentialToCredentialQueryIdMapping ] ) async throws -> ( vpTokenSigningPayload: VPTokenSigningPayload , unsignedVPTokens: [ UnsignedVPToken ] ) {
61+ var uuidToDeviceAuthenticationBytes : [ String : String ] = [ : ]
7462 var unsignedVPTokens : [ UnsignedVPToken ] = [ ]
75-
63+
7664 let sessionTranscript = try getSessionTranscript ( )
7765 let deviceNamespacesBytes = getDeviceNamespacesBytes ( )
78-
66+ var existingDocTypes : Set < String > = [ ]
67+
7968 for index in 0 ..< credentialToCredentialQueryIdMappings. count {
8069 let credentialToCredentialQueryIdMapping = credentialToCredentialQueryIdMappings [ index]
81- let ( docType , deviceAuthentication , unsignedVPToken) = try await buildPayloadAndUnsignedVPToken (
70+ let ( _ , _ , unsignedVPToken) = try await buildPayloadAndUnsignedVPToken (
8271 with: credentialToCredentialQueryIdMapping. credential,
8372 sessionTranscript: sessionTranscript,
8473 deviceNamespacesBytes: deviceNamespacesBytes,
85- docTypeToDeviceAuthenticationBytes : docTypeToDeviceAuthenticationBytes ,
86- updateIdentifier: { docTypeString in
87- credentialToCredentialQueryIdMappings [ index] . identifier = docTypeString
88- }
89- )
74+ uuidToDeviceAuthenticationBytes : & uuidToDeviceAuthenticationBytes ,
75+ updateIdentifier: { identifier in
76+ credentialToCredentialQueryIdMappings [ index] . identifier = identifier
77+ } , existingDocTypes : & existingDocTypes
78+ )
9079
91- docTypeToDeviceAuthenticationBytes [ docType] = deviceAuthentication
9280 unsignedVPTokens. append ( unsignedVPToken)
9381 }
94-
95- unsignedVPTokens = [ ]
96- for docType in docTypeToDeviceAuthenticationBytes. keys. sorted ( ) {
97- let mapping = credentialToCredentialQueryIdMappings. first ( where: { $0. identifier == docType } ) !
98- let mdocCredential = mapping. credential. value as! String
99- let ( keyRef, alg) = try resolveMdocKeyAndAlg ( mdocCredential)
100- unsignedVPTokens. append ( UnsignedVPToken (
101- format: . mso_mdoc,
102- holderKeyReference: keyRef,
103- signatureAlgorithm: alg,
104- dataToSign: Data ( ( docTypeToDeviceAuthenticationBytes [ docType] !) . utf8)
105- ) )
106- }
107-
82+
10883 return (
109- vpTokenSigningPayload: docTypeToDeviceAuthenticationBytes ,
84+ vpTokenSigningPayload: uuidToDeviceAuthenticationBytes ,
11085 unsignedVPTokens: unsignedVPTokens
11186 )
11287 }
@@ -131,19 +106,21 @@ struct UnsignedMdocVPTokenBuilder: UnsignedVPTokenBuilder {
131106 private func buildPayloadAndUnsignedVPToken( with credential: AnyCodable ,
132107 sessionTranscript: CBOR ,
133108 deviceNamespacesBytes: CBOR ,
134- docTypeToDeviceAuthenticationBytes: [ String : String ] ,
135- updateIdentifier: ( String ) -> Void
109+ uuidToDeviceAuthenticationBytes: inout [ String : String ] ,
110+ updateIdentifier: ( String ) -> Void ,
111+ existingDocTypes: inout Set < String >
136112 ) async throws -> ( docType: String , deviceAuthenticationBytes: String , unsignedVPToken: UnsignedVPToken ) {
137113 let ( mdocCredential, decodedMdocCredential) = try decodeMdoc ( credential, className: Self . className)
138114
139115 let ( docType, docTypeString) = try extractMdocDocType ( from: decodedMdocCredential, className: Self . className)
140116
141- if docTypeToDeviceAuthenticationBytes [ docTypeString] != nil {
117+ if existingDocTypes . contains ( docTypeString) {
142118 throw InvalidData (
143119 message: " Duplicate Mdoc Credentials with same doctype found " ,
144120 className: Self . className
145121 )
146122 }
123+ existingDocTypes. insert ( docTypeString)
147124
148125 let deviceAuthentication = CBOR . array ( [
149126 . utf8String( " DeviceAuthentication " ) ,
@@ -154,15 +131,18 @@ struct UnsignedMdocVPTokenBuilder: UnsignedVPTokenBuilder {
154131
155132 let wrapped = wrapCBORInputWithTag24 ( input: deviceAuthentication) !
156133 let dataToSign = cborToByteString ( cbor: wrapped)
134+ let identifier = UUIDGenerator . generateUUID ( )
157135
158- updateIdentifier ( docTypeString)
136+ updateIdentifier ( identifier)
137+ uuidToDeviceAuthenticationBytes [ identifier] = dataToSign
159138
160139 let ( keyRef, alg) = try resolveMdocKeyAndAlg ( mdocCredential)
161140
162141 return (
163142 docTypeString,
164143 dataToSign,
165144 UnsignedVPToken (
145+ id: identifier,
166146 format: . mso_mdoc,
167147 holderKeyReference: keyRef,
168148 signatureAlgorithm: alg,
@@ -183,7 +163,7 @@ struct UnsignedMdocVPTokenBuilder: UnsignedVPTokenBuilder {
183163 case . draft23:
184164 let clientIdToHash = CBOR . array ( [ . utf8String( authorizationRequest. clientId) , . utf8String( mdocGeneratedNonce) ] )
185165 let clientIdHash = CBOR . byteString ( sha256Hash ( from: clientIdToHash) )
186-
166+
187167 let responseUriToHash = CBOR . array ( [ . utf8String( responseUri) , . utf8String( mdocGeneratedNonce) ] )
188168 let responseUriHash = CBOR . byteString ( sha256Hash ( from: responseUriToHash) )
189169 return CBOR . array ( [ clientIdHash, responseUriHash, . utf8String( authorizationRequest. nonce) ] )
0 commit comments