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

[Chat] PubKey removed from invite #557

Merged
merged 1 commit into from
Oct 28, 2022
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
13 changes: 6 additions & 7 deletions Example/IntegrationTests/Chat/ChatTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions Example/Showcase/Classes/DomainLayer/Chat/ChatService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
5 changes: 3 additions & 2 deletions Sources/Chat/ChatClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand All @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion Sources/Chat/ChatClientFactory.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public struct ChatClientFactory {
let threadStore = Database<Thread>(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<Message>(keyValueStorage: keyValueStorage, identifier: ChatStorageIdentifiers.messages.rawValue)
let messagingService = MessagingService(networkingInteractor: networkingInteractor, messagesStore: messagesStore, threadStore: threadStore, logger: logger)
Expand Down
18 changes: 12 additions & 6 deletions Sources/Chat/ProtocolServices/Inviter/InviteService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,31 +12,37 @@ class InviteService {
private let kms: KeyManagementService
private let threadStore: Database<Thread>
private let rpcHistory: RPCHistory
private let registry: Registry

var onNewThread: ((Thread) -> Void)?
var onInvite: ((Invite) -> Void)?

init(networkingInteractor: NetworkInteracting,
kms: KeyManagementService,
threadStore: Database<Thread>,
rpcHistory: RPCHistory,
logger: ConsoleLogging) {
init(
networkingInteractor: NetworkInteracting,
kms: KeyManagementService,
threadStore: Database<Thread>,
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()

Expand Down