Skip to content

Commit

Permalink
improve UpdateSessionHandler
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrey Scherbovich committed Jul 9, 2021
1 parent 2b9693e commit f2eefeb
Show file tree
Hide file tree
Showing 12 changed files with 74 additions and 58 deletions.
10 changes: 5 additions & 5 deletions Sources/Internal/Communicator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ class Communicator {
return sessions.find(url: url)
}

func addSession(_ session: Session) {
sessions.add(session)
func addOrUpdateSession(_ session: Session) {
sessions.addOrUpdate(session)
}

func removeSession(by url: WCURL) {
Expand All @@ -53,8 +53,8 @@ class Communicator {
return pendingDisconnectSessions.find(url: url)
}

func addPendingDisconnectSession(_ session: Session) {
pendingDisconnectSessions.add(session)
func addOrUpdatePendingDisconnectSession(_ session: Session) {
pendingDisconnectSessions.addOrUpdate(session)
}

func removePendingDisconnectSession(by url: WCURL) {
Expand Down Expand Up @@ -107,7 +107,7 @@ class Communicator {
self.queue = queue
}

func add(_ session: Session) {
func addOrUpdate(_ session: Session) {
dispatchPrecondition(condition: .notOnQueue(queue))
queue.sync { [unowned self] in
self.sessions[session.url] = session
Expand Down
2 changes: 1 addition & 1 deletion Sources/Internal/HandshakeHandler.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

import Foundation

protocol HandshakeHandlerDelegate: class {
protocol HandshakeHandlerDelegate: AnyObject {
// TODO: instead of IDType, use RequestID
func handler(_ handler: HandshakeHandler, didReceiveRequestToCreateSession: Session, requestId: RequestID)
}
Expand Down
11 changes: 8 additions & 3 deletions Sources/Internal/UpdateSessionHandler.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import Foundation

protocol UpdateSessionHandlerDelegate: AnyObject {
func handler(_ handler: UpdateSessionHandler, didUpdateSessionByURL: WCURL, approved: Bool)
func handler(_ handler: UpdateSessionHandler, didUpdateSessionByURL: WCURL, sessionInfo: SessionInfo)
}

class UpdateSessionHandler: RequestHandler {
Expand All @@ -22,16 +22,21 @@ class UpdateSessionHandler: RequestHandler {
func handle(request: Request) {
do {
let sessionInfo = try request.parameter(of: SessionInfo.self, at: 0)
delegate?.handler(self, didUpdateSessionByURL: request.url, approved: sessionInfo.approved)
delegate?.handler(self, didUpdateSessionByURL: request.url, sessionInfo: sessionInfo)
} catch {
LogService.shared.log("WC: wrong format of wc_sessionUpdate request: \(error)")
// TODO: send error response
}
}
}

/// https://docs.walletconnect.org/tech-spec#session-update
struct SessionInfo: Decodable {
var approved: Bool
var accounts: [String]?
var chainId: Int
var chainId: Int?
}

enum ChainID {
static let mainnet = 1
}
34 changes: 17 additions & 17 deletions Sources/PublicInterface/Client.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

import Foundation

public protocol ClientDelegate: class {
public protocol ClientDelegate: AnyObject {
func client(_ client: Client, didFailToConnect url: WCURL)
func client(_ client: Client, didConnect url: WCURL)
func client(_ client: Client, didConnect session: Session)
Expand Down Expand Up @@ -208,7 +208,7 @@ public class Client: WalletConnect {
return
}

communicator.addSession(session)
communicator.addOrUpdateSession(session)
delegate?.client(self, didConnect: session)
} catch {
// TODO: handle error
Expand Down Expand Up @@ -236,29 +236,29 @@ public class Client: WalletConnect {
try! send(Response(request: request, error: .invalidJSON))
return
}

guard let session = communicator.session(by: request.url) else { return }

if !info.approved {
do {
try disconnect(from: session)
} catch { // session already disconnected
delegate?.client(self, didDisconnect: session)
}
} else {
if let walletInfo = session.walletInfo {
let updatedInfo = Session.WalletInfo(
approved: info.approved,
accounts: info.accounts ?? [],
chainId: info.chainId,
peerId: walletInfo.peerId,
peerMeta: walletInfo.peerMeta
)
var updatedSesson = session
updatedSesson.walletInfo = updatedInfo
communicator.addSession(updatedSesson)
delegate?.client(self, didUpdate: updatedSesson)
} else {
delegate?.client(self, didUpdate: session)
}
// we do not add sessions without walletInfo
let walletInfo = session.walletInfo!
let updatedInfo = Session.WalletInfo(
approved: info.approved,
accounts: info.accounts ?? [],
chainId: info.chainId ?? ChainID.mainnet,
peerId: walletInfo.peerId,
peerMeta: walletInfo.peerMeta
)
var updatedSesson = session
updatedSesson.walletInfo = updatedInfo
communicator.addOrUpdateSession(updatedSesson)
delegate?.client(self, didUpdate: updatedSesson)
}
} else {
// TODO: error handling
Expand Down
23 changes: 18 additions & 5 deletions Sources/PublicInterface/Server.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@

import Foundation

public protocol RequestHandler: class {
public protocol RequestHandler: AnyObject {
func canHandle(request: Request) -> Bool
func handle(request: Request)
}

public protocol ServerDelegate: class {
public protocol ServerDelegate: AnyObject {
/// Websocket connection was dropped during handshake. The connectoin process should be initiated again.
func server(_ server: Server, didFailToConnect url: WCURL)

Expand Down Expand Up @@ -172,7 +172,7 @@ extension Server: HandshakeHandlerDelegate {
self.communicator.send(response, topic: session.dAppInfo.peerId)
if walletInfo.approved {
let updatedSession = Session(url: session.url, dAppInfo: session.dAppInfo, walletInfo: walletInfo)
self.communicator.addSession(updatedSession)
self.communicator.addOrUpdateSession(updatedSession)
self.communicator.subscribe(on: walletInfo.peerId, url: updatedSession.url)
self.delegate?.server(self, didConnect: updatedSession)
}
Expand All @@ -181,14 +181,27 @@ extension Server: HandshakeHandlerDelegate {
}

extension Server: UpdateSessionHandlerDelegate {
func handler(_ handler: UpdateSessionHandler, didUpdateSessionByURL url: WCURL, approved: Bool) {
func handler(_ handler: UpdateSessionHandler, didUpdateSessionByURL url: WCURL, sessionInfo: SessionInfo) {
guard let session = communicator.session(by: url) else { return }
if !approved {
if !sessionInfo.approved {
do {
try disconnect(from: session)
} catch { // session already disconnected
delegate?.server(self, didDisconnect: session)
}
} else {
// we do not add sessions without walletInfo
let walletInfo = session.walletInfo!
let updatedInfo = Session.WalletInfo(
approved: sessionInfo.approved,
accounts: sessionInfo.accounts ?? [],
chainId: sessionInfo.chainId ?? ChainID.mainnet,
peerId: walletInfo.peerId,
peerMeta: walletInfo.peerMeta
)
var updatedSesson = session
updatedSesson.walletInfo = updatedInfo
communicator.addOrUpdateSession(updatedSesson)
}
}
}
2 changes: 1 addition & 1 deletion Sources/PublicInterface/Session.swift
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public struct Session: Codable {
self.peerMeta = peerMeta
}

func with(approved: Bool) -> WalletInfo {
public func with(approved: Bool) -> WalletInfo {
return WalletInfo(approved: approved,
accounts: self.accounts,
chainId: self.chainId,
Expand Down
4 changes: 2 additions & 2 deletions Sources/PublicInterface/WalletConnect.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ open class WalletConnect {
guard session.walletInfo != nil else {
throw WalletConnectError.missingWalletInfoInSession
}
communicator.addSession(session)
communicator.addOrUpdateSession(session)
listen(on: session.url)
}

Expand All @@ -46,7 +46,7 @@ open class WalletConnect {
throw WalletConnectError.tryingToDisconnectInactiveSession
}
try sendDisconnectSessionRequest(for: session)
communicator.addPendingDisconnectSession(session)
communicator.addOrUpdatePendingDisconnectSession(session)
communicator.disconnect(from: session.url)
}

Expand Down
10 changes: 5 additions & 5 deletions Tests/ClientTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,15 @@ class ClientTests: XCTestCase {
}

func test_sendRequest_whenNoWalletInfo_thenThrows() {
communicator.addSession(Session.testSessionWithoutWalletInfo)
communicator.addOrUpdateSession(Session.testSessionWithoutWalletInfo)
XCTAssertThrowsError(try client.send(Request.testRequest, completion: nil),
"missingWalletInfoInSession") { error in
XCTAssertEqual(error as? Client.ClientError, .missingWalletInfoInSession)
}
}

func test_sendRequest_callsCommunicator() {
communicator.addSession(Session.testSession)
communicator.addOrUpdateSession(Session.testSession)
try? client.send(Request.testRequest, completion: nil)
XCTAssertNotNil(communicator.sentRequest)
}
Expand Down Expand Up @@ -68,12 +68,12 @@ class ClientTests: XCTestCase {

@discardableResult
private func prepareAccountWithTestSession() -> String {
communicator.addSession(Session.testSession)
communicator.addOrUpdateSession(Session.testSession)
return Session.testSession.walletInfo!.accounts[0]
}

func test_onConnect_whenSessionExists_thenSubscribesOnDappPeerIdTopic() {
communicator.addSession(Session.testSession)
communicator.addOrUpdateSession(Session.testSession)
client.onConnect(to: WCURL.testURL)
XCTAssertEqual(communicator.subscribedOn?.topic, Session.testSession.dAppInfo.peerId)
XCTAssertEqual(communicator.subscribedOn?.url, Session.testSession.url)
Expand All @@ -86,7 +86,7 @@ class ClientTests: XCTestCase {
}

func test_onConnect_whenSessionExists_thenCallsDelegate() {
communicator.addSession(Session.testSession)
communicator.addOrUpdateSession(Session.testSession)
XCTAssertNil(delegate.connectedSession)
client.onConnect(to: WCURL.testURL)
XCTAssertEqual(delegate.connectedSession, Session.testSession)
Expand Down
2 changes: 1 addition & 1 deletion Tests/Helpers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class MockCommunicator: Communicator {
didListen = true
}

override func addSession(_ session: Session) {
override func addOrUpdateSession(_ session: Session) {
sessions.append(session)
}

Expand Down
2 changes: 1 addition & 1 deletion Tests/WalletConnectTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class WalletConnectTests: XCTestCase {
}

func test_whenConnectingToExistingURL_thenThrows() {
mockCommunicator.addSession(Session.testSession)
mockCommunicator.addOrUpdateSession(Session.testSession)
XCTAssertThrowsError(try wc.connect(to: WCURL.testURL))
}

Expand Down
4 changes: 3 additions & 1 deletion WalletConnectSwift.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@
isa = PBXProject;
attributes = {
LastSwiftUpdateCheck = 1030;
LastUpgradeCheck = 1030;
LastUpgradeCheck = 1250;
ORGANIZATIONNAME = "Gnosis Ltd.";
TargetAttributes = {
0A92784F230BFB2600FDCC0D = {
Expand Down Expand Up @@ -487,6 +487,7 @@
CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES;
CLANG_WARN_OBJC_LITERAL_CONVERSION = YES;
CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR;
CLANG_WARN_QUOTED_INCLUDE_IN_FRAMEWORK_HEADER = YES;
CLANG_WARN_RANGE_LOOP_ANALYSIS = YES;
CLANG_WARN_STRICT_PROTOTYPES = YES;
CLANG_WARN_SUSPICIOUS_MOVE = YES;
Expand Down Expand Up @@ -548,6 +549,7 @@
CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES;
CLANG_WARN_OBJC_LITERAL_CONVERSION = YES;
CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR;
CLANG_WARN_QUOTED_INCLUDE_IN_FRAMEWORK_HEADER = YES;
CLANG_WARN_RANGE_LOOP_ANALYSIS = YES;
CLANG_WARN_STRICT_PROTOTYPES = YES;
CLANG_WARN_SUSPICIOUS_MOVE = YES;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "1030"
LastUpgradeVersion = "1250"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
Expand All @@ -26,9 +26,18 @@
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
shouldUseLaunchSchemeArgsEnv = "YES"
codeCoverageEnabled = "YES"
onlyGenerateCoverageForSpecifiedTargets = "YES"
shouldUseLaunchSchemeArgsEnv = "YES">
onlyGenerateCoverageForSpecifiedTargets = "YES">
<MacroExpansion>
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "0A92784F230BFB2600FDCC0D"
BuildableName = "WalletConnectSwift.framework"
BlueprintName = "WalletConnectSwift"
ReferencedContainer = "container:WalletConnectSwift.xcodeproj">
</BuildableReference>
</MacroExpansion>
<CodeCoverageTargets>
<BuildableReference
BuildableIdentifier = "primary"
Expand All @@ -50,17 +59,6 @@
</BuildableReference>
</TestableReference>
</Testables>
<MacroExpansion>
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "0A92784F230BFB2600FDCC0D"
BuildableName = "WalletConnectSwift.framework"
BlueprintName = "WalletConnectSwift"
ReferencedContainer = "container:WalletConnectSwift.xcodeproj">
</BuildableReference>
</MacroExpansion>
<AdditionalOptions>
</AdditionalOptions>
</TestAction>
<LaunchAction
buildConfiguration = "Debug"
Expand All @@ -81,8 +79,6 @@
ReferencedContainer = "container:WalletConnectSwift.xcodeproj">
</BuildableReference>
</MacroExpansion>
<AdditionalOptions>
</AdditionalOptions>
</LaunchAction>
<ProfileAction
buildConfiguration = "Release"
Expand Down

0 comments on commit f2eefeb

Please sign in to comment.