diff --git a/Example/IntegrationTests/Chat/ChatTests.swift b/Example/IntegrationTests/Chat/ChatTests.swift index 03d9be7be..4c305a88f 100644 --- a/Example/IntegrationTests/Chat/ChatTests.swift +++ b/Example/IntegrationTests/Chat/ChatTests.swift @@ -29,8 +29,8 @@ final class ChatTests: XCTestCase { let inviteExpectation = expectation(description: "invitation expectation") let inviteeAccount = Account(chainIdentifier: "eip155:1", address: "0x3627523167367216556273151")! let inviterAccount = Account(chainIdentifier: "eip155:1", address: "0x36275231673672234423f")! - let pubKey = try! await invitee.register(account: inviteeAccount) - try! await inviter.invite(publicKey: pubKey, peerAccount: inviteeAccount, openingMessage: "", account: inviterAccount) + try! await invitee.register(account: inviteeAccount) + try! await inviter.invite(peerAccount: inviteeAccount, openingMessage: "", account: inviterAccount) invitee.invitePublisher.sink { _ in inviteExpectation.fulfill() }.store(in: &publishers) @@ -44,9 +44,8 @@ final class ChatTests: XCTestCase { let inviterAccount = Account(chainIdentifier: "eip155:1", address: "0x36275231673672234423f")! Task(priority: .high) { - let pubKey = try! await invitee.register(account: inviteeAccount) - - try! await inviter.invite(publicKey: pubKey, peerAccount: inviteeAccount, openingMessage: "opening message", account: inviterAccount) + try! await invitee.register(account: inviteeAccount) + try! await inviter.invite(peerAccount: inviteeAccount, openingMessage: "opening message", account: inviterAccount) } invitee.invitePublisher.sink { [unowned self] invite in @@ -72,8 +71,8 @@ final class ChatTests: XCTestCase { let inviterAccount = Account(chainIdentifier: "eip155:1", address: "0x36275231673672234423f")! Task(priority: .high) { - let pubKey = try! await invitee.register(account: inviteeAccount) - try! await inviter.invite(publicKey: pubKey, peerAccount: inviteeAccount, openingMessage: "opening message", account: inviterAccount) + try! await invitee.register(account: inviteeAccount) + try! await inviter.invite(peerAccount: inviteeAccount, openingMessage: "opening message", account: inviterAccount) } invitee.invitePublisher.sink { [unowned self] invite in diff --git a/Example/Showcase/Classes/DomainLayer/Chat/ChatService.swift b/Example/Showcase/Classes/DomainLayer/Chat/ChatService.swift index a98e1902e..991c06c66 100644 --- a/Example/Showcase/Classes/DomainLayer/Chat/ChatService.swift +++ b/Example/Showcase/Classes/DomainLayer/Chat/ChatService.swift @@ -53,8 +53,8 @@ final class ChatService { try await client.reject(inviteId: invite.id) } - func invite(peerPubkey publicKey: String, peerAccount: Account, message: String, selfAccount: Account) async throws { - try await client.invite(publicKey: publicKey, peerAccount: peerAccount, openingMessage: message, account: selfAccount) + func invite(peerAccount: Account, message: String, selfAccount: Account) async throws { + try await client.invite(peerAccount: peerAccount, openingMessage: message, account: selfAccount) } func register(account: Account) async throws { diff --git a/Example/Showcase/Classes/PresentationLayer/Chat/Invite/InviteInteractor.swift b/Example/Showcase/Classes/PresentationLayer/Chat/Invite/InviteInteractor.swift index e51575584..26af501c6 100644 --- a/Example/Showcase/Classes/PresentationLayer/Chat/Invite/InviteInteractor.swift +++ b/Example/Showcase/Classes/PresentationLayer/Chat/Invite/InviteInteractor.swift @@ -6,7 +6,6 @@ final class InviteInteractor { } func invite(peerAccount: Account, message: String, selfAccount: Account) async { - let publicKey = try! await chatService.resolve(account: peerAccount) - try! await chatService.invite(peerPubkey: publicKey, peerAccount: peerAccount, message: message, selfAccount: selfAccount) + try! await chatService.invite(peerAccount: peerAccount, message: message, selfAccount: selfAccount) } } diff --git a/Sources/Chat/ChatClient.swift b/Sources/Chat/ChatClient.swift index 19b3fc550..fdae06f01 100644 --- a/Sources/Chat/ChatClient.swift +++ b/Sources/Chat/ChatClient.swift @@ -73,6 +73,7 @@ public class ChatClient { /// record is a blockchain account with a client generated public key /// - Parameter account: CAIP10 blockchain account /// - Returns: public key + @discardableResult public func register(account: Account) async throws -> String { try await registryService.register(account: account) } @@ -89,8 +90,8 @@ public class ChatClient { /// - publicKey: publicKey associated with a peer /// - openingMessage: oppening message for a chat invite /// TODO - peerAccount should be derived - public func invite(publicKey: String, peerAccount: Account, openingMessage: String, account: Account) async throws { - try await inviteService.invite(peerPubKey: publicKey, peerAccount: peerAccount, openingMessage: openingMessage, account: account) + public func invite(peerAccount: Account, openingMessage: String, account: Account) async throws { + try await inviteService.invite(peerAccount: peerAccount, openingMessage: openingMessage, account: account) } public func accept(inviteId: String) async throws { diff --git a/Sources/Chat/ChatClientFactory.swift b/Sources/Chat/ChatClientFactory.swift index 7d52dab90..538040bbd 100644 --- a/Sources/Chat/ChatClientFactory.swift +++ b/Sources/Chat/ChatClientFactory.swift @@ -35,7 +35,7 @@ public struct ChatClientFactory { let threadStore = Database(keyValueStorage: keyValueStorage, identifier: ChatStorageIdentifiers.threads.rawValue) let resubscriptionService = ResubscriptionService(networkingInteractor: networkingInteractor, threadStore: threadStore, logger: logger) let invitationHandlingService = InvitationHandlingService(registry: registry, networkingInteractor: networkingInteractor, kms: kms, logger: logger, topicToRegistryRecordStore: topicToRegistryRecordStore, invitePayloadStore: invitePayloadStore, threadsStore: threadStore) - let inviteService = InviteService(networkingInteractor: networkingInteractor, kms: kms, threadStore: threadStore, rpcHistory: rpcHistory, logger: logger) + let inviteService = InviteService(networkingInteractor: networkingInteractor, kms: kms, threadStore: threadStore, rpcHistory: rpcHistory, logger: logger, registry: registry) let leaveService = LeaveService() let messagesStore = Database(keyValueStorage: keyValueStorage, identifier: ChatStorageIdentifiers.messages.rawValue) let messagingService = MessagingService(networkingInteractor: networkingInteractor, messagesStore: messagesStore, threadStore: threadStore, logger: logger) diff --git a/Sources/Chat/ProtocolServices/Inviter/InviteService.swift b/Sources/Chat/ProtocolServices/Inviter/InviteService.swift index 9568bfac4..9e88cc815 100644 --- a/Sources/Chat/ProtocolServices/Inviter/InviteService.swift +++ b/Sources/Chat/ProtocolServices/Inviter/InviteService.swift @@ -12,31 +12,37 @@ class InviteService { private let kms: KeyManagementService private let threadStore: Database private let rpcHistory: RPCHistory + private let registry: Registry var onNewThread: ((Thread) -> Void)? var onInvite: ((Invite) -> Void)? - init(networkingInteractor: NetworkInteracting, - kms: KeyManagementService, - threadStore: Database, - rpcHistory: RPCHistory, - logger: ConsoleLogging) { + init( + networkingInteractor: NetworkInteracting, + kms: KeyManagementService, + threadStore: Database, + rpcHistory: RPCHistory, + logger: ConsoleLogging, + registry: Registry + ) { self.kms = kms self.networkingInteractor = networkingInteractor self.logger = logger self.threadStore = threadStore self.rpcHistory = rpcHistory + self.registry = registry setUpResponseHandling() } var peerAccount: Account! - func invite(peerPubKey: String, peerAccount: Account, openingMessage: String, account: Account) async throws { + func invite(peerAccount: Account, openingMessage: String, account: Account) async throws { // TODO ad storage let protocolMethod = ChatInviteProtocolMethod() self.peerAccount = peerAccount let selfPubKeyY = try kms.createX25519KeyPair() let invite = Invite(message: openingMessage, account: account, publicKey: selfPubKeyY.hexRepresentation) + let peerPubKey = try await registry.resolve(account: peerAccount) let symKeyI = try kms.performKeyAgreement(selfPublicKey: selfPubKeyY, peerPublicKey: peerPubKey) let inviteTopic = try AgreementPublicKey(hex: peerPubKey).rawRepresentation.sha256().toHexString()