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

[Push] Add push messages history getter, add account to subscription #675

Merged
merged 5 commits into from
Jan 25, 2023
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
7 changes: 6 additions & 1 deletion Example/IntegrationTests/Push/PushTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ final class PushTests: XCTestCase {
func testDappSendsPushMessage() async {
let expectation = expectation(description: "expects wallet to receive push message")
let pushMessage = PushMessage.stub()
var pushSubscription: PushSubscription!
let uri = try! await dappPairingClient.create()
try! await walletPairingClient.pair(uri: uri)
try! await dappPushClient.request(account: Account.stub(), topic: uri.topic)
Expand All @@ -160,14 +161,18 @@ final class PushTests: XCTestCase {
XCTFail()
return
}
pushSubscription = subscription
Task(priority: .userInitiated) { try! await dappPushClient.notify(topic: subscription.topic, message: pushMessage) }
}.store(in: &publishers)

walletPushClient.pushMessagePublisher.sink { receivedPushMessage in
walletPushClient.pushMessagePublisher.sink { [unowned self] receivedPushMessage in
let messageHistory = walletPushClient.getMessageHistory(topic: pushSubscription.topic)
XCTAssertEqual(pushMessage, receivedPushMessage)
XCTAssertTrue(messageHistory.contains(receivedPushMessage))
expectation.fulfill()
}.store(in: &publishers)


wait(for: [expectation], timeout: InputConfig.defaultTimeout)

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ class ProposalResponseSubscriber {
let selfpublicKeyHex = payload.request.publicKey
let (topic, _) = try generateAgreementKeys(peerPublicKeyHex: peerPublicKeyHex, selfpublicKeyHex: selfpublicKeyHex)

let pushSubscription = PushSubscription(topic: topic, relay: relay, metadata: metadata)
let pushSubscription = PushSubscription(topic: topic, account: payload.request.account, relay: relay, metadata: metadata)
subscriptionsStore.set(pushSubscription, forKey: topic)
kms.deletePrivateKey(for: selfpublicKeyHex)
try await networkingInteractor.subscribe(topic: topic)
Expand Down
15 changes: 15 additions & 0 deletions Sources/WalletConnectPush/Client/Wallet/PushMessagesProvider.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@

import Foundation
import WalletConnectUtils

class PushMessagesProvider {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

private let history: RPCHistory

init(history: RPCHistory) {
self.history = history
}

public func getMessageHistory(topic: String) -> [PushMessage] {
history.getAll(of: PushMessage.self, topic: topic)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ class PushRequestResponder {
let response = RPCResponse(id: requestId, result: responseParams)

let requestParams = try requestRecord.request.params!.get(PushRequestParams.self)
let pushSubscription = PushSubscription(topic: pushTopic, relay: RelayProtocolOptions(protocol: "irn", data: nil), metadata: requestParams.metadata)
let pushSubscription = PushSubscription(topic: pushTopic, account: requestParams.account, relay: RelayProtocolOptions(protocol: "irn", data: nil), metadata: requestParams.metadata)
subscriptionsStore.set(pushSubscription, forKey: pushTopic)

try await networkingInteractor.respond(topic: pairingTopic, response: response, protocolMethod: PushRequestProtocolMethod())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ public class WalletPushClient {
private let proposeResponder: PushRequestResponder
private let pushMessageSubscriber: PushMessageSubscriber
private let subscriptionsProvider: SubscriptionsProvider
private let pushMessagesProvider: PushMessagesProvider
private let resubscribeService: PushResubscribeService

init(logger: ConsoleLogging,
Expand All @@ -45,6 +46,7 @@ public class WalletPushClient {
proposeResponder: PushRequestResponder,
pushMessageSubscriber: PushMessageSubscriber,
subscriptionsProvider: SubscriptionsProvider,
pushMessagesProvider: PushMessagesProvider,
deletePushSubscriptionService: DeletePushSubscriptionService,
deletePushSubscriptionSubscriber: DeletePushSubscriptionSubscriber,
resubscribeService: PushResubscribeService) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

resubscribeService unused in PushClient

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it is in use, it's calling setUpResubscription func after initialisation

Expand All @@ -54,6 +56,7 @@ public class WalletPushClient {
self.echoClient = echoClient
self.pushMessageSubscriber = pushMessageSubscriber
self.subscriptionsProvider = subscriptionsProvider
self.pushMessagesProvider = pushMessagesProvider
self.deletePushSubscriptionService = deletePushSubscriptionService
self.deletePushSubscriptionSubscriber = deletePushSubscriptionSubscriber
self.resubscribeService = resubscribeService
Expand All @@ -72,6 +75,10 @@ public class WalletPushClient {
subscriptionsProvider.getActiveSubscriptions()
}

public func getMessageHistory(topic: String) -> [PushMessage] {
pushMessagesProvider.getMessageHistory(topic: topic)
}

public func delete(topic: String) async throws {
try await deletePushSubscriptionService.delete(topic: topic)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ public struct WalletPushClientFactory {
let deletePushSubscriptionService = DeletePushSubscriptionService(networkingInteractor: networkInteractor, kms: kms, logger: logger, pushSubscriptionStore: subscriptionStore)
let deletePushSubscriptionSubscriber = DeletePushSubscriptionSubscriber(networkingInteractor: networkInteractor, kms: kms, logger: logger, pushSubscriptionStore: subscriptionStore)
let resubscribeService = PushResubscribeService(networkInteractor: networkInteractor, subscriptionsStorage: subscriptionStore)
let pushMessagesProvider = PushMessagesProvider(history: history)
return WalletPushClient(
logger: logger,
kms: kms,
Expand All @@ -44,6 +45,7 @@ public struct WalletPushClientFactory {
proposeResponder: proposeResponder,
pushMessageSubscriber: pushMessageSubscriber,
subscriptionsProvider: subscriptionProvider,
pushMessagesProvider: pushMessagesProvider,
deletePushSubscriptionService: deletePushSubscriptionService,
deletePushSubscriptionSubscriber: deletePushSubscriptionSubscriber,
resubscribeService: resubscribeService
Expand Down
1 change: 1 addition & 0 deletions Sources/WalletConnectPush/Types/PushSubscription.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import WalletConnectPairing

public struct PushSubscription: Codable, Equatable {
public let topic: String
public let account: Account
public let relay: RelayProtocolOptions
public let metadata: AppMetadata
}
6 changes: 6 additions & 0 deletions Sources/WalletConnectUtils/RPCHistory/RPCHistory.swift
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,12 @@ public final class RPCHistory {
}
}

public func getAll<Object: Codable>(of type: Object.Type, topic: String) -> [Object] {
return storage.getAll()
.filter{$0.topic == topic}
.compactMap { try? $0.request.params?.get(Object.self) }
}

public func getAll<Object: Codable>(of type: Object.Type) -> [Object] {
return getAllWithIDs(of: type).map { $0.value }
}
Expand Down