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

#379 auth core types #381

Merged
merged 10 commits into from
Jul 28, 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
52 changes: 52 additions & 0 deletions .swiftpm/xcode/xcshareddata/xcschemes/AuthTests.xcscheme
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "1340"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
buildImplicitDependencies = "YES">
</BuildAction>
<TestAction
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
shouldUseLaunchSchemeArgsEnv = "YES">
<Testables>
<TestableReference
skipped = "NO">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "AuthTests"
BuildableName = "AuthTests"
BlueprintName = "AuthTests"
ReferencedContainer = "container:">
</BuildableReference>
</TestableReference>
</Testables>
</TestAction>
<LaunchAction
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
launchStyle = "0"
useCustomWorkingDirectory = "NO"
ignoresPersistentStateOnLaunch = "NO"
debugDocumentVersioning = "YES"
debugServiceExtension = "internal"
allowLocationSimulation = "YES">
</LaunchAction>
<ProfileAction
buildConfiguration = "Release"
shouldUseLaunchSchemeArgsEnv = "YES"
savedToolIdentifier = ""
useCustomWorkingDirectory = "NO"
debugDocumentVersioning = "YES">
</ProfileAction>
<AnalyzeAction
buildConfiguration = "Debug">
</AnalyzeAction>
<ArchiveAction
buildConfiguration = "Release"
revealArchiveInOrganizer = "YES">
</ArchiveAction>
</Scheme>
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ final class RelayClientEndToEndTests: XCTestCase {
)
let urlFactory = RelayUrlFactory(socketAuthenticator: socketAuthenticator)
let socket = WebSocket(url: urlFactory.create(host: relayHost, projectId: projectId))

let logger = ConsoleLogger()
let dispatcher = Dispatcher(socket: socket, socketConnectionHandler: ManualSocketConnectionHandler(socket: socket), logger: logger)
return RelayClient(dispatcher: dispatcher, logger: logger, keyValueStorage: RuntimeKeyValueStorage())
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

final class ImportInteractor {
private let registerService: RegisterService
private let accountStorage: AccountStorage
Expand Down
7 changes: 7 additions & 0 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ let package = Package(
name: "Chat",
dependencies: ["WalletConnectRelay", "WalletConnectUtils", "WalletConnectKMS"],
path: "Sources/Chat"),
.target(
name: "Auth",
dependencies: ["WalletConnectRelay", "WalletConnectUtils", "WalletConnectKMS"],
path: "Sources/Auth"),
.target(
name: "WalletConnectRelay",
dependencies: ["WalletConnectUtils", "WalletConnectKMS"],
Expand All @@ -50,6 +54,9 @@ let package = Package(
.testTarget(
name: "ChatTests",
dependencies: ["Chat", "WalletConnectUtils", "TestingUtils"]),
.testTarget(
name: "AuthTests",
dependencies: ["Auth", "WalletConnectUtils", "TestingUtils"]),
.testTarget(
name: "RelayerTests",
dependencies: ["WalletConnectRelay", "WalletConnectUtils", "TestingUtils"]),
Expand Down
5 changes: 5 additions & 0 deletions Sources/Auth/AuthClient.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import Foundation

class AuthClient {

}
8 changes: 8 additions & 0 deletions Sources/Auth/Services/App/AuthRequestService.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import Foundation

actor AuthRequestService {

func request(params: RequestParams, topic: String) async throws {

}
}
8 changes: 8 additions & 0 deletions Sources/Auth/Services/App/CreatePairingService.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import Foundation

actor CreatePairingService {

func create() async throws -> WalletConnectURI {
fatalError("not implemented")
}
}
8 changes: 8 additions & 0 deletions Sources/Auth/Services/Wallet/AuthService.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import Foundation

actor AuthService {

func respond(respondParams: RespondParams) async throws {

}
}
8 changes: 8 additions & 0 deletions Sources/Auth/Services/Wallet/PairService.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import Foundation

actor PairService {

func pair(_ uri: WalletConnectURI) async throws {
fatalError("not implemented")
}
}
16 changes: 16 additions & 0 deletions Sources/Auth/Types/AuthPayloadParams.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import Foundation

struct AuthPayloadParams {
let type: String
let chainId: String
let domain: String
let aud: String
let version: String
let nonce: String
let iat: String
let nbf: String?
let exp: String?
Comment on lines +7 to +12

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I still defend the approach of using readable names and declaring the abbreviated coding keys 😬

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That’s according to cacao specs :-)

let statement: String?
let requestId: String?
let resources: String?
}
5 changes: 5 additions & 0 deletions Sources/Auth/Types/Cacao/CacaoHeader.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import Foundation

struct CacaoHeader {
let t: String
}
15 changes: 15 additions & 0 deletions Sources/Auth/Types/Cacao/CacaoPayload.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import Foundation

struct CacaoPayload {
let iss: String
let domain: String
let aud: String
let version: String
let nonce: String
let iat: String
let nbf: String
let exp: String
let statement: String
let requestId: String
let resources: String
}
7 changes: 7 additions & 0 deletions Sources/Auth/Types/Cacao/CacaoSignature.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import Foundation

struct CacaoSignature {
let t: String
let s: String
let m: String
}
6 changes: 6 additions & 0 deletions Sources/Auth/Types/RelayProtocolOptions.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
//

import Foundation
import WalletConnectUtils

typealias RelayProtocolOptions = WalletConnectUtils.RelayProtocolOptions
13 changes: 13 additions & 0 deletions Sources/Auth/Types/RequestParams.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import Foundation

struct RequestParams {
let domain: String
let chainId: String
let nonce: String
let aud: String
let nbf: String?
let exp: String?
let statement: String?
let requestId: String?
let resources: String?
}
6 changes: 6 additions & 0 deletions Sources/Auth/Types/RespondParams.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import Foundation

struct RespondParams {
let topic: String
let signature: CacaoSignature
}
6 changes: 6 additions & 0 deletions Sources/Auth/Types/WalletConnectURI.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
//

import Foundation
import WalletConnectUtils

typealias WalletConnectURI = WalletConnectUtils.WalletConnectURI

This file was deleted.

6 changes: 6 additions & 0 deletions Sources/WalletConnectSign/Types/RelayProtocolOptions.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
//

import Foundation
import WalletConnectUtils

typealias RelayProtocolOptions = WalletConnectUtils.RelayProtocolOptions
2 changes: 1 addition & 1 deletion Sources/WalletConnectSign/Types/Session/WCSession.swift
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ struct WCSession: SequenceObject, Equatable {
peerParticipant: Participant,
namespaces: [String: SessionNamespace],
requiredNamespaces: [String: ProposalNamespace],
events: Set<String>,
events: Set<String>,
accounts: Set<Account>,
acknowledged: Bool,
expiry: Int64
Expand Down
52 changes: 4 additions & 48 deletions Sources/WalletConnectSign/Types/WalletConnectURI.swift
Original file line number Diff line number Diff line change
@@ -1,50 +1,6 @@
import Foundation

public struct WalletConnectURI: Equatable {

let topic: String
let version: String
let symKey: String
let relay: RelayProtocolOptions

init(topic: String, symKey: String, relay: RelayProtocolOptions) {
self.version = "2"
self.topic = topic
self.symKey = symKey
self.relay = relay
}
//

public init?(string: String) {
guard string.hasPrefix("wc:") else {
return nil
}
let urlString = !string.hasPrefix("wc://") ? string.replacingOccurrences(of: "wc:", with: "wc://") : string
guard let components = URLComponents(string: urlString) else {
return nil
}
let query: [String: String]? = components.queryItems?.reduce(into: [:]) { $0[$1.name] = $1.value }

guard let topic = components.user,
let version = components.host,
let symKey = query?["symKey"],
let relayProtocol = query?["relay-protocol"]
else { return nil }
let relayData = query?["relay-data"]
self.version = version
self.topic = topic
self.symKey = symKey
self.relay = RelayProtocolOptions(protocol: relayProtocol, data: relayData)
}

public var absoluteString: String {
return "wc:\(topic)@\(version)?symKey=\(symKey)&\(relayQuery)"
}
import Foundation
import WalletConnectUtils

private var relayQuery: String {
var query = "relay-protocol=\(relay.protocol)"
if let relayData = relay.data {
query = "\(query)&relay-data=\(relayData)"
}
return query
}
}
typealias WalletConnectURI = WalletConnectUtils.WalletConnectURI
11 changes: 11 additions & 0 deletions Sources/WalletConnectUtils/RelayProtocolOptions.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import Foundation

public struct RelayProtocolOptions: Codable, Equatable {
public let `protocol`: String
public let data: String?

public init(protocol: String, data: String?) {
self.protocol = `protocol`
self.data = data
}
}
50 changes: 50 additions & 0 deletions Sources/WalletConnectUtils/WalletConnectURI.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import Foundation

public struct WalletConnectURI: Equatable {

public let topic: String
public let version: String
public let symKey: String
public let relay: RelayProtocolOptions

public init(topic: String, symKey: String, relay: RelayProtocolOptions) {
self.version = "2"
self.topic = topic
self.symKey = symKey
self.relay = relay
}

public init?(string: String) {
guard string.hasPrefix("wc:") else {
return nil
}
let urlString = !string.hasPrefix("wc://") ? string.replacingOccurrences(of: "wc:", with: "wc://") : string
guard let components = URLComponents(string: urlString) else {
return nil
}
let query: [String: String]? = components.queryItems?.reduce(into: [:]) { $0[$1.name] = $1.value }

guard let topic = components.user,
let version = components.host,
let symKey = query?["symKey"],
let relayProtocol = query?["relay-protocol"]
else { return nil }
let relayData = query?["relay-data"]
self.version = version
self.topic = topic
self.symKey = symKey
self.relay = RelayProtocolOptions(protocol: relayProtocol, data: relayData)
}

public var absoluteString: String {
return "wc:\(topic)@\(version)?symKey=\(symKey)&\(relayQuery)"
}

private var relayQuery: String {
var query = "relay-protocol=\(relay.protocol)"
if let relayData = relay.data {
query = "\(query)&relay-data=\(relayData)"
}
return query
}
}
6 changes: 6 additions & 0 deletions Tests/AuthTests/AuthClientTest.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import Foundation
import XCTest

class AuthClientTest: XCTestCase {

}
1 change: 0 additions & 1 deletion Tests/RelayerTests/DispatcherTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import Combine
class WebSocketMock: WebSocketConnecting {
var request: URLRequest = URLRequest(url: URL(string: "wss://relay.walletconnect.com")!)


var onText: ((String) -> Void)?
var onConnect: (() -> Void)?
var onDisconnect: ((Error?) -> Void)?
Expand Down