Skip to content

Commit

Permalink
expose client id
Browse files Browse the repository at this point in the history
  • Loading branch information
llbartekll committed Oct 13, 2022
1 parent 28109d7 commit 6b8af6c
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 8 deletions.
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: ED25519DIDKeyFactory

init(keychain: KeychainStorageProtocol) {
init(keychain: KeychainStorageProtocol,
didKeyFactory: ED25519DIDKeyFactory) {
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: ClientIdStorage

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: ClientIdStorage
) {
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

0 comments on commit 6b8af6c

Please sign in to comment.