Skip to content

Commit 796d241

Browse files
committedMar 5, 2023
Replace DappError with JsonRpcError #6456
1 parent 052ea3d commit 796d241

13 files changed

+44
-58
lines changed
 

‎AlphaWallet/ActiveWalletCoordinator.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -1168,14 +1168,14 @@ extension ActiveWalletCoordinator: DappBrowserCoordinatorDelegate {
11681168
return firstly {
11691169
showAskSendRawTransaction(title: R.string.localizable.walletConnectSendRawTransactionTitle(), message: transaction)
11701170
}.then { shouldSend -> Promise<ConfirmResult> in
1171-
guard shouldSend else { return .init(error: DAppError.cancelled) }
1171+
guard shouldSend else { return .init(error: JsonRpcError.requestRejected) }
11721172
let prompt = R.string.localizable.keystoreAccessKeySign()
11731173
let sender = SendTransaction(session: session, keystore: self.keystore, confirmType: .signThenSend, config: session.config, analytics: self.analytics, prompt: prompt)
11741174
return sender.send(rawTransaction: transaction)
11751175
}.map { data in
11761176
switch data {
11771177
case .signedTransaction, .sentTransaction:
1178-
throw DAppError.cancelled
1178+
throw JsonRpcError.requestRejected
11791179
case .sentRawTransaction(let transactionId, _):
11801180
return transactionId
11811181
}

‎AlphaWallet/Browser/Coordinators/DappBrowserCoordinator.swift

+12-12
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ final class DappBrowserCoordinator: NSObject, Coordinator {
142142
delegate.requestSignTransaction(session: session, source: .browser, requester: nil, transaction: transaction, configuration: .dappTransaction(confirmType: .sign))
143143
.sink(receiveCompletion: { [browserViewController] result in
144144
guard case .failure = result else { return }
145-
browserViewController.notifyFinish(callbackID: callbackId, value: .failure(DAppError.cancelled))
145+
browserViewController.notifyFinish(callbackID: callbackId, value: .failure(.responseError))
146146
}, receiveValue: { [browserViewController] data in
147147
let callback = DappCallback(id: callbackId, value: .signTransaction(data))
148148
browserViewController.notifyFinish(callbackID: callbackId, value: .success(callback))
@@ -157,7 +157,7 @@ final class DappBrowserCoordinator: NSObject, Coordinator {
157157
delegate.requestSendTransaction(session: session, source: .browser, requester: nil, transaction: transaction, configuration: .dappTransaction(confirmType: .signThenSend))
158158
.sink(receiveCompletion: { [browserViewController] result in
159159
guard case .failure = result else { return }
160-
browserViewController.notifyFinish(callbackID: callbackId, value: .failure(DAppError.cancelled))
160+
browserViewController.notifyFinish(callbackID: callbackId, value: .failure(.responseError))
161161
}, receiveValue: { [browserViewController] transaction in
162162
let callback = DappCallback(id: callbackId, value: .sentTransaction(Data(_hex: transaction.id)))
163163
browserViewController.notifyFinish(callbackID: callbackId, value: .success(callback))
@@ -176,11 +176,11 @@ final class DappBrowserCoordinator: NSObject, Coordinator {
176176
.sink(receiveCompletion: { [browserViewController] result in
177177
guard case .failure(let error) = result else { return }
178178

179-
if case JSONRPCError.responseError(_, let message, _) = error.embedded {
180-
browserViewController.notifyFinish(callbackID: callbackId, value: .failure(.nodeError(message)))
179+
if case JSONRPCError.responseError(let code, let message, _) = error.embedded {
180+
browserViewController.notifyFinish(callbackID: callbackId, value: .failure(.init(code: code, message: message)))
181181
} else {
182182
//TODO better handle. User didn't cancel
183-
browserViewController.notifyFinish(callbackID: callbackId, value: .failure(.cancelled))
183+
browserViewController.notifyFinish(callbackID: callbackId, value: .failure(.responseError))
184184
}
185185

186186
}, receiveValue: { [browserViewController] value in
@@ -241,7 +241,7 @@ final class DappBrowserCoordinator: NSObject, Coordinator {
241241
requester: nil)
242242
}.sink(receiveCompletion: { [browserViewController] result in
243243
guard case .failure = result else { return }
244-
browserViewController.notifyFinish(callbackID: callbackId, value: .failure(DAppError.cancelled))
244+
browserViewController.notifyFinish(callbackID: callbackId, value: .failure(.responseError))
245245
}, receiveValue: { [browserViewController] data in
246246
let callback: DappCallback
247247
switch message {
@@ -406,7 +406,7 @@ final class DappBrowserCoordinator: NSObject, Coordinator {
406406
delegate.requestAddCustomChain(server: server, customChain: customChain)
407407
.sink(receiveCompletion: { [weak self] result in
408408
guard case .failure(let e) = result else { return }
409-
let error = e.embedded as? DAppError ?? .nodeError("Unknown Error")
409+
let error = e.embedded as? JsonRpcError ?? .internalError
410410

411411
self?.notifyFinish(callbackID: callbackId, value: .failure(error))
412412
}, receiveValue: { [weak self] operation in
@@ -432,7 +432,7 @@ final class DappBrowserCoordinator: NSObject, Coordinator {
432432
delegate.requestSwitchChain(server: server, currentUrl: currentUrl, targetChain: targetChain)
433433
.sink(receiveCompletion: { [weak self] result in
434434
guard case .failure(let e) = result else { return }
435-
let error = e.embedded as? DAppError ?? .nodeError("Unknown Error")
435+
let error = e.embedded as? JsonRpcError ?? .internalError
436436

437437
self?.notifyFinish(callbackID: callbackId, value: .failure(error))
438438
}, receiveValue: { [weak self] operation in
@@ -448,7 +448,7 @@ final class DappBrowserCoordinator: NSObject, Coordinator {
448448
}).store(in: &cancellable)
449449
}
450450

451-
private func notifyFinish(callbackID: Int, value: Swift.Result<DappCallback, DAppError>) {
451+
private func notifyFinish(callbackID: Int, value: Swift.Result<DappCallback, JsonRpcError>) {
452452
browserViewController.notifyFinish(callbackID: callbackID, value: value)
453453
}
454454
}
@@ -529,16 +529,16 @@ extension DappBrowserCoordinator: BrowserViewControllerDelegate {
529529

530530
func didCall(action: DappAction, callbackID: Int, inBrowserViewController viewController: BrowserViewController) {
531531
guard let session = sessionsProvider.session(for: server) else {
532-
browserViewController.notifyFinish(callbackID: callbackID, value: .failure(.cancelled))
532+
browserViewController.notifyFinish(callbackID: callbackID, value: .failure(.requestRejected))
533533
return
534534
}
535535
guard let delegate = delegate else {
536-
browserViewController.notifyFinish(callbackID: callbackID, value: .failure(.cancelled))
536+
browserViewController.notifyFinish(callbackID: callbackID, value: .failure(.requestRejected))
537537
return
538538
}
539539

540540
func rejectDappAction() {
541-
browserViewController.notifyFinish(callbackID: callbackID, value: .failure(DAppError.cancelled))
541+
browserViewController.notifyFinish(callbackID: callbackID, value: .failure(JsonRpcError.requestRejected))
542542
navigationController.topViewController?.displayError(error: ActiveWalletViewModel.Error.onlyWatchAccount)
543543
}
544544

‎AlphaWallet/Browser/Coordinators/DappRequestSwitchCustomChainCoordinator.swift

+10-10
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,11 @@ class DappRequestSwitchCustomChainCoordinator: NSObject, Coordinator {
4848

4949
func start() -> AnyPublisher<SwitchCustomChainOperation, PromiseError> {
5050
guard let customChainId = Int(chainId0xString: customChain.chainId) else {
51-
return .fail(PromiseError(error: DAppError.nodeError(R.string.localizable.addCustomChainErrorInvalidChainId(customChain.chainId))))
51+
return .fail(PromiseError(error: JsonRpcError.internalError(message: R.string.localizable.addCustomChainErrorInvalidChainId(customChain.chainId))))
5252
}
5353
guard customChain.rpcUrls?.first != nil else {
5454
//Not to spec since RPC URLs are optional according to EIP3085, but it is so much easier to assume it's needed, and quite useless if it isn't provided
55-
return .fail(PromiseError(error: DAppError.nodeError(R.string.localizable.addCustomChainErrorInvalidChainId(customChain.chainId))))
55+
return .fail(PromiseError(error: JsonRpcError.internalError(message: R.string.localizable.addCustomChainErrorInvalidChainId(customChain.chainId))))
5656
}
5757
if let existingServer = ServersCoordinator.serversOrdered.first(where: { $0.chainID == customChainId }) {
5858
if config.enabledServers.contains(where: { $0.chainID == customChainId }) {
@@ -85,7 +85,7 @@ class DappRequestSwitchCustomChainCoordinator: NSObject, Coordinator {
8585
case .action:
8686
runEnableChain()
8787
case .canceled:
88-
subject.send(completion: .failure(PromiseError(error: DAppError.cancelled)))
88+
subject.send(completion: .failure(PromiseError(error: JsonRpcError.requestRejected)))
8989
}
9090
}.cauterize()
9191
}
@@ -118,10 +118,10 @@ class DappRequestSwitchCustomChainCoordinator: NSObject, Coordinator {
118118
case 1:
119119
runAddCustomChain(isTestnet: true)
120120
default:
121-
subject.send(completion: .failure(PromiseError(error: DAppError.cancelled)))
121+
subject.send(completion: .failure(PromiseError(error: JsonRpcError.requestRejected)))
122122
}
123123
case .canceled:
124-
subject.send(completion: .failure(PromiseError(error: DAppError.cancelled)))
124+
subject.send(completion: .failure(PromiseError(error: JsonRpcError.requestRejected)))
125125
}
126126
}.cauterize()
127127
}
@@ -135,7 +135,7 @@ class DappRequestSwitchCustomChainCoordinator: NSObject, Coordinator {
135135
subject.send(.switchBrowserToExistingServer(existingServer, url: self.currentUrl))
136136
subject.send(completion: .finished)
137137
case .canceled:
138-
subject.send(completion: .failure(PromiseError(error: DAppError.cancelled)))
138+
subject.send(completion: .failure(PromiseError(error: JsonRpcError.requestRejected)))
139139
}
140140
}.cauterize()
141141
}
@@ -172,16 +172,16 @@ extension DappRequestSwitchCustomChainCoordinator: AddCustomChainDelegate {
172172
subject.send(completion: .finished)
173173
return
174174
}
175-
let dAppError: DAppError
175+
let jsonRpcError: JsonRpcError
176176
switch error {
177177
case .cancelled:
178-
dAppError = .cancelled
178+
jsonRpcError = .requestRejected
179179
case .missingBlockchainExplorerUrl, .invalidBlockchainExplorerUrl, .noRpcNodeUrl, .invalidChainId, .chainIdNotMatch, .unknown:
180-
dAppError = .nodeError(error.localizedDescription)
180+
jsonRpcError = JsonRpcError.internalError(message: error.localizedDescription)
181181
UIAlertController.alert(title: nil, message: error.localizedDescription, alertButtonTitles: [R.string.localizable.oK()], alertButtonStyles: [.cancel], viewController: viewController)
182182

183183
}
184184

185-
subject.send(completion: .failure(.some(error: dAppError)))
185+
subject.send(completion: .failure(.some(error: jsonRpcError)))
186186
}
187187
}

‎AlphaWallet/Browser/Coordinators/DappRequestSwitchExistingChainCoordinator.swift

+4-4
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ class DappRequestSwitchExistingChainCoordinator: NSObject, Coordinator {
3636

3737
func start() -> AnyPublisher<SwitchExistingChainOperation, PromiseError> {
3838
guard let targetChainId = Int(chainId0xString: targetChain.chainId) else {
39-
return .fail(PromiseError(error: DAppError.nodeError(R.string.localizable.switchChainErrorInvalidChainId(targetChain.chainId))))
39+
return .fail(PromiseError(error: JsonRpcError.internalError(message: R.string.localizable.switchChainErrorInvalidChainId(targetChain.chainId))))
4040
}
4141
if let existingServer = ServersCoordinator.serversOrdered.first(where: { $0.chainID == targetChainId }) {
4242
if config.enabledServers.contains(where: { $0.chainID == targetChainId }) {
@@ -50,7 +50,7 @@ class DappRequestSwitchExistingChainCoordinator: NSObject, Coordinator {
5050
promptAndActivateExistingServer(existingServer: existingServer, inViewController: viewController)
5151
}
5252
} else {
53-
return .fail(PromiseError(error: DAppError.nodeError(R.string.localizable.switchChainErrorNotSupportedChainId(targetChain.chainId))))
53+
return .fail(PromiseError(error: JsonRpcError.unsupportedChain(chainId: targetChain.chainId)))
5454
}
5555

5656
return subject.eraseToAnyPublisher()
@@ -70,7 +70,7 @@ class DappRequestSwitchExistingChainCoordinator: NSObject, Coordinator {
7070
case .action:
7171
runEnableChain()
7272
case .canceled:
73-
subject.send(completion: .failure(PromiseError(error: DAppError.cancelled)))
73+
subject.send(completion: .failure(PromiseError(error: JsonRpcError.requestRejected)))
7474
}
7575
}.cauterize()
7676
}
@@ -84,7 +84,7 @@ class DappRequestSwitchExistingChainCoordinator: NSObject, Coordinator {
8484
subject.send(.switchBrowserToExistingServer(existingServer, url: self.currentUrl))
8585
subject.send(completion: .finished)
8686
case .canceled:
87-
subject.send(completion: .failure(PromiseError(error: DAppError.cancelled)))
87+
subject.send(completion: .failure(PromiseError(error: JsonRpcError.requestRejected)))
8888
}
8989
}.cauterize()
9090
}

‎AlphaWallet/Browser/ViewControllers/BrowserViewController.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ final class BrowserViewController: UIViewController {
149149
webView.load(URLRequest(url: url))
150150
}
151151

152-
func notifyFinish(callbackID: Int, value: Swift.Result<DappCallback, DAppError>) {
152+
func notifyFinish(callbackID: Int, value: Swift.Result<DappCallback, JsonRpcError>) {
153153
let script: String = {
154154
switch value {
155155
case .success(let result):

‎AlphaWallet/Common/Views/TokenCardRowView.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -347,7 +347,7 @@ extension TokenCardRowView: TokenInstanceWebViewDelegate {
347347
source: Analytics.SignMessageRequestSource,
348348
requester: RequesterViewModel?) -> AnyPublisher<Data, PromiseError> {
349349

350-
return .fail(PromiseError(error: DAppError.cancelled))
350+
return .fail(PromiseError(error: JsonRpcError.requestRejected))
351351
}
352352

353353
func shouldClose(tokenInstanceWebView: TokenInstanceWebView) {

‎AlphaWallet/Swap/Swap/SwapTokensCoordinator.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -269,8 +269,8 @@ extension SwapTokensCoordinator: ApproveSwapProviderDelegate {
269269
return transaction.id
270270
}
271271
}.recover { error -> Promise<String> in
272-
//TODO no good to have `DAppError` here, but this is because of `TransactionConfirmationCoordinatorBridgeToPromise`. Maybe good to have a global "UserCancelled" or something? If enum, not too many cases? To avoid `switch`
273-
if case DAppError.cancelled = error {
272+
//TODO no good to have `JsonRpcError` here, but this is because of `TransactionConfirmationCoordinatorBridgeToPromise`. Maybe good to have a global "UserCancelled" or something? If enum, not too many cases? To avoid `switch`
273+
if let e = error as? JsonRpcError, e == .requestRejected {
274274
throw SwapError.userCancelledApproval
275275
} else {
276276
throw error

‎AlphaWallet/Tokens/Views/TokenInstanceWebView.swift

+5-5
Original file line numberDiff line numberDiff line change
@@ -342,7 +342,7 @@ extension TokenInstanceWebView: WKScriptMessageHandler {
342342
switch action {
343343
case .signPersonalMessage(let message):
344344
guard let delegate = self.delegate else {
345-
self.notifyFinish(callbackID: command.id, value: .failure(DAppError.cancelled))
345+
self.notifyFinish(callbackID: command.id, value: .failure(JsonRpcError.requestRejected))
346346
return
347347
}
348348

@@ -353,10 +353,10 @@ extension TokenInstanceWebView: WKScriptMessageHandler {
353353
source: .tokenScript,
354354
requester: nil)
355355
.handleEvents(receiveCancel: {
356-
self.notifyFinish(callbackID: command.id, value: .failure(DAppError.cancelled))
356+
self.notifyFinish(callbackID: command.id, value: .failure(JsonRpcError.requestRejected))
357357
})
358358
.sinkAsync(receiveCompletion: { _ in
359-
self.notifyFinish(callbackID: command.id, value: .failure(DAppError.cancelled))
359+
self.notifyFinish(callbackID: command.id, value: .failure(JsonRpcError.requestRejected))
360360
}, receiveValue: { value in
361361
let callback = DappCallback(id: command.id, value: .signPersonalMessage(value))
362362
self.notifyFinish(callbackID: command.id, value: .success(callback))
@@ -399,13 +399,13 @@ extension TokenInstanceWebView: WKUIDelegate {
399399

400400
//TODO this contains functions duplicated and modified from BrowserViewController. Clean this up. Or move it somewhere, to a coordinator?
401401
extension TokenInstanceWebView {
402-
func notifyFinish(callbackID: Int, value: Swift.Result<DappCallback, DAppError>) {
402+
func notifyFinish(callbackID: Int, value: Swift.Result<DappCallback, JsonRpcError>) {
403403
let script: String = {
404404
switch value {
405405
case .success(let result):
406406
return "executeCallback(\(callbackID), null, \"\(result.value.object)\")"
407407
case .failure(let error):
408-
return "executeCallback(\(callbackID), \"\(error)\", null)"
408+
return "executeCallback(\(callbackID), \"\(error.message)\", null)"
409409
}
410410
}()
411411
webView.evaluateJavaScript(script, completionHandler: nil)

‎AlphaWallet/WalletConnect/Controllers/SignMessageCoordinatorBridgeToPromise.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ extension SignMessageCoordinatorBridgeToPromise: SignMessageCoordinatorDelegate
5656
}
5757

5858
func didCancel(in coordinator: SignMessageCoordinator) {
59-
seal.reject(DAppError.cancelled)
59+
seal.reject(JsonRpcError.requestRejected)
6060
}
6161
}
6262

‎AlphaWallet/WalletConnect/Controllers/TransactionConfirmationCoordinatorBridgeToPromise.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ extension TransactionConfirmationCoordinatorBridgeToPromise: TransactionConfirma
108108
}
109109

110110
func didClose(in coordinator: TransactionConfirmationCoordinator) {
111-
seal.reject(DAppError.cancelled)
111+
seal.reject(JsonRpcError.requestRejected)
112112
}
113113

114114
func buyCrypto(wallet: Wallet, server: RPCServer, viewController: UIViewController, source: Analytics.BuyCryptoSource) {

‎AlphaWallet/WalletConnect/WalletConnectServer.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ enum WalletConnectError: Error {
2121
case `internal`(JsonRpcError)
2222

2323
init(error: PromiseError) {
24-
if case DAppError.cancelled = error.embedded {
24+
if let e = error.embedded as? JsonRpcError, e == .requestRejected {
2525
self = .cancelled
2626
} else if case PMKError.cancelled = error.embedded {
2727
self = .cancelled

‎modules/AlphaWalletFoundation/AlphaWalletFoundation/Browser/Types/DAppError.swift

-18
This file was deleted.

‎modules/AlphaWalletFoundation/AlphaWalletFoundation/Types/RpcJson/JsonRpcError.swift

+4
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,8 @@ public extension JsonRpcError {
2929
static func unsupportedChain(chainId: String) -> JsonRpcError {
3030
JsonRpcError(code: 4902, message: "Unrecognized chain ID \(chainId). Try adding the chain using wallet_addEthereumChain first.")
3131
}
32+
33+
static func internalError(message: String) -> JsonRpcError {
34+
JsonRpcError(code: -32603, message: message)
35+
}
3236
}

0 commit comments

Comments
 (0)
Please sign in to comment.