-
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
[Auth] #395 Auth Request Subscriber
- Loading branch information
Showing
21 changed files
with
185 additions
and
20 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
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,11 @@ | ||
import Foundation | ||
|
||
protocol SIWEMessageFormatting { | ||
func formatMessage(from request: AuthRequestParams) throws -> String | ||
} | ||
|
||
struct SIWEMessageFormatter: SIWEMessageFormatting { | ||
func formatMessage(from request: AuthRequestParams) throws -> String { | ||
fatalError("not implemented") | ||
} | ||
} |
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,38 @@ | ||
import Combine | ||
import Foundation | ||
import WalletConnectUtils | ||
import JSONRPC | ||
|
||
class AuthRequestSubscriber { | ||
private let networkingInteractor: NetworkInteracting | ||
private let logger: ConsoleLogging | ||
private var publishers = [AnyCancellable]() | ||
private let messageFormatter: SIWEMessageFormatting | ||
var onRequest: ((_ id: RPCID, _ message: String)->())? | ||
|
||
init(networkingInteractor: NetworkInteracting, | ||
logger: ConsoleLogging, | ||
messageFormatter: SIWEMessageFormatting) { | ||
self.networkingInteractor = networkingInteractor | ||
self.logger = logger | ||
self.messageFormatter = messageFormatter | ||
subscribeForRequest() | ||
} | ||
|
||
private func subscribeForRequest() { | ||
networkingInteractor.requestPublisher.sink { [unowned self] subscriptionPayload in | ||
guard subscriptionPayload.request.method == "wc_authRequest" else { return } | ||
guard let authRequest = try? subscriptionPayload.request.params?.get(AuthRequestParams.self) else { | ||
logger.debug("Malformed auth request params") | ||
return | ||
} | ||
do { | ||
let message = try messageFormatter.formatMessage(from: authRequest) | ||
guard let requestId = subscriptionPayload.request.id else { return } | ||
onRequest?(requestId, message) | ||
} catch { | ||
logger.debug(error) | ||
} | ||
}.store(in: &publishers) | ||
} | ||
} |
2 changes: 1 addition & 1 deletion
2
...es/Auth/Services/Wallet/AuthService.swift → .../Services/Wallet/AuthRespondService.swift
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 |
---|---|---|
@@ -1,5 +1,5 @@ | ||
import Foundation | ||
|
||
struct CacaoHeader { | ||
struct CacaoHeader: Codable, Equatable { | ||
let t: String | ||
} |
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
File renamed without changes.
13 changes: 13 additions & 0 deletions
13
Sources/Auth/Types/ProtocolRPCParams/AuthResponseParams.swift
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,13 @@ | ||
|
||
import Foundation | ||
import WalletConnectUtils | ||
|
||
struct AuthResponseParams: Codable, Equatable { | ||
let header: CacaoHeader | ||
let payload: CacaoPayload | ||
let signature: CacaoSignature | ||
|
||
static var tag: Int { | ||
return 3001 | ||
} | ||
} |
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,7 @@ | ||
import Foundation | ||
import JSONRPC | ||
|
||
struct RequestSubscriptionPayload: Codable { | ||
let topic: String | ||
let request: RPCRequest | ||
} |
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
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
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,42 @@ | ||
import Foundation | ||
import XCTest | ||
@testable import Auth | ||
import WalletConnectUtils | ||
@testable import WalletConnectKMS | ||
@testable import TestingUtils | ||
import JSONRPC | ||
|
||
class AuthRequstSubscriberTests: XCTestCase { | ||
var networkingInteractor: NetworkingInteractorMock! | ||
var sut: AuthRequestSubscriber! | ||
var messageFormatter: SIWEMessageFormatterMock! | ||
let defaultTimeout: TimeInterval = 0.01 | ||
|
||
override func setUp() { | ||
networkingInteractor = NetworkingInteractorMock() | ||
messageFormatter = SIWEMessageFormatterMock() | ||
sut = AuthRequestSubscriber(networkingInteractor: networkingInteractor, | ||
logger: ConsoleLoggerMock(), | ||
messageFormatter: messageFormatter) | ||
} | ||
|
||
func testSubscribeRequest() { | ||
let expectedMessage = "Expected Message" | ||
let expectedRequestId: RPCID = RPCID(1234) | ||
let messageExpectation = expectation(description: "receives formatted message") | ||
messageFormatter.formattedMessage = expectedMessage | ||
var messageId: RPCID! | ||
var message: String! | ||
sut.onRequest = { id, formattedMessage in | ||
messageId = id | ||
message = formattedMessage | ||
messageExpectation.fulfill() | ||
} | ||
|
||
networkingInteractor.requestPublisherSubject.send(RequestSubscriptionPayload.stub(id: expectedRequestId)) | ||
|
||
wait(for: [messageExpectation], timeout: defaultTimeout) | ||
XCTAssertEqual(message, expectedMessage) | ||
XCTAssertEqual(messageId, expectedRequestId) | ||
} | ||
} |
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,16 @@ | ||
import Foundation | ||
import Combine | ||
@testable import Auth | ||
|
||
struct NetworkingInteractorMock: NetworkInteracting { | ||
let requestPublisherSubject = PassthroughSubject<RequestSubscriptionPayload, Never>() | ||
var requestPublisher: AnyPublisher<RequestSubscriptionPayload, Never> { | ||
requestPublisherSubject.eraseToAnyPublisher() | ||
} | ||
|
||
func subscribe(topic: String) async throws { | ||
|
||
} | ||
|
||
|
||
} |
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,10 @@ | ||
import Foundation | ||
@testable import Auth | ||
|
||
class SIWEMessageFormatterMock: SIWEMessageFormatting { | ||
var formattedMessage: String! | ||
func formatMessage(from request: AuthRequestParams) throws -> String { | ||
return formattedMessage | ||
} | ||
} | ||
|
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,8 @@ | ||
import Foundation | ||
@testable import Auth | ||
|
||
extension RequestParams { | ||
static func stub() -> RequestParams { | ||
return RequestParams(domain: "", chainId: "", nonce: "", aud: "", nbf: nil, exp: nil, statement: nil, requestId: nil, resources: nil) | ||
} | ||
} |
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,16 @@ | ||
import Foundation | ||
@testable import Auth | ||
import JSONRPC | ||
|
||
extension RequestSubscriptionPayload { | ||
static func stub(id: RPCID) -> RequestSubscriptionPayload { | ||
let appMetadata = AppMetadata(name: "", description: "", url: "", icons: []) | ||
let requester = AuthRequestParams.Requester(publicKey: "", metadata: appMetadata) | ||
let issueAt = ISO8601DateFormatter().string(from: Date()) | ||
let payload = AuthPayload(requestParams: RequestParams.stub(), iat: issueAt) | ||
let params = AuthRequestParams(requester: requester, payloadParams: payload) | ||
let request = RPCRequest(method: "wc_authRequest", params: params, rpcid: id) | ||
return RequestSubscriptionPayload(topic: "", request: request) | ||
} | ||
} | ||
|
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