-
Notifications
You must be signed in to change notification settings - Fork 191
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
Changes from 3 commits
a2ff353
39af3cb
c6587b6
4de254a
7a00520
cc10b94
53bf659
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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) | ||
try await networkingInteractor.request(request, topic: topic) | ||
try await networkingInteractor.subscribe(topic: responseTopic) | ||
} | ||
} | ||
|
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> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
|
||
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) | ||
} | ||
} |
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.
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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
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 | ||
} | ||
} |
There was a problem hiding this comment.
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 itThere was a problem hiding this comment.
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.