Skip to content

Commit

Permalink
Merge pull request #385 from WalletConnect/#376-auth-request-service
Browse files Browse the repository at this point in the history
#376 auth request service
  • Loading branch information
llbartekll authored Aug 2, 2022
2 parents 21b6598 + 53bf659 commit db0f22f
Show file tree
Hide file tree
Showing 7 changed files with 106 additions and 18 deletions.
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
}
}

0 comments on commit db0f22f

Please sign in to comment.