diff --git a/lib/features/dapps/subfeatures/open_dapp/domain/helpers/bridge/bridge.dart b/lib/features/dapps/subfeatures/open_dapp/domain/helpers/bridge/bridge.dart new file mode 100644 index 00000000..c0e0d640 --- /dev/null +++ b/lib/features/dapps/subfeatures/open_dapp/domain/helpers/bridge/bridge.dart @@ -0,0 +1,2 @@ +export 'bridge_helper.dart'; +export 'bridge_functions_helper.dart'; diff --git a/lib/features/dapps/subfeatures/open_dapp/domain/helpers/bridge/bridge_functions_helper.dart b/lib/features/dapps/subfeatures/open_dapp/domain/helpers/bridge/bridge_functions_helper.dart new file mode 100644 index 00000000..779ca0de --- /dev/null +++ b/lib/features/dapps/subfeatures/open_dapp/domain/helpers/bridge/bridge_functions_helper.dart @@ -0,0 +1,152 @@ +import 'dart:typed_data'; + +import 'package:datadashwallet/common/components/components.dart'; +import 'package:datadashwallet/features/common/common.dart'; +import 'package:datadashwallet/features/portfolio/subfeatures/token/add_token/domain/custom_tokens_use_case.dart'; +import 'package:datadashwallet/features/settings/settings.dart'; +import 'package:flutter/material.dart'; +import 'package:mxc_logic/mxc_logic.dart'; + +import '../../../open_dapp.dart'; + +class BridgeFunctionsHelper { + BridgeFunctionsHelper({ + required this.state, + required this.context, + required this.translate, + required this.navigator, + required this.customTokensUseCase, + required this.tokenContractUseCase, + required this.transactionHistoryUseCase, + required this.chainConfigurationUseCase, + required this.addError, + required this.loading, + }); + + OpenDAppState state; + TokenContractUseCase tokenContractUseCase; + CustomTokensUseCase customTokensUseCase; + TransactionsHistoryUseCase transactionHistoryUseCase; + ChainConfigurationUseCase chainConfigurationUseCase; + NavigatorState? navigator; + BuildContext? context; + String? Function(String) translate; + void Function(bool v) loading; + void Function(dynamic error, [StackTrace? stackTrace]) addError; + + Future estimatedFee( + String from, + String to, + EtherAmount? gasPrice, + Uint8List data, + BigInt? amountOfGas, + ) async { + loading(true); + try { + final gasFee = await tokenContractUseCase.estimateGasFeeForContractCall( + from: from, + to: to, + gasPrice: gasPrice, + data: data, + amountOfGas: amountOfGas); + loading(false); + + return gasFee; + } catch (e, s) { + addError(e, s); + } finally { + loading(false); + } + } + + Future sendTransaction(String to, EtherAmount amount, + Uint8List? data, TransactionGasEstimation? estimatedGasFee, String url, + {String? from}) async { + final res = await tokenContractUseCase.sendTransaction( + privateKey: state.account!.privateKey, + to: to, + from: from, + amount: amount, + data: data, + estimatedGasFee: estimatedGasFee); + if (!MXCChains.isMXCChains(state.network!.chainId)) { + recordTransaction(res); + } + + return res.hash; + } + + String? signMessage( + String hexData, + ) { + loading(true); + try { + final res = tokenContractUseCase.signMessage( + privateKey: state.account!.privateKey, message: hexData); + return res; + } catch (e, s) { + addError(e, s); + } finally { + loading(false); + } + } + + String? signTypedMessage( + String hexData, + ) { + loading(true); + try { + final res = tokenContractUseCase.signTypedMessage( + privateKey: state.account!.privateKey, data: hexData); + return res; + } catch (e, s) { + addError(e, s); + } finally { + loading(false); + } + } + + bool addAsset(Token token) { + loading(true); + try { + customTokensUseCase.addItem(token); + return true; + } catch (error, stackTrace) { + addError(error, stackTrace); + return false; + } finally { + loading(false); + } + } + + void recordTransaction(TransactionModel tx) { + final currentNetwork = state.network!; + final chainId = currentNetwork.chainId; + final token = Token( + chainId: currentNetwork.chainId, + logoUri: currentNetwork.logo, + name: currentNetwork.label ?? currentNetwork.web3RpcHttpUrl, + symbol: currentNetwork.symbol, + address: null, + ); + + tx = tx.copyWith(token: token); + + transactionHistoryUseCase.spyOnTransaction( + tx, + ); + transactionHistoryUseCase.updateItem( + tx, + ); + } + + Network? updateNetwork(Network network, int index) { + chainConfigurationUseCase.updateItem(network, index); + return network; + } + + Network? addNewNetwork(Network newNetwork) { + chainConfigurationUseCase.addItem(newNetwork); + return newNetwork; + } +} diff --git a/lib/features/dapps/subfeatures/open_dapp/domain/helpers/bridge/bridge_helper.dart b/lib/features/dapps/subfeatures/open_dapp/domain/helpers/bridge/bridge_helper.dart new file mode 100644 index 00000000..47655bb5 --- /dev/null +++ b/lib/features/dapps/subfeatures/open_dapp/domain/helpers/bridge/bridge_helper.dart @@ -0,0 +1,353 @@ +import 'dart:convert'; + +import 'package:datadashwallet/common/common.dart'; +import 'package:datadashwallet/core/src/providers/providers.dart'; +import 'package:datadashwallet/features/common/common.dart'; + +import 'package:flutter/material.dart'; +import 'package:mxc_logic/mxc_logic.dart'; +import 'package:web3_provider/web3_provider.dart'; + +import '../../../../../../settings/subfeatures/chain_configuration/domain/domain.dart'; +import '../../../open_dapp.dart'; + +class BridgeHelper { + BridgeHelper({ + required this.tokenContractUseCase, + required this.chainConfigurationUseCase, + required this.authUseCase, + required this.errorUseCase, + required this.bridgeFunctionsHelper, + required this.state, + required this.context, + required this.translate, + required this.addError, + required this.loading, + required this.notify, + required this.addMessage, + }); + + OpenDAppState state; + TokenContractUseCase tokenContractUseCase; + ChainConfigurationUseCase chainConfigurationUseCase; + AuthUseCase authUseCase; + ErrorUseCase errorUseCase; + BridgeFunctionsHelper bridgeFunctionsHelper; + BuildContext? context; + String? Function(String) translate; + void Function(bool v) loading; + void Function(dynamic error, [StackTrace? stackTrace]) addError; + void Function([void Function()? fun]) notify; + void Function(dynamic message) addMessage; + + void signTransaction({ + required BridgeParams bridge, + required VoidCallback cancel, + required Function(String idHaethClientsh) success, + required String url, + }) async { + final amountEther = EtherAmount.inWei(bridge.value ?? BigInt.zero); + final amount = amountEther.getValueInUnit(EtherUnit.ether).toString(); + final bridgeData = MXCType.hexToUint8List(bridge.data ?? ''); + EtherAmount? gasPrice; + double? gasFee; + TransactionGasEstimation? estimatedGasFee; + BigInt? amountOfGas; + + if (bridge.gasPrice != null) { + gasPrice = EtherAmount.fromBase10String(EtherUnit.wei, bridge.gasPrice!); + } + + if (bridge.gas != null) { + amountOfGas = BigInt.parse(bridge.gas.toString()); + gasPrice = gasPrice ?? await tokenContractUseCase.getGasPrice(); + final gasPriceDouble = + gasPrice.getValueInUnit(EtherUnit.ether).toDouble(); + gasFee = gasPriceDouble * amountOfGas.toDouble(); + + estimatedGasFee = TransactionGasEstimation( + gasPrice: gasPrice, gas: amountOfGas, gasFee: gasFee); + } else { + estimatedGasFee = await bridgeFunctionsHelper.estimatedFee( + bridge.from!, bridge.to!, gasPrice, bridgeData, amountOfGas); + + if (estimatedGasFee == null) { + cancel.call(); + return; + } + } + + String finalFee = + (estimatedGasFee.gasFee / Config.dappSectionFeeDivision).toString(); + final maxFeeDouble = estimatedGasFee.gasFee * Config.priority; + final maxFeeString = + (maxFeeDouble / Config.dappSectionFeeDivision).toString(); + final maxFee = + Validation.isExpoNumber(maxFeeString) ? '0.000' : maxFeeString; + + if (Validation.isExpoNumber(finalFee)) { + finalFee = '0.000'; + } + + final symbol = state.network!.symbol; + + try { + final result = await showTransactionDialog(context!, + title: translate('confirm_transaction')!, + amount: amount, + from: bridge.from!, + to: bridge.to!, + estimatedFee: finalFee, + maxFee: maxFee, + symbol: symbol); + + if (result != null && result) { + loading(true); + + final hash = await bridgeFunctionsHelper.sendTransaction( + bridge.to!, amountEther, bridgeData, estimatedGasFee, url, + from: bridge.from); + if (hash != null) success.call(hash); + } else { + cancel.call(); + } + } catch (e, s) { + cancel.call(); + callErrorHandler(e, s); + } finally { + loading(false); + } + } + + void switchEthereumChain(dynamic id, Map params) async { + final rawChainId = params["object"]["chainId"] as String; + final chainId = MXCFormatter.hexToDecimal(rawChainId); + final networks = chainConfigurationUseCase.networks.value; + final foundChainIdIndex = + networks.indexWhere((element) => element.chainId == chainId); + + if (foundChainIdIndex != -1) { + final foundNetwork = networks[foundChainIdIndex]; + final res = await showSwitchNetworkDialog(context!, + fromNetwork: state.network!.label ?? state.network!.web3RpcHttpUrl, + toNetwork: foundNetwork.label ?? foundNetwork.web3RpcHttpUrl, + onTap: () { + switchDefaultNetwork(id, foundNetwork, rawChainId); + }); + if (!(res ?? false)) { + cancelRequest(id); + } + } else { + addError(translate('network_not_found')); + final e = + DAppErrors.switchEthereumChainErrors.unRecognizedChain(rawChainId); + sendProviderError( + id, e['code'], MXCFormatter.escapeDoubleQuotes(e['message'])); + } + } + + void addEthereumChain(dynamic id, Map params) async { + final networkDetails = AddEthereumChain.fromMap(params["object"]); + + final rawChainId = networkDetails.chainId; + final chainId = MXCFormatter.hexToDecimal(rawChainId); + final networks = chainConfigurationUseCase.networks.value; + final foundChainIdIndex = + networks.indexWhere((element) => element.chainId == chainId); + // user can add a network again meaning It will override the old network + final alreadyExists = foundChainIdIndex != -1; + final alreadyEnabled = + alreadyExists ? networks[foundChainIdIndex].enabled : false; + + // Add network + final newNetwork = Network.fromAddEthereumChain(networkDetails, chainId); + + final res = await showAddNetworkDialog( + context!, + network: newNetwork, + approveFunction: (network) => alreadyExists + ? bridgeFunctionsHelper.updateNetwork(network, foundChainIdIndex) + : bridgeFunctionsHelper.addNewNetwork(network), + ); + + if (!(res ?? false)) { + cancelRequest(id); + } else { + if (!alreadyEnabled) { + final res = await showSwitchNetworkDialog(context!, + fromNetwork: state.network!.label ?? state.network!.web3RpcHttpUrl, + toNetwork: newNetwork.label ?? newNetwork.web3RpcHttpUrl, + onTap: () { + switchDefaultNetwork(id, newNetwork, rawChainId); + }); + if (!(res ?? false)) { + cancelRequest(id); + } + } + } + } + + /// This function is used for both sign message and sign personal message + void signMessage({ + required Map object, + required VoidCallback cancel, + required Function(String hash) success, + }) async { + final hexData = object['data'] as String; + String message = MXCType.hexToString(hexData); + int chainId = state.network!.chainId; + String name = state.network!.symbol; + + try { + final result = await showSignMessageDialog( + context!, + title: translate('signature_request')!, + message: message, + networkName: '$name ($chainId)', + ); + + if (result != null && result) { + final hash = bridgeFunctionsHelper.signMessage( + hexData, + ); + if (hash != null) success.call(hash); + } else { + cancel.call(); + } + } catch (e, s) { + cancel.call(); + addError(e, s); + } + } + + void signTypedMessage({ + required Map object, + required VoidCallback cancel, + required Function(String hash) success, + }) async { + String hexData = object['raw'] as String; + Map data = + jsonDecode(object['raw'] as String) as Map; + Map domain = data['domain'] as Map; + String primaryType = data['primaryType']; + int chainId = (domain['chainId']) as int; + String name = domain['name'] as String; + + try { + final result = await showTypedMessageDialog(context!, + title: translate('signature_request')!, + message: data['message'] as Map, + networkName: '$name ($chainId)', + primaryType: primaryType); + + if (result != null && result) { + final hash = bridgeFunctionsHelper.signTypedMessage( + hexData, + ); + if (hash != null) success.call(hash); + } else { + cancel.call(); + } + } catch (e, s) { + cancel.call(); + addError(e, s); + } + } + + void setAddress(dynamic id) { + if (state.account != null) { + final walletAddress = state.account!.address; + state.webviewController?.setAddress(walletAddress, id); + } + } + + void switchDefaultNetwork(int id, Network toNetwork, String rawChainId) { + // "{"id":1692336424091,"name":"switchEthereumChain","object":{"chainId":"0x66eed"},"network":"ethereum"}" + chainConfigurationUseCase.switchDefaultNetwork(toNetwork); + authUseCase.resetNetwork(toNetwork); + loadDataDashProviders(toNetwork); + notify(() => state.network = toNetwork); + + setChain(id); + } + + void addAsset(int id, Map data, + {required VoidCallback cancel, + required Function(String status) success}) async { + final watchAssetData = WatchAssetModel.fromMap(data); + String titleText = translate('add_x') + ?.replaceFirst('{0}', translate('token')?.toLowerCase() ?? '--') ?? + '--'; + + try { + final result = await showAddAssetDialog( + context!, + token: watchAssetData, + title: titleText, + ); + + if (result != null && result) { + final res = bridgeFunctionsHelper.addAsset(Token( + decimals: watchAssetData.decimals, + address: watchAssetData.contract, + symbol: watchAssetData.symbol, + chainId: state.network?.chainId)); + + if (res) { + success.call(res.toString()); + addMessage(translate('add_token_success_message')); + } else { + cancel.call(); + } + } else { + cancel.call(); + } + } catch (e, s) { + cancel.call(); + addError(e, s); + } + } + + void callErrorHandler(dynamic e, StackTrace s) { + final isHandled = errorUseCase.handleError( + context!, + e, + addError, + translate, + ); + if (!isHandled) { + addError(e, s); + } + } + + void setChain(int? id) { + state.webviewController + ?.setChain(getProviderConfig(), state.network!.chainId, id); + } + + void cancelRequest(int id) { + state.webviewController?.cancel(id); + } + + void checkCancel(bool? res, Function moveOn, int id) { + if (!(res ?? false)) { + cancelRequest(id); + } else { + moveOn(); + } + } + + void sendProviderError(int id, int code, String message) { + state.webviewController?.sendProviderError(id, code, message); + } + + void sendError(String error, int id) { + state.webviewController + ?.sendError(MXCFormatter.escapeDoubleQuotes(error), id); + } + + String getProviderConfig() { + return JSChannelScripts.walletProviderInfoScript(state.network!.chainId, + state.network!.web3RpcHttpUrl, state.account!.address); + } +} diff --git a/lib/features/dapps/subfeatures/open_dapp/domain/helpers/helpers.dart b/lib/features/dapps/subfeatures/open_dapp/domain/helpers/helpers.dart index 8d683965..4ad096da 100644 --- a/lib/features/dapps/subfeatures/open_dapp/domain/helpers/helpers.dart +++ b/lib/features/dapps/subfeatures/open_dapp/domain/helpers/helpers.dart @@ -1 +1,2 @@ export 'js_channels/js_channel.dart'; +export 'bridge/bridge.dart'; diff --git a/lib/features/dapps/subfeatures/open_dapp/open_dapp_presenter.dart b/lib/features/dapps/subfeatures/open_dapp/open_dapp_presenter.dart index 4195c02b..16bc5187 100644 --- a/lib/features/dapps/subfeatures/open_dapp/open_dapp_presenter.dart +++ b/lib/features/dapps/subfeatures/open_dapp/open_dapp_presenter.dart @@ -96,6 +96,34 @@ class OpenDAppPresenter extends CompletePresenter { state: state, ); + BridgeFunctionsHelper get bridgeFunctionsHelper => BridgeFunctionsHelper( + translate: translate, + context: context, + state: state, + addError: addError, + chainConfigurationUseCase: _chainConfigurationUseCase, + tokenContractUseCase: _tokenContractUseCase, + transactionHistoryUseCase: _transactionHistoryUseCase, + customTokensUseCase: _customTokensUseCase, + loading: (bool value) => loading = value, + navigator: navigator, + ); + + BridgeHelper get bridgeHelper => BridgeHelper( + translate: translate, + context: context, + state: state, + addError: addError, + addMessage: addMessage, + authUseCase: _authUseCase, + chainConfigurationUseCase: _chainConfigurationUseCase, + errorUseCase: _errorUseCase, + tokenContractUseCase: _tokenContractUseCase, + bridgeFunctionsHelper: bridgeFunctionsHelper, + loading: (bool value) => loading = value, + notify: notify, + ); + @override void initState() { super.initState(); @@ -167,402 +195,8 @@ class OpenDAppPresenter extends CompletePresenter { ); } - Future _estimatedFee( - String from, - String to, - EtherAmount? gasPrice, - Uint8List data, - BigInt? amountOfGas, - ) async { - loading = true; - try { - final gasFee = await _tokenContractUseCase.estimateGasFeeForContractCall( - from: from, - to: to, - gasPrice: gasPrice, - data: data, - amountOfGas: amountOfGas); - loading = false; - - return gasFee; - } catch (e, s) { - addError(e, s); - } finally { - loading = false; - } - } - - Future _sendTransaction(String to, EtherAmount amount, - Uint8List? data, TransactionGasEstimation? estimatedGasFee, String url, - {String? from}) async { - final res = await _tokenContractUseCase.sendTransaction( - privateKey: state.account!.privateKey, - to: to, - from: from, - amount: amount, - data: data, - estimatedGasFee: estimatedGasFee); - if (!MXCChains.isMXCChains(state.network!.chainId)) { - recordTransaction(res); - } - - return res.hash; - } - - String? _signMessage( - String hexData, - ) { - loading = true; - try { - final res = _tokenContractUseCase.signMessage( - privateKey: state.account!.privateKey, message: hexData); - return res; - } catch (e, s) { - addError(e, s); - } finally { - loading = false; - } - } - - String? _signTypedMessage( - String hexData, - ) { - loading = true; - try { - final res = _tokenContractUseCase.signTypedMessage( - privateKey: state.account!.privateKey, data: hexData); - return res; - } catch (e, s) { - addError(e, s); - } finally { - loading = false; - } - } - - bool _addAsset(Token token) { - loading = true; - try { - _customTokensUseCase.addItem(token); - return true; - } catch (error, stackTrace) { - addError(error, stackTrace); - return false; - } finally { - loading = false; - } - } - - void recordTransaction(TransactionModel tx) { - final currentNetwork = state.network!; - final chainId = currentNetwork.chainId; - final token = Token( - chainId: currentNetwork.chainId, - logoUri: currentNetwork.logo, - name: currentNetwork.label ?? currentNetwork.web3RpcHttpUrl, - symbol: currentNetwork.symbol, - address: null, - ); - - tx = tx.copyWith(token: token); - - _transactionHistoryUseCase.spyOnTransaction( - tx, - ); - _transactionHistoryUseCase.updateItem( - tx, - ); - } - - void signTransaction({ - required BridgeParams bridge, - required VoidCallback cancel, - required Function(String idHaethClientsh) success, - required String url, - }) async { - final amountEther = EtherAmount.inWei(bridge.value ?? BigInt.zero); - final amount = amountEther.getValueInUnit(EtherUnit.ether).toString(); - final bridgeData = MXCType.hexToUint8List(bridge.data ?? ''); - EtherAmount? gasPrice; - double? gasFee; - TransactionGasEstimation? estimatedGasFee; - BigInt? amountOfGas; - - if (bridge.gasPrice != null) { - gasPrice = EtherAmount.fromBase10String(EtherUnit.wei, bridge.gasPrice!); - } - - if (bridge.gas != null) { - amountOfGas = BigInt.parse(bridge.gas.toString()); - gasPrice = gasPrice ?? await _tokenContractUseCase.getGasPrice(); - final gasPriceDouble = - gasPrice.getValueInUnit(EtherUnit.ether).toDouble(); - gasFee = gasPriceDouble * amountOfGas.toDouble(); - - estimatedGasFee = TransactionGasEstimation( - gasPrice: gasPrice, gas: amountOfGas, gasFee: gasFee); - } else { - estimatedGasFee = await _estimatedFee( - bridge.from!, bridge.to!, gasPrice, bridgeData, amountOfGas); - - if (estimatedGasFee == null) { - cancel.call(); - return; - } - } - - String finalFee = - (estimatedGasFee.gasFee / Config.dappSectionFeeDivision).toString(); - final maxFeeDouble = estimatedGasFee.gasFee * Config.priority; - final maxFeeString = - (maxFeeDouble / Config.dappSectionFeeDivision).toString(); - final maxFee = - Validation.isExpoNumber(maxFeeString) ? '0.000' : maxFeeString; - - if (Validation.isExpoNumber(finalFee)) { - finalFee = '0.000'; - } - - final symbol = state.network!.symbol; - - try { - final result = await showTransactionDialog(context!, - title: translate('confirm_transaction')!, - amount: amount, - from: bridge.from!, - to: bridge.to!, - estimatedFee: finalFee, - maxFee: maxFee, - symbol: symbol); - - if (result != null && result) { - loading = true; - - final hash = await _sendTransaction( - bridge.to!, amountEther, bridgeData, estimatedGasFee, url, - from: bridge.from); - if (hash != null) success.call(hash); - } else { - cancel.call(); - } - } catch (e, s) { - cancel.call(); - callErrorHandler(e, s); - } finally { - loading = false; - } - } - - void callErrorHandler(dynamic e, StackTrace s) { - final isHandled = _errorUseCase.handleError( - context!, - e, - addError, - translate, - ); - if (!isHandled) { - addError(e, s); - } - } - - void switchEthereumChain(dynamic id, Map params) async { - final rawChainId = params["object"]["chainId"] as String; - final chainId = MXCFormatter.hexToDecimal(rawChainId); - final networks = _chainConfigurationUseCase.networks.value; - final foundChainIdIndex = - networks.indexWhere((element) => element.chainId == chainId); - - if (foundChainIdIndex != -1) { - final foundNetwork = networks[foundChainIdIndex]; - final res = await showSwitchNetworkDialog(context!, - fromNetwork: state.network!.label ?? state.network!.web3RpcHttpUrl, - toNetwork: foundNetwork.label ?? foundNetwork.web3RpcHttpUrl, - onTap: () { - switchDefaultNetwork(id, foundNetwork, rawChainId); - }); - if (!(res ?? false)) { - cancelRequest(id); - } - } else { - addError(translate('network_not_found')); - final e = - DAppErrors.switchEthereumChainErrors.unRecognizedChain(rawChainId); - sendProviderError( - id, e['code'], MXCFormatter.escapeDoubleQuotes(e['message'])); - } - } - - void checkCancel(bool? res, Function moveOn, int id) { - if (!(res ?? false)) { - cancelRequest(id); - } else { - moveOn(); - } - } - - void sendProviderError(int id, int code, String message) { - state.webviewController?.sendProviderError(id, code, message); - } - - void sendError(String error, int id) { - state.webviewController - ?.sendError(MXCFormatter.escapeDoubleQuotes(error), id); - } - - void cancelRequest(int id) { - state.webviewController?.cancel(id); - } - - void unSupportedRequest() { - addError(translate('network_not_found')); - } - - void addEthereumChain(dynamic id, Map params) async { - final networkDetails = AddEthereumChain.fromMap(params["object"]); - - final rawChainId = networkDetails.chainId; - final chainId = MXCFormatter.hexToDecimal(rawChainId); - final networks = _chainConfigurationUseCase.networks.value; - final foundChainIdIndex = - networks.indexWhere((element) => element.chainId == chainId); - // user can add a network again meaning It will override the old network - final alreadyExists = foundChainIdIndex != -1; - final alreadyEnabled = - alreadyExists ? networks[foundChainIdIndex].enabled : false; - - // Add network - final newNetwork = Network.fromAddEthereumChain(networkDetails, chainId); - - final res = await showAddNetworkDialog( - context!, - network: newNetwork, - approveFunction: (network) => alreadyExists - ? updateNetwork(network, foundChainIdIndex) - : addNewNetwork(network), - ); - - if (!(res ?? false)) { - cancelRequest(id); - } else { - if (!alreadyEnabled) { - final res = await showSwitchNetworkDialog(context!, - fromNetwork: state.network!.label ?? state.network!.web3RpcHttpUrl, - toNetwork: newNetwork.label ?? newNetwork.web3RpcHttpUrl, - onTap: () { - switchDefaultNetwork(id, newNetwork, rawChainId); - }); - if (!(res ?? false)) { - cancelRequest(id); - } - } - } - } - - Network? updateNetwork(Network network, int index) { - _chainConfigurationUseCase.updateItem(network, index); - return network; - } - - Network? addNewNetwork(Network newNetwork) { - _chainConfigurationUseCase.addItem(newNetwork); - return newNetwork; - } - - /// This function is used for both sign message and sign personal message - void signMessage({ - required Map object, - required VoidCallback cancel, - required Function(String hash) success, - }) async { - final hexData = object['data'] as String; - String message = MXCType.hexToString(hexData); - int chainId = state.network!.chainId; - String name = state.network!.symbol; - - try { - final result = await showSignMessageDialog( - context!, - title: translate('signature_request')!, - message: message, - networkName: '$name ($chainId)', - ); - - if (result != null && result) { - final hash = _signMessage( - hexData, - ); - if (hash != null) success.call(hash); - } else { - cancel.call(); - } - } catch (e, s) { - cancel.call(); - addError(e, s); - } - } - - void signTypedMessage({ - required Map object, - required VoidCallback cancel, - required Function(String hash) success, - }) async { - String hexData = object['raw'] as String; - Map data = - jsonDecode(object['raw'] as String) as Map; - Map domain = data['domain'] as Map; - String primaryType = data['primaryType']; - int chainId = (domain['chainId']) as int; - String name = domain['name'] as String; - - try { - final result = await showTypedMessageDialog(context!, - title: translate('signature_request')!, - message: data['message'] as Map, - networkName: '$name ($chainId)', - primaryType: primaryType); - - if (result != null && result) { - final hash = _signTypedMessage( - hexData, - ); - if (hash != null) success.call(hash); - } else { - cancel.call(); - } - } catch (e, s) { - cancel.call(); - addError(e, s); - } - } - void changeProgress(int progress) => notify(() => state.progress = progress); - void setAddress(dynamic id) { - if (state.account != null) { - final walletAddress = state.account!.address; - state.webviewController?.setAddress(walletAddress, id); - } - } - - void switchDefaultNetwork(int id, Network toNetwork, String rawChainId) { - // "{"id":1692336424091,"name":"switchEthereumChain","object":{"chainId":"0x66eed"},"network":"ethereum"}" - _chainConfigurationUseCase.switchDefaultNetwork(toNetwork); - _authUseCase.resetNetwork(toNetwork); - loadDataDashProviders(toNetwork); - notify(() => state.network = toNetwork); - - setChain(id); - } - - void setChain(int? id) { - state.webviewController - ?.setChain(getProviderConfig(), state.network!.chainId, id); - } - - String getProviderConfig() { - return JSChannelScripts.walletProviderInfoScript(state.network!.chainId, - state.network!.web3RpcHttpUrl, state.account!.address); - } - void copy(List params) { Clipboard.setData(ClipboardData(text: params[0])); } @@ -586,43 +220,6 @@ class OpenDAppPresenter extends CompletePresenter { return Validation.isAddress(address); } - void addAsset(int id, Map data, - {required VoidCallback cancel, - required Function(String status) success}) async { - final watchAssetData = WatchAssetModel.fromMap(data); - String titleText = translate('add_x') - ?.replaceFirst('{0}', translate('token')?.toLowerCase() ?? '--') ?? - '--'; - - try { - final result = await showAddAssetDialog( - context!, - token: watchAssetData, - title: titleText, - ); - - if (result != null && result) { - final res = _addAsset(Token( - decimals: watchAssetData.decimals, - address: watchAssetData.contract, - symbol: watchAssetData.symbol, - chainId: state.network?.chainId)); - - if (res) { - success.call(res.toString()); - addMessage(translate('add_token_success_message')); - } else { - cancel.call(); - } - } else { - cancel.call(); - } - } catch (e, s) { - cancel.call(); - addError(e, s); - } - } - void launchAddress(String address) { _launcherUseCase.viewAddress(address); } @@ -663,6 +260,76 @@ class OpenDAppPresenter extends CompletePresenter { void hidePanel() => PanelUtils.hidePanel(state, panelTimer); + void signTransaction({ + required BridgeParams bridge, + required VoidCallback cancel, + required Function(String idHaethClientsh) success, + required String url, + }) async => + bridgeHelper.signTransaction( + bridge: bridge, + cancel: cancel, + success: success, + url: url, + ); + + void switchEthereumChain(dynamic id, Map params) async => + bridgeHelper.switchEthereumChain( + id, + params, + ); + + void addEthereumChain(dynamic id, Map params) async => + bridgeHelper.addEthereumChain( + id, + params, + ); + + void signMessage({ + required Map object, + required VoidCallback cancel, + required Function(String hash) success, + }) async => + bridgeHelper.signMessage( + object: object, + cancel: cancel, + success: success, + ); + + void signTypedMessage({ + required Map object, + required VoidCallback cancel, + required Function(String hash) success, + }) async => + bridgeHelper.signTypedMessage( + object: object, + cancel: cancel, + success: success, + ); + + void setAddress(dynamic id) => bridgeHelper.setAddress( + id, + ); + + void switchDefaultNetwork(int id, Network toNetwork, String rawChainId) => + bridgeHelper.switchDefaultNetwork( + id, + toNetwork, + rawChainId, + ); + + void addAsset(int id, Map data, + {required VoidCallback cancel, + required Function(String status) success}) async => + bridgeHelper.addAsset( + id, + data, + cancel: cancel, + success: success, + ); + + void setChain(int? id) => bridgeHelper.setChain(id); + void closedApp() { navigator!.pop(); } @@ -690,9 +357,8 @@ class OpenDAppPresenter extends CompletePresenter { } } - void changeOnLoadStopCalled() { - state.isLoadStopCalled = !state.isLoadStopCalled; - } + void changeOnLoadStopCalled() => + state.isLoadStopCalled = !state.isLoadStopCalled; void injectAXSWalletJSChannel() => JSChannelUtils.injectAXSWalletJSChannel(state);