-
Notifications
You must be signed in to change notification settings - Fork 183
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1187 from WalletConnect/dapp-extend-session
[Sign] Dapp extend session exp
- Loading branch information
Showing
12 changed files
with
216 additions
and
113 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
66 changes: 66 additions & 0 deletions
66
Sources/WalletConnectSign/Engine/Common/SessionExtendRequestSubscriber.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import Foundation | ||
import Combine | ||
|
||
final class SessionExtendRequestSubscriber { | ||
var onExtend: ((String, Date) -> Void)? | ||
|
||
private let sessionStore: WCSessionStorage | ||
private let networkingInteractor: NetworkInteracting | ||
private var publishers = [AnyCancellable]() | ||
private let logger: ConsoleLogging | ||
|
||
init( | ||
networkingInteractor: NetworkInteracting, | ||
sessionStore: WCSessionStorage, | ||
logger: ConsoleLogging | ||
) { | ||
self.networkingInteractor = networkingInteractor | ||
self.sessionStore = sessionStore | ||
self.logger = logger | ||
|
||
setupSubscriptions() | ||
} | ||
} | ||
|
||
// MARK: - Private functions | ||
extension SessionExtendRequestSubscriber { | ||
private func setupSubscriptions() { | ||
networkingInteractor.requestSubscription(on: SessionExtendProtocolMethod()) | ||
.sink { [unowned self] (payload: RequestSubscriptionPayload<SessionType.UpdateExpiryParams>) in | ||
onSessionUpdateExpiry(payload: payload, updateExpiryParams: payload.request) | ||
}.store(in: &publishers) | ||
} | ||
|
||
private func onSessionUpdateExpiry(payload: SubscriptionPayload, updateExpiryParams: SessionType.UpdateExpiryParams) { | ||
let protocolMethod = SessionExtendProtocolMethod() | ||
let topic = payload.topic | ||
guard var session = sessionStore.getSession(forTopic: topic) else { | ||
return respondError(payload: payload, reason: .noSessionForTopic, protocolMethod: protocolMethod) | ||
} | ||
guard session.peerIsController else { | ||
return respondError(payload: payload, reason: .unauthorizedExtendRequest, protocolMethod: protocolMethod) | ||
} | ||
do { | ||
try session.updateExpiry(to: updateExpiryParams.expiry) | ||
} catch { | ||
return respondError(payload: payload, reason: .invalidExtendRequest, protocolMethod: protocolMethod) | ||
} | ||
sessionStore.setSession(session) | ||
|
||
Task(priority: .high) { | ||
try await networkingInteractor.respondSuccess(topic: payload.topic, requestId: payload.id, protocolMethod: protocolMethod) | ||
} | ||
|
||
onExtend?(session.topic, session.expiryDate) | ||
} | ||
|
||
private func respondError(payload: SubscriptionPayload, reason: SignReasonCode, protocolMethod: ProtocolMethod) { | ||
Task(priority: .high) { | ||
do { | ||
try await networkingInteractor.respondError(topic: payload.topic, requestId: payload.id, protocolMethod: protocolMethod, reason: reason) | ||
} catch { | ||
logger.error("Respond Error failed with: \(error.localizedDescription)") | ||
} | ||
} | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
Sources/WalletConnectSign/Engine/Common/SessionExtendRequester.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import Foundation | ||
|
||
final class SessionExtendRequester { | ||
private let sessionStore: WCSessionStorage | ||
private let networkingInteractor: NetworkInteracting | ||
|
||
init( | ||
sessionStore: WCSessionStorage, | ||
networkingInteractor: NetworkInteracting | ||
) { | ||
self.sessionStore = sessionStore | ||
self.networkingInteractor = networkingInteractor | ||
} | ||
|
||
func extend(topic: String, by ttl: Int64) async throws { | ||
guard var session = sessionStore.getSession(forTopic: topic) else { | ||
throw WalletConnectError.noSessionMatchingTopic(topic) | ||
} | ||
|
||
let protocolMethod = SessionExtendProtocolMethod() | ||
try session.updateExpiry(by: ttl) | ||
let newExpiry = Int64(session.expiryDate.timeIntervalSince1970) | ||
sessionStore.setSession(session) | ||
let request = RPCRequest(method: protocolMethod.method, params: SessionType.UpdateExpiryParams(expiry: newExpiry)) | ||
try await networkingInteractor.request(request, topic: topic, protocolMethod: protocolMethod) | ||
} | ||
} |
48 changes: 48 additions & 0 deletions
48
Sources/WalletConnectSign/Engine/Common/SessionExtendResponseSubscriber.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import Foundation | ||
import Combine | ||
|
||
final class SessionExtendResponseSubscriber { | ||
var onExtend: ((String, Date) -> Void)? | ||
|
||
private let sessionStore: WCSessionStorage | ||
private let networkingInteractor: NetworkInteracting | ||
private var publishers = [AnyCancellable]() | ||
private let logger: ConsoleLogging | ||
|
||
init( | ||
networkingInteractor: NetworkInteracting, | ||
sessionStore: WCSessionStorage, | ||
logger: ConsoleLogging | ||
) { | ||
self.networkingInteractor = networkingInteractor | ||
self.sessionStore = sessionStore | ||
self.logger = logger | ||
|
||
setupSubscriptions() | ||
} | ||
|
||
// MARK: - Handle Response | ||
private func setupSubscriptions() { | ||
networkingInteractor.responseSubscription(on: SessionExtendProtocolMethod()) | ||
.sink { [unowned self] (payload: ResponseSubscriptionPayload<SessionType.UpdateExpiryParams, RPCResult>) in | ||
handleUpdateExpiryResponse(payload: payload) | ||
} | ||
.store(in: &publishers) | ||
} | ||
|
||
private func handleUpdateExpiryResponse(payload: ResponseSubscriptionPayload<SessionType.UpdateExpiryParams, RPCResult>) { | ||
guard var session = sessionStore.getSession(forTopic: payload.topic) else { return } | ||
switch payload.response { | ||
case .response: | ||
do { | ||
try session.updateExpiry(to: payload.request.expiry) | ||
sessionStore.setSession(session) | ||
onExtend?(session.topic, session.expiryDate) | ||
} catch { | ||
logger.error("Update expiry error: \(error.localizedDescription)") | ||
} | ||
case .error: | ||
logger.error("Peer failed to extend session") | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.