@@ -60,7 +60,7 @@ extension DIDCore.DIDDocument {
60
60
assertionMethod: nil ,
61
61
capabilityDelegation: nil ,
62
62
keyAgreement: keyAgreementIds. map { . stringValue( $0) } ,
63
- services: services
63
+ services: services. map { $0 . toAnyCodable ( ) }
64
64
)
65
65
}
66
66
@@ -98,23 +98,40 @@ extension DIDCore.DIDDocument {
98
98
}
99
99
100
100
let services = try self . services? . map {
101
- guard
102
- let endpoint = $0. serviceEndpoint. value as? [ String : Any ] ,
103
- let uri = endpoint [ " uri " ] as? String
104
- else {
105
- throw CastorError . notPossibleToResolveDID ( did: $0. id, reason: " Invalid service " )
101
+ let service = try DIDCore . DIDDocument. Service ( from: $0)
102
+ switch service. serviceEndpoint. value {
103
+ case let endpoint as [ String : Any ] :
104
+ guard
105
+ let uri = endpoint [ " uri " ] as? String
106
+ else {
107
+ throw CastorError . notPossibleToResolveDID ( did: service. id, reason: " Invalid service " )
108
+ }
109
+ return Domain . DIDDocument. Service (
110
+ id: service. id,
111
+ type: [ service. type] ,
112
+ serviceEndpoint: [
113
+ . init(
114
+ uri: uri,
115
+ accept: endpoint [ " accept " ] as? [ String ] ?? [ ] ,
116
+ routingKeys: endpoint [ " routing_keys " ] as? [ String ] ?? [ ]
117
+ )
118
+ ]
119
+ )
120
+ case let endpoint as String :
121
+ return Domain . DIDDocument. Service (
122
+ id: service. id,
123
+ type: [ service. type] ,
124
+ serviceEndpoint: [
125
+ . init(
126
+ uri: endpoint,
127
+ accept: ( $0. value as? [ String : Any ] ) ? [ " accept " ] as? [ String ] ?? [ ] ,
128
+ routingKeys: ( $0. value as? [ String : Any ] ) ? [ " routing_keys " ] as? [ String ] ?? [ ]
129
+ )
130
+ ]
131
+ )
132
+ default :
133
+ throw CastorError . notPossibleToResolveDID ( did: service. id, reason: " Invalid service " )
106
134
}
107
- return Domain . DIDDocument. Service (
108
- id: $0. id,
109
- type: [ $0. type] ,
110
- serviceEndpoint: [
111
- . init(
112
- uri: uri,
113
- accept: endpoint [ " accept " ] as? [ String ] ?? [ ] ,
114
- routingKeys: endpoint [ " routing_keys " ] as? [ String ] ?? [ ]
115
- )
116
- ]
117
- )
118
135
} ?? [ Domain . DIDDocument. Service] ( )
119
136
120
137
return Domain . DIDDocument (
@@ -184,3 +201,50 @@ extension DIDCore.DIDDocument.VerificationMethod {
184
201
}
185
202
}
186
203
}
204
+
205
+ extension DIDCore . DIDDocument . Service {
206
+ init ( from: AnyCodable ) throws {
207
+ guard
208
+ let dic = from. value as? [ String : Any ] ,
209
+ let id = dic [ " id " ] as? String ,
210
+ let type = dic [ " type " ] as? String ,
211
+ let serviceEndpoint = dic [ " serviceEndpoint " ]
212
+ else { throw CommonError . invalidCoding ( message: " Could not decode service " ) }
213
+ switch serviceEndpoint {
214
+ case let value as AnyCodable :
215
+ self = . init(
216
+ id: id,
217
+ type: type,
218
+ serviceEndpoint: value
219
+ )
220
+ case let value as String :
221
+ self = . init(
222
+ id: id,
223
+ type: type,
224
+ serviceEndpoint: AnyCodable ( value)
225
+ )
226
+ case let value as [ String : Any ] :
227
+ self = . init(
228
+ id: id,
229
+ type: type,
230
+ serviceEndpoint: AnyCodable ( value)
231
+ )
232
+ case let value as [ String ] :
233
+ self = . init(
234
+ id: id,
235
+ type: type,
236
+ serviceEndpoint: AnyCodable ( value)
237
+ )
238
+ default :
239
+ throw CommonError . invalidCoding ( message: " Could not decode service " )
240
+ }
241
+ }
242
+
243
+ func toAnyCodable( ) -> AnyCodable {
244
+ AnyCodable ( dictionaryLiteral:
245
+ ( " id " , self . id) ,
246
+ ( " type " , self . type) ,
247
+ ( " serviceEndpoint " , self . serviceEndpoint. value)
248
+ )
249
+ }
250
+ }
0 commit comments