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] Pairing Services #383

Merged
merged 9 commits into from
Jul 29, 2022
Merged
Show file tree
Hide file tree
Changes from 4 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
67 changes: 67 additions & 0 deletions .swiftpm/xcode/xcshareddata/xcschemes/WalletConnectAuth.xcscheme
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?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 = "WalletConnectAuth"
BuildableName = "WalletConnectAuth"
BlueprintName = "WalletConnectAuth"
ReferencedContainer = "container:">
</BuildableReference>
</BuildActionEntry>
</BuildActionEntries>
</BuildAction>
<TestAction
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
shouldUseLaunchSchemeArgsEnv = "YES">
<Testables>
</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 = "WalletConnectAuth"
BuildableName = "WalletConnectAuth"
BlueprintName = "WalletConnectAuth"
ReferencedContainer = "container:">
</BuildableReference>
</MacroExpansion>
</ProfileAction>
<AnalyzeAction
buildConfiguration = "Debug">
</AnalyzeAction>
<ArchiveAction
buildConfiguration = "Release"
revealArchiveInOrganizer = "YES">
</ArchiveAction>
</Scheme>
1 change: 1 addition & 0 deletions Example/DApp/SceneDelegate.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import UIKit
import WalletConnectSign
import WalletConnectRelay
import WalletConnectUtils
import Combine
import Starscream

Expand Down
1 change: 1 addition & 0 deletions Example/ExampleApp/SceneDelegate.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import UIKit
import WalletConnectSign
import WalletConnectRelay
import WalletConnectUtils
import Starscream

extension WebSocket: WebSocketConnecting { }
Expand Down
2 changes: 1 addition & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ let package = Package(
targets: ["WalletConnectSign"]),
.library(
name: "WalletConnectChat",
targets: ["Chat"])
targets: ["Chat"]),
],
dependencies: [],
targets: [
Expand Down
25 changes: 25 additions & 0 deletions Sources/Auth/Services/App/AppPairService.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import Foundation
import WalletConnectKMS
import WalletConnectUtils

actor AppPairService {
private let networkingInteractor: NetworkInteracting
private let kms: KeyManagementServiceProtocol
private let pairingStorage: WCPairingStorage

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

func create() async throws -> WalletConnectURI {
let topic = String.generateTopic()
try await networkingInteractor.subscribe(topic: topic)
let symKey = try! kms.createSymmetricKey(topic)
let pairing = WCPairing(topic: topic)
let uri = WalletConnectURI(topic: topic, symKey: symKey.hexRepresentation, relay: pairing.relay)
pairingStorage.setPairing(pairing)
return uri
}
}
8 changes: 0 additions & 8 deletions Sources/Auth/Services/App/CreatePairingService.swift

This file was deleted.

18 changes: 18 additions & 0 deletions Sources/Auth/Services/Common/NetworkingInteractor.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import Foundation
import WalletConnectRelay

protocol NetworkInteracting {
func subscribe(topic: String) async throws
}

class NetworkingInteractor: NetworkInteracting {
private let relayClient: RelayClient

init(relayClient: RelayClient) {
self.relayClient = relayClient
}

func subscribe(topic: String) async throws {
try await relayClient.subscribe(topic: topic)
}
}
24 changes: 24 additions & 0 deletions Sources/Auth/Services/Wallet/AuthService.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,30 @@
import Foundation

actor AuthService {
enum Errors: Error {
case malformedPairingURI
}

private let appPairService: AppPairService

private let walletPairService: WalletPairService

init(appPairService: AppPairService, walletPairService: WalletPairService) {
self.appPairService = appPairService
self.walletPairService = walletPairService
}

func connect() async throws -> String {
let uri = try await appPairService.create()
return uri.absoluteString
}

func pair(uri: String) async throws {
guard let pairingURI = WalletConnectURI(string: uri) else {
throw Errors.malformedPairingURI
}
try await walletPairService.pair(pairingURI)
}

func respond(respondParams: RespondParams) async throws {
flypaper0 marked this conversation as resolved.
Show resolved Hide resolved

Expand Down
8 changes: 0 additions & 8 deletions Sources/Auth/Services/Wallet/PairService.swift

This file was deleted.

37 changes: 37 additions & 0 deletions Sources/Auth/Services/Wallet/WalletPairService.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import Foundation
import WalletConnectKMS
import WalletConnectUtils

actor WalletPairService {
enum Errors: Error {
case pairingAlreadyExist
}

private let networkingInteractor: NetworkInteracting
private let kms: KeyManagementServiceProtocol
private let pairingStorage: WCPairingStorage

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

func pair(_ uri: WalletConnectURI) async throws {
guard !hasPairing(for: uri.topic) else {
throw Errors.pairingAlreadyExist
}
var pairing = WCPairing(uri: uri)
try await networkingInteractor.subscribe(topic: pairing.topic)
let symKey = try SymmetricKey(hex: uri.symKey)
try kms.setSymmetricKey(symKey, for: pairing.topic)
pairing.activate()
pairingStorage.setPairing(pairing)
}

func hasPairing(for topic: String) -> Bool {
return pairingStorage.hasPairing(forTopic: topic)
}
}
2 changes: 0 additions & 2 deletions Sources/Auth/Types/RelayProtocolOptions.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
//

import Foundation
import WalletConnectUtils

Expand Down
2 changes: 0 additions & 2 deletions Sources/Auth/Types/RequestParams.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
//

import Foundation

struct RequestParams {
Expand Down
2 changes: 0 additions & 2 deletions Sources/Auth/Types/RespondParams.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
//

import Foundation

struct RespondParams {
Expand Down
2 changes: 0 additions & 2 deletions Sources/Auth/Types/WalletConnectURI.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
//

import Foundation
import WalletConnectUtils

Expand Down
2 changes: 0 additions & 2 deletions Sources/WalletConnectKMS/Crypto/Hash.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
//

import Foundation
import CryptoKit

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import Foundation
import WalletConnectKMS
import WalletConnectUtils

actor PairEngine {
private let networkingInteractor: NetworkInteracting
Expand Down
1 change: 1 addition & 0 deletions Sources/WalletConnectSign/Pairing.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import Foundation
import WalletConnectUtils
/**
A representation of an active pairing connection.
*/
Expand Down
3 changes: 2 additions & 1 deletion Sources/WalletConnectSign/Sign/SignConfig.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import WalletConnectRelay
import Foundation
import WalletConnectRelay
import WalletConnectUtils

public extension Sign {
struct Config {
Expand Down
2 changes: 2 additions & 0 deletions Sources/WalletConnectSign/Storage/SessionStorage.swift
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import WalletConnectUtils

protocol WCSessionStorage: AnyObject {
var onSessionExpiration: ((WCSession) -> Void)? { get set }
@discardableResult
Expand Down
2 changes: 2 additions & 0 deletions Sources/WalletConnectSign/Types/Common/Participant.swift
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import WalletConnectUtils

struct Participant: Codable, Equatable {
let publicKey: String
let metadata: AppMetadata
Expand Down
16 changes: 0 additions & 16 deletions Sources/WalletConnectSign/Types/Pairing/PairingType.swift

This file was deleted.

2 changes: 0 additions & 2 deletions Sources/WalletConnectSign/Types/RelayProtocolOptions.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
//

import Foundation
import WalletConnectUtils

Expand Down
2 changes: 2 additions & 0 deletions Sources/WalletConnectSign/Types/WCMethod.swift
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import WalletConnectUtils

enum WCMethod {
case wcPairingPing
case wcSessionPropose(SessionType.ProposeParams)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
protocol WCPairingStorage: AnyObject {
public protocol WCPairingStorage: AnyObject {
var onPairingExpiration: ((WCPairing) -> Void)? { get set }
func hasPairing(forTopic topic: String) -> Bool
func setPairing(_ pairing: WCPairing)
Expand All @@ -8,40 +8,40 @@ protocol WCPairingStorage: AnyObject {
func deleteAll()
}

final class PairingStorage: WCPairingStorage {
public final class PairingStorage: WCPairingStorage {

var onPairingExpiration: ((WCPairing) -> Void)? {
public var onPairingExpiration: ((WCPairing) -> Void)? {
get { storage.onSequenceExpiration }
set { storage.onSequenceExpiration = newValue }
}

private let storage: SequenceStore<WCPairing>

init(storage: SequenceStore<WCPairing>) {
public init(storage: SequenceStore<WCPairing>) {
self.storage = storage
}

func hasPairing(forTopic topic: String) -> Bool {
public func hasPairing(forTopic topic: String) -> Bool {
storage.hasSequence(forTopic: topic)
}

func setPairing(_ pairing: WCPairing) {
public func setPairing(_ pairing: WCPairing) {
storage.setSequence(pairing)
}

func getPairing(forTopic topic: String) -> WCPairing? {
public func getPairing(forTopic topic: String) -> WCPairing? {
try? storage.getSequence(forTopic: topic)
}

func getAll() -> [WCPairing] {
public func getAll() -> [WCPairing] {
storage.getAll()
}

func delete(topic: String) {
public func delete(topic: String) {
storage.delete(topic: topic)
}

func deleteAll() {
public func deleteAll() {
storage.deleteAll()
}
}
18 changes: 18 additions & 0 deletions Sources/WalletConnectUtils/Pairing/Types/PairingType.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import Foundation

// Internal namespace for pairing payloads.
public enum PairingType {

public struct DeleteParams: Codable, Equatable {
let reason: Reason
}

public struct Reason: Codable, Equatable {
let code: Int
let message: String
}

public struct PingParams: Codable, Equatable {
public init() { }
}
}
Loading