Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(agent): add logging capabilities #71

Merged
merged 1 commit into from
Jan 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion Castor/Sources/CastorImpl+Public.swift
Original file line number Diff line number Diff line change
Expand Up @@ -48,16 +48,25 @@ extension CastorImpl: Castor {
}

public func resolveDID(did: DID) async throws -> DIDDocument {
logger.info(message: "Trying to resolve DID", metadata: [
.maskedMetadataByLevel(key: "DID", value: did.string, level: .debug)
])
guard
let resolver = resolvers.first(where: { $0.method == did.method })
else {
logger.error(message: "No resolvers for DID method \(did.method)", metadata: [
.maskedMetadataByLevel(key: "DID", value: did.string, level: .debug)
])
throw CastorError.notPossibleToResolveDID
}
return try await resolver.resolve(did: did)
}

public func getEcnumbasis(did: DID, keyPair: KeyPair) throws -> String {
try CreatePeerDIDOperation(
logger.debug(message: "Getting ecnumbasis", metadata: [
.maskedMetadataByLevel(key: "DID", value: did.string, level: .debug)
])
return try CreatePeerDIDOperation(
autenticationKeyPair: keyPair,
agreementKeyPair: keyPair,
services: []
Expand Down
4 changes: 3 additions & 1 deletion Castor/Sources/CastorImpl.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@ import Foundation
public struct CastorImpl {
let apollo: Apollo
let resolvers: [DIDResolverDomain]
let logger: PrismLogger

public init(apollo: Apollo, resolvers: [DIDResolverDomain] = []) {
self.logger = PrismLogger(category: .castor)
self.apollo = apollo
self.resolvers = resolvers + [
LongFormPrismDIDResolver(apollo: apollo),
LongFormPrismDIDResolver(apollo: apollo, logger: logger),
PeerDIDResolver()
]
}
Expand Down
2 changes: 1 addition & 1 deletion Castor/Sources/Operations/CreatePeerDIDOperation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ struct CreatePeerDIDOperation {
let services: [DIDDocument.Service]

func compute() throws -> DID {
try createPeerDID(
return try createPeerDID(
encryptionKeys: [try keyAgreementFromKeyPair(keyPair: agreementKeyPair)],
signingKeys: [try authenticationFromKeyPair(keyPair: autenticationKeyPair)],
services: services
Expand Down
1 change: 1 addition & 0 deletions Castor/Sources/Operations/VerifySignatureOperation.swift
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import Core
import Domain
import Foundation

Expand Down
17 changes: 15 additions & 2 deletions Castor/Sources/Resolvers/LongFormPrismDIDResolver.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,20 @@ import Foundation

struct LongFormPrismDIDResolver: DIDResolverDomain {
let apollo: Apollo
let logger: PrismLogger

var method = "prism"

func resolve(did: DID) throws -> DIDDocument {
let prismDID = try LongFormPrismDID(did: did)
guard
let data = Data(fromBase64URL: prismDID.encodedState)
else { throw CastorError.initialStateOfDIDChanged }
else {
logger.error(message: "The DID state hash doesn't match the state", metadata: [
.maskedMetadataByLevel(key: "DID", value: did.string, level: .debug)
])
throw CastorError.initialStateOfDIDChanged
}

let (verificationMethods, services) = try decodeState(
did: did,
Expand Down Expand Up @@ -48,7 +54,14 @@ struct LongFormPrismDIDResolver: DIDResolverDomain {
guard stateHash == verifyEncodedState else { throw CastorError.initialStateOfDIDChanged }
let operation = try Io_Iohk_Atala_Prism_Protos_AtalaOperation(serializedData: encodedData)
let publicKeys = try operation.createDid.didData.publicKeys.map {
try PrismDIDPublicKey(apollo: apollo, proto: $0)
do {
return try PrismDIDPublicKey(apollo: apollo, proto: $0)
} catch {
logger.error(message: "Failed to decode public key from document", metadata: [
.maskedMetadataByLevel(key: "DID", value: did.string, level: .debug)
])
throw error
}
}
let services = operation.createDid.didData.services.map {
DIDDocument.Service(
Expand Down
2 changes: 1 addition & 1 deletion Core/Sources/Logger/PrismLogger.swift
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ private let METADATA_PRIVACY_STR = "------"
// MARK: Prism Logger

public struct PrismLogger {
static var logLevels = [LogComponent: LogLevel]()
public static var logLevels = [LogComponent: LogLevel]()
private static let hashingLog = UUID().uuidString
private let logLevel: LogLevel

Expand Down
17 changes: 14 additions & 3 deletions Mercury/Sources/DIDCommWrappers/DIDCommDIDResolverWrapper.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@ import Domain
import Foundation

class DIDCommDIDResolverWrapper {
let logger: PrismLogger
let castor: Castor
var publisher = PassthroughSubject<DIDDocument, Error>()
var cancellables = [AnyCancellable]()

init(castor: Castor) {
init(castor: Castor, logger: PrismLogger) {
self.castor = castor
self.logger = logger
}

fileprivate func resolve(did: String) {
Expand All @@ -25,20 +27,29 @@ extension DIDCommDIDResolverWrapper: DidResolver {
func resolve(did: String, cb: OnDidResolverResult) -> ErrorCode {
publisher
.first()
.sink {
.sink { [weak self] in
switch $0 {
case .finished:
break
case let .failure(error):
self?.logger.error(message: "Error trying to resolve DID", metadata: [
.publicMetadata(key: "Error", value: error.localizedDescription)
])
try? cb.error(
err: ErrorKind.DidNotResolved(message: error.localizedDescription),
msg: error.localizedDescription
)
}
} receiveValue: {
} receiveValue: { [weak self] in
do {
self?.logger.debug(message: "Success resolving DID", metadata: [
.maskedMetadataByLevel(key: "DID", value: did, level: .debug)
])
try cb.success(result: try DidDoc(from: $0))
} catch {
self?.logger.error(message: "Error trying to resolve DID", metadata: [
.publicMetadata(key: "Error", value: error.localizedDescription)
])
try? cb.error(
err: ErrorKind.DidNotResolved(message: error.localizedDescription),
msg: error.localizedDescription
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import Combine
import Core
import DIDCommxSwift
import Domain
import Foundation
Expand All @@ -9,13 +10,15 @@ class DIDCommSecretsResolverWrapper {
let apollo: Apollo
let pluto: Pluto
let castor: Castor
let logger: PrismLogger
@Published var availableSecrets = [Domain.Secret]()
var cancellables = [AnyCancellable]()

init(apollo: Apollo, pluto: Pluto, castor: Castor) {
init(apollo: Apollo, pluto: Pluto, castor: Castor, logger: PrismLogger) {
self.apollo = apollo
self.pluto = pluto
self.castor = castor
self.logger = logger

startUpdating()
}
Expand Down Expand Up @@ -68,11 +71,14 @@ extension DIDCommSecretsResolverWrapper: SecretsResolver {
$availableSecrets
.first()
.map { $0.first { $0.id == secretid } }
.sink {
.sink { [weak self] in
do {
try cb.success(result: $0.map { DIDCommxSwift.Secret(from: $0) })
} catch {
print(error.localizedDescription)
self?.logger.error(message: "Could not find secret", metadata: [
.publicMetadata(key: "SecretId", value: secretid),
.publicMetadata(key: "Error", value: error.localizedDescription)
])
}
}
.store(in: &cancellables)
Expand All @@ -90,11 +96,14 @@ extension DIDCommSecretsResolverWrapper: SecretsResolver {
.filter { secretids.contains($0.id) }
.map { $0.id }
}
.sink {
.sink { [weak self] in
do {
try cb.success(result: $0)
} catch {
print(error.localizedDescription)
self?.logger.error(message: "Could not find secrets", metadata: [
.publicMetadata(key: "SecretsIds", value: secretids.description),
.publicMetadata(key: "Error", value: error.localizedDescription)
])
}
}
.store(in: &cancellables)
Expand Down
24 changes: 22 additions & 2 deletions Mercury/Sources/DIDCommWrappers/PackEncryptedOperation.swift
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
import Combine
import Core
import DIDCommxSwift
import Domain
import Foundation

final class PackEncryptedOperation: OnPackEncryptedResult {
private let didcomm: DIDCommProtocol
private let logger: PrismLogger
private var published = CurrentValueSubject<String?, Error>(nil)
private var cancellable: AnyCancellable?

init(didcomm: DIDCommProtocol) {
init(didcomm: DIDCommProtocol, logger: PrismLogger) {
self.didcomm = didcomm
self.logger = logger
}

func packEncrypted(msg: Domain.Message) async throws -> String {
Expand All @@ -22,11 +25,17 @@ final class PackEncryptedOperation: OnPackEncryptedResult {
self.cancellable = self.published
.drop(while: { $0 == nil })
.first()
.sink(receiveCompletion: {
.sink(receiveCompletion: { [weak self] in
switch $0 {
case .finished:
break
case let .failure(error):
self?.logger.error(
message: "Could not pack message",
metadata: [
.publicMetadata(key: "Error", value: error.localizedDescription)
]
)
continuation.resume(throwing: error)
}
}, receiveValue: {
Expand Down Expand Up @@ -68,6 +77,17 @@ final class PackEncryptedOperation: OnPackEncryptedResult {
}

func error(err: DIDCommxSwift.ErrorKind, msg: String) {
logger.error(
message: "Could not pack message",
metadata: [
.publicMetadata(
key: "Error",
value: MercuryError
.didcommError(msg: msg)
.localizedDescription
)
]
)
published.send(completion: .failure(MercuryError.didcommError(msg: msg)))
}
}
19 changes: 17 additions & 2 deletions Mercury/Sources/DIDCommWrappers/UnpackOperation.swift
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
import Combine
import Core
import DIDCommxSwift
import Domain
import Foundation

final class UnpackOperation: OnUnpackResult {
private let didcomm: DIDCommProtocol
private let castor: Castor
private let logger: PrismLogger
private var published = CurrentValueSubject<Domain.Message?, Error>(nil)
private var cancellable: AnyCancellable?

init(didcomm: DIDCommProtocol, castor: Castor) {
init(didcomm: DIDCommProtocol, castor: Castor, logger: PrismLogger) {
self.didcomm = didcomm
self.castor = castor
self.logger = logger
}

func unpackEncrypted(messageString: String) async throws -> Domain.Message {
Expand All @@ -28,11 +31,17 @@ final class UnpackOperation: OnUnpackResult {
self.cancellable = self.published
.drop(while: { $0 == nil })
.first()
.sink(receiveCompletion: {
.sink(receiveCompletion: { [weak self] in
switch $0 {
case .finished:
break
case let .failure(error):
self?.logger.error(
message: "Could not unpack message",
metadata: [
.publicMetadata(key: "Error", value: error.localizedDescription)
]
)
continuation.resume(throwing: error)
}
}, receiveValue: {
Expand All @@ -50,6 +59,12 @@ final class UnpackOperation: OnUnpackResult {
let message: Domain.Message = try result.toDomain(castor: castor)
published.send(message)
} catch {
logger.error(
message: "Could not unpack message",
metadata: [
.publicMetadata(key: "Error", value: error.localizedDescription)
]
)
published.send(completion: .failure(error))
}
}
Expand Down
9 changes: 6 additions & 3 deletions Mercury/Sources/MercuryImpl+Public.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ import Foundation

extension MercuryImpl: Mercury {
public func packMessage(msg: Domain.Message) async throws -> String {
try await PackEncryptedOperation(didcomm: didcomm).packEncrypted(msg: msg)
try await PackEncryptedOperation(didcomm: didcomm, logger: logger).packEncrypted(msg: msg)
}

public func unpackMessage(msg: String) async throws -> Domain.Message {
try await UnpackOperation(didcomm: didcomm, castor: castor).unpackEncrypted(messageString: msg)
try await UnpackOperation(didcomm: didcomm, castor: castor, logger: logger).unpackEncrypted(messageString: msg)
}

public func sendMessage(msg: Domain.Message) async throws -> Data? {
Expand All @@ -18,7 +18,10 @@ extension MercuryImpl: Mercury {
guard
let urlString = document.services.first?.serviceEndpoint.uri,
let url = URL(string: urlString)
else { throw MercuryError.noValidServiceFoundError }
else {
logger.error(message: "Could not find a valid service on the DID to send message")
throw MercuryError.noValidServiceFoundError
}
let packedMessage = try await packMessage(msg: msg)
return try await session.post(
url: url,
Expand Down
9 changes: 7 additions & 2 deletions Mercury/Sources/MercuryImpl.swift
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import Core
import DIDCommxSwift
import Domain
import Foundation
Expand All @@ -8,6 +9,7 @@ public struct MercuryImpl {
let apollo: Apollo
let pluto: Pluto
let didcomm: DidComm
let logger: PrismLogger

public init(
session: URLSession = .shared,
Expand All @@ -16,15 +18,18 @@ public struct MercuryImpl {
castor: Castor,
pluto: Pluto
) {
let logger = PrismLogger(category: .mercury)
self.logger = logger
self.session = SessionManager(session: session, timeout: timeout)
self.castor = castor
self.apollo = apollo
self.pluto = pluto
let didResolver = DIDCommDIDResolverWrapper(castor: castor)
let didResolver = DIDCommDIDResolverWrapper(castor: castor, logger: logger)
let secretsResolver = DIDCommSecretsResolverWrapper(
apollo: apollo,
pluto: pluto,
castor: castor
castor: castor,
logger: logger
)
self.didcomm = DidComm(
didResolver: didResolver,
Expand Down
Loading