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

[Networking] Expose client ID #545

Merged
merged 3 commits into from
Oct 13, 2022
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: 4 additions & 3 deletions Example/IntegrationTests/Relay/RelayClientEndToEndTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,19 @@ final class RelayClientEndToEndTests: XCTestCase {
private var publishers = Set<AnyCancellable>()

func makeRelayClient() -> RelayClient {
let clientIdStorage = ClientIdStorage(keychain: KeychainStorageMock())
let didKeyFactory = ED25519DIDKeyFactory()
let clientIdStorage = ClientIdStorage(keychain: KeychainStorageMock(), didKeyFactory: didKeyFactory)
let socketAuthenticator = SocketAuthenticator(
clientIdStorage: clientIdStorage,
didKeyFactory: ED25519DIDKeyFactory(),
didKeyFactory: didKeyFactory,
relayHost: InputConfig.relayHost
)
let urlFactory = RelayUrlFactory(socketAuthenticator: socketAuthenticator)
let socket = WebSocket(url: urlFactory.create(host: InputConfig.relayHost, projectId: InputConfig.projectId))

let logger = ConsoleLogger()
let dispatcher = Dispatcher(socket: socket, socketConnectionHandler: ManualSocketConnectionHandler(socket: socket), logger: logger)
return RelayClient(dispatcher: dispatcher, logger: logger, keyValueStorage: RuntimeKeyValueStorage())
return RelayClient(dispatcher: dispatcher, logger: logger, keyValueStorage: RuntimeKeyValueStorage(), clientIdStorage: clientIdStorage)
}

func testSubscribe() {
Expand Down
1 change: 1 addition & 0 deletions Sources/WalletConnectNetworking/NetworkingClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ public protocol NetworkingClient {
var socketConnectionStatusPublisher: AnyPublisher<SocketConnectionStatus, Never> { get }
func connect() throws
func disconnect(closeCode: URLSessionWebSocketTask.CloseCode) throws
func getClientId() throws -> String
}
4 changes: 4 additions & 0 deletions Sources/WalletConnectNetworking/NetworkingInteractor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -180,4 +180,8 @@ extension NetworkingInteractor: NetworkingClient {
public func disconnect(closeCode: URLSessionWebSocketTask.CloseCode) throws {
try relayClient.disconnect(closeCode: closeCode)
}

public func getClientId() throws -> String {
try relayClient.getClientId()
}
}
12 changes: 11 additions & 1 deletion Sources/WalletConnectRelay/ClientAuth/ClientIdStorage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,18 @@ import WalletConnectKMS

protocol ClientIdStoring {
func getOrCreateKeyPair() throws -> SigningPrivateKey
func getClientId() throws -> String
}

struct ClientIdStorage: ClientIdStoring {
private let key = "com.walletconnect.iridium.client_id"
private let keychain: KeychainStorageProtocol
private let didKeyFactory: DIDKeyFactory

init(keychain: KeychainStorageProtocol) {
init(keychain: KeychainStorageProtocol,
didKeyFactory: DIDKeyFactory) {
self.keychain = keychain
self.didKeyFactory = didKeyFactory
}

func getOrCreateKeyPair() throws -> SigningPrivateKey {
Expand All @@ -22,4 +26,10 @@ struct ClientIdStorage: ClientIdStoring {
return privateKey
}
}

func getClientId() throws -> String {
let privateKey: SigningPrivateKey = try keychain.read(key: key)
let pubKey = privateKey.publicKey.rawRepresentation
return didKeyFactory.make(pubKey: pubKey, prefix: true)
}
}
18 changes: 14 additions & 4 deletions Sources/WalletConnectRelay/RelayClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ public final class RelayClient {
requestAcknowledgePublisherSubject.eraseToAnyPublisher()
}

private let clientIdStorage: ClientIdStoring

private var dispatcher: Dispatching
private let rpcHistory: RPCHistory
private let logger: ConsoleLogging
Expand All @@ -52,11 +54,13 @@ public final class RelayClient {
init(
dispatcher: Dispatching,
logger: ConsoleLogging,
keyValueStorage: KeyValueStorage
keyValueStorage: KeyValueStorage,
clientIdStorage: ClientIdStoring
) {
self.logger = logger
self.dispatcher = dispatcher
self.rpcHistory = RPCHistoryFactory.createForRelay(keyValueStorage: keyValueStorage)
self.clientIdStorage = clientIdStorage
setUpBindings()
}

Expand All @@ -82,9 +86,11 @@ public final class RelayClient {
socketConnectionType: SocketConnectionType = .automatic,
logger: ConsoleLogging = ConsoleLogger(loggingLevel: .off)
) {
let didKeyFactory = ED25519DIDKeyFactory()
let clientIdStorage = ClientIdStorage(keychain: keychainStorage, didKeyFactory: didKeyFactory)
let socketAuthenticator = SocketAuthenticator(
clientIdStorage: ClientIdStorage(keychain: keychainStorage),
didKeyFactory: ED25519DIDKeyFactory(),
clientIdStorage: clientIdStorage,
didKeyFactory: didKeyFactory,
relayHost: relayHost
)
let relayUrlFactory = RelayUrlFactory(socketAuthenticator: socketAuthenticator)
Expand All @@ -101,7 +107,7 @@ public final class RelayClient {
socketConnectionHandler = ManualSocketConnectionHandler(socket: socket)
}
let dispatcher = Dispatcher(socket: socket, socketConnectionHandler: socketConnectionHandler, logger: logger)
self.init(dispatcher: dispatcher, logger: logger, keyValueStorage: keyValueStorage)
self.init(dispatcher: dispatcher, logger: logger, keyValueStorage: keyValueStorage, clientIdStorage: clientIdStorage)
}

/// Connects web socket
Expand Down Expand Up @@ -231,6 +237,10 @@ public final class RelayClient {
}
}

public func getClientId() throws -> String {
try clientIdStorage.getClientId()
}

// FIXME: Parse data to string once before trying to decode -> respond error on fail
private func handlePayloadMessage(_ payload: String) {
if let request = tryDecode(RPCRequest.self, from: payload) {
Expand Down
27 changes: 22 additions & 5 deletions Tests/RelayerTests/AuthTests/ClientIdStorageTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,33 @@ import WalletConnectKMS

final class ClientIdStorageTests: XCTestCase {

func testGetOrCreate() throws {
let keychain = KeychainStorageMock()
let storage = ClientIdStorage(keychain: keychain)
var sut: ClientIdStorage!
var keychain: KeychainStorageMock!
var didKeyFactory: ED25519DIDKeyFactoryMock!

override func setUp() {
keychain = KeychainStorageMock()
didKeyFactory = ED25519DIDKeyFactoryMock()
sut = ClientIdStorage(keychain: keychain, didKeyFactory: didKeyFactory)
}

func testGetOrCreate() throws {
XCTAssertThrowsError(try keychain.read(key: "com.walletconnect.iridium.client_id") as SigningPrivateKey)

let saved = try storage.getOrCreateKeyPair()
let saved = try sut.getOrCreateKeyPair()
XCTAssertEqual(saved, try keychain.read(key: "com.walletconnect.iridium.client_id"))

let restored = try storage.getOrCreateKeyPair()
let restored = try sut.getOrCreateKeyPair()
XCTAssertEqual(saved, restored)
}

func testGetClientId() {
let did = "did:key:z6MkodHZwneVRShtaLf8JKYkxpDGp1vGZnpGmdBpX8M2exxH"
didKeyFactory.did = did
_ = try! sut.getOrCreateKeyPair()

let clientId = try! sut.getClientId()
XCTAssertEqual(did, clientId)

}
}
5 changes: 5 additions & 0 deletions Tests/RelayerTests/Mocks/ClientIdStorageMock.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,14 @@ import WalletConnectKMS
import Foundation

class ClientIdStorageMock: ClientIdStoring {

var keyPair: SigningPrivateKey!

func getOrCreateKeyPair() throws -> SigningPrivateKey {
return keyPair
}

func getClientId() throws -> String {
fatalError()
}
}
2 changes: 1 addition & 1 deletion Tests/RelayerTests/Mocks/ED25519DIDKeyFactoryMock.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import WalletConnectKMS
@testable import WalletConnectRelay
import Foundation

struct ED25519DIDKeyFactoryMock: DIDKeyFactory {
class ED25519DIDKeyFactoryMock: DIDKeyFactory {
var did: String!
func make(pubKey: Data, prefix: Bool) -> String {
return did
Expand Down
4 changes: 2 additions & 2 deletions Tests/RelayerTests/RelayClientTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ final class RelayClientTests: XCTestCase {

var sut: RelayClient!
var dispatcher: DispatcherMock!

var publishers = Set<AnyCancellable>()

override func setUp() {
dispatcher = DispatcherMock()
let logger = ConsoleLogger()
sut = RelayClient(dispatcher: dispatcher, logger: logger, keyValueStorage: RuntimeKeyValueStorage())
let clientIdStorage = ClientIdStorageMock()
sut = RelayClient(dispatcher: dispatcher, logger: logger, keyValueStorage: RuntimeKeyValueStorage(), clientIdStorage: clientIdStorage)
}

override func tearDown() {
Expand Down