-
Notifications
You must be signed in to change notification settings - Fork 191
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
#376 auth request service
- Loading branch information
Showing
7 changed files
with
106 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import WalletConnectPairing | ||
|
||
public typealias AppMetadata = WalletConnectPairing.AppMetadata |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |