Skip to content

Commit

Permalink
Merge pull request #663 from WalletConnect/push-config
Browse files Browse the repository at this point in the history
[Push] configure echo over push config
  • Loading branch information
llbartekll committed Jan 17, 2023
2 parents c2cba73 + 993de84 commit 198652e
Show file tree
Hide file tree
Showing 9 changed files with 56 additions and 32 deletions.
7 changes: 0 additions & 7 deletions Example/ExampleApp.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
76B149F02821C03B00F05F91 /* Proposal.swift in Sources */ = {isa = PBXBuildFile; fileRef = 76B149EF2821C03B00F05F91 /* Proposal.swift */; };
76B6E39F2807A3B6004DF775 /* WalletViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 76B6E39E2807A3B6004DF775 /* WalletViewController.swift */; };
840BCF142949B9F900CB0655 /* WalletConnectPush in Frameworks */ = {isa = PBXBuildFile; productRef = 840BCF132949B9F900CB0655 /* WalletConnectPush */; };
840BCF162949C6C100CB0655 /* WalletConnectEcho in Frameworks */ = {isa = PBXBuildFile; productRef = 840BCF152949C6C100CB0655 /* WalletConnectEcho */; };
8439CB89293F658E00F2F2E2 /* PushMessage.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8439CB88293F658E00F2F2E2 /* PushMessage.swift */; };
8448F1D427E4726F0000B866 /* WalletConnect in Frameworks */ = {isa = PBXBuildFile; productRef = 8448F1D327E4726F0000B866 /* WalletConnect */; };
84494388278D9C1B00CC26BB /* UIAlertController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 84494387278D9C1B00CC26BB /* UIAlertController.swift */; };
Expand Down Expand Up @@ -524,7 +523,6 @@
A5AE354728A1A2AC0059AE8A /* Web3 in Frameworks */,
A5434023291E6A270068F706 /* SolanaSwift in Frameworks */,
849D7A90292665D3006A2BD4 /* WalletConnectVerify in Frameworks */,
840BCF162949C6C100CB0655 /* WalletConnectEcho in Frameworks */,
8485617D295079730064877B /* WalletConnectPairing in Frameworks */,
764E1D5826F8DBAB00A1FB15 /* WalletConnect in Frameworks */,
840BCF142949B9F900CB0655 /* WalletConnectPush in Frameworks */,
Expand Down Expand Up @@ -1550,7 +1548,6 @@
A5434022291E6A270068F706 /* SolanaSwift */,
849D7A8F292665D3006A2BD4 /* WalletConnectVerify */,
840BCF132949B9F900CB0655 /* WalletConnectPush */,
840BCF152949C6C100CB0655 /* WalletConnectEcho */,
8485617C295079730064877B /* WalletConnectPairing */,
);
productName = ExampleApp;
Expand Down Expand Up @@ -2724,10 +2721,6 @@
isa = XCSwiftPackageProductDependency;
productName = WalletConnectPush;
};
840BCF152949C6C100CB0655 /* WalletConnectEcho */ = {
isa = XCSwiftPackageProductDependency;
productName = WalletConnectEcho;
};
8448F1D327E4726F0000B866 /* WalletConnect */ = {
isa = XCSwiftPackageProductDependency;
productName = WalletConnect;
Expand Down
13 changes: 5 additions & 8 deletions Example/ExampleApp/AppDelegate.swift
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import UIKit
import UserNotifications
import WalletConnectNetworking
import WalletConnectEcho
import WalletConnectPairing
import WalletConnectPush
import Combine
Expand All @@ -23,13 +22,11 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
Networking.configure(projectId: InputConfig.projectId, socketFactory: DefaultSocketFactory())
Pair.configure(metadata: metadata)

let clientId = try! Networking.interactor.getClientId()
let sanitizedClientId = clientId.replacingOccurrences(of: "did:key:", with: "")

Echo.configure(projectId: InputConfig.projectId, clientId: sanitizedClientId)
Push.wallet.requestPublisher.sink { id, _ in
Push.configure()
Push.wallet.requestPublisher.sink { (id: RPCID, account: Account, metadata: AppMetadata) in
Task(priority: .high) { try! await Push.wallet.approve(id: id) }
}.store(in: &publishers)

Push.wallet.pushMessagePublisher.sink { pm in
print(pm)
}.store(in: &publishers)
Expand Down Expand Up @@ -70,7 +67,7 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
let deviceToken = InputConfig.simulatorIdentifier
assert(deviceToken != "SIMULATOR_IDENTIFIER", "Please set your Simulator identifier")
Task(priority: .high) {
try await Echo.instance.register(deviceToken: deviceToken)
try await Push.wallet.register(deviceToken: deviceToken)
}
}.store(in: &self!.publishers)
#endif
Expand All @@ -89,7 +86,7 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data
) {
Task(priority: .high) {
try await Echo.instance.register(deviceToken: deviceToken)
try await Push.wallet.register(deviceToken: deviceToken)
}
}

Expand Down
12 changes: 6 additions & 6 deletions Example/IntegrationTests/Push/PushTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ final class PushTests: XCTestCase {
try! await walletPairingClient.pair(uri: uri)
try! await dappPushClient.request(account: Account.stub(), topic: uri.topic)

walletPushClient.requestPublisher.sink { (_, _) in
walletPushClient.requestPublisher.sink { (_, _, _) in
expectation.fulfill()
}
.store(in: &publishers)
Expand All @@ -105,7 +105,7 @@ final class PushTests: XCTestCase {
try! await walletPairingClient.pair(uri: uri)
try! await dappPushClient.request(account: Account.stub(), topic: uri.topic)

walletPushClient.requestPublisher.sink { [unowned self] (id, _) in
walletPushClient.requestPublisher.sink { [unowned self] (id, _, _) in

Task(priority: .high) { try! await walletPushClient.approve(id: id) }
}.store(in: &publishers)
Expand All @@ -128,7 +128,7 @@ final class PushTests: XCTestCase {
try! await walletPairingClient.pair(uri: uri)
try! await dappPushClient.request(account: Account.stub(), topic: uri.topic)

walletPushClient.requestPublisher.sink { [unowned self] (id, _) in
walletPushClient.requestPublisher.sink { [unowned self] (id, _, _) in

Task(priority: .high) { try! await walletPushClient.reject(id: id) }
}.store(in: &publishers)
Expand All @@ -151,7 +151,7 @@ final class PushTests: XCTestCase {
try! await walletPairingClient.pair(uri: uri)
try! await dappPushClient.request(account: Account.stub(), topic: uri.topic)

walletPushClient.requestPublisher.sink { [unowned self] (id, _) in
walletPushClient.requestPublisher.sink { [unowned self] (id, _, _) in
Task(priority: .high) { try! await walletPushClient.approve(id: id) }
}.store(in: &publishers)

Expand Down Expand Up @@ -179,7 +179,7 @@ final class PushTests: XCTestCase {
try! await dappPushClient.request(account: Account.stub(), topic: uri.topic)
var subscriptionTopic: String!

walletPushClient.requestPublisher.sink { [unowned self] (id, _) in
walletPushClient.requestPublisher.sink { [unowned self] (id, _, _) in
Task(priority: .high) { try! await walletPushClient.approve(id: id) }
}.store(in: &publishers)

Expand All @@ -206,7 +206,7 @@ final class PushTests: XCTestCase {
try! await dappPushClient.request(account: Account.stub(), topic: uri.topic)
var subscriptionTopic: String!

walletPushClient.requestPublisher.sink { [unowned self] (id, _) in
walletPushClient.requestPublisher.sink { [unowned self] (id, _, _) in
Task(priority: .high) { try! await walletPushClient.approve(id: id) }
}.store(in: &publishers)

Expand Down
10 changes: 5 additions & 5 deletions Sources/WalletConnectEcho/Echo.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import Foundation
import WalletConnectNetworking

public class Echo {

Expand All @@ -8,7 +9,7 @@ public class Echo {
}

return EchoClientFactory.create(
projectId: config.projectId,
projectId: Networking.projectId,
clientId: config.clientId)
}()

Expand All @@ -17,9 +18,8 @@ public class Echo {
private init() { }

/// Echo instance config method
/// - Parameters:
/// - tenantId:
static public func configure(projectId: String, clientId: String) {
Echo.config = Echo.Config(clientId: clientId, projectId: projectId)
/// - Parameter clientId: https://github.com/WalletConnect/walletconnect-docs/blob/main/docs/specs/clients/core/relay/relay-client-auth.md#overview
static public func configure(clientId: String) {
Echo.config = Echo.Config(clientId: clientId)
}
}
1 change: 0 additions & 1 deletion Sources/WalletConnectEcho/EchoConfig.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,5 @@ import Foundation
extension Echo {
struct Config {
let clientId: String
let projectId: String
}
}
8 changes: 6 additions & 2 deletions Sources/WalletConnectEcho/Register/EchoRegisterService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ actor EchoRegisterService {
private let projectId: String
private let clientId: String
private let logger: ConsoleLogging
// DID method specific identifier
private var clientIdMutlibase: String {
return clientId.replacingOccurrences(of: "did:key:", with: "")
}

enum Errors: Error {
case registrationFailed
Expand All @@ -27,7 +31,7 @@ actor EchoRegisterService {
logger.debug("APNS device token: \(token)")
let response = try await httpClient.request(
EchoResponse.self,
at: EchoAPI.register(clientId: clientId, token: token, projectId: projectId)
at: EchoAPI.register(clientId: clientIdMutlibase, token: token, projectId: projectId)
)
guard response.status == .ok else {
throw Errors.registrationFailed
Expand All @@ -38,7 +42,7 @@ actor EchoRegisterService {
public func register(deviceToken: String) async throws {
let response = try await httpClient.request(
EchoResponse.self,
at: EchoAPI.register(clientId: clientId, token: deviceToken, projectId: projectId)
at: EchoAPI.register(clientId: clientIdMutlibase, token: deviceToken, projectId: projectId)
)
guard response.status == .ok else {
throw Errors.registrationFailed
Expand Down
14 changes: 11 additions & 3 deletions Sources/WalletConnectPush/Client/Wallet/WalletPushClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ public class WalletPushClient {

private var publishers = Set<AnyCancellable>()

private let requestPublisherSubject = PassthroughSubject<(id: RPCID, metadata: AppMetadata), Never>()
private let requestPublisherSubject = PassthroughSubject<(id: RPCID, account: Account, metadata: AppMetadata), Never>()

public var requestPublisher: AnyPublisher<(id: RPCID, metadata: AppMetadata), Never> {
public var requestPublisher: AnyPublisher<(id: RPCID, account: Account, metadata: AppMetadata), Never> {
requestPublisherSubject.eraseToAnyPublisher()
}

Expand Down Expand Up @@ -88,7 +88,7 @@ private extension WalletPushClient {

pairingRegisterer.register(method: protocolMethod)
.sink { [unowned self] (payload: RequestSubscriptionPayload<PushRequestParams>) in
requestPublisherSubject.send((id: payload.id, metadata: payload.request.metadata))
requestPublisherSubject.send((id: payload.id, account: payload.request.account, metadata: payload.request.metadata))
}.store(in: &publishers)

pushMessageSubscriber.onPushMessage = { [unowned self] pushMessage in
Expand All @@ -99,3 +99,11 @@ private extension WalletPushClient {
}
}
}

#if targetEnvironment(simulator)
extension WalletPushClient {
public func register(deviceToken: String) async throws {
try await echoClient.register(deviceToken: deviceToken)
}
}
#endif
16 changes: 16 additions & 0 deletions Sources/WalletConnectPush/Push.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ import WalletConnectPairing
import WalletConnectEcho

public class Push {
enum Errors: Error {
case failedToGetClientId
}

public static var dapp: DappPushClient = {
return DappPushClientFactory.create(
Expand All @@ -13,12 +16,25 @@ public class Push {
}()

public static var wallet: WalletPushClient = {
guard let config = Push.config else {
fatalError("Error - you must call Push.configure(_:) before accessing the shared wallet instance.")
}
Echo.configure(clientId: config.clientId)
return WalletPushClientFactory.create(
networkInteractor: Networking.interactor,
pairingRegisterer: Pair.registerer,
echoClient: Echo.instance
)
}()

private static var config: Config?

private init() { }

/// Wallet's configuration method
static public func configure() {
let clientId = try! Networking.interactor.getClientId()
Push.config = Push.Config(clientId: clientId)
}

}
7 changes: 7 additions & 0 deletions Sources/WalletConnectPush/PushConfig.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import Foundation

extension Push {
struct Config {
let clientId: String
}
}

0 comments on commit 198652e

Please sign in to comment.