-
Notifications
You must be signed in to change notification settings - Fork 172
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/registry #262
#chat/registry #262
Changes from all commits
46aac86
ff48341
bf61703
57c872d
f30f1f7
ce2d6d6
2e61a6d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
|
||
import Foundation | ||
import WalletConnectUtils | ||
import WalletConnectKMS | ||
|
||
actor RegistryManager { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What the difference between Manager and Engine naming? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I am always scratching my head when giving them a names. in this context it just sounds better to me. Any suggestions? |
||
let networkingInteractor: NetworkInteracting | ||
let topicToInvitationPubKeyStore: CodableStore<String> | ||
let registry: Registry | ||
let logger: ConsoleLogging | ||
let kms: KeyManagementServiceProtocol | ||
|
||
init(registry: Registry, | ||
networkingInteractor: NetworkInteracting, | ||
kms: KeyManagementServiceProtocol, | ||
logger: ConsoleLogging, | ||
topicToInvitationPubKeyStore: CodableStore<String>) { | ||
self.registry = registry | ||
self.kms = kms | ||
self.networkingInteractor = networkingInteractor | ||
self.logger = logger | ||
self.topicToInvitationPubKeyStore = topicToInvitationPubKeyStore | ||
} | ||
|
||
func register(account: Account) async throws -> String { | ||
let pubKey = try kms.createX25519KeyPair() | ||
let pubKeyHex = pubKey.hexRepresentation | ||
try await registry.register(account: account, pubKey: pubKeyHex) | ||
let topic = pubKey.rawRepresentation.sha256().toHexString() | ||
topicToInvitationPubKeyStore.set(pubKeyHex, forKey: topic) | ||
try await networkingInteractor.subscribe(topic: topic) | ||
logger.debug("Did register an account: \(account) and is subscribing on topic: \(topic)") | ||
return pubKeyHex | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,4 +4,5 @@ import Foundation | |
enum ChatError: Error { | ||
case noPublicKeyForInviteId | ||
case noInviteForId | ||
case recordNotFound | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
|
||
import Foundation | ||
@testable import Chat | ||
|
||
class NetworkingInteractorMock: NetworkInteracting { | ||
private(set) var subscriptions: [String] = [] | ||
|
||
func subscribe(topic: String) async throws { | ||
subscriptions.append(topic) | ||
} | ||
|
||
func didSubscribe(to topic: String) -> Bool { | ||
subscriptions.contains { $0 == topic } | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import Foundation | ||
import XCTest | ||
@testable import Chat | ||
import WalletConnectUtils | ||
@testable import WalletConnectKMS | ||
@testable import TestingUtils | ||
|
||
final class RegistryManagerTests: XCTestCase { | ||
var registryManager: RegistryManager! | ||
var networkingInteractor: NetworkingInteractorMock! | ||
var topicToInvitationPubKeyStore: CodableStore<String>! | ||
var registry: Registry! | ||
var kms: KeyManagementServiceMock! | ||
|
||
override func setUp() { | ||
registry = KeyValueRegistry() | ||
networkingInteractor = NetworkingInteractorMock() | ||
kms = KeyManagementServiceMock() | ||
topicToInvitationPubKeyStore = CodableStore(defaults: RuntimeKeyValueStorage(), identifier: "") | ||
registryManager = RegistryManager( | ||
registry: registry, | ||
networkingInteractor: networkingInteractor, | ||
kms: kms, | ||
logger: ConsoleLoggerMock(), | ||
topicToInvitationPubKeyStore: topicToInvitationPubKeyStore) | ||
} | ||
|
||
func testRegister() async { | ||
let account = Account("eip155:1:0xab16a96d359ec26a11e2c2b3d8f8b8942d5bfcdb")! | ||
try! await registryManager.register(account: account) | ||
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") | ||
} | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What benefits actor gives us here?
We could make it class and make registryStore private. So it couldn't be mutated from outside too
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
true it is better
registryStore
is privatebut I think making it an actor gives thread safety
It could be mutated by calling register() from different threads
anyway this class is mimicking an http client right now