Skip to content

Commit

Permalink
feat(didcore): update didcore library version
Browse files Browse the repository at this point in the history
  • Loading branch information
beatt83 committed May 24, 2024
1 parent e17057d commit 9483130
Show file tree
Hide file tree
Showing 5 changed files with 264 additions and 109 deletions.
4 changes: 1 addition & 3 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,10 @@ let package = Package(
],
dependencies: [
.package(url: "https://github.com/swift-libp2p/swift-multibase.git", .upToNextMajor(from: "0.0.1")),
.package(url: "https://github.com/beatt83/didcore-swift.git", .upToNextMinor(from: "1.1.0")),
.package(url: "https://github.com/beatt83/didcore-swift.git", .upToNextMinor(from: "2.0.0")),
.package(url: "https://github.com/beatt83/jose-swift.git", .upToNextMinor(from: "2.2.1"))
],
targets: [
// Targets are the basic building blocks of a package, defining a module or a test suite.
// Targets can depend on other targets in this package and products from dependencies.
.target(
name: "DIDCommSwift",
dependencies: [
Expand Down
193 changes: 147 additions & 46 deletions Sources/DIDCommSwift/Models/Routing/ServiceTree.swift
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ private func buildBranchsTree(

private func getRoutingURIAndKeys(document: DIDDocument) throws -> [(uri: String, keys: [String])] {
try document.services?
.first { $0.type.contains("DIDCommMessaging") }
.first { (try? $0.toDIDCommService()) != nil }
.map {
try $0.toDIDCommService().serviceEndpoint
.map { ($0.uri, $0.routingKeys) }
Expand All @@ -98,71 +98,172 @@ private func getRoutingURIAndKeys(document: DIDDocument) throws -> [(uri: String

private func getRoutingKeys(document: DIDDocument) throws -> [String] {
try document.services?
.first { $0.type.contains("DIDCommMessaging") }
.first { (try? $0.toDIDCommService()) != nil }
.flatMap {
try $0.toDIDCommService().serviceEndpoint
.flatMap(\.routingKeys)
} ?? []
}

private extension DIDDocument.Service {
private extension AnyCodable {
func toDIDCommService() throws -> Routing.DIDCommService {
guard self.type == didcommServiceType else {
throw DIDCommError.notDidCommServiceType
guard
let dic = value as? [String: Any],
let id = dic["id"] as? String,
let type = dic["type"] as? String,
type == didcommServiceType,
let serviceEndpoint = dic["serviceEndpoint"]
else { throw DIDCommError.notDidCommServiceType }

switch serviceEndpoint {
case let endpoint as String:
return try parseServiceString(id: id, type: type, service: endpoint)
case let endpoint as [String]:
return try parseServiceStringArray(id: id, type: type, service: endpoint)
case let endpoint as [String: Any]:
return try parseServiceDic(id: id, type: type, service: endpoint)
case let endpoint as [[String: Any]]:
return try parseServiceArrayDic(id: id, type: type, service: endpoint)
case let endpoint as AnyCodable:
return try parseServiceAnyCodable(id: id, type: type, service: endpoint)
case let endpoint as [AnyCodable]:
return try parseServiceAnyCodableArray(id: id, type: type, service: endpoint)
default:
return .init(
id: id,
type: type,
serviceEndpoint: []
)
}
}

func parseServiceString(id: String, type: String, service: String) throws -> Routing.DIDCommService {
return .init(
id: id,
type: type,
serviceEndpoint: [
.init(uri: service, accept: [], routingKeys: [])
]
)
}

func parseServiceStringArray(id: String, type: String, service: [String]) throws -> Routing.DIDCommService {
return .init(
id: id,
type: type,
serviceEndpoint: service.map {
.init(uri: $0, accept: [], routingKeys: [])
}
)
}

func parseServiceDic(id: String, type: String, service: [String: Any]) throws -> Routing.DIDCommService {
guard let uri = service["uri"] as? String else {
throw DIDCommError.missingUri
}
switch self.serviceEndpoint.value {
return .init(
id: id,
type: type,
serviceEndpoint: [
.init(
uri: uri,
accept: (service["accept"] as? [String]) ?? [],
routingKeys: (service["routing_keys"] as? [String]) ?? []
)
]
)
}

func parseServiceArrayDic(id: String, type: String, service: [[String: Any]]) throws -> Routing.DIDCommService {
return .init(
id: id,
type: type,
serviceEndpoint: try service.map {
guard let uri = $0["uri"] as? String else {
throw DIDCommError.missingUri
}
return .init(
uri: uri,
accept: ($0["accept"] as? [String]) ?? [],
routingKeys: ($0["routing_keys"] as? [String]) ?? []
)
}
)
}

func parseServiceAnyCodableArray(id: String, type: String, service: [AnyCodable]) throws -> Routing.DIDCommService {
return .init(
id: id,
type: type,
serviceEndpoint: try service.flatMap { try parseServiceEndpoint(serviceEndpoint: $0) }
)
}

func parseServiceAnyCodable(id: String, type: String, service: AnyCodable) throws -> Routing.DIDCommService {
switch service.value {
case let value as String:
return try parseServiceString(id: id, type: type, service: value)
case let value as [String]:
return try parseServiceStringArray(id: id, type: type, service: value)
case let value as [String: Any]:
return try parseServiceDic(id: id, type: type, service: value)
case let value as [[String: Any]]:
return try parseServiceArrayDic(id: id, type: type, service: value)
case let value as AnyCodable:
return .init(
id: self.id,
type: self.type,
serviceEndpoint: [
.init(uri: value, accept: [], routingKeys: [])
]
id: id,
type: type,
serviceEndpoint: try parseServiceEndpoint(serviceEndpoint: value)
)
case let value as [String]:
case let value as [AnyCodable]:
return .init(
id: self.id,
type: self.type,
serviceEndpoint: value.map {
.init(uri: $0, accept: [], routingKeys: [])
}
id: id,
type: type,
serviceEndpoint: try value.flatMap { try parseServiceEndpoint(serviceEndpoint: $0) }
)
default:
return .init(
id: id,
type: type,
serviceEndpoint: []
)
}
}

func parseServiceEndpoint(serviceEndpoint: AnyCodable) throws -> [Routing.DIDCommService.ServiceEndpoint] {
switch serviceEndpoint.value {
case let value as String:
return [
.init(uri: value, accept: [], routingKeys: [])
]
case let value as [String]:
return value.map {
.init(uri: $0, accept: [], routingKeys: [])
}
case let value as [String: Any]:
guard let uri = value["uri"] as? String else {
throw DIDCommError.missingUri
}
return .init(
id: self.id,
type: self.type,
serviceEndpoint: [
.init(
uri: uri,
accept: (value["accept"] as? [String]) ?? [],
routingKeys: (value["routing_keys"] as? [String]) ?? []
)
]
)
return [
.init(
uri: uri,
accept: (value["accept"] as? [String]) ?? [],
routingKeys: (value["routing_keys"] as? [String]) ?? []
)
]
case let value as [[String: Any]]:
return .init(
id: self.id,
type: self.type,
serviceEndpoint: try value.map {
guard let uri = $0["uri"] as? String else {
throw DIDCommError.missingUri
}
return .init(
uri: uri,
accept: ($0["accept"] as? [String]) ?? [],
routingKeys: ($0["routing_keys"] as? [String]) ?? []
)
return try value.map {
guard let uri = $0["uri"] as? String else {
throw DIDCommError.missingUri
}
)
return .init(
uri: uri,
accept: ($0["accept"] as? [String]) ?? [],
routingKeys: ($0["routing_keys"] as? [String]) ?? []
)
}
default:
return .init(
id: self.id,
type: self.type,
serviceEndpoint: []
)
return []
}
}
}
Expand Down
26 changes: 17 additions & 9 deletions Tests/DIDCommSwiftTests/TestData/Mock/DIDAliceMockData.swift
Original file line number Diff line number Diff line change
Expand Up @@ -157,15 +157,23 @@ let aliceAuthMethodSecp256k1 = DIDDocument.VerificationMethod(
)
)

let aliceServiceRoutingKeys = DIDDocument.Service(
id: "service1",
type: "DIDCommMessaging",
serviceEndpoint: AnyCodable(dictionaryLiteral:
("uri", "http://didcomm.com"),
("accept", [String]()),
("routing_keys", ["did:example:alice#key-x25519-1"])
)
)
//let aliceServiceEndpoint = AnyCodable(dictionaryLiteral:
// ("uri", "http://didcomm.com"),
// ("accept", [String]()),
// ("routing_keys", ["did:example:alice#key-x25519-1"])
// )

let aliceServiceRoutingKeysDic = [
"id": "service1",
"type": "DIDCommMessaging",
"serviceEndpoint": [
"uri": "http://didcomm.com",
"accept": [String](),
"routing_keys": ["did:example:alice#key-x25519-1"]
]
] as! [String: Any]

let aliceServiceRoutingKeys = AnyCodable(aliceServiceRoutingKeysDic)

let didDocAliceSpecTestVectors = DIDDocument(
id: "did:example:alice",
Expand Down
Loading

0 comments on commit 9483130

Please sign in to comment.