-
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
#396 auth respond subscriber
- Loading branch information
Showing
7 changed files
with
83 additions
and
3 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 |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import Combine | ||
import Foundation | ||
import WalletConnectUtils | ||
import JSONRPC | ||
|
||
class AuthRespondSubscriber { | ||
private let networkingInteractor: NetworkInteracting | ||
private let logger: ConsoleLogging | ||
private let rpcHistory: RPCHistory | ||
private var publishers = [AnyCancellable]() | ||
var onResponse: ((_ id: RPCID, _ cacao: Cacao) -> Void)? | ||
|
||
init(networkingInteractor: NetworkInteracting, | ||
logger: ConsoleLogging, | ||
rpcHistory: RPCHistory) { | ||
self.networkingInteractor = networkingInteractor | ||
self.logger = logger | ||
self.rpcHistory = rpcHistory | ||
subscribeForResponse() | ||
} | ||
|
||
private func subscribeForResponse() { | ||
networkingInteractor.responsePublisher.sink { [unowned self] subscriptionPayload in | ||
guard let request = rpcHistory.get(recordId: subscriptionPayload.request.id!)?.request, | ||
request.method == "wc_authRequest" else { return } | ||
networkingInteractor.unsubscribe(topic: subscriptionPayload.topic) | ||
guard let cacao = try? subscriptionPayload.request.result?.get(Cacao.self) else { | ||
logger.debug("Malformed auth response params") | ||
return | ||
} | ||
do { | ||
try CacaoSignatureVerifier().verifySignature(cacao) | ||
onResponse?(subscriptionPayload.request.id!, cacao) | ||
} catch { | ||
logger.debug("Received response with invalid signature") | ||
} | ||
}.store(in: &publishers) | ||
} | ||
} |
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,15 @@ | ||
import Foundation | ||
|
||
protocol CacaoSignatureVerifying { | ||
func verifySignature(_ cacao: Cacao) throws | ||
} | ||
|
||
class CacaoSignatureVerifier: CacaoSignatureVerifying { | ||
enum Errors: Error { | ||
case signatureInvalid | ||
} | ||
|
||
func verifySignature(_ cacao: Cacao) throws { | ||
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
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,7 +1,7 @@ | ||
import Foundation | ||
import JSONRPC | ||
|
||
struct RequestSubscriptionPayload: Codable { | ||
let id: Int64 | ||
struct RequestSubscriptionPayload: Codable, Equatable { | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import Foundation | ||
import JSONRPC | ||
|
||
struct ResponseSubscriptionPayload: Codable, Equatable { | ||
let topic: String | ||
let request: RPCResponse | ||
} |
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