Skip to content

Commit

Permalink
feat(diddocument): service will now be a AnyCodable to comply with sp…
Browse files Browse the repository at this point in the history
…ecifications
  • Loading branch information
beatt83 committed Apr 16, 2024
1 parent 3d62fc9 commit 2503b16
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
19 changes: 17 additions & 2 deletions Sources/DIDCore/DIDDocument/DIDDocument.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public struct DIDDocument {
}
}

// Service may contain extensions, with this in mind this Service model is the basic structure of a Service as described in [did-core](https://www.w3.org/TR/did-core/#services). But the DIDDocument itself uses AnyCodable for the service.
public struct Service {
public let id: String
public let type: String
Expand All @@ -43,6 +44,20 @@ public struct DIDDocument {
self.type = type
self.serviceEndpoint = serviceEndpoint
}

public init(from: AnyCodable) throws {
guard
let value = from.value as? [String: Any],
let id = value["id"] as? String,
let type = value["type"] as? String,
let serviceEndpoint = value["serviceEndpoint"]
else {
throw DIDCoreError.somethingWentWrong
}
self.id = id
self.type = type
self.serviceEndpoint = AnyCodable(serviceEndpoint)
}
}

public enum VerificationMethodMapping {
Expand All @@ -58,7 +73,7 @@ public struct DIDDocument {
public let assertionMethod: [VerificationMethodMapping]?
public let capabilityDelegation: [VerificationMethodMapping]?
public let keyAgreement: [VerificationMethodMapping]?
public let services: [Service]?
public let services: [AnyCodable]?

public init(
id: String,
Expand All @@ -69,7 +84,7 @@ public struct DIDDocument {
assertionMethod: [VerificationMethodMapping]? = nil,
capabilityDelegation: [VerificationMethodMapping]? = nil,
keyAgreement: [VerificationMethodMapping]? = nil,
services: [Service]? = nil
services: [AnyCodable]? = nil
) {
self.id = id
self.alsoKnownAs = alsoKnownAs
Expand Down
2 changes: 1 addition & 1 deletion Sources/DIDCore/Helper/AnyCodable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ public struct AnyCodable {
self.value = value
}

init(_ value: Any) {
public init(_ value: Any) {
self.value = value
}

Expand Down

0 comments on commit 2503b16

Please sign in to comment.