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

#376 auth request service #385

Merged
merged 7 commits into from
Aug 2, 2022
Merged
Show file tree
Hide file tree
Changes from 3 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
24 changes: 23 additions & 1 deletion Sources/Auth/Services/App/AuthRequestService.swift
Original file line number Diff line number Diff line change
@@ -1,8 +1,30 @@
import Foundation
import WalletConnectUtils
import WalletConnectKMS

actor AuthRequestService {
private let networkingInteractor: NetworkInteracting
private let appMetadata: AppMetadata
private let kms: KeyManagementService

func request(params: RequestParams, topic: String) async throws {
init(networkingInteractor: NetworkInteracting,
kms: KeyManagementService,
appMetadata: AppMetadata) {
self.networkingInteractor = networkingInteractor
self.kms = kms
self.appMetadata = appMetadata
}

func request(params: RequestParams, topic: String) async throws {
let pubKey = try kms.createX25519KeyPair()
let responseTopic = pubKey.rawRepresentation.sha256().toHexString()
let requester = AuthRequestParams.Requester(publicKey: pubKey.hexRepresentation, metadata: appMetadata)
let issueAt = ISO8601DateFormatter().string(from: Date())
let payload = AuthPayload(requestParams: params, iat: issueAt)
let params = AuthRequestParams(requester: requester, payloadParams: payload)
let request = JSONRPCRequest<AuthRequestParams>(method: "wc_authRequest", params: params)

Choose a reason for hiding this comment

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

What do you think of using RPCRequest in here? Seems like a good place to use it

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I was waiting for your first demo integration 😄 but ok, let's give it a shot.

try await networkingInteractor.request(request, topic: topic)
try await networkingInteractor.subscribe(topic: responseTopic)
}
}

24 changes: 23 additions & 1 deletion Sources/Auth/Services/Common/NetworkingInteractor.swift
Original file line number Diff line number Diff line change
@@ -1,18 +1,40 @@
import Foundation
import WalletConnectRelay
import WalletConnectUtils
import WalletConnectKMS

protocol NetworkInteracting {
func subscribe(topic: String) async throws
func request(_ request: JSONRPCRequest<AuthRequestParams>, topic: String, envelopeType: Envelope.EnvelopeType) async throws
}

extension NetworkInteracting {
func request(_ request: JSONRPCRequest<AuthRequestParams>, topic: String, envelopeType: Envelope.EnvelopeType = .type0) async throws {
try await self.request(request, topic: topic, envelopeType: envelopeType)
}
}


class NetworkingInteractor: NetworkInteracting {
private let relayClient: RelayClient
private let serializer: Serializing
private let jsonRpcHistory: JsonRpcHistory<AuthRequestParams>

Choose a reason for hiding this comment

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

As it was already merged to develop, what do you also think of using the improved RPCHistory in here, so you can avoid binding the storage to a single specialized type?


init(relayClient: RelayClient) {
init(relayClient: RelayClient,
serializer: Serializing,
jsonRpcHistory: JsonRpcHistory<AuthRequestParams>) {
self.relayClient = relayClient
self.serializer = serializer
self.jsonRpcHistory = jsonRpcHistory
}

func subscribe(topic: String) async throws {
try await relayClient.subscribe(topic: topic)
}

func request(_ request: JSONRPCRequest<AuthRequestParams>, topic: String, envelopeType: Envelope.EnvelopeType) async throws {
try jsonRpcHistory.set(topic: topic, request: request)
let message = try! serializer.serialize(topic: topic, encodable: request, envelopeType: envelopeType)
try await relayClient.publish(topic: topic, payload: message, tag: AuthRequestParams.tag)
}
}
31 changes: 31 additions & 0 deletions Sources/Auth/Types/AuthPayload.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import Foundation

struct AuthPayload: Codable, Equatable {
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?
let statement: String?
let requestId: String?
let resources: String?

init(requestParams: RequestParams, iat: String) {
self.type = "eip4361"
self.chainId = requestParams.chainId
self.domain = requestParams.domain
self.aud = requestParams.aud
self.version = "1"
self.nonce = requestParams.nonce
self.iat = iat
self.nbf = requestParams.nbf
self.exp = requestParams.exp
self.statement = requestParams.statement
self.requestId = requestParams.requestId
self.resources = requestParams.resources
}
}
16 changes: 0 additions & 16 deletions Sources/Auth/Types/AuthPayloadParams.swift

This file was deleted.

33 changes: 33 additions & 0 deletions Sources/Auth/Types/AuthRequestParams.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@

import Foundation
import WalletConnectUtils

struct AuthRequestParams: Codable, Equatable {
let requester: Requester
let payloadParams: AuthPayload

static var tag: Int {
return 3000
}
}

extension AuthRequestParams {
struct Requester: Codable, Equatable {
let publicKey: String
let metadata: AppMetadata
}
}

// TODO - temporarly duplicated - moved do utils in concurrent PR

Choose a reason for hiding this comment

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

I believe that, if this is moved to Utils, we'll have to expose it via a typealias in each SDK. Remember to test it if you do

public struct AppMetadata: Codable, Equatable {
public let name: String
public let description: String
public let url: String
public let icons: [String]
public init(name: String, description: String, url: String, icons: [String]) {
self.name = name
self.description = description
self.url = url
self.icons = icons
}
}