handleRequest method

Future handleRequest(
  1. String method,
  2. List? params
)

Implementation

Future<dynamic> handleRequest(String method, List<dynamic>? params) async {
  if (_context == null) {
    throw WalletException('Provider context not set');
  }
  if (_web3client == null ||
      _credentials == null ||
      _txHandler == null ||
      _signingHandler == null) {
    throw WalletException(
        'Provider not initialized. Call initialize() first.');
  }
  try {
    switch (JsonRpcMethod.fromString(method)) {
      case JsonRpcMethod.ETH_REQUEST_ACCOUNTS:
        return await _handleConnect();
      case JsonRpcMethod.ETH_ACCOUNTS:
        return _state.isConnected ? _getConnectedAccounts() : [];
      case JsonRpcMethod.ETH_BLOCK_NUMBER:
        return await _handleBlockNumber();
      case JsonRpcMethod.ETH_CHAIN_ID:
        return _state.chainId;
      case JsonRpcMethod.NET_VERSION:
        return _state.chainId;
      case JsonRpcMethod.ETH_CALL:
        return await _txHandler!.handleTransaction(params?.first);
      case JsonRpcMethod.ETH_SEND_TRANSACTION:
        if (params == null || params.isEmpty) {
          throw WalletException('Missing call parameters');
        }
        return await _handleSignTransaction(params.first);
      case JsonRpcMethod.ETH_GET_BALANCE:
        final balance = await _handleGetBalance(params);
        return balance;
      case JsonRpcMethod.ETH_GET_BLOCK_BY_NUMBER:
        return await _handleGetBlockByNumber(params);
      case JsonRpcMethod.ETH_GET_BLOCK_BY_HASH:
        return await _handleGetBlockByHash(params);
      case JsonRpcMethod.ETH_GET_TRANSACTION_BY_HASH:
        return await _handleGetTransactionByHash(params);
      case JsonRpcMethod.ETH_GET_TRANSACTION_RECEIPT:
        return await _handleGetTransactionReceipt(params);
      case JsonRpcMethod.ETH_GET_CODE:
        return await _handleGetCode(params);
      case JsonRpcMethod.ETH_GET_STORAGE_AT:
        return await _handleGetStorageAt(params);
      case JsonRpcMethod.ETH_GET_TRANSACTION_COUNT:
        return await _handleGetTransactionCount(params);
      case JsonRpcMethod.ETH_GAS_PRICE:
        final gasPrice = await _web3client!.getGasPrice();
        return HexUtils.numberToHex(gasPrice.getInWei);
      case JsonRpcMethod.ETH_ESTIMATE_GAS:
        if (params == null || params.isEmpty) {
          throw WalletException('Missing transaction parameters');
        }
        final gas = await _txHandler!.estimateGas(params[0]);
        return HexUtils.numberToHex(gas);
      case JsonRpcMethod.PERSONAL_SIGN:
      case JsonRpcMethod.ETH_SIGN:
      case JsonRpcMethod.ETH_SIGN_TYPED_DATA:
      case JsonRpcMethod.ETH_SIGN_TYPED_DATA_V1:
      case JsonRpcMethod.ETH_SIGN_TYPED_DATA_V3:
      case JsonRpcMethod.ETH_SIGN_TYPED_DATA_V4:
        if (params == null || params.isEmpty) {
          throw WalletException('Missing sign parameters');
        }
        return await _handleSignMessage(method, params);
      // case JsonRpcMethod.PERSONAL_EC_RECOVER:
      //   return await _handlePersonalEcRecover(params);
      case JsonRpcMethod.WALLET_SWITCH_ETHEREUM_CHAIN:
        if (params?.isNotEmpty == true) {
          final newChainId = params?.first['chainId'];
          return await _handleSwitchNetwork(newChainId);
        }
        throw WalletException('Invalid chain ID');
      case JsonRpcMethod.WALLET_ADD_ETHEREUM_CHAIN:
        if (params?.isNotEmpty == true) {
          return await _handleAddEthereumChain(params?.first);
        }
        throw WalletException('Invalid network parameters');
      case JsonRpcMethod.WALLET_GET_PERMISSIONS:
        return ['eth_accounts', 'eth_chainId', 'personal_sign'];
      case JsonRpcMethod.WALLET_REVOKE_PERMISSIONS:
        return true;
      default:
        debugPrint('=======================> Method $method not supported');
        return null;
      // throw WalletException('Method $method not supported');
    }
  } catch (e) {
    // ✅ PRESERVE ORIGINAL EXCEPTION MESSAGE
    if (e is WalletException) {
      rethrow; // Re-throw WalletException as-is
    } else {
      throw WalletException(e.toString());
    }
  }
}