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

[Auth, Sign] #485 remove pairing #486

Merged
merged 14 commits into from
Sep 5, 2022
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>
2 changes: 2 additions & 0 deletions Example/ExampleApp.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -1752,6 +1752,7 @@
DEVELOPMENT_TEAM = W5R8AG9K22;
GENERATE_INFOPLIST_FILE = YES;
INFOPLIST_FILE = Showcase/Other/Info.plist;
INFOPLIST_KEY_NSCameraUsageDescription = "Allow the app to scan for QR codes";
INFOPLIST_KEY_UIApplicationSupportsIndirectInputEvents = YES;
INFOPLIST_KEY_UILaunchStoryboardName = LaunchScreen;
INFOPLIST_KEY_UISupportedInterfaceOrientations_iPad = "UIInterfaceOrientationPortrait UIInterfaceOrientationPortraitUpsideDown UIInterfaceOrientationLandscapeLeft UIInterfaceOrientationLandscapeRight";
Expand Down Expand Up @@ -1781,6 +1782,7 @@
DEVELOPMENT_TEAM = W5R8AG9K22;
GENERATE_INFOPLIST_FILE = YES;
INFOPLIST_FILE = Showcase/Other/Info.plist;
INFOPLIST_KEY_NSCameraUsageDescription = "Allow the app to scan for QR codes";
INFOPLIST_KEY_UIApplicationSupportsIndirectInputEvents = YES;
INFOPLIST_KEY_UILaunchStoryboardName = LaunchScreen;
INFOPLIST_KEY_UISupportedInterfaceOrientations_iPad = "UIInterfaceOrientationPortrait UIInterfaceOrientationPortraitUpsideDown UIInterfaceOrientationLandscapeLeft UIInterfaceOrientationLandscapeRight";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class SceneDelegate: UIResponder, UIWindowSceneDelegate {

let uri = context.url.absoluteString.replacingOccurrences(of: "showcase://wc?uri=", with: "")
Task {
try await Auth.instance.pair(uri: uri)
try await Auth.instance.pair(uri: WalletConnectURI(string: uri)!)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import Auth

final class WalletInteractor {

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

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
8 changes: 7 additions & 1 deletion Sources/Auth/AuthClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ public class AuthClient {
private var authRequestPublisherSubject = PassthroughSubject<AuthRequest, Never>()
private let appPairService: AppPairService
private let appRequestService: AppRequestService
private let deletePairingService: DeletePairingService
private let appRespondSubscriber: AppRespondSubscriber
private let walletPairService: WalletPairService
private let walletRequestSubscriber: WalletRequestSubscriber
Expand All @@ -61,6 +62,7 @@ public class AuthClient {
walletPairService: WalletPairService,
walletRequestSubscriber: WalletRequestSubscriber,
walletRespondService: WalletRespondService,
deletePairingService: DeletePairingService,
account: Account?,
pendingRequestsProvider: PendingRequestsProvider,
cleanupService: CleanupService,
Expand All @@ -80,7 +82,7 @@ public class AuthClient {
self.logger = logger
self.pairingStorage = pairingStorage
self.socketConnectionStatusPublisher = socketConnectionStatusPublisher

self.deletePairingService = deletePairingService
setUpPublishers()
}

Expand Down Expand Up @@ -135,6 +137,10 @@ public class AuthClient {
try await walletRespondService.respondError(requestId: requestId)
}

public func disconnect(topic: String) async throws {
try await deletePairingService.delete(topic: topic)
}

/// Query pending authentication requests
/// - Returns: Pending authentication requests
public func getPendingRequests() throws -> [AuthRequest] {
Expand Down
7 changes: 5 additions & 2 deletions 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 @@ -30,17 +31,19 @@ public struct AuthClientFactory {
let walletRespondService = WalletRespondService(networkingInteractor: networkingInteractor, logger: logger, kms: kms, rpcHistory: history)
let pendingRequestsProvider = PendingRequestsProvider(rpcHistory: history)
let cleanupService = CleanupService(pairingStore: pairingStore, kms: kms)
let deletePairingService = DeletePairingService(networkingInteractor: networkingInteractor, kms: kms, pairingStorage: pairingStore, logger: logger)

return AuthClient(appPairService: appPairService,
appRequestService: appRequestService,
appRespondSubscriber: appRespondSubscriber,
walletPairService: walletPairService,
walletRequestSubscriber: walletRequestSubscriber,
walletRespondService: walletRespondService,
walletRespondService: walletRespondService, deletePairingService: deletePairingService,
account: account,
pendingRequestsProvider: pendingRequestsProvider,
cleanupService: cleanupService,
logger: logger,
pairingStorage: pairingStore, socketConnectionStatusPublisher: relayClient.socketConnectionStatusPublisher)
pairingStorage: pairingStore,
socketConnectionStatusPublisher: relayClient.socketConnectionStatusPublisher)
}
}
29 changes: 29 additions & 0 deletions Sources/Auth/AuthProtocolMethods.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import Foundation

enum AuthProtocolMethods: String {
case authRequest = "wc_authRequest"
case pairingDelete = "wc_pairingDelete"
case pairingPing = "wc_pairingPing"

var requestTag: Int {
switch self {
case .authRequest:
return 3000
case .pairingDelete:
return 1000
case .pairingPing:
return 1002
}
}

var responseTag: Int {
switch self {
case .authRequest:
return 3001
case .pairingDelete:
return 1001
case .pairingPing:
return 1003
}
}
}
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
37 changes: 37 additions & 0 deletions Sources/Auth/Services/Common/DeletePairingService.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import Foundation
import WalletConnectNetworking
import JSONRPC
import WalletConnectKMS
import WalletConnectUtils
import WalletConnectPairing

class DeletePairingService {
enum Errors: Error {
case pairingNotFound
}
private let networkingInteractor: NetworkInteracting
private let kms: KeyManagementServiceProtocol
private let pairingStorage: WCPairingStorage
private let logger: ConsoleLogging

init(networkingInteractor: NetworkInteracting,
kms: KeyManagementServiceProtocol,
pairingStorage: WCPairingStorage,
logger: ConsoleLogging) {
self.networkingInteractor = networkingInteractor
self.kms = kms
self.pairingStorage = pairingStorage
self.logger = logger
}

func delete(topic: String) async throws {
guard pairingStorage.hasPairing(forTopic: topic) else { throw Errors.pairingNotFound}
let reason = AuthError.userDisconnected
logger.debug("Will delete pairing for reason: message: \(reason.message) code: \(reason.code)")
let request = RPCRequest(method: AuthProtocolMethods.pairingDelete.rawValue, params: reason)
try await networkingInteractor.request(request, topic: topic, tag: AuthProtocolMethods.pairingDelete.requestTag)
pairingStorage.delete(topic: topic)
kms.deleteSymmetricKey(for: topic)
networkingInteractor.unsubscribe(topic: topic)
}
}
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
Loading