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 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
25 changes: 24 additions & 1 deletion Sources/Auth/Services/App/AuthRequestService.swift
Original file line number Diff line number Diff line change
@@ -1,8 +1,31 @@
import Foundation
import WalletConnectUtils
import WalletConnectKMS
import JSONRPC

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 = RPCRequest(method: "wc_authRequest", params: params)
try await networkingInteractor.request(request, topic: topic)
try await networkingInteractor.subscribe(topic: responseTopic)
}
}

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

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

extension NetworkInteracting {
func request(_ request: RPCRequest, 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 rpcHistory: RPCHistory

init(relayClient: RelayClient) {
init(relayClient: RelayClient,
serializer: Serializing,
rpcHistory: RPCHistory) {
self.relayClient = relayClient
self.serializer = serializer
self.rpcHistory = rpcHistory
}

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

func request(_ request: RPCRequest, topic: String, envelopeType: Envelope.EnvelopeType) async throws {
try rpcHistory.set(request, forTopic: topic, emmitedBy: .local)
let message = try! serializer.serialize(topic: topic, encodable: request, envelopeType: envelopeType)
try await relayClient.publish(topic: topic, payload: message, tag: AuthRequestParams.tag)
}
}
5 changes: 5 additions & 0 deletions Sources/Auth/Services/Wallet/AuthService.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import Foundation

actor AuthService {
private let networkingInteractor: NetworkInteracting

init(networkingInteractor: NetworkInteracting) {
self.networkingInteractor = networkingInteractor
}

func respond(respondParams: RespondParams) async throws {

Expand Down
3 changes: 3 additions & 0 deletions Sources/Auth/Types/AppMetadata.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import WalletConnectPairing

public typealias AppMetadata = WalletConnectPairing.AppMetadata
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.

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

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
}
}