Skip to content

Commit

Permalink
Merge pull request #80 from WalletConnect/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
llbartekll authored Feb 18, 2022
2 parents 784eafb + 01e523d commit 5ea0265
Show file tree
Hide file tree
Showing 97 changed files with 1,236 additions and 837 deletions.
10 changes: 10 additions & 0 deletions .swiftpm/xcode/xcshareddata/xcschemes/WalletConnect.xcscheme
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,16 @@
ReferencedContainer = "container:">
</BuildableReference>
</TestableReference>
<TestableReference
skipped = "NO">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "WalletConnectKMSTests"
BuildableName = "WalletConnectKMSTests"
BlueprintName = "WalletConnectKMSTests"
ReferencedContainer = "container:">
</BuildableReference>
</TestableReference>
</Testables>
</TestAction>
<LaunchAction
Expand Down
11 changes: 4 additions & 7 deletions Example/DApp/ClientDelegate.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import WalletConnect
import Relayer

class ClientDelegate: WalletConnectClientDelegate {
var client: WalletConnectClient
Expand All @@ -13,12 +14,8 @@ class ClientDelegate: WalletConnectClientDelegate {
description: "a description",
url: "wallet.connect",
icons: ["https://gblobscdn.gitbook.com/spaces%2F-LJJeCjcLrr53DcT1Ml7%2Favatar.png?alt=media"])
self.client = WalletConnectClient(
metadata: metadata,
projectId: "52af113ee0c1e1a20f4995730196c13e",
isController: false,
relayHost: "relay.dev.walletconnect.com"
)
let relayer = Relayer(relayHost: "relay.dev.walletconnect.com", projectId: "52af113ee0c1e1a20f4995730196c13e")
self.client = WalletConnectClient(metadata: metadata, relayer: relayer)
client.delegate = self
}

Expand All @@ -34,7 +31,7 @@ class ClientDelegate: WalletConnectClientDelegate {
onSessionResponse?(sessionResponse)
}

func didUpdate(sessionTopic: String, accounts: Set<String>) {
func didUpdate(sessionTopic: String, accounts: Set<Account>) {
}

func didUpgrade(sessionTopic: String, permissions: Session.Permissions) {
Expand Down
2 changes: 1 addition & 1 deletion Example/DApp/Connect/ConnectViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ class ConnectViewController: UIViewController, UITableViewDataSource, UITableVie

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "pairing_cell", for: indexPath)
cell.textLabel?.text = activePairings[indexPath.row].peer!.name
cell.textLabel?.text = activePairings[indexPath.row].peer?.name ?? ""
return cell
}

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 @@ -754,7 +754,7 @@
ASSETCATALOG_COMPILER_GLOBAL_ACCENT_COLOR_NAME = AccentColor;
CLANG_CXX_LANGUAGE_STANDARD = "gnu++17";
CODE_SIGN_STYLE = Automatic;
CURRENT_PROJECT_VERSION = 2;
CURRENT_PROJECT_VERSION = 3;
DEVELOPMENT_TEAM = W5R8AG9K22;
GENERATE_INFOPLIST_FILE = YES;
INFOPLIST_FILE = DApp/Info.plist;
Expand Down Expand Up @@ -786,7 +786,7 @@
ASSETCATALOG_COMPILER_GLOBAL_ACCENT_COLOR_NAME = AccentColor;
CLANG_CXX_LANGUAGE_STANDARD = "gnu++17";
CODE_SIGN_STYLE = Automatic;
CURRENT_PROJECT_VERSION = 2;
CURRENT_PROJECT_VERSION = 3;
DEVELOPMENT_TEAM = W5R8AG9K22;
GENERATE_INFOPLIST_FILE = YES;
INFOPLIST_FILE = DApp/Info.plist;
Expand Down
9 changes: 4 additions & 5 deletions Example/ExampleApp/Responder/ResponderViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ final class ResponderViewController: UIViewController {
return WalletConnectClient(
metadata: metadata,
projectId: "52af113ee0c1e1a20f4995730196c13e",
isController: true,
relayHost: "relay.dev.walletconnect.com"
)
}()
Expand Down Expand Up @@ -151,15 +150,15 @@ extension ResponderViewController: SessionViewControllerDelegate {
print("[RESPONDER] Approving session...")
let proposal = currentProposal!
currentProposal = nil
let accounts = proposal.permissions.blockchains.map {$0+":\(account)"}
client.approve(proposal: proposal, accounts: Set(accounts))
let accounts = Set(proposal.permissions.blockchains.compactMap { Account($0+":\(account)") })
client.approve(proposal: proposal, accounts: accounts)
}

func didRejectSession() {
print("did reject session")
let proposal = currentProposal!
currentProposal = nil
client.reject(proposal: proposal, reason: Reason(code: 0, message: "reject"))
client.reject(proposal: proposal, reason: .disapprovedChains)
}
}

Expand Down Expand Up @@ -197,7 +196,7 @@ extension ResponderViewController: WalletConnectClientDelegate {

}

func didUpdate(sessionTopic: String, accounts: Set<String>) {
func didUpdate(sessionTopic: String, accounts: Set<Account>) {

}

Expand Down
18 changes: 11 additions & 7 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,34 +12,38 @@ let package = Package(
.library(
name: "WalletConnect",
targets: ["WalletConnect"]),
],
dependencies: [

],
targets: [
.target(
name: "WalletConnect",
dependencies: ["Relayer", "WalletConnectUtils"],
dependencies: ["Relayer", "WalletConnectUtils", "WalletConnectKMS"],
path: "Sources/WalletConnect"),
.target(
name: "Relayer",
dependencies: ["WalletConnectUtils"],
path: "Sources/Relayer"),
.target(
name: "WalletConnectKMS",
dependencies: ["WalletConnectUtils"],
path: "Sources/WalletConnectKMS"),
.target(
name: "WalletConnectUtils",
dependencies: []),
.testTarget(
name: "WalletConnectTests",
dependencies: ["WalletConnect", "TestingUtils"]),
dependencies: ["WalletConnect", "TestingUtils", "WalletConnectKMS"]),
.testTarget(
name: "IntegrationTests",
dependencies: ["WalletConnect", "TestingUtils"]),
dependencies: ["WalletConnect", "TestingUtils", "WalletConnectKMS"]),
.testTarget(
name: "RelayerTests",
dependencies: ["Relayer", "WalletConnectUtils", "TestingUtils"]),
.testTarget(
name: "WalletConnectKMSTests",
dependencies: ["WalletConnectKMS", "WalletConnectUtils", "TestingUtils"]),
.target(
name: "TestingUtils",
dependencies: ["WalletConnectUtils"],
dependencies: ["WalletConnectUtils", "WalletConnectKMS"],
path: "Tests/TestingUtils"),
],
swiftLanguageVersions: [.v5]
Expand Down
2 changes: 0 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,8 @@ You usually want to have a single instance of a client in you app.
icons: [String]?)
let client = WalletConnectClient(metadata: AppMetadata,
projectId: String,
isController: Bool,
relayHost: String)
```
The `controller` client will always be the "wallet" which is exposing blockchain accounts to a "dapp" and therefore is also in charge of signing.

After instantiation of a client set its delegate.
#### Pair Clients
Expand Down
48 changes: 48 additions & 0 deletions Sources/Relayer/AppStateObserving.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@

import Foundation
#if os(iOS)
import UIKit
#endif

protocol AppStateObserving {
var onWillEnterForeground: (()->())? {get set}
var onWillEnterBackground: (()->())? {get set}
}

class AppStateObserver: AppStateObserving {
@objc var onWillEnterForeground: (() -> ())?

@objc var onWillEnterBackground: (() -> ())?

init() {
subscribeNotificationCenter()
}

private func subscribeNotificationCenter() {
#if os(iOS)
NotificationCenter.default.addObserver(
self,
selector: #selector(appWillEnterForeground),
name: UIApplication.willEnterForegroundNotification,
object: nil)
NotificationCenter.default.addObserver(
self,
selector: #selector(appWillEnterBackground),
name: UIApplication.willResignActiveNotification,
object: nil)
#endif
}

@objc
private func appWillEnterBackground() {
onWillEnterBackground?()
}

@objc
private func appWillEnterForeground() {
onWillEnterForeground?()
}

}


44 changes: 13 additions & 31 deletions Sources/Relayer/Dispatching.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,60 +6,53 @@ protocol Dispatching {
var onDisconnect: (()->())? {get set}
var onMessage: ((String) -> ())? {get set}
func send(_ string: String, completion: @escaping (Error?)->())
func connect()
func disconnect(closeCode: URLSessionWebSocketTask.CloseCode)
func connect() throws
func disconnect(closeCode: URLSessionWebSocketTask.CloseCode) throws
}

final class Dispatcher: NSObject, Dispatching {
var onConnect: (() -> ())?
var onDisconnect: (() -> ())?
var onMessage: ((String) -> ())?
private var textFramesQueue = Queue<String>()
private var networkMonitor: NetworkMonitoring
private let url: URL
var socket: WebSocketSessionProtocol
var socketConnectionObserver: SocketConnectionObserving
var socketConnectionHandler: SocketConnectionHandler

init(url: URL,
networkMonitor: NetworkMonitoring = NetworkMonitor(),
socket: WebSocketSessionProtocol,
socketConnectionObserver: SocketConnectionObserving) {
self.url = url
self.networkMonitor = networkMonitor
init(socket: WebSocketSessionProtocol,
socketConnectionObserver: SocketConnectionObserving,
socketConnectionHandler: SocketConnectionHandler) {
self.socket = socket
self.socketConnectionObserver = socketConnectionObserver
self.socketConnectionHandler = socketConnectionHandler
super.init()
setUpWebSocketSession()
setUpSocketConnectionObserving()
setUpNetworkMonitoring()
socket.connect(on: url)
}

func send(_ string: String, completion: @escaping (Error?) -> Void) {
if socket.isConnected {
self.socket.send(string, completionHandler: completion)
//TODO - enqueue if fails
} else {
textFramesQueue.enqueue(string)
}
}

func connect() {
if !socket.isConnected {
socket.connect(on: url)
}
func connect() throws {
try socketConnectionHandler.handleConnect()
}

func disconnect(closeCode: URLSessionWebSocketTask.CloseCode) {
socket.disconnect(with: closeCode)
onDisconnect?()
func disconnect(closeCode: URLSessionWebSocketTask.CloseCode) throws {
try socketConnectionHandler.handleDisconnect(closeCode: closeCode)
}

private func setUpWebSocketSession() {
socket.onMessageReceived = { [weak self] in
self?.onMessage?($0)
}
socket.onMessageError = { error in
print(error)
print("WebSocket Error \(error)")
}
}

Expand All @@ -73,16 +66,6 @@ final class Dispatcher: NSObject, Dispatching {
}
}

private func setUpNetworkMonitoring() {
networkMonitor.onSatisfied = { [weak self] in
self?.connect()
}
networkMonitor.onUnsatisfied = { [weak self] in
self?.disconnect(closeCode: .goingAway)
}
networkMonitor.startMonitoring()
}

private func dequeuePendingTextFrames() {
while let frame = textFramesQueue.dequeue() {
send(frame) { error in
Expand All @@ -93,4 +76,3 @@ final class Dispatcher: NSObject, Dispatching {
}
}
}

17 changes: 0 additions & 17 deletions Sources/Relayer/Misc/Encodable.swift

This file was deleted.

1 change: 1 addition & 0 deletions Sources/Relayer/NetworkMonitoring.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,4 @@ class NetworkMonitor: NetworkMonitoring {
monitor.start(queue: monitorQueue)
}
}

1 change: 1 addition & 0 deletions Sources/Relayer/RelayJSONRPC.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ enum RelayJSONRPC {
let topic: String
let message: String
let ttl: Int
let prompt: Bool?
}

struct SubscribeParams: Codable, Equatable {
Expand Down
Loading

0 comments on commit 5ea0265

Please sign in to comment.