@@ -3,6 +3,42 @@ import Domain
3
3
import Foundation
4
4
5
5
struct LongFormPrismDIDResolver : DIDResolverDomain {
6
+ enum KeyType {
7
+ case master
8
+ case issuing
9
+ case authentication
10
+ case agreement
11
+ case capabilityDelegation
12
+ case capabilityInvocation
13
+ case revocation
14
+ case unknown
15
+
16
+ init ( usage: PrismDIDPublicKey . Usage ) {
17
+ switch usage {
18
+ case . masterKey:
19
+ self = . master
20
+ case . issuingKey:
21
+ self = . issuing
22
+ case . keyAgreementKey:
23
+ self = . agreement
24
+ case . capabilityDelegationKey:
25
+ self = . capabilityDelegation
26
+ case . capabilityInvocationKey:
27
+ self = . capabilityInvocation
28
+ case . authenticationKey:
29
+ self = . authentication
30
+ case . revocationKey:
31
+ self = . revocation
32
+ case . unknownKey:
33
+ self = . unknown
34
+ }
35
+ }
36
+ }
37
+ struct PublicKeyDecoded {
38
+ let id : String
39
+ let keyType : KeyType
40
+ let method : DIDDocument . VerificationMethod
41
+ }
6
42
let apollo : Apollo
7
43
let logger : PrismLogger
8
44
@@ -25,16 +61,21 @@ struct LongFormPrismDIDResolver: DIDResolverDomain {
25
61
encodedData: data
26
62
)
27
63
28
- let authenticate = verificationMethods. first. map {
29
- DIDDocument . Authentication ( urls: [ $0. key] , verificationMethods: [ ] )
64
+ let authenticates = verificationMethods. filter { $0. keyType == . authentication } . map {
65
+ DIDDocument . Authentication ( urls: [ $0. id] , verificationMethods: [ ] )
66
+ }
67
+
68
+ let agreements = verificationMethods. filter { $0. keyType == . authentication } . map {
69
+ DIDDocument . KeyAgreement ( urls: [ $0. id] , verificationMethods: [ ] )
30
70
}
31
71
32
72
let servicesProperty = DIDDocument . Services ( values: services)
33
73
34
- let verificationMethodsProperty = DIDDocument . VerificationMethods ( values: Array ( verificationMethods. values ) )
74
+ let verificationMethodsProperty = DIDDocument . VerificationMethods ( values: verificationMethods. map ( \ . method ) )
35
75
36
76
let properties = [
37
- authenticate,
77
+ authenticates,
78
+ agreements,
38
79
servicesProperty,
39
80
verificationMethodsProperty
40
81
] . compactMap { $0 as? DIDDocumentCoreProperty }
@@ -49,7 +90,7 @@ struct LongFormPrismDIDResolver: DIDResolverDomain {
49
90
did: DID ,
50
91
stateHash: String ,
51
92
encodedData: Data
52
- ) throws -> ( [ String : DIDDocument . VerificationMethod ] , [ DIDDocument . Service ] ) {
93
+ ) throws -> ( [ PublicKeyDecoded ] , [ DIDDocument . Service ] ) {
53
94
let verifyEncodedState = encodedData. sha256 ( )
54
95
guard stateHash == verifyEncodedState else { throw CastorError . initialStateOfDIDChanged }
55
96
let operation = try Io_Iohk_Atala_Prism_Protos_AtalaOperation ( serializedData: encodedData)
@@ -63,30 +104,35 @@ struct LongFormPrismDIDResolver: DIDResolverDomain {
63
104
throw error
64
105
}
65
106
}
107
+
66
108
let services = operation. createDid. didData. services. map {
67
109
DIDDocument . Service (
68
110
id: $0. id,
69
111
type: [ $0. type] ,
70
112
serviceEndpoint: $0. serviceEndpoint. map { . init( uri: $0) }
71
113
)
72
114
}
73
- return ( publicKeys. reduce (
74
- [ String: DIDDocument . VerificationMethod] ( ) )
75
- { partialResult, publicKey in
115
+
116
+ let decodedPublicKeys = publicKeys. map {
76
117
let didUrl = DIDUrl (
77
118
did: did,
78
- fragment: publicKey . id
119
+ fragment: $0 . id
79
120
)
80
121
81
122
let method = DIDDocument . VerificationMethod (
82
123
id: didUrl,
83
124
controller: did,
84
- type: publicKey. keyData. getProperty ( . curve) ?? " " ,
85
- publicKeyMultibase: publicKey. keyData. raw. base64EncodedString ( )
125
+ type: $0. keyData. getProperty ( . curve) ?? " " ,
126
+ publicKeyMultibase: $0. keyData. raw. base64EncodedString ( )
127
+ )
128
+
129
+ return PublicKeyDecoded (
130
+ id: didUrl. string,
131
+ keyType: . init( usage: $0. usage) ,
132
+ method: method
86
133
)
87
- var result = partialResult
88
- result [ didUrl. string] = method
89
- return result
90
- } , services)
134
+ }
135
+
136
+ return ( decodedPublicKeys, services)
91
137
}
92
138
}
0 commit comments