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

fix Auth connect method #229

Merged
merged 3 commits into from
May 25, 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
2 changes: 1 addition & 1 deletion Example/DApp/SceneDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class SceneDelegate: UIResponder, UIWindowSceneDelegate {
}.store(in: &publishers)


if let session = Auth.instance.getSettledSessions().first {
if let session = Auth.instance.getSessions().first {
showAccountsScreen(session)
} else {
showSelectChainScreen()
Expand Down
4 changes: 2 additions & 2 deletions Example/ExampleApp.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -729,7 +729,7 @@
CODE_SIGN_ENTITLEMENTS = Wallet.entitlements;
CODE_SIGN_IDENTITY = "Apple Development";
CODE_SIGN_STYLE = Automatic;
CURRENT_PROJECT_VERSION = 11;
CURRENT_PROJECT_VERSION = 12;
DEVELOPMENT_TEAM = W5R8AG9K22;
INFOPLIST_FILE = ExampleApp/Info.plist;
IPHONEOS_DEPLOYMENT_TARGET = 15.0;
Expand All @@ -754,7 +754,7 @@
CODE_SIGN_ENTITLEMENTS = Wallet.entitlements;
CODE_SIGN_IDENTITY = "Apple Development";
CODE_SIGN_STYLE = Automatic;
CURRENT_PROJECT_VERSION = 11;
CURRENT_PROJECT_VERSION = 12;
DEVELOPMENT_TEAM = W5R8AG9K22;
INFOPLIST_FILE = ExampleApp/Info.plist;
IPHONEOS_DEPLOYMENT_TARGET = 15.0;
Expand Down
6 changes: 3 additions & 3 deletions Example/ExampleApp/Wallet/WalletViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ final class WalletViewController: UIViewController {

walletView.tableView.dataSource = self
walletView.tableView.delegate = self
let settledSessions = Auth.instance.getSettledSessions()
let settledSessions = Auth.instance.getSessions()
sessionItems = getActiveSessionItem(for: settledSessions)
setUpAuthSubscribing()
}
Expand Down Expand Up @@ -129,7 +129,7 @@ extension WalletViewController: UITableViewDataSource, UITableViewDelegate {
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
print("did select row \(indexPath)")
let itemTopic = sessionItems[indexPath.row].topic
if let session = Auth.instance.getSettledSessions().first{$0.topic == itemTopic} {
if let session = Auth.instance.getSessions().first{$0.topic == itemTopic} {
showSessionDetailsViewController(session)
}
}
Expand Down Expand Up @@ -225,7 +225,7 @@ extension WalletViewController {
}

private func reloadActiveSessions() {
let settledSessions = Auth.instance.getSettledSessions()
let settledSessions = Auth.instance.getSessions()
let activeSessions = getActiveSessionItem(for: settledSessions)
DispatchQueue.main.async { // FIXME: Delegate being called from background thread
self.sessionItems = activeSessions
Expand Down
8 changes: 4 additions & 4 deletions Sources/WalletConnectAuth/Auth/Auth.swift
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ extension Auth {
/// - Parameter topic: Optional parameter - use it if you already have an established pairing with peer client.
/// - Returns: Pairing URI that should be shared with responder out of bound. Common way is to present it as a QR code. Pairing URI will be nil if you are going to establish a session on existing Pairing and `topic` function parameter was provided.
public func connect(requiredNamespaces: [String : ProposalNamespace], topic: String? = nil) async throws -> String? {
try await client.connect(requiredNamespaces: requiredNamespaces)
try await client.connect(requiredNamespaces: requiredNamespaces, topic: topic)
}

/// For responder to receive a session proposal from a proposer
Expand Down Expand Up @@ -218,9 +218,9 @@ extension Auth {
try await client.disconnect(topic: topic, reason: reason)
}

/// - Returns: All settled sessions that are active
public func getSettledSessions() -> [Session] {
client.getSettledSessions()
/// - Returns: All sessions
public func getSessions() -> [Session] {
client.getSessions()
}

/// - Returns: All settled pairings that are active
Expand Down
6 changes: 3 additions & 3 deletions Sources/WalletConnectAuth/Auth/AuthClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -251,9 +251,9 @@ public final class AuthClient {
try await sessionEngine.delete(topic: topic, reason: reason)
}

/// - Returns: All settled sessions that are active
public func getSettledSessions() -> [Session] {
sessionEngine.getAcknowledgedSessions()
/// - Returns: All sessions
public func getSessions() -> [Session] {
sessionEngine.getSessions()
}

/// - Returns: All settled pairings that are active
Expand Down
9 changes: 4 additions & 5 deletions Sources/WalletConnectAuth/Engine/Common/SessionEngine.swift
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,12 @@ final class SessionEngine {
return sessionStore.hasSession(forTopic: topic)
}

func getAcknowledgedSessions() -> [Session] {
sessionStore.getAcknowledgedSessions().map{$0.publicRepresentation()}
func getSessions() -> [Session] {
sessionStore.getAll().map{$0.publicRepresentation()}
}

func settle(topic: String, proposal: SessionProposal, namespaces: [String: SessionNamespace]) throws {
let agreementKeys = try! kms.getAgreementSecret(for: topic)!

let selfParticipant = Participant(publicKey: agreementKeys.publicKey.hexRepresentation, metadata: metadata)

let expectedExpiryTimeStamp = Date().addingTimeInterval(TimeInterval(WCSession.defaultTimeToLive))
Expand All @@ -83,6 +82,7 @@ final class SessionEngine {
Task { try? await networkingInteractor.subscribe(topic: topic) }
sessionStore.setSession(session)
networkingInteractor.request(.wcSessionSettle(settleParams), onTopic: topic)
onSessionSettle?(session.publicRepresentation())
}

func delete(topic: String, reason: Reason) async throws {
Expand Down Expand Up @@ -134,7 +134,7 @@ final class SessionEngine {
}

func emit(topic: String, event: SessionType.EventParams.Event, chainId: Blockchain) async throws {
guard let session = sessionStore.getSession(forTopic: topic), session.acknowledged else {
guard let session = sessionStore.getSession(forTopic: topic) else {
logger.debug("Could not find session for topic \(topic)")
return
}
Expand Down Expand Up @@ -297,7 +297,6 @@ final class SessionEngine {
guard var session = sessionStore.getSession(forTopic: topic) else {return}
session.acknowledge()
sessionStore.setSession(session)
onSessionSettle?(session.publicRepresentation())
case .error(let error):
logger.error("Error - session rejected, Reason: \(error)")
networkingInteractor.unsubscribe(topic: topic)
Expand Down
5 changes: 0 additions & 5 deletions Sources/WalletConnectAuth/Storage/SessionStorage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ protocol WCSessionStorage: AnyObject {
func getSession(forTopic topic: String) -> WCSession?
func getAll() -> [WCSession]
func delete(topic: String)
func getAcknowledgedSessions() -> [WCSession]
}

final class SessionStorage: WCSessionStorage {
Expand Down Expand Up @@ -40,8 +39,4 @@ final class SessionStorage: WCSessionStorage {
func delete(topic: String) {
storage.delete(topic: topic)
}

func getAcknowledgedSessions() -> [WCSession] {
getAll().filter {$0.acknowledged}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ class ControllerSessionStateMachineTests: XCTestCase {
storageMock.setSession(session)
let twoDays = 2*Time.day
await XCTAssertNoThrowAsync(try await sut.extend(topic: session.topic, by: Int64(twoDays)))
let extendedSession = storageMock.getAcknowledgedSessions().first{$0.topic == session.topic}!
let extendedSession = storageMock.getAll().first{$0.topic == session.topic}!
XCTAssertEqual(extendedSession.expiryDate.timeIntervalSinceReferenceDate, TimeTraveler.dateByAdding(days: 2).timeIntervalSinceReferenceDate, accuracy: 1)
}

Expand Down
7 changes: 0 additions & 7 deletions Tests/WalletConnectTests/Mocks/WCSessionStorageMock.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,5 @@ final class WCSessionStorageMock: WCSessionStorage {
func delete(topic: String) {
sessions[topic] = nil
}

func getAcknowledgedSessions() -> [WCSession] {
getAll().compactMap {
guard $0.acknowledged else { return nil }
return $0
}
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ class NonControllerSessionStateMachineTests: XCTestCase {
let twoDaysFromNowTimestamp = Int64(TimeTraveler.dateByAdding(days: 2).timeIntervalSince1970)

networkingInteractor.wcRequestPublisherSubject.send(WCRequestSubscriptionPayload.stubUpdateExpiry(topic: session.topic, expiry: twoDaysFromNowTimestamp))
let extendedSession = storageMock.getAcknowledgedSessions().first{$0.topic == session.topic}!
let extendedSession = storageMock.getAll().first{$0.topic == session.topic}!
print(extendedSession.expiryDate)

XCTAssertEqual(extendedSession.expiryDate.timeIntervalSince1970, TimeTraveler.dateByAdding(days: 2).timeIntervalSince1970, accuracy: 1)
Expand All @@ -88,7 +88,7 @@ class NonControllerSessionStateMachineTests: XCTestCase {
networkingInteractor.wcRequestPublisherSubject.send(WCRequestSubscriptionPayload.stubUpdateExpiry(topic: session.topic, expiry: twoDaysFromNowTimestamp))


let potentiallyExtendedSession = storageMock.getAcknowledgedSessions().first{$0.topic == session.topic}!
let potentiallyExtendedSession = storageMock.getAll().first{$0.topic == session.topic}!
XCTAssertEqual(potentiallyExtendedSession.expiryDate.timeIntervalSinceReferenceDate, tomorrow.timeIntervalSinceReferenceDate, accuracy: 1, "expiry date has been extended for peer non controller request ")
}

Expand All @@ -99,7 +99,7 @@ class NonControllerSessionStateMachineTests: XCTestCase {
let tenDaysFromNowTimestamp = Int64(TimeTraveler.dateByAdding(days: 10).timeIntervalSince1970)
networkingInteractor.wcRequestPublisherSubject.send(WCRequestSubscriptionPayload.stubUpdateExpiry(topic: session.topic, expiry: tenDaysFromNowTimestamp))

let potentaillyExtendedSession = storageMock.getAcknowledgedSessions().first{$0.topic == session.topic}!
let potentaillyExtendedSession = storageMock.getAll().first{$0.topic == session.topic}!
XCTAssertEqual(potentaillyExtendedSession.expiryDate.timeIntervalSinceReferenceDate, tomorrow.timeIntervalSinceReferenceDate, accuracy: 1, "expiry date has been extended despite ttl to high")
}

Expand All @@ -110,7 +110,7 @@ class NonControllerSessionStateMachineTests: XCTestCase {
let oneDayFromNowTimestamp = Int64(TimeTraveler.dateByAdding(days: 10).timeIntervalSince1970)

networkingInteractor.wcRequestPublisherSubject.send(WCRequestSubscriptionPayload.stubUpdateExpiry(topic: session.topic, expiry: oneDayFromNowTimestamp))
let potentaillyExtendedSession = storageMock.getAcknowledgedSessions().first{$0.topic == session.topic}!
let potentaillyExtendedSession = storageMock.getAll().first{$0.topic == session.topic}!
XCTAssertEqual(potentaillyExtendedSession.expiryDate.timeIntervalSinceReferenceDate, tomorrow.timeIntervalSinceReferenceDate, accuracy: 1, "expiry date has been extended despite ttl to low")
}
}
1 change: 0 additions & 1 deletion Tests/WalletConnectTests/SessionEngineTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,6 @@ final class SessionEngineTests: XCTestCase {
networkingInteractor.onResponse?(response)

XCTAssertTrue(storageMock.getSession(forTopic: session.topic)!.acknowledged, "Responder must acknowledged session")
XCTAssertTrue(didCallBackOnSessionApproved, "Responder's engine must call back with session")
}

func testHandleSessionSettleError() {
Expand Down