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

[Refactor] Networking package Part 1 #391 #480

Merged
merged 4 commits into from
Sep 2, 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
77 changes: 77 additions & 0 deletions .swiftpm/xcode/xcshareddata/xcschemes/WalletConnectChat.xcscheme
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "1340"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
buildImplicitDependencies = "YES">
<BuildActionEntries>
<BuildActionEntry
buildForTesting = "YES"
buildForRunning = "YES"
buildForProfiling = "YES"
buildForArchiving = "YES"
buildForAnalyzing = "YES">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "WalletConnectChat"
BuildableName = "WalletConnectChat"
BlueprintName = "WalletConnectChat"
ReferencedContainer = "container:">
</BuildableReference>
</BuildActionEntry>
</BuildActionEntries>
</BuildAction>
<TestAction
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
shouldUseLaunchSchemeArgsEnv = "YES">
<Testables>
<TestableReference
skipped = "NO">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "ChatTests"
BuildableName = "ChatTests"
BlueprintName = "ChatTests"
ReferencedContainer = "container:">
</BuildableReference>
</TestableReference>
</Testables>
</TestAction>
<LaunchAction
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
launchStyle = "0"
useCustomWorkingDirectory = "NO"
ignoresPersistentStateOnLaunch = "NO"
debugDocumentVersioning = "YES"
debugServiceExtension = "internal"
allowLocationSimulation = "YES">
</LaunchAction>
<ProfileAction
buildConfiguration = "Release"
shouldUseLaunchSchemeArgsEnv = "YES"
savedToolIdentifier = ""
useCustomWorkingDirectory = "NO"
debugDocumentVersioning = "YES">
<MacroExpansion>
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "WalletConnectChat"
BuildableName = "WalletConnectChat"
BlueprintName = "WalletConnectChat"
ReferencedContainer = "container:">
</BuildableReference>
</MacroExpansion>
</ProfileAction>
<AnalyzeAction
buildConfiguration = "Debug">
</AnalyzeAction>
<ArchiveAction
buildConfiguration = "Release"
revealArchiveInOrganizer = "YES">
</ArchiveAction>
</Scheme>
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import Auth

final class WalletInteractor {

func pair(uri: String) async throws {
try await Auth.instance.pair(uri: WalletConnectURI(string: uri)!)
func pair(uri: WalletConnectURI) async throws {
try await Auth.instance.pair(uri: uri)
}

var requestPublisher: AnyPublisher<AuthRequest, Never> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,14 @@ final class WalletPresenter: ObservableObject {
}

func didPastePairingURI() {
guard let uri = UIPasteboard.general.string else { return }
guard let string = UIPasteboard.general.string, let uri = WalletConnectURI(string: string) else { return }
pair(uri: uri)
}

func didScanPairingURI() {
router.presentScan { [unowned self] value in
self.pair(uri: value)
guard let uri = WalletConnectURI(string: value) else { return }
self.pair(uri: uri)
self.router.dismiss()
} onError: { error in
print(error.localizedDescription)
Expand Down Expand Up @@ -53,7 +54,7 @@ private extension WalletPresenter {
}.store(in: &disposeBag)
}

func pair(uri: String) {
func pair(uri: WalletConnectURI) {
Task(priority: .high) { [unowned self] in
try await self.interactor.pair(uri: uri)
}
Expand Down
20 changes: 10 additions & 10 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@ let package = Package(
targets: ["Auth"]),
.library(
name: "WalletConnectRouter",
targets: ["WalletConnectRouter"])
targets: ["WalletConnectRouter"]),
.library(
name: "WalletConnectNetworking",
targets: ["WalletConnectNetworking"]),
],
dependencies: [
.package(url: "https://github.com/flypaper0/Web3.swift", .branch("feature/eip-155"))
Expand All @@ -33,17 +36,11 @@ let package = Package(
path: "Sources/WalletConnectSign"),
.target(
name: "Chat",
dependencies: ["WalletConnectRelay", "WalletConnectUtils", "WalletConnectKMS"],
dependencies: ["WalletConnectNetworking"],
path: "Sources/Chat"),
.target(
name: "Auth",
dependencies: [
"WalletConnectRelay",
"WalletConnectUtils",
"WalletConnectKMS",
"WalletConnectPairing",
.product(name: "Web3", package: "Web3.swift")
],
dependencies: ["WalletConnectPairing", "WalletConnectNetworking", .product(name: "Web3", package: "Web3.swift")],
path: "Sources/Auth"),
.target(
name: "WalletConnectRelay",
Expand All @@ -65,6 +62,9 @@ let package = Package(
.target(
name: "Commons",
dependencies: []),
.target(
name: "WalletConnectNetworking",
dependencies: ["JSONRPC", "WalletConnectKMS", "WalletConnectRelay", "WalletConnectUtils"]),
.target(
name: "WalletConnectRouter",
dependencies: []),
Expand All @@ -85,7 +85,7 @@ let package = Package(
dependencies: ["WalletConnectKMS", "WalletConnectUtils", "TestingUtils"]),
.target(
name: "TestingUtils",
dependencies: ["WalletConnectUtils", "WalletConnectKMS", "JSONRPC", "WalletConnectPairing"],
dependencies: ["WalletConnectPairing", "WalletConnectNetworking"],
path: "Tests/TestingUtils"),
.testTarget(
name: "WalletConnectUtilsTests",
Expand Down
4 changes: 3 additions & 1 deletion Sources/Auth/AuthClientFactory.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import WalletConnectRelay
import WalletConnectUtils
import WalletConnectKMS
import WalletConnectPairing
import WalletConnectNetworking

public struct AuthClientFactory {

Expand Down Expand Up @@ -41,6 +42,7 @@ public struct AuthClientFactory {
pendingRequestsProvider: pendingRequestsProvider,
cleanupService: cleanupService,
logger: logger,
pairingStorage: pairingStore, socketConnectionStatusPublisher: relayClient.socketConnectionStatusPublisher)
pairingStorage: pairingStore,
socketConnectionStatusPublisher: relayClient.socketConnectionStatusPublisher)
}
}
1 change: 1 addition & 0 deletions Sources/Auth/Services/App/AppPairService.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import Foundation
import WalletConnectKMS
import WalletConnectPairing
import WalletConnectNetworking

actor AppPairService {
private let networkingInteractor: NetworkInteracting
Expand Down
3 changes: 2 additions & 1 deletion Sources/Auth/Services/App/AppRequestService.swift
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import Foundation
import JSONRPC
import WalletConnectNetworking
import WalletConnectUtils
import WalletConnectKMS
import JSONRPC

actor AppRequestService {
private let networkingInteractor: NetworkInteracting
Expand Down
5 changes: 3 additions & 2 deletions Sources/Auth/Services/App/AppRespondSubscriber.swift
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import Combine
import Foundation
import WalletConnectUtils
import Combine
import JSONRPC
import WalletConnectNetworking
import WalletConnectUtils
import WalletConnectPairing

class AppRespondSubscriber {
Expand Down
1 change: 1 addition & 0 deletions Sources/Auth/Services/Wallet/WalletPairService.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import Foundation
import WalletConnectKMS
import WalletConnectPairing
import WalletConnectNetworking

actor WalletPairService {
enum Errors: Error {
Expand Down
5 changes: 3 additions & 2 deletions Sources/Auth/Services/Wallet/WalletRequestSubscriber.swift
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import Combine
import Foundation
import WalletConnectUtils
import Combine
import JSONRPC
import WalletConnectNetworking
import WalletConnectUtils
import WalletConnectKMS

class WalletRequestSubscriber {
Expand Down
3 changes: 2 additions & 1 deletion Sources/Auth/Services/Wallet/WalletRespondService.swift
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import Foundation
import WalletConnectKMS
import JSONRPC
import WalletConnectKMS
import WalletConnectUtils
import WalletConnectNetworking

actor WalletRespondService {
enum Errors: Error {
Expand Down
1 change: 1 addition & 0 deletions Sources/Auth/Types/Errors/AuthError.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import Foundation
import WalletConnectNetworking

/// Authentication error
public enum AuthError: Codable, Equatable, Error {
Expand Down
7 changes: 0 additions & 7 deletions Sources/Auth/Types/RequestSubscriptionPayload.swift

This file was deleted.

7 changes: 0 additions & 7 deletions Sources/Auth/Types/ResponseSubscriptionPayload.swift

This file was deleted.

7 changes: 4 additions & 3 deletions Sources/Chat/ChatClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import Foundation
import WalletConnectUtils
import WalletConnectKMS
import WalletConnectRelay
import WalletConnectNetworking
import Combine

public class ChatClient {
Expand All @@ -16,7 +17,7 @@ public class ChatClient {
private let kms: KeyManagementService
private let threadStore: Database<Thread>
private let messagesStore: Database<Message>
private let invitePayloadStore: CodableStore<(RequestSubscriptionPayload)>
private let invitePayloadStore: CodableStore<RequestSubscriptionPayload>

public let socketConnectionStatusPublisher: AnyPublisher<SocketConnectionStatus, Never>

Expand Down Expand Up @@ -47,7 +48,7 @@ public class ChatClient {
kms: KeyManagementService,
threadStore: Database<Thread>,
messagesStore: Database<Message>,
invitePayloadStore: CodableStore<(RequestSubscriptionPayload)>,
invitePayloadStore: CodableStore<RequestSubscriptionPayload>,
socketConnectionStatusPublisher: AnyPublisher<SocketConnectionStatus, Never>
) {
self.registry = registry
Expand Down Expand Up @@ -121,7 +122,7 @@ public class ChatClient {
public func getInvites(account: Account) -> [Invite] {
var invites = [Invite]()
invitePayloadStore.getAll().forEach {
guard case .invite(let invite) = $0.request.params else {return}
guard let invite = try? $0.request.params?.get(Invite.self) else {return}
invites.append(invite)
}
return invites
Expand Down
10 changes: 6 additions & 4 deletions Sources/Chat/ChatClientFactory.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import Foundation
import WalletConnectRelay
import WalletConnectUtils
import WalletConnectKMS
import WalletConnectNetworking

public struct ChatClientFactory {

Expand All @@ -10,17 +11,18 @@ public struct ChatClientFactory {
relayClient: RelayClient,
kms: KeyManagementService,
logger: ConsoleLogging,
keyValueStorage: KeyValueStorage) -> ChatClient {
keyValueStorage: KeyValueStorage
) -> ChatClient {
let topicToRegistryRecordStore = CodableStore<RegistryRecord>(defaults: keyValueStorage, identifier: StorageDomainIdentifiers.topicToInvitationPubKey.rawValue)
let serialiser = Serializer(kms: kms)
let jsonRpcHistory = JsonRpcHistory<ChatRequestParams>(logger: logger, keyValueStore: CodableStore<JsonRpcRecord>(defaults: keyValueStorage, identifier: StorageDomainIdentifiers.jsonRpcHistory.rawValue))
let networkingInteractor = NetworkingInteractor(relayClient: relayClient, serializer: serialiser, logger: logger, jsonRpcHistory: jsonRpcHistory)
let rpcHistory = RPCHistory(keyValueStore: CodableStore<RPCHistory.Record>(defaults: keyValueStorage, identifier: StorageDomainIdentifiers.jsonRpcHistory.rawValue))
Comment on lines -16 to +18
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

have you tried if old records will be derived with new RPCHistory

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

decided to not make migration

let networkingInteractor = NetworkingInteractor(relayClient: relayClient, serializer: serialiser, logger: logger, rpcHistory: rpcHistory)
let invitePayloadStore = CodableStore<RequestSubscriptionPayload>(defaults: keyValueStorage, identifier: StorageDomainIdentifiers.invite.rawValue)
let registryService = RegistryService(registry: registry, networkingInteractor: networkingInteractor, kms: kms, logger: logger, topicToRegistryRecordStore: topicToRegistryRecordStore)
let threadStore = Database<Thread>(keyValueStorage: keyValueStorage, identifier: StorageDomainIdentifiers.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, logger: logger)
let inviteService = InviteService(networkingInteractor: networkingInteractor, kms: kms, threadStore: threadStore, rpcHistory: rpcHistory, logger: logger)
let leaveService = LeaveService()
let messagesStore = Database<Message>(keyValueStorage: keyValueStorage, identifier: StorageDomainIdentifiers.messages.rawValue)
let messagingService = MessagingService(networkingInteractor: networkingInteractor, messagesStore: messagesStore, threadStore: threadStore, logger: logger)
Expand Down
Loading