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] #337 fix hardcoded self account #338

Merged
merged 2 commits into from
Jul 15, 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
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ private extension ChatListPresenter {
func loadThreads() async {
let threads = await interactor.getThreads()
self.threads = threads
// .filter { $0.selfAccount == account }
.filter { $0.selfAccount == account }
.sorted(by: { $0.topic < $1.topic })
.map { ThreadViewModel(thread: $0) }
}
Expand Down
6 changes: 3 additions & 3 deletions Sources/Chat/ChatClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public class ChatClient {
kms: KeyManagementService,
logger: ConsoleLogging = ConsoleLogger(loggingLevel: .debug),
keyValueStorage: KeyValueStorage) {
let topicToInvitationPubKeyStore = CodableStore<String>(defaults: keyValueStorage, identifier: StorageDomainIdentifiers.topicToInvitationPubKey.rawValue)
let topicToRegistryRecordStore = CodableStore<RegistryRecord>(defaults: keyValueStorage, identifier: StorageDomainIdentifiers.topicToInvitationPubKey.rawValue)
self.registry = registry
self.kms = kms
let serialiser = Serializer(kms: kms)
Expand All @@ -51,14 +51,14 @@ public class ChatClient {
logger: logger,
jsonRpcHistory: jsonRpcHistory)
self.invitePayloadStore = CodableStore<RequestSubscriptionPayload>(defaults: keyValueStorage, identifier: StorageDomainIdentifiers.invite.rawValue)
self.registryService = RegistryService(registry: registry, networkingInteractor: networkingInteractor, kms: kms, logger: logger, topicToInvitationPubKeyStore: topicToInvitationPubKeyStore)
self.registryService = RegistryService(registry: registry, networkingInteractor: networkingInteractor, kms: kms, logger: logger, topicToRegistryRecordStore: topicToRegistryRecordStore)
threadStore = Database<Thread>(keyValueStorage: keyValueStorage, identifier: StorageDomainIdentifiers.threads.rawValue)
self.resubscriptionService = ResubscriptionService(networkingInteractor: networkingInteractor, threadStore: threadStore, logger: logger)
self.invitationHandlingService = InvitationHandlingService(registry: registry,
networkingInteractor: networkingInteractor,
kms: kms,
logger: logger,
topicToInvitationPubKeyStore: topicToInvitationPubKeyStore,
topicToRegistryRecordStore: topicToRegistryRecordStore,
invitePayloadStore: invitePayloadStore,
threadsStore: threadStore)
self.inviteService = InviteService(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class InvitationHandlingService {
var onNewThread: ((Thread) -> Void)?
private let networkingInteractor: NetworkInteracting
private let invitePayloadStore: CodableStore<(RequestSubscriptionPayload)>
private let topicToInvitationPubKeyStore: CodableStore<String>
private let topicToRegistryRecordStore: CodableStore<RegistryRecord>
private let registry: Registry
private let logger: ConsoleLogging
private let kms: KeyManagementService
Expand All @@ -23,14 +23,14 @@ class InvitationHandlingService {
networkingInteractor: NetworkInteracting,
kms: KeyManagementService,
logger: ConsoleLogging,
topicToInvitationPubKeyStore: CodableStore<String>,
topicToRegistryRecordStore: CodableStore<RegistryRecord>,
invitePayloadStore: CodableStore<RequestSubscriptionPayload>,
threadsStore: Database<Thread>) {
self.registry = registry
self.kms = kms
self.networkingInteractor = networkingInteractor
self.logger = logger
self.topicToInvitationPubKeyStore = topicToInvitationPubKeyStore
self.topicToRegistryRecordStore = topicToRegistryRecordStore
self.invitePayloadStore = invitePayloadStore
self.threadsStore = threadsStore
setUpRequestHandling()
Expand Down Expand Up @@ -63,7 +63,7 @@ class InvitationHandlingService {
logger.debug("Accepting an invite on topic: \(threadTopic)")

// TODO - derive account
let selfAccount = Account("eip155:56:0xe5EeF1368781911d265fDB6946613dA61915a501")!
let selfAccount = try! topicToRegistryRecordStore.get(key: payload.topic)!.account
let thread = Thread(topic: threadTopic, selfAccount: selfAccount, peerAccount: invite.account)
await threadsStore.add(thread)

Expand Down Expand Up @@ -113,12 +113,12 @@ class InvitationHandlingService {
private func getInviteResponseTopic(_ payload: RequestSubscriptionPayload, _ invite: Invite) throws -> String {
// todo - remove topicToInvitationPubKeyStore ?

guard let selfPubKeyHex = try? topicToInvitationPubKeyStore.get(key: payload.topic) else {
guard let record = try? topicToRegistryRecordStore.get(key: payload.topic) else {
logger.debug("PubKey for invitation topic not found")
fatalError("todo")
}

let selfPubKey = try AgreementPublicKey(hex: selfPubKeyHex)
let selfPubKey = try AgreementPublicKey(hex: record.pubKey)

let agreementKeysI = try kms.performKeyAgreement(selfPublicKey: selfPubKey, peerPublicKey: invite.publicKey)

Expand Down
15 changes: 10 additions & 5 deletions Sources/Chat/ProtocolServices/Invitee/RegistryService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import WalletConnectKMS

actor RegistryService {
let networkingInteractor: NetworkInteracting
let topicToInvitationPubKeyStore: CodableStore<String>
let topicToRegistryRecordStore: CodableStore<RegistryRecord>
let registry: Registry
let logger: ConsoleLogging
let kms: KeyManagementServiceProtocol
Expand All @@ -13,12 +13,12 @@ actor RegistryService {
networkingInteractor: NetworkInteracting,
kms: KeyManagementServiceProtocol,
logger: ConsoleLogging,
topicToInvitationPubKeyStore: CodableStore<String>) {
topicToRegistryRecordStore: CodableStore<RegistryRecord>) {
self.registry = registry
self.kms = kms
self.networkingInteractor = networkingInteractor
self.logger = logger
self.topicToInvitationPubKeyStore = topicToInvitationPubKeyStore
self.topicToRegistryRecordStore = topicToRegistryRecordStore
}

func register(account: Account) async throws -> String {
Expand All @@ -27,10 +27,15 @@ actor RegistryService {
try await registry.register(account: account, pubKey: pubKeyHex)
let topic = pubKey.rawRepresentation.sha256().toHexString()
try kms.setPublicKey(publicKey: pubKey, for: topic)

topicToInvitationPubKeyStore.set(pubKeyHex, forKey: topic)
let record = RegistryRecord(account: account, pubKey: pubKeyHex)
topicToRegistryRecordStore.set(record, forKey: topic)
try await networkingInteractor.subscribe(topic: topic)
logger.debug("Did register an account: \(account) and is subscribing on topic: \(topic)")
return pubKeyHex
}
}

struct RegistryRecord: Codable {
let account: Account
let pubKey: String
}
4 changes: 3 additions & 1 deletion Tests/ChatTests/Mocks/NetworkingInteractorMock.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@ import WalletConnectRelay

class NetworkingInteractorMock: NetworkInteracting {
var socketConnectionStatusPublisher: AnyPublisher<SocketConnectionStatus, Never> {
fatalError()
socketConnectionStatusPublisherSubject.eraseToAnyPublisher()
}
let socketConnectionStatusPublisherSubject = PassthroughSubject<SocketConnectionStatus, Never>()

let responsePublisherSubject = PassthroughSubject<ChatResponse, Never>()
let requestPublisherSubject = PassthroughSubject<RequestSubscriptionPayload, Never>()

Expand Down
8 changes: 4 additions & 4 deletions Tests/ChatTests/RegistryManagerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,21 @@ import WalletConnectUtils
final class RegistryManagerTests: XCTestCase {
var registryManager: RegistryService!
var networkingInteractor: NetworkingInteractorMock!
var topicToInvitationPubKeyStore: CodableStore<String>!
var topicToRegistryRecordStore: CodableStore<RegistryRecord>!
var registry: Registry!
var kms: KeyManagementServiceMock!

override func setUp() {
registry = KeyValueRegistry()
networkingInteractor = NetworkingInteractorMock()
kms = KeyManagementServiceMock()
topicToInvitationPubKeyStore = CodableStore(defaults: RuntimeKeyValueStorage(), identifier: "")
topicToRegistryRecordStore = CodableStore(defaults: RuntimeKeyValueStorage(), identifier: "")
registryManager = RegistryService(
registry: registry,
networkingInteractor: networkingInteractor,
kms: kms,
logger: ConsoleLoggerMock(),
topicToInvitationPubKeyStore: topicToInvitationPubKeyStore)
topicToRegistryRecordStore: topicToRegistryRecordStore)
}

func testRegister() async {
Expand All @@ -31,6 +31,6 @@ final class RegistryManagerTests: XCTestCase {
XCTAssert(!networkingInteractor.subscriptions.isEmpty, "networkingInteractors subscribes to new topic")
let resolved = try! await registry.resolve(account: account)
XCTAssertNotNil(resolved, "register account is resolvable")
XCTAssertFalse(topicToInvitationPubKeyStore.getAll().isEmpty, "stores topic to invitation")
XCTAssertFalse(topicToRegistryRecordStore.getAll().isEmpty, "stores topic to invitation")
}
}