• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

input-output-hk / atala-prism-wallet-sdk-swift / 9466819027

11 Jun 2024 01:48PM UTC coverage: 40.328% (-0.5%) from 40.822%
9466819027

Pull #145

github

web-flow
Merge 1399bca59 into 8e68386ce
Pull Request #145: feat(pollux): add support for sd-jwt

76 of 394 new or added lines in 20 files covered. (19.29%)

2 existing lines in 2 files now uncovered.

4518 of 11203 relevant lines covered (40.33%)

16.34 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

45.61
/EdgeAgentSDK/Castor/Sources/Resolvers/PeerDIDResolver.swift
1
import DIDCore
2
import Domain
3
import Foundation
4
import PeerDID
5

6
struct PeerDIDResolver: DIDResolverDomain {
7
    var method = "peer"
24✔
8

9
    func resolve(did: Domain.DID) async throws -> Domain.DIDDocument {
6✔
10
        try PeerDIDHelper.resolve(peerDIDStr: did.string).toDomain()
6✔
11
    }
6✔
12
}
13

14
extension DIDCore.DIDDocument {
15

16
    init(from: Domain.DIDDocument) throws {
×
17
        let verificationMethods = try from.verificationMethods.map {
×
18
            try DIDCore.DIDDocument.VerificationMethod(from: $0)
×
19
        }
×
20
        let verificationMethodsIds = verificationMethods.map(\.id)
×
21

×
22
        let authenticationMethods = try from.authenticate
×
23
            .filter {
×
24
                verificationMethodsIds.contains($0.id.string)
×
25
            }
×
26
            .map {
×
27
                try DIDCore.DIDDocument.VerificationMethod(from: $0)
×
28
            }
×
29
        let authenticationIds = from.authenticate.map(\.id.string)
×
30

×
31
        let keyAgreementMethods = try from.keyAgreement
×
32
            .filter {
×
33
                verificationMethodsIds.contains($0.id.string)
×
34
            }
×
35
            .map {
×
36
                try DIDCore.DIDDocument.VerificationMethod(from: $0)
×
37
            }
×
38

×
39
        let keyAgreementIds = from.keyAgreement.map(\.id.string)
×
40

×
41
        let services = from.services.flatMap { service in
×
42
            service.serviceEndpoint.map {
×
43
                DIDCore.DIDDocument.Service(
×
44
                    id: service.id,
×
45
                    type: service.type.first ?? "",
×
46
                    serviceEndpoint: AnyCodable(
×
47
                        dictionaryLiteral:
×
48
                            ("uri", $0.uri),
×
49
                            ("accept", $0.accept),
×
50
                            ("routing_keys", $0.routingKeys)
×
51
                    )
×
52
                )
×
53
            }
×
54
        }
×
55

×
56
        self.init(
×
57
            id: from.id.string,
×
58
            verificationMethods: verificationMethods + authenticationMethods + keyAgreementMethods,
×
59
            authentication: authenticationIds.map { .stringValue($0) },
×
60
            assertionMethod: nil,
×
61
            capabilityDelegation: nil,
×
62
            keyAgreement: keyAgreementIds.map { .stringValue($0) },
×
NEW
63
            services: services.map { $0.toAnyCodable() }
×
64
        )
×
65
    }
×
66

67
    func toDomain() throws -> Domain.DIDDocument {
6✔
68
        let authenticationUrls = self.verificationMethods
6✔
69
            .filter {
12✔
70
                guard let type = KnownVerificationMaterialType(rawValue: $0.type) else {
12✔
71
                    return false
×
72
                }
12✔
73
                switch type {
12✔
74
                case .authentication:
12✔
75
                    return true
6✔
76
                default:
12✔
77
                    return false
6✔
78
                }
12✔
79
            }
12✔
80
            .map { $0.id }
6✔
81

6✔
82
        let keyAgreementUrls = self.verificationMethods
6✔
83
            .filter {
12✔
84
                guard let type = KnownVerificationMaterialType(rawValue: $0.type) else {
12✔
85
                    return false
×
86
                }
12✔
87
                switch type {
12✔
88
                case .agreement:
12✔
89
                    return true
6✔
90
                default:
12✔
91
                    return false
6✔
92
                }
12✔
93
            }
12✔
94
            .map { $0.id }
6✔
95

6✔
96
        let verificationMethods = try self.verificationMethods.map {
12✔
97
            try $0.toDomain()
12✔
98
        }
12✔
99

6✔
100
        let services = try self.services?.map {
6✔
101
            let service = try DIDCore.DIDDocument.Service(from: $0)
3✔
102
            switch service.serviceEndpoint.value {
3✔
103
            case let endpoint as [String: Any]:
3✔
NEW
104
                guard
×
NEW
105
                    let uri = endpoint["uri"] as? String
×
NEW
106
                else {
×
NEW
107
                    throw CastorError.notPossibleToResolveDID(did: service.id, reason: "Invalid service")
×
NEW
108
                }
×
NEW
109
                return Domain.DIDDocument.Service(
×
NEW
110
                    id: service.id,
×
NEW
111
                    type: [service.type],
×
NEW
112
                    serviceEndpoint: [
×
NEW
113
                        .init(
×
NEW
114
                            uri: uri,
×
NEW
115
                            accept: endpoint["accept"] as? [String] ?? [],
×
NEW
116
                            routingKeys: endpoint["routing_keys"] as? [String] ?? []
×
NEW
117
                        )
×
NEW
118
                    ]
×
NEW
119
                )
×
120
            case let endpoint as String:
3✔
121
                return Domain.DIDDocument.Service(
3✔
122
                    id: service.id,
3✔
123
                    type: [service.type],
3✔
124
                    serviceEndpoint: [
3✔
125
                        .init(
3✔
126
                            uri: endpoint,
3✔
127
                            accept: ($0.value as? [String: Any])?["accept"] as? [String] ?? [],
3✔
128
                            routingKeys: ($0.value as? [String: Any])?["routing_keys"] as? [String] ?? []
3✔
129
                        )
3✔
130
                    ]
3✔
131
                )
3✔
132
            default:
3✔
NEW
133
                throw CastorError.notPossibleToResolveDID(did: service.id, reason: "Invalid service")
×
134
            }
3✔
135
        } ?? [Domain.DIDDocument.Service]()
3✔
136

6✔
137
        return Domain.DIDDocument(
6✔
138
            id: try DID(string: self.id),
6✔
139
            coreProperties: [
6✔
140
                Domain.DIDDocument.Authentication(
6✔
141
                    urls: authenticationUrls,
6✔
142
                    verificationMethods: []
6✔
143
                ),
6✔
144
                Domain.DIDDocument.KeyAgreement(
6✔
145
                    urls: keyAgreementUrls,
6✔
146
                    verificationMethods: []
6✔
147
                ),
6✔
148
                Domain.DIDDocument.VerificationMethods(values: verificationMethods),
6✔
149
                Domain.DIDDocument.Services(values: services)
6✔
150
            ]
6✔
151
        )
6✔
152
    }
6✔
153
}
154

155
extension DIDCore.DIDDocument.VerificationMethod {
156

157
    init(from: Domain.DIDDocument.VerificationMethod) throws {
×
158
        if let publicKeyMultibase = from.publicKeyMultibase {
×
159
            self.init(
×
160
                id: from.id.string,
×
161
                controller: from.controller.string,
×
162
                type: from.type,
×
163
                material: .init(
×
164
                    format: .multibase,
×
165
                    value: try publicKeyMultibase.tryData(using: .utf8)
×
166
                )
×
167
            )
×
168
        } else if let publicKeyJwk = from.publicKeyJwk {
×
169
            self.init(
×
170
                id: from.id.string,
×
171
                controller: from.controller.string,
×
172
                type: from.type,
×
173
                material: .init(
×
174
                    format: .jwk,
×
175
                    value: try JSONSerialization.data(withJSONObject: publicKeyJwk)
×
176
                )
×
177
            )
×
178
        } else {
×
179
            throw PeerDIDError.invalidMaterialType("")
×
180
        }
×
181
    }
×
182

183
    func toDomain() throws -> Domain.DIDDocument.VerificationMethod {
12✔
184
        switch material.format {
12✔
185
        case .jwk:
12✔
186
            return Domain.DIDDocument.VerificationMethod(
×
187
                id: try DIDUrl(string: id),
×
188
                controller: try DID(string: controller),
×
189
                type: type,
×
190
                publicKeyJwk: try JSONSerialization.jsonObject(with: material.value) as? [String: String]
×
191
            )
×
192
        case .multibase:
12✔
193
            return Domain.DIDDocument.VerificationMethod(
12✔
194
                id: try DIDUrl(string: id),
12✔
195
                controller: try DID(string: controller),
12✔
196
                type: type,
12✔
197
                publicKeyMultibase: String(data: material.value, encoding: .utf8)
12✔
198
            )
12✔
199
        default:
12✔
200
            throw CastorError.notPossibleToResolveDID(did: id, reason: "Invalid did peer")
×
201
        }
12✔
202
    }
12✔
203
}
204

205
extension DIDCore.DIDDocument.Service {
206
    init(from: AnyCodable) throws {
3✔
207
        guard
3✔
208
            let dic = from.value as? [String: Any],
3✔
209
            let id = dic["id"] as? String,
3✔
210
            let type = dic["type"] as? String,
3✔
211
            let serviceEndpoint = dic["serviceEndpoint"]
3✔
212
        else { throw CommonError.invalidCoding(message: "Could not decode service") }
3✔
213
        switch serviceEndpoint {
3✔
214
        case let value as AnyCodable:
3✔
NEW
215
            self = .init(
×
NEW
216
                id: id,
×
NEW
217
                type: type,
×
NEW
218
                serviceEndpoint: value
×
NEW
219
            )
×
220
        case let value as String:
3✔
221
            self = .init(
3✔
222
                id: id,
3✔
223
                type: type,
3✔
224
                serviceEndpoint: AnyCodable(value)
3✔
225
            )
3✔
226
        case let value as [String: Any]:
3✔
NEW
227
            self = .init(
×
NEW
228
                id: id,
×
NEW
229
                type: type,
×
NEW
230
                serviceEndpoint: AnyCodable(value)
×
NEW
231
            )
×
232
        case let value as [String]:
3✔
NEW
233
            self = .init(
×
NEW
234
                id: id,
×
NEW
235
                type: type,
×
NEW
236
                serviceEndpoint: AnyCodable(value)
×
NEW
237
            )
×
238
        default:
3✔
NEW
239
            throw CommonError.invalidCoding(message: "Could not decode service")
×
240
        }
3✔
241
    }
3✔
242

NEW
243
    func toAnyCodable() -> AnyCodable {
×
NEW
244
        AnyCodable(dictionaryLiteral:
×
NEW
245
            ("id", self.id),
×
NEW
246
            ("type", self.type),
×
NEW
247
            ("serviceEndpoint", self.serviceEndpoint.value)
×
NEW
248
        )
×
NEW
249
    }
×
250
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc