diff --git a/Castor/Sources/CastorImpl+Public.swift b/Castor/Sources/CastorImpl+Public.swift index 6088f76d..cb10bfbe 100644 --- a/Castor/Sources/CastorImpl+Public.swift +++ b/Castor/Sources/CastorImpl+Public.swift @@ -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: [] diff --git a/Castor/Sources/CastorImpl.swift b/Castor/Sources/CastorImpl.swift index 990040f7..db785773 100644 --- a/Castor/Sources/CastorImpl.swift +++ b/Castor/Sources/CastorImpl.swift @@ -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() ] } diff --git a/Castor/Sources/Operations/CreatePeerDIDOperation.swift b/Castor/Sources/Operations/CreatePeerDIDOperation.swift index 5c8af37f..358fc5b7 100644 --- a/Castor/Sources/Operations/CreatePeerDIDOperation.swift +++ b/Castor/Sources/Operations/CreatePeerDIDOperation.swift @@ -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 diff --git a/Castor/Sources/Operations/VerifySignatureOperation.swift b/Castor/Sources/Operations/VerifySignatureOperation.swift index 84c76671..a0277d31 100644 --- a/Castor/Sources/Operations/VerifySignatureOperation.swift +++ b/Castor/Sources/Operations/VerifySignatureOperation.swift @@ -1,3 +1,4 @@ +import Core import Domain import Foundation diff --git a/Castor/Sources/Resolvers/LongFormPrismDIDResolver.swift b/Castor/Sources/Resolvers/LongFormPrismDIDResolver.swift index 23e9ace7..b375733d 100644 --- a/Castor/Sources/Resolvers/LongFormPrismDIDResolver.swift +++ b/Castor/Sources/Resolvers/LongFormPrismDIDResolver.swift @@ -4,6 +4,7 @@ import Foundation struct LongFormPrismDIDResolver: DIDResolverDomain { let apollo: Apollo + let logger: PrismLogger var method = "prism" @@ -11,7 +12,12 @@ struct LongFormPrismDIDResolver: DIDResolverDomain { 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, @@ -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( diff --git a/Core/Sources/Logger/PrismLogger.swift b/Core/Sources/Logger/PrismLogger.swift index b4a71aa3..6438cb49 100644 --- a/Core/Sources/Logger/PrismLogger.swift +++ b/Core/Sources/Logger/PrismLogger.swift @@ -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 diff --git a/Mercury/Sources/DIDCommWrappers/DIDCommDIDResolverWrapper.swift b/Mercury/Sources/DIDCommWrappers/DIDCommDIDResolverWrapper.swift index 49fbfd6b..139156fa 100644 --- a/Mercury/Sources/DIDCommWrappers/DIDCommDIDResolverWrapper.swift +++ b/Mercury/Sources/DIDCommWrappers/DIDCommDIDResolverWrapper.swift @@ -5,12 +5,14 @@ import Domain import Foundation class DIDCommDIDResolverWrapper { + let logger: PrismLogger let castor: Castor var publisher = PassthroughSubject() var cancellables = [AnyCancellable]() - init(castor: Castor) { + init(castor: Castor, logger: PrismLogger) { self.castor = castor + self.logger = logger } fileprivate func resolve(did: String) { @@ -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 diff --git a/Mercury/Sources/DIDCommWrappers/DIDCommSecretsResolverWrapper.swift b/Mercury/Sources/DIDCommWrappers/DIDCommSecretsResolverWrapper.swift index 6cd76e1c..316c9b53 100644 --- a/Mercury/Sources/DIDCommWrappers/DIDCommSecretsResolverWrapper.swift +++ b/Mercury/Sources/DIDCommWrappers/DIDCommSecretsResolverWrapper.swift @@ -1,4 +1,5 @@ import Combine +import Core import DIDCommxSwift import Domain import Foundation @@ -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() } @@ -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) @@ -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) diff --git a/Mercury/Sources/DIDCommWrappers/PackEncryptedOperation.swift b/Mercury/Sources/DIDCommWrappers/PackEncryptedOperation.swift index 03d602df..9f3c3893 100644 --- a/Mercury/Sources/DIDCommWrappers/PackEncryptedOperation.swift +++ b/Mercury/Sources/DIDCommWrappers/PackEncryptedOperation.swift @@ -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(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 { @@ -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: { @@ -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))) } } diff --git a/Mercury/Sources/DIDCommWrappers/UnpackOperation.swift b/Mercury/Sources/DIDCommWrappers/UnpackOperation.swift index eceabe31..ea399195 100644 --- a/Mercury/Sources/DIDCommWrappers/UnpackOperation.swift +++ b/Mercury/Sources/DIDCommWrappers/UnpackOperation.swift @@ -1,4 +1,5 @@ import Combine +import Core import DIDCommxSwift import Domain import Foundation @@ -6,12 +7,14 @@ import Foundation final class UnpackOperation: OnUnpackResult { private let didcomm: DIDCommProtocol private let castor: Castor + private let logger: PrismLogger private var published = CurrentValueSubject(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 { @@ -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: { @@ -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)) } } diff --git a/Mercury/Sources/MercuryImpl+Public.swift b/Mercury/Sources/MercuryImpl+Public.swift index 146afe03..80baad44 100644 --- a/Mercury/Sources/MercuryImpl+Public.swift +++ b/Mercury/Sources/MercuryImpl+Public.swift @@ -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? { @@ -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, diff --git a/Mercury/Sources/MercuryImpl.swift b/Mercury/Sources/MercuryImpl.swift index 0e2f18f7..3100b54b 100644 --- a/Mercury/Sources/MercuryImpl.swift +++ b/Mercury/Sources/MercuryImpl.swift @@ -1,3 +1,4 @@ +import Core import DIDCommxSwift import Domain import Foundation @@ -8,6 +9,7 @@ public struct MercuryImpl { let apollo: Apollo let pluto: Pluto let didcomm: DidComm + let logger: PrismLogger public init( session: URLSession = .shared, @@ -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, diff --git a/PrismAgent/Sources/ConnectionsManager/ConnectionsManager.swift b/PrismAgent/Sources/ConnectionsManager/ConnectionsManager.swift index 9b0f94e5..11dac634 100644 --- a/PrismAgent/Sources/ConnectionsManager/ConnectionsManager.swift +++ b/PrismAgent/Sources/ConnectionsManager/ConnectionsManager.swift @@ -4,25 +4,21 @@ import Domain import Foundation class ConnectionsManagerImpl: ConnectionsManager { - struct Mediator { - let peerDID: DID - let routingDID: DID - let mediatorDID: DID - } - + let mediationHandler: MediatorHandler private let castor: Castor private let mercury: Mercury private let pluto: Pluto private var pairings = [DIDPair]() - var mediator: Mediator? private var cancellables = [AnyCancellable]() init( castor: Castor, mercury: Mercury, pluto: Pluto, + mediationHandler: MediatorHandler, pairings: [DIDPair] = [] ) { + self.mediationHandler = mediationHandler self.castor = castor self.mercury = mercury self.pluto = pluto @@ -30,32 +26,9 @@ class ConnectionsManagerImpl: ConnectionsManager { } func startMediator() async throws { - let pluto = self.pluto - try await withCheckedThrowingContinuation { [weak self] continuation in - guard let self else { return } - pluto - .getAllMediators() - .first() - .map { $0.first } - .sink(receiveCompletion: { - switch $0 { - case .finished: - continuation.resume(returning: ()) - case let .failure(error): - continuation.resume(throwing: error) - } - }, receiveValue: { - $0.map { - self.mediator = .init( - peerDID: $0.did, - routingDID: $0.routingDID, - mediatorDID: $0.mediatorDID - ) - } - }) - .store(in: &self.cancellables) - } - guard mediator != nil else { throw PrismAgentError.noMediatorAvailableError } + guard + try await mediationHandler.bootRegisteredMediator() != nil + else { throw PrismAgentError.noMediatorAvailableError } } func stopAllEvents() { @@ -94,45 +67,8 @@ class ConnectionsManagerImpl: ConnectionsManager { } } - func registerMediator(hostDID: DID, mediatorDID: DID) async throws { - let mercury = self.mercury - let pluto = self.pluto - - guard - let message: Message = try await mercury - .sendMessageParseMessage(msg: MediationRequest( - from: hostDID, - to: mediatorDID - ).makeMessage()) - else { throw PrismAgentError.mediationRequestFailedError } - - let grantMessage = try MediationGrant(fromMessage: message) - let routingDID = try castor.parseDID(str: grantMessage.body.routingDid) - - try await withCheckedThrowingContinuation { [weak self] continuation in - guard let self else { return } - pluto - .storeMediator( - peer: hostDID, - routingDID: routingDID, - mediatorDID: mediatorDID - ) - .sink(receiveCompletion: { - switch $0 { - case .finished: - self.mediator = .init( - peerDID: hostDID, - routingDID: routingDID, - mediatorDID: mediatorDID - ) - case let .failure(error): - continuation.resume(throwing: error) - } - }, receiveValue: { - continuation.resume(returning: $0) - }) - .store(in: &self.cancellables) - } + func registerMediator(hostDID: DID) async throws { + try await mediationHandler.achieveMediation(host: hostDID) } } @@ -160,28 +96,22 @@ extension ConnectionsManagerImpl: DIDCommConnection { } func awaitMessages() throws -> AnyPublisher<[Message], Error> { - guard let mediator else { throw PrismAgentError.noMediatorAvailableError } - let mercury = self.mercury - let pluto = self.pluto - return getMessagesPublisher(message: try PickUpRequest( - from: mediator.peerDID, - to: mediator.mediatorDID, - body: .init( - limit: "10" - ) - ).makeMessage()) - .flatMap { msg in - Future { - try await PickupRunner(message: msg, mercury: mercury).run() - } + guard mediationHandler.mediator == nil else { throw PrismAgentError.noMediatorAvailableError } + let mediationHandler = mediationHandler + let pluto = pluto + return Future { + try await mediationHandler.pickupUnreadMessages(limit: 10) } .flatMap { messages in pluto - .storeMessages(messages: messages.map { ($0.message, .received) }) + .storeMessages(messages: messages.map { ($0.1, .received) }) .map { messages } } .flatMap { messages in - pickupReceivedMessages(messages: messages, mediator: mediator, mercury: mercury) + Future { + try await mediationHandler.registerMessagesAsRead(ids: messages.map { $0.0 }) + return messages.map { $0.1 } + } } .eraseToAnyPublisher() } @@ -241,26 +171,3 @@ extension ConnectionsManagerImpl: DIDCommConnection { .eraseToAnyPublisher() } } - -private func pickupReceivedMessages( - messages: [(Message, String)], - mediator: ConnectionsManagerImpl.Mediator, - mercury: Mercury -) -> AnyPublisher<[Message], Error> { - if !messages.isEmpty { - return Future { - let message = try PickUpReceived( - from: mediator.peerDID, - to: mediator.mediatorDID, - body: .init(messageIdList: messages.map { $0.1 }) - ).makeMessage() - return try await mercury.sendMessage(msg: message) - } - .map { _ in messages.map { $0.0 } } - .eraseToAnyPublisher() - } else { - return Just(messages.map { $0.0 }) - .tryMap { $0 } - .eraseToAnyPublisher() - } -} diff --git a/PrismAgent/Sources/ConnectionsManager/DIDCommProtocol.swift b/PrismAgent/Sources/ConnectionsManager/DIDCommProtocol.swift index 621bf15a..f3322f77 100644 --- a/PrismAgent/Sources/ConnectionsManager/DIDCommProtocol.swift +++ b/PrismAgent/Sources/ConnectionsManager/DIDCommProtocol.swift @@ -11,5 +11,4 @@ public protocol DIDCommConnection { protocol ConnectionsManager { func addConnection(_ paired: DIDPair) async throws func removeConnection(_ pair: DIDPair) async throws -> DIDPair? - func registerMediator(hostDID: DID, mediatorDID: DID) async throws } diff --git a/PrismAgent/Sources/Mediator/BasicMediatorHandler.swift b/PrismAgent/Sources/Mediator/BasicMediatorHandler.swift new file mode 100644 index 00000000..16f65b19 --- /dev/null +++ b/PrismAgent/Sources/Mediator/BasicMediatorHandler.swift @@ -0,0 +1,125 @@ +import Domain +import Foundation + +public class BasicMediatorHandler: MediatorHandler { + public struct PlutoMediatorStoreImpl: MediatorStore { + let pluto: Pluto + + public init(pluto: Pluto) { + self.pluto = pluto + } + + public func getAllMediators() async throws -> [Mediator] { + try await pluto + .getAllMediators() + .map { $0.map { Mediator( + hostDID: $0.did, + routingDID: $0.routingDID, + mediatorDID: $0.routingDID + )}} + .first() + .await() + } + + public func storeMediator(mediator: Mediator) async throws { + try await pluto + .storeMediator( + peer: mediator.hostDID, + routingDID: mediator.routingDID, + mediatorDID: mediator.mediatorDID + ) + .first() + .await() + } + } + + public let mediatorDID: DID + public private(set) var mediator: Mediator? + + private let mercury: Mercury + private let mediatorStore: MediatorStore + + public init(mediatorDID: DID, mercury: Mercury, store: MediatorStore) { + self.mediatorDID = mediatorDID + self.mercury = mercury + self.mediatorStore = store + } + + public init(mediator: Mediator, mercury: Mercury, store: MediatorStore) { + self.mediatorDID = mediator.mediatorDID + self.mediator = mediator + self.mercury = mercury + self.mediatorStore = store + } + + public func bootRegisteredMediator() async throws -> Mediator? { + guard let mediator else { + self.mediator = try await mediatorStore.getAllMediators().first + return self.mediator + } + return mediator + } + + public func achieveMediation(host: DID) async throws -> Mediator { + guard let mediator = try await bootRegisteredMediator() else { + guard let message: Message = try await mercury + .sendMessageParseMessage(msg: MediationRequest( + from: host, + to: mediatorDID + ).makeMessage()) + else { throw PrismAgentError.mediationRequestFailedError } + + let grantMessage = try MediationGrant(fromMessage: message) + let routingDID = try DID(string: grantMessage.body.routingDid) + + let mediator = Mediator( + hostDID: host, + routingDID: routingDID, + mediatorDID: mediatorDID + ) + + try await mediatorStore.storeMediator(mediator: mediator) + + self.mediator = mediator + return mediator + } + + return mediator + } + + public func updateKeyListWithDIDs(dids: [DID]) async throws { + guard let mediator else { throw PrismAgentError.noMediatorAvailableError } + let keyListUpdateMessage = try MediationKeysUpdateList( + from: mediator.hostDID, + to: mediator.mediatorDID, + recipientDids: dids + ).makeMessage() + try await mercury.sendMessage(msg: keyListUpdateMessage) + } + + public func pickupUnreadMessages(limit: Int) async throws -> [(String, Message)] { + guard let mediator else { throw PrismAgentError.noMediatorAvailableError } + let request = try PickUpRequest( + from: mediator.hostDID, + to: mediator.mediatorDID, + body: .init( + limit: "10" + ) + ).makeMessage() + guard let message = try await mercury.sendMessageParseMessage(msg: request) else { + return [] + } + let parsedMessages = try await PickupRunner(message: message, mercury: mercury).run() + return parsedMessages + } + + public func registerMessagesAsRead(ids: [String]) async throws { + guard let mediator else { throw PrismAgentError.noMediatorAvailableError } + let message = try PickUpReceived( + from: mediator.hostDID, + to: mediator.mediatorDID, + body: .init(messageIdList: ids) + ).makeMessage() + try await mercury.sendMessage(msg: message) + } +} diff --git a/PrismAgent/Sources/Mediator/Mediator.swift b/PrismAgent/Sources/Mediator/Mediator.swift new file mode 100644 index 00000000..f598ab68 --- /dev/null +++ b/PrismAgent/Sources/Mediator/Mediator.swift @@ -0,0 +1,7 @@ +import Domain + +public struct Mediator { + let hostDID: DID + let routingDID: DID + let mediatorDID: DID +} diff --git a/PrismAgent/Sources/Mediator/MediatorHandler.swift b/PrismAgent/Sources/Mediator/MediatorHandler.swift new file mode 100644 index 00000000..309cbb48 --- /dev/null +++ b/PrismAgent/Sources/Mediator/MediatorHandler.swift @@ -0,0 +1,14 @@ +import Domain + +public protocol MediatorHandler { + var mediatorDID: DID { get } + var mediator: Mediator? { get } + + @discardableResult + func bootRegisteredMediator() async throws -> Mediator? + @discardableResult + func achieveMediation(host: DID) async throws -> Mediator + func updateKeyListWithDIDs(dids: [DID]) async throws + func pickupUnreadMessages(limit: Int) async throws -> [(String, Message)] + func registerMessagesAsRead(ids: [String]) async throws +} diff --git a/PrismAgent/Sources/Mediator/MediatorStore.swift b/PrismAgent/Sources/Mediator/MediatorStore.swift new file mode 100644 index 00000000..b4731851 --- /dev/null +++ b/PrismAgent/Sources/Mediator/MediatorStore.swift @@ -0,0 +1,6 @@ +import Domain + +public protocol MediatorStore { + func storeMediator(mediator: Mediator) async throws + func getAllMediators() async throws -> [Mediator] +} diff --git a/PrismAgent/Sources/PrismAgent+Credentials.swift b/PrismAgent/Sources/PrismAgent+Credentials.swift index fc10e7f0..34917d28 100644 --- a/PrismAgent/Sources/PrismAgent+Credentials.swift +++ b/PrismAgent/Sources/PrismAgent+Credentials.swift @@ -9,6 +9,7 @@ public extension PrismAgent { /// - Returns: A publisher that emits an array of `VerifiableCredential` and completes when all the /// credentials are emitted or terminates with an error if any occurs func verifiableCredentials() -> AnyPublisher<[VerifiableCredential], Error> { - pluto.getAllCredentials() + pluto + .getAllCredentials() } } diff --git a/PrismAgent/Sources/PrismAgent+DIDHigherFucntions.swift b/PrismAgent/Sources/PrismAgent+DIDHigherFucntions.swift index 3592bd8b..3e936d28 100644 --- a/PrismAgent/Sources/PrismAgent+DIDHigherFucntions.swift +++ b/PrismAgent/Sources/PrismAgent+DIDHigherFucntions.swift @@ -24,30 +24,29 @@ public extension PrismAgent { let seed = self.seed let apollo = self.apollo let pluto = self.pluto - return try await withCheckedThrowingContinuation { continuation in - pluto - // First get DID info (KeyPathIndex in this case) - .getPrismDIDInfo(did: did) - .tryMap { - // if no register is found throw an error - guard let index = $0?.keyPairIndex else { throw PrismAgentError.cannotFindDIDKeyPairIndex } - // Re-Create the key pair to sign the message - let keyPair = apollo.createKeyPair(seed: seed, curve: .secp256k1(index: index)) - return apollo.signMessage(privateKey: keyPair.privateKey, message: message) + return try await pluto + // First get DID info (KeyPathIndex in this case) + .getPrismDIDInfo(did: did) + .tryMap { [weak self] in + // if no register is found throw an error + guard let index = $0?.keyPairIndex else { + self?.logger.error( + message: """ +Could not find key in storage please use Castor instead and provide the private key +""" + ) + throw PrismAgentError.cannotFindDIDKeyPairIndex } - .first() - .sink(receiveCompletion: { - switch $0 { - case .finished: - break - case let .failure(error): - continuation.resume(throwing: error) - } - }, receiveValue: { - continuation.resume(returning: $0) - }) - .store(in: &self.cancellables) - } + // Re-Create the key pair to sign the message + let keyPair = apollo.createKeyPair(seed: seed, curve: .secp256k1(index: index)) + + self?.logger.debug(message: "Signing message", metadata: [ + .maskedMetadataByLevel(key: "messageB64", value: message.base64Encoded(), level: .debug) + ]) + return apollo.signMessage(privateKey: keyPair.privateKey, message: message) + } + .first() + .await() } /// This method create a new Prism DID, that can be used to identify the agent and interact with other agents. @@ -64,39 +63,47 @@ public extension PrismAgent { let seed = self.seed let apollo = self.apollo let castor = self.castor - let pluto = self.pluto - return try await withCheckedThrowingContinuation { continuation in - pluto - // Retrieve the last keyPath index used - .getPrismLastKeyPairIndex() - .tryMap { - // If the user provided a key path index use it, if not use the last + 1 - let index = keyPathIndex ?? ($0 + 1) - // Create the key pair - let keyPair = apollo.createKeyPair(seed: seed, curve: .secp256k1(index: index)) - let newDID = try castor.createPrismDID(masterPublicKey: keyPair.publicKey, services: services) - return (newDID, index, alias) - } - .flatMap { did, index, alias in - // Store the did and its index path - return pluto - .storePrismDID(did: did, keyPairIndex: index, alias: alias) - .map { did } - } - .first() - .sink(receiveCompletion: { - switch $0 { - case .finished: - break - case let .failure(error): - continuation.resume(throwing: error) - } - }, receiveValue: { - continuation.resume(returning: $0) - }) - .store(in: &self.cancellables) - } + let (newDID, keyPathIndex) = try await pluto + .getPrismLastKeyPairIndex() + .tryMap { [weak self] in + // If the user provided a key path index use it, if not use the last + 1 + let index = keyPathIndex ?? ($0 + 1) + // Create the key pair + let keyPair = apollo.createKeyPair(seed: seed, curve: .secp256k1(index: index)) + let newDID = try castor.createPrismDID(masterPublicKey: keyPair.publicKey, services: services) + self?.logger.debug(message: "Created new Prism DID", metadata: [ + .maskedMetadataByLevel(key: "DID", value: newDID.string, level: .debug), + .maskedMetadataByLevel(key: "keyPathIndex", value: "\(index)", level: .debug) + ]) + return (newDID, index) + } + .first() + .await() + + try await registerPrismDID(did: newDID, keyPathIndex: keyPathIndex, alias: alias) + return newDID + } + + /// This method registers a Prism DID, that can be used to identify the agent and interact with other agents. + /// - Parameters: + /// - did: the DID which will be registered. + /// - keyPathIndex: key path index used to identify the DID + /// - alias: An alias that can be used to identify the DID + /// - Returns: The new created DID + func registerPrismDID( + did: DID, + keyPathIndex: Int, + alias: String? = nil + ) async throws { + logger.debug(message: "Register of DID in storage", metadata: [ + .maskedMetadataByLevel(key: "DID", value: did.string, level: .debug) + ]) + + try await pluto + .storePrismDID(did: did, keyPairIndex: keyPathIndex, alias: alias) + .first() + .await() } /// This function creates a new Peer DID, stores it in pluto database and updates the mediator if requested. @@ -110,53 +117,72 @@ public extension PrismAgent { services: [DIDDocument.Service] = [], updateMediator: Bool ) async throws -> DID { - let apollo = self.apollo - let castor = self.castor - let pluto = self.pluto - let keyAgreementKeyPair = apollo.createKeyPair(seed: seed, curve: .x25519) let authenticationKeyPair = apollo.createKeyPair(seed: seed, curve: .ed25519) - let did = try castor.createPeerDID( + let newDID = try castor.createPeerDID( keyAgreementKeyPair: keyAgreementKeyPair, authenticationKeyPair: authenticationKeyPair, services: services ) - if updateMediator { - guard let mediator = connectionManager.mediator else { - throw PrismAgentError.noMediatorAvailableError - } - let keyListUpdateMessage = try MediationKeysUpdateList( - from: mediator.peerDID, - to: mediator.mediatorDID, - recipientDid: did - ).makeMessage() + logger.debug(message: "Created new Peer DID", metadata: [ + .maskedMetadataByLevel(key: "DID", value: newDID.string, level: .debug) + ]) - try await mercury.sendMessage(msg: keyListUpdateMessage) - } + try await registerPeerDID( + did: newDID, + privateKeys: [ + keyAgreementKeyPair.privateKey, + authenticationKeyPair.privateKey + ], + updateMediator: updateMediator + ) + + return newDID + } - return try await withCheckedThrowingContinuation { continuation in - pluto - .storePeerDID( - did: did, - privateKeys: [ - keyAgreementKeyPair.privateKey, - authenticationKeyPair.privateKey - ]) - .map { did } - .first() - .sink(receiveCompletion: { - switch $0 { - case .finished: - break - case let .failure(error): - continuation.resume(throwing: error) - } - }, receiveValue: { - continuation.resume(returning: $0) - }) - .store(in: &self.cancellables) + /// This function registers a Peer DID, stores it and his private key in pluto database and updates the mediator if requested. + /// + /// - Parameters: + /// - services: The services associated to the new DID. + /// - updateMediator: Indicates if the new DID should be added to the mediator's list. + /// - Returns: A new DID + /// - Throws: PrismAgentError, if updateMediator is true and there is no mediator available or if storing the new DID failed + func registerPeerDID( + did: DID, + privateKeys: [PrivateKey], + updateMediator: Bool + ) async throws { + if updateMediator { + try await updateMediatorWithDID(did: did) } + + logger.debug(message: "Register of DID in storage", metadata: [ + .maskedMetadataByLevel(key: "DID", value: did.string, level: .debug) + ]) + + try await pluto + .storePeerDID( + did: did, + privateKeys: privateKeys + ) + .first() + .await() + } + + /// This function updates the mediator key list with a new DID. + /// + /// - Parameters: + /// - services: The services associated to the new DID. + /// - updateMediator: Indicates if the new DID should be added to the mediator's list. + /// - Returns: A new DID + /// - Throws: PrismAgentError, if updateMediator is true and there is no mediator available + func updateMediatorWithDID(did: DID) async throws { + logger.debug(message: "Update mediator key list with DID", metadata: [ + .maskedMetadataByLevel(key: "DID", value: did.string, level: .debug) + ]) + + try await mediationHandler.updateKeyListWithDIDs(dids: [did]) } } diff --git a/PrismAgent/Sources/PrismAgent+Invitations.swift b/PrismAgent/Sources/PrismAgent+Invitations.swift index ff4bbb26..665b5618 100644 --- a/PrismAgent/Sources/PrismAgent+Invitations.swift +++ b/PrismAgent/Sources/PrismAgent+Invitations.swift @@ -98,9 +98,7 @@ public extension PrismAgent { updateMediator: true ) - logger.info(message: "Created DID", metadata: [ - .publicMetadata(key: "DID", value: ownDID.string) - ]) + logger.info(message: "Sending DIDComm Connection message") let pair = try await DIDCommConnectionRunner( invitationMessage: invitation, diff --git a/PrismAgent/Sources/PrismAgent+MessageEvents.swift b/PrismAgent/Sources/PrismAgent+MessageEvents.swift index 4634ab51..87a069bd 100644 --- a/PrismAgent/Sources/PrismAgent+MessageEvents.swift +++ b/PrismAgent/Sources/PrismAgent+MessageEvents.swift @@ -8,11 +8,14 @@ public extension PrismAgent { func startFetchingMessages() { // Check if messagesStreamTask is nil guard messagesStreamTask == nil else { return } + + logger.info(message: "Start streaming new unread messages") let manager = connectionManager messagesStreamTask = Task { // Keep trying to fetch messages until the task is cancelled while true { do { + logger.debug(message: "Fetching new batch of 10 unread messages") // Wait for new messages to arrive _ = try await manager.awaitMessages() sleep(5) @@ -26,6 +29,7 @@ public extension PrismAgent { /// Stop fetching messages func stopFetchingMessages() { + logger.info(message: "Stop streaming new unread messages") messagesStreamTask?.cancel() } diff --git a/PrismAgent/Sources/PrismAgent.swift b/PrismAgent/Sources/PrismAgent.swift index 1c13cc12..bf3aee93 100644 --- a/PrismAgent/Sources/PrismAgent.swift +++ b/PrismAgent/Sources/PrismAgent.swift @@ -28,24 +28,26 @@ public class PrismAgent { /// The mediator routing DID if one is currently registered. public var mediatorRoutingDID: DID? { - connectionManager.mediator?.routingDID + connectionManager.mediationHandler.mediator?.routingDID } - static let prismMediatorEndpoint = DID(method: "peer", methodId: "other") - let logger = PrismLogger(category: .prismAgent) let apollo: Apollo let castor: Castor let pluto: Pluto let pollux: Pollux let mercury: Mercury - let mediatorServiceEnpoint: DID + let mediationHandler: MediatorHandler var connectionManager: ConnectionsManagerImpl var cancellables = [AnyCancellable]() // Not a "stream" var messagesStreamTask: Task? + public static func setupLogging(logLevels: [LogComponent: LogLevel]) { + PrismLogger.logLevels = logLevels + } + /// Initializes a PrismAgent with the given dependency objects and seed data. /// /// - Parameters: @@ -62,8 +64,8 @@ public class PrismAgent { pluto: Pluto, pollux: Pollux, mercury: Mercury, - seed: Seed? = nil, - mediatorServiceEnpoint: DID? = nil + mediationHandler: MediatorHandler, + seed: Seed? = nil ) { self.apollo = apollo self.castor = castor @@ -71,11 +73,12 @@ public class PrismAgent { self.pollux = pollux self.mercury = mercury self.seed = seed ?? apollo.createRandomSeed().seed - self.mediatorServiceEnpoint = mediatorServiceEnpoint ?? Self.prismMediatorEndpoint + self.mediationHandler = mediationHandler self.connectionManager = ConnectionsManagerImpl( castor: castor, mercury: mercury, pluto: pluto, + mediationHandler: mediationHandler, pairings: [] ) } @@ -87,24 +90,32 @@ public class PrismAgent { - seedData: Optional seed data for creating a new seed. If not provided, a random seed will be generated. - mediatorServiceEnpoint: Optional DID representing the service endpoint of the mediator. If not provided, the default Prism mediator endpoint will be used. */ - public convenience init(seedData: Data? = nil, mediatorServiceEnpoint: DID? = nil) { + public convenience init( + seedData: Data? = nil, + mediatorDID: DID + ) { let apollo = ApolloBuilder().build() let castor = CastorBuilder(apollo: apollo).build() let pluto = PlutoBuilder().build() let pollux = PolluxBuilder(castor: castor).build() + let mercury = MercuryBuilder( + apollo: apollo, + castor: castor, + pluto: pluto + ).build() let seed = seedData.map { Seed(value: $0) } ?? apollo.createRandomSeed().seed self.init( apollo: apollo, castor: castor, pluto: pluto, pollux: pollux, - mercury: MercuryBuilder( - apollo: apollo, - castor: castor, - pluto: pluto - ).build(), - seed: seed, - mediatorServiceEnpoint: mediatorServiceEnpoint ?? Self.prismMediatorEndpoint + mercury: mercury, + mediationHandler: BasicMediatorHandler( + mediatorDID: mediatorDID, + mercury: mercury, + store: BasicMediatorHandler.PlutoMediatorStoreImpl(pluto: pluto) + ), + seed: seed ) } @@ -115,29 +126,28 @@ public class PrismAgent { as well as any error thrown by `createNewPeerDID` and `connectionManager.registerMediator` */ public func start() async throws { - guard state == .stoped else { return } - state = .starting - do { - try await connectionManager.startMediator() - } catch PrismAgentError.noMediatorAvailableError { - let hostDID = try await createNewPeerDID( - services: [.init( - id: "#didcomm-1", - type: ["DIDCommMessaging"], - serviceEndpoint:.init(uri: mediatorServiceEnpoint.string)) - ], - updateMediator: false - ) - try await connectionManager.registerMediator( - hostDID: hostDID, - mediatorDID: mediatorServiceEnpoint - ) - } - state = .running - logger.info(message: "Mediation Achieved", metadata: [ - .publicMetadata(key: "Routing DID", value: mediatorRoutingDID?.string ?? "") - ]) + guard state == .stoped else { return } + logger.info(message: "Starting agent") + state = .starting + do { + try await connectionManager.startMediator() + } catch PrismAgentError.noMediatorAvailableError { + let hostDID = try await createNewPeerDID( + services: [.init( + id: "#didcomm-1", + type: ["DIDCommMessaging"], + serviceEndpoint:.init(uri: mediationHandler.mediatorDID.string)) + ], + updateMediator: false + ) + try await connectionManager.registerMediator(hostDID: hostDID) } + state = .running + logger.info(message: "Mediation Achieved", metadata: [ + .publicMetadata(key: "Routing DID", value: mediatorRoutingDID?.string ?? "") + ]) + logger.info(message: "Agent running") + } /** This function is used to stop the PrismAgent. @@ -149,10 +159,12 @@ public class PrismAgent { */ public func stop() async throws { guard state == .running else { return } + logger.info(message: "Stoping agent") state = .stoping cancellables.forEach { $0.cancel() } connectionManager.stopAllEvents() state = .stoped + logger.info(message: "Agent not running") } // TODO: This is to be deleted in the future. For now it helps with proof of request logic diff --git a/PrismAgent/Sources/Protocols/Mediation/MediationKeysUpdateList.swift b/PrismAgent/Sources/Protocols/Mediation/MediationKeysUpdateList.swift index 2ae520cf..f0a626e9 100644 --- a/PrismAgent/Sources/Protocols/Mediation/MediationKeysUpdateList.swift +++ b/PrismAgent/Sources/Protocols/Mediation/MediationKeysUpdateList.swift @@ -21,15 +21,15 @@ struct MediationKeysUpdateList { id: String = UUID().uuidString, from: DID, to: DID, - recipientDid: DID + recipientDids: [DID] ) { self.id = id self.from = from self.to = to self.body = .init( - updates: [.init( - recipientDid: recipientDid.string - )] + updates: recipientDids.map { + Body.Update(recipientDid: $0.string) + } ) } diff --git a/PrismAgent/Sources/Protocols/Pickup/PickupRunner.swift b/PrismAgent/Sources/Protocols/Pickup/PickupRunner.swift index 9ee44142..8339f4af 100644 --- a/PrismAgent/Sources/Protocols/Pickup/PickupRunner.swift +++ b/PrismAgent/Sources/Protocols/Pickup/PickupRunner.swift @@ -22,7 +22,7 @@ class PickupRunner { self.mercury = mercury } - func run() async throws -> [(message: Message, attachmentId: String)] { + func run() async throws -> [(attachmentId: String, message: Message)] { switch message { case let .delivery(message): return try await message.attachments.compactMap { attachment in @@ -36,7 +36,7 @@ class PickupRunner { } } .asyncMap { messageString, id in - (try await mercury.unpackMessage(msg: messageString), id) + (id, try await mercury.unpackMessage(msg: messageString)) } case .status: return [] diff --git a/PrismAgent/Tests/CheckTest.swift b/PrismAgent/Tests/CheckTest.swift index 8c7bc610..28df8187 100644 --- a/PrismAgent/Tests/CheckTest.swift +++ b/PrismAgent/Tests/CheckTest.swift @@ -30,8 +30,7 @@ final class CheckTests: XCTestCase { func testOOB() async throws { let oob = "https://mediator.rootsid.cloud?_oob=eyJ0eXBlIjoiaHR0cHM6Ly9kaWRjb21tLm9yZy9vdXQtb2YtYmFuZC8yLjAvaW52aXRhdGlvbiIsImlkIjoiNzk0Mjc4MzctY2MwNi00ODUzLWJiMzktNjg2ZWFjM2U2YjlhIiwiZnJvbSI6ImRpZDpwZWVyOjIuRXo2TFNtczU1NVloRnRobjFXVjhjaURCcFptODZoSzl0cDgzV29qSlVteFBHazFoWi5WejZNa21kQmpNeUI0VFM1VWJiUXc1NHN6bTh5dk1NZjFmdEdWMnNRVllBeGFlV2hFLlNleUpwWkNJNkltNWxkeTFwWkNJc0luUWlPaUprYlNJc0luTWlPaUpvZEhSd2N6b3ZMMjFsWkdsaGRHOXlMbkp2YjNSemFXUXVZMnh2ZFdRaUxDSmhJanBiSW1ScFpHTnZiVzB2ZGpJaVhYMCIsImJvZHkiOnsiZ29hbF9jb2RlIjoicmVxdWVzdC1tZWRpYXRlIiwiZ29hbCI6IlJlcXVlc3RNZWRpYXRlIiwibGFiZWwiOiJNZWRpYXRvciIsImFjY2VwdCI6WyJkaWRjb21tL3YyIl19fQ" - - let agent = PrismAgent() + let agent = PrismAgent(mediatorDID: DID(method: "peer", methodId: "123")) let parsed = try await agent.parseInvitation(str: oob) print(parsed) diff --git a/Sample/AtalaPrismWalletDemo/AtalaPrismWalletDemo.xcodeproj/project.pbxproj b/Sample/AtalaPrismWalletDemo/AtalaPrismWalletDemo.xcodeproj/project.pbxproj index f5cba1ce..3ea2946d 100644 --- a/Sample/AtalaPrismWalletDemo/AtalaPrismWalletDemo.xcodeproj/project.pbxproj +++ b/Sample/AtalaPrismWalletDemo/AtalaPrismWalletDemo.xcodeproj/project.pbxproj @@ -832,7 +832,6 @@ INFOPLIST_FILE = AtalaPrismWalletDemo/Info.plist; INFOPLIST_KEY_CFBundleDisplayName = "Atala PRISM Wallet Demo"; INFOPLIST_KEY_NSCameraUsageDescription = NSCameraUsageDescription; - INFOPLIST_KEY_UIApplicationSceneManifest_Generation = YES; INFOPLIST_KEY_UIApplicationSupportsIndirectInputEvents = YES; INFOPLIST_KEY_UILaunchScreen_Generation = YES; INFOPLIST_KEY_UISupportedInterfaceOrientations_iPad = "UIInterfaceOrientationPortrait UIInterfaceOrientationPortraitUpsideDown UIInterfaceOrientationLandscapeLeft UIInterfaceOrientationLandscapeRight"; @@ -864,7 +863,6 @@ INFOPLIST_FILE = AtalaPrismWalletDemo/Info.plist; INFOPLIST_KEY_CFBundleDisplayName = "Atala PRISM Wallet Demo"; INFOPLIST_KEY_NSCameraUsageDescription = NSCameraUsageDescription; - INFOPLIST_KEY_UIApplicationSceneManifest_Generation = YES; INFOPLIST_KEY_UIApplicationSupportsIndirectInputEvents = YES; INFOPLIST_KEY_UILaunchScreen_Generation = YES; INFOPLIST_KEY_UISupportedInterfaceOrientations_iPad = "UIInterfaceOrientationPortrait UIInterfaceOrientationPortraitUpsideDown UIInterfaceOrientationLandscapeLeft UIInterfaceOrientationLandscapeRight"; diff --git a/Sample/AtalaPrismWalletDemo/AtalaPrismWalletDemo/Info.plist b/Sample/AtalaPrismWalletDemo/AtalaPrismWalletDemo/Info.plist index 14242ae0..67e5f5ef 100644 --- a/Sample/AtalaPrismWalletDemo/AtalaPrismWalletDemo/Info.plist +++ b/Sample/AtalaPrismWalletDemo/AtalaPrismWalletDemo/Info.plist @@ -20,5 +20,12 @@ NSAllowsArbitraryLoads + UIApplicationSceneManifest + + UIApplicationSupportsMultipleScenes + + UISceneConfigurations + + diff --git a/Sample/AtalaPrismWalletDemo/AtalaPrismWalletDemo/Modules/DID/DIDFuncionalitiesViewModel.swift b/Sample/AtalaPrismWalletDemo/AtalaPrismWalletDemo/Modules/DID/DIDFuncionalitiesViewModel.swift index bc64b2be..5658011c 100644 --- a/Sample/AtalaPrismWalletDemo/AtalaPrismWalletDemo/Modules/DID/DIDFuncionalitiesViewModel.swift +++ b/Sample/AtalaPrismWalletDemo/AtalaPrismWalletDemo/Modules/DID/DIDFuncionalitiesViewModel.swift @@ -11,7 +11,7 @@ final class DIDFuncionalitiesViewModel: ObservableObject { self.castor = CastorBuilder( apollo: ApolloBuilder().build() ).build() - self.agent = PrismAgent() + self.agent = PrismAgent(mediatorDID: DID(method: "peer", methodId: "123")) } @Published var createdDID: DID? diff --git a/Sample/AtalaPrismWalletDemo/AtalaPrismWalletDemo/Modules/SetupPrismAgent/SetupPrismAgentViewModel.swift b/Sample/AtalaPrismWalletDemo/AtalaPrismWalletDemo/Modules/SetupPrismAgent/SetupPrismAgentViewModel.swift index 61f590df..6153d496 100644 --- a/Sample/AtalaPrismWalletDemo/AtalaPrismWalletDemo/Modules/SetupPrismAgent/SetupPrismAgentViewModel.swift +++ b/Sample/AtalaPrismWalletDemo/AtalaPrismWalletDemo/Modules/SetupPrismAgent/SetupPrismAgentViewModel.swift @@ -17,7 +17,7 @@ final class SetupPrismAgentViewModelImpl: ObservableObject, SetupPrismAgentViewM init() { let did = try! DID(string: "did:peer:2.Ez6LSms555YhFthn1WV8ciDBpZm86hK9tp83WojJUmxPGk1hZ.Vz6MkmdBjMyB4TS5UbbQw54szm8yvMMf1ftGV2sQVYAxaeWhE.SeyJpZCI6Im5ldy1pZCIsInQiOiJkbSIsInMiOiJodHRwczovL21lZGlhdG9yLnJvb3RzaWQuY2xvdWQiLCJhIjpbImRpZGNvbW0vdjIiXX0") - self.agent = PrismAgent(mediatorServiceEnpoint: did) + self.agent = PrismAgent(mediatorDID: did) status = agent.state.rawValue } diff --git a/Sample/AtalaPrismWalletDemo/AtalaPrismWalletDemo/Modules/SigningVerification/SigningVerificationViewModel.swift b/Sample/AtalaPrismWalletDemo/AtalaPrismWalletDemo/Modules/SigningVerification/SigningVerificationViewModel.swift index 6afb8acf..7ee9efa0 100644 --- a/Sample/AtalaPrismWalletDemo/AtalaPrismWalletDemo/Modules/SigningVerification/SigningVerificationViewModel.swift +++ b/Sample/AtalaPrismWalletDemo/AtalaPrismWalletDemo/Modules/SigningVerification/SigningVerificationViewModel.swift @@ -11,7 +11,7 @@ final class SigningVerificationViewModel: ObservableObject { self.castor = CastorBuilder( apollo: ApolloBuilder().build() ).build() - self.agent = PrismAgent() + self.agent = PrismAgent(mediatorDID: DID(method: "peer", methodId: "1234")) } @Published var createdDID: DID? diff --git a/Sample/AtalaPrismWalletDemo/AtalaPrismWalletDemo/Modules/WalletDemo/Main/MainViewModel.swift b/Sample/AtalaPrismWalletDemo/AtalaPrismWalletDemo/Modules/WalletDemo/Main/MainViewModel.swift index 13c1134b..d6169946 100644 --- a/Sample/AtalaPrismWalletDemo/AtalaPrismWalletDemo/Modules/WalletDemo/Main/MainViewModel.swift +++ b/Sample/AtalaPrismWalletDemo/AtalaPrismWalletDemo/Modules/WalletDemo/Main/MainViewModel.swift @@ -28,7 +28,9 @@ final class MainViewModelImpl: MainViewModel { func startWithMediatorOOB() { do { - let invitation = try PrismAgent().parseOOBInvitation(url: oobString) + let invitation = try PrismAgent( + mediatorDID: DID(method: "peer", methodId: "123") + ).parseOOBInvitation(url: oobString) self.router.didOnUse = try DID(string: invitation.from) self.routeToDashboard = true } catch { diff --git a/Sample/AtalaPrismWalletDemo/AtalaPrismWalletDemo/Modules/WalletDemo/Main/MainViewRouter.swift b/Sample/AtalaPrismWalletDemo/AtalaPrismWalletDemo/Modules/WalletDemo/Main/MainViewRouter.swift index 2d58bf9b..6027fe2a 100644 --- a/Sample/AtalaPrismWalletDemo/AtalaPrismWalletDemo/Modules/WalletDemo/Main/MainViewRouter.swift +++ b/Sample/AtalaPrismWalletDemo/AtalaPrismWalletDemo/Modules/WalletDemo/Main/MainViewRouter.swift @@ -12,7 +12,7 @@ final class MainViewRouterImpl: MainViewRouter { } func routeToDashboard() -> some View { - let agent = PrismAgent(mediatorServiceEnpoint: didOnUse) + let agent = PrismAgent(mediatorDID: didOnUse) let container = DIContainerImpl() container.register(type: PrismAgent.self, component: agent) return DashboardBuilder().build(component: .init(container: container))