Skip to content

Commit

Permalink
Merge pull request #635 from WalletConnect/decryption-service
Browse files Browse the repository at this point in the history
[Push, Echo] exposes all push client methods with implementations
  • Loading branch information
llbartekll authored Dec 19, 2022
2 parents 8db3be7 + 4a6fac1 commit a67da75
Show file tree
Hide file tree
Showing 62 changed files with 500 additions and 194 deletions.
4 changes: 2 additions & 2 deletions Example/DApp/Sign/Connect/ConnectViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -95,11 +95,11 @@ class ConnectViewController: UIViewController, UITableViewDataSource, UITableVie
),
"solana": ProposalNamespace(
chains: [
Blockchain("solana:4sGjMW1sUnHzSxGspuhpqLDx6wiyjNtZ")!,
Blockchain("solana:4sGjMW1sUnHzSxGspuhpqLDx6wiyjNtZ")!
],
methods: [
"solana_signMessage",
"solana_signTransaction",
"solana_signTransaction"
], events: [], extensions: nil
)
]
Expand Down
4 changes: 2 additions & 2 deletions Example/DApp/Sign/SelectChain/SelectChainViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,11 @@ class SelectChainViewController: UIViewController, UITableViewDataSource {
),
"solana": ProposalNamespace(
chains: [
Blockchain("solana:4sGjMW1sUnHzSxGspuhpqLDx6wiyjNtZ")!,
Blockchain("solana:4sGjMW1sUnHzSxGspuhpqLDx6wiyjNtZ")!
],
methods: [
"solana_signMessage",
"solana_signTransaction",
"solana_signTransaction"
], events: [], extensions: nil
)
]
Expand Down
23 changes: 22 additions & 1 deletion Example/ExampleApp.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,17 @@
7694A5262874296A0001257E /* RegistryTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7694A5252874296A0001257E /* RegistryTests.swift */; };
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 */; };
845B8D8C2934B36C0084A966 /* Account.swift in Sources */ = {isa = PBXBuildFile; fileRef = 845B8D8B2934B36C0084A966 /* Account.swift */; };
8460DCFC274F98A10081F94C /* RequestViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8460DCFB274F98A10081F94C /* RequestViewController.swift */; };
847CF3AF28E3141700F1D760 /* WalletConnectPush in Frameworks */ = {isa = PBXBuildFile; productRef = 847CF3AE28E3141700F1D760 /* WalletConnectPush */; settings = {ATTRIBUTES = (Required, ); }; };
849D7A93292E2169006A2BD4 /* PushTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 849D7A92292E2169006A2BD4 /* PushTests.swift */; };
8485617D295079730064877B /* WalletConnectPairing in Frameworks */ = {isa = PBXBuildFile; productRef = 8485617C295079730064877B /* WalletConnectPairing */; };
849D7A90292665D3006A2BD4 /* WalletConnectVerify in Frameworks */ = {isa = PBXBuildFile; productRef = 849D7A8F292665D3006A2BD4 /* WalletConnectVerify */; };
849D7A93292E2169006A2BD4 /* PushTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 849D7A92292E2169006A2BD4 /* PushTests.swift */; };
84AA01DB28CF0CD7005D48D8 /* XCTest.swift in Sources */ = {isa = PBXBuildFile; fileRef = 84AA01DA28CF0CD7005D48D8 /* XCTest.swift */; };
84CE641F27981DED00142511 /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 84CE641E27981DED00142511 /* AppDelegate.swift */; };
84CE642127981DED00142511 /* SceneDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 84CE642027981DED00142511 /* SceneDelegate.swift */; };
Expand Down Expand Up @@ -399,7 +402,10 @@
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 */,
A5D85226286333D500DAF5C3 /* Starscream in Frameworks */,
A5C4DD8728A2DE88006A626D /* WalletConnectRouter in Frameworks */,
);
Expand Down Expand Up @@ -1198,6 +1204,9 @@
A5C4DD8628A2DE88006A626D /* WalletConnectRouter */,
A5434022291E6A270068F706 /* SolanaSwift */,
849D7A8F292665D3006A2BD4 /* WalletConnectVerify */,
840BCF132949B9F900CB0655 /* WalletConnectPush */,
840BCF152949C6C100CB0655 /* WalletConnectEcho */,
8485617C295079730064877B /* WalletConnectPairing */,
);
productName = ExampleApp;
productReference = 764E1D3C26F8D3FC00A1FB15 /* WalletConnect Wallet.app */;
Expand Down Expand Up @@ -2097,6 +2106,14 @@
isa = XCSwiftPackageProductDependency;
productName = WalletConnect;
};
840BCF132949B9F900CB0655 /* WalletConnectPush */ = {
isa = XCSwiftPackageProductDependency;
productName = WalletConnectPush;
};
840BCF152949C6C100CB0655 /* WalletConnectEcho */ = {
isa = XCSwiftPackageProductDependency;
productName = WalletConnectEcho;
};
8448F1D327E4726F0000B866 /* WalletConnect */ = {
isa = XCSwiftPackageProductDependency;
productName = WalletConnect;
Expand All @@ -2105,6 +2122,10 @@
isa = XCSwiftPackageProductDependency;
productName = WalletConnectPush;
};
8485617C295079730064877B /* WalletConnectPairing */ = {
isa = XCSwiftPackageProductDependency;
productName = WalletConnectPairing;
};
849D7A8F292665D3006A2BD4 /* WalletConnectVerify */ = {
isa = XCSwiftPackageProductDependency;
productName = WalletConnectVerify;
Expand Down
102 changes: 42 additions & 60 deletions Example/ExampleApp/AppDelegate.swift
Original file line number Diff line number Diff line change
@@ -1,20 +1,38 @@
import UIKit
import UserNotifications
import WalletConnectNetworking
import WalletConnectEcho
import WalletConnectPairing
import WalletConnectPush
import Combine

@main
class AppDelegate: UIResponder, UIApplicationDelegate {
private var publishers = [AnyCancellable]()

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
registerForPushNotifications()

let notificationOption = launchOptions?[.remoteNotification]

// 1
if
let notification = notificationOption as? [String: AnyObject],
let aps = notification["aps"] as? [String: AnyObject] {
}
let metadata = AppMetadata(
name: "Example Wallet",
description: "wallet description",
url: "example.wallet",
icons: ["https://avatars.githubusercontent.com/u/37784886"])

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
Task(priority: .high) { try! await Push.wallet.approve(id: id) }
}.store(in: &publishers)
Push.wallet.pushMessagePublisher.sink { pm in
print(pm)
}.store(in: &publishers)

return true
}
Expand All @@ -27,7 +45,7 @@ class AppDelegate: UIResponder, UIApplicationDelegate {

func application(_ application: UIApplication, didDiscardSceneSessions sceneSessions: Set<UISceneSession>) {
}

func getNotificationSettings() {
UNUserNotificationCenter.current().getNotificationSettings { settings in
print("Notification settings: \(settings)")
Expand All @@ -37,7 +55,7 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
}
}
}

func registerForPushNotifications() {
UNUserNotificationCenter.current()
.requestAuthorization(
Expand All @@ -46,39 +64,33 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
guard granted else { return }
self?.getNotificationSettings()
#if targetEnvironment(simulator)
// Avoid networking not instantiated error by sleeping...
Thread.sleep(forTimeInterval: 1)
let clientId = try! Networking.interactor.getClientId()
let deviceToken = InputConfig.simulatorIdentifier
assert(deviceToken != "SIMULATOR_IDENTIFIER", "Please set your Simulator identifier")
self?.registerClientWithPushServer(clientId: clientId, deviceToken: deviceToken, then: { result in

})

// let clientId = try! Networking.interactor.socketConnectionStatusPublisher
// .first {$0 == .connected}
// .sink(receiveValue: { status in
// let deviceToken = InputConfig.simulatorIdentifier
// assert(deviceToken != "SIMULATOR_IDENTIFIER", "Please set your Simulator identifier")
// Task(priority: .high) {
// try await Echo.instance.register(deviceToken: deviceToken)
// }
// })
#endif
// print(result)
// }
}
}
}

func modelIdentifier() -> String {
if let simulatorModelIdentifier = ProcessInfo().environment["SIMULATOR_MODEL_IDENTIFIER"] { return simulatorModelIdentifier }
var sysinfo = utsname()
uname(&sysinfo) // ignore return value
return String(bytes: Data(bytes: &sysinfo.machine, count: Int(_SYS_NAMELEN)), encoding: .ascii)!.trimmingCharacters(in: .controlCharacters)
}

func application(
_ application: UIApplication,
didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data
) {
let tokenParts = deviceToken.map { data in String(format: "%02.2hhx", data) }
let token = tokenParts.joined()
let clientId = try! Networking.interactor.getClientId()
let sanitizedClientId = clientId.replacingOccurrences(of: "did:key:", with: "")
print(sanitizedClientId)
print(token)
registerClientWithPushServer(clientId: sanitizedClientId, deviceToken: token) { result in
print("Successfully registered")
Task(priority: .high) {
try await Echo.instance.register(deviceToken: deviceToken)
}
}

Expand All @@ -89,35 +101,5 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
// TODO: when is this invoked?
print("Failed to register: \(error)")
}

func registerClientWithPushServer(
clientId: String,
deviceToken: String,
then handler: @escaping (Result<Data, Error>) -> Void
) {
//Request Body
let json: [String: Any] = ["client_id": clientId, "type": "apns", "token": deviceToken]
let jsonData = try? JSONSerialization.data(withJSONObject: json)

// create post request
let url = URL(string: "https://echo.walletconnect.com/a43fdd31-5f92-43a3-88f8-98664b313113/clients")!
var request = URLRequest(url: url)
request.httpMethod = "POST"
request.addValue("application/json", forHTTPHeaderField: "Content-Type")
request.httpBody = jsonData

// Send request
let task = URLSession.shared.dataTask(with: request) { data, response, error in
guard let data = data, error == nil else {
// TODO: Error handling?
return
}
let responseJSON = try? JSONSerialization.jsonObject(with: data, options: [])
if let responseJSON = responseJSON as? [String: Any] {
// TODO: Error handling?
print(responseJSON)
}
}
task.resume()
}

}
10 changes: 2 additions & 8 deletions Example/ExampleApp/SceneDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,15 @@ import WalletConnectSign
import WalletConnectNetworking
import WalletConnectRelay
import WalletConnectPairing
import Combine

class SceneDelegate: UIResponder, UIWindowSceneDelegate {

var window: UIWindow?
private var publishers = [AnyCancellable]()

func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {

let metadata = AppMetadata(
name: "Example Wallet",
description: "wallet description",
url: "example.wallet",
icons: ["https://avatars.githubusercontent.com/u/37784886"])

Networking.configure(projectId: InputConfig.projectId, socketFactory: DefaultSocketFactory())
Pair.configure(metadata: metadata)
#if DEBUG
if CommandLine.arguments.contains("-cleanInstall") {
try? Sign.instance.cleanup()
Expand Down
2 changes: 1 addition & 1 deletion Example/ExampleApp/Shared/Signer/SOLSigner.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ struct SOLSigner {
}
}

fileprivate struct SolSignTransaction: Codable {
private struct SolSignTransaction: Codable {
let instructions: [TransactionInstruction]
let recentBlockhash: String
let feePayer: PublicKey
Expand Down
10 changes: 4 additions & 6 deletions Example/IntegrationTests/Pairing/PairingTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,10 @@ final class PairingTests: XCTestCase {
keychainStorage: keychain,
networkingClient: networkingClient)


return (pairingClient, networkingClient, keychain, keyValueStorage)
}

func makeDappClients() {
func makeDappClients() {
let prefix = "🤖 Dapp: "
let (pairingClient, networkingInteractor, keychain, keyValueStorage) = makeClientDependencies(prefix: prefix)
let pushLogger = ConsoleLogger(suffix: prefix + " [Push]", loggingLevel: .debug)
Expand All @@ -65,12 +64,12 @@ final class PairingTests: XCTestCase {
networkInteractor: networkingInteractor)
}

func makeWalletClients() {
func makeWalletClients() {
let prefix = "🐶 Wallet: "
let (pairingClient, networkingInteractor, keychain, keyValueStorage) = makeClientDependencies(prefix: prefix)
let pushLogger = ConsoleLogger(suffix: prefix + " [Push]", loggingLevel: .debug)
walletPairingClient = pairingClient
let echoClient = EchoClientFactory.create(tenantId: "", clientId: "")
let echoClient = EchoClientFactory.create(projectId: "", clientId: "")
walletPushClient = WalletPushClientFactory.create(logger: pushLogger,
keyValueStorage: keyValueStorage,
keychainStorage: keychain,
Expand Down Expand Up @@ -125,7 +124,7 @@ final class PairingTests: XCTestCase {
let expectation = expectation(description: "wallet responds unsupported method for unregistered method")

appPushClient.responsePublisher.sink { (_, response) in
XCTAssertEqual(response, .failure(WalletConnectPairing.PairError(code: 10001)!))
XCTAssertEqual(response, .failure(PushError(code: 10001)!))
expectation.fulfill()
}.store(in: &publishers)

Expand All @@ -136,7 +135,6 @@ final class PairingTests: XCTestCase {
try! await appPushClient.request(account: Account.stub(), topic: uri.topic)

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

}

func testDisconnect() {
Expand Down
Loading

0 comments on commit a67da75

Please sign in to comment.