dapp_injected_eip155

Support DApp Injected on Ethereum network - EIP155

Features

Support Ethereum Network

Methods Support
signTransaction :heavy_check_mark:
signMessage :heavy_check_mark:
signTypedMessage :heavy_check_mark:
signPersonalMessage :heavy_check_mark:
sendTransaction :heavy_check_mark:
ecRecover
requestAccounts :heavy_check_mark:
watchAsset
addEthereumChain :heavy_check_mark:
switchEthereumChain :heavy_check_mark:

Support

There are two method I haven't been able to test that work

  • watchAsset
  • ecRecover.

Please provide me with information and step by step to test watchAsset & ecRecover

My Address

0xDBEC6913a697B61F218B4a5D33B7561800Fe04E9

Installation

First, add dapp_injected_eip155 as a dependency in your pubspec.yaml file.

dependencies
  dapp_injected_eip155: ^1.0.4

Requirement

You need to run this function the first time to make sure the library works.

await ScriptUtils.initProviderScript();

Methods

wallet_addEthereumChain

Creates a confirmation asking the user to add the specified chain to the wallet application. The caller must specify a chain ID and some chain metadata. The wallet application may refuse or accept the request. null is returned if the chain is added, and an error otherwise. Introduced by EIP-3085.

wallet_switchEthereumChain

Requests that the wallet switches its active Ethereum chain. Introduced by EIP-3326.

Future<NetworkSupport> _switchChain(int chainId) async {
  /// 1. check case: Unknown chain id: \(chainId)
  /// 
  int index = networkLi.indexWhere((e) => e.chainId == chainId);
  if (index == -1) {
    throw const NotFoundChainException();
  }
  /// 2. Show dialog confirm switch chain
  /// User reject
  if (userReject) {
    throw const UserRejectException();
    // OR Using RpcException with errorCode, message
    // throw const RpcException(4001, 'User rejected the request.');
  }
  return networkLi[index].rpcUrl;
}

personal_sign

Presents a plain text signature challenge to the user and returns the signed response. Equivalent to eth_sign on some other wallets, and prepends a safe prefix to the signed message to prevent the challenge tricking users into signing a financial transaction. This method requires that the user has granted permission to interact with their account first, so make sure to call eth_requestAccounts (recommended) or wallet_requestPermissions first.

eth_signTypedData_v4

Presents a data message for the user to sign in a structured and readable format and returns the signed response. Introduced by EIP-712. This method requires that the user has granted permission to interact with their account first, so make sure to call eth_requestAccounts (recommended) or wallet_requestPermissions first.

eth_sendTransaction

Creates a new wallet confirmation to make an Ethereum transaction from the user's account. This method requires that the user has granted permission to interact with their account first, so make sure to call eth_requestAccounts (recommended) or wallet_requestPermissions first.

Future<String> _onSignTransaction(int? chainId, String? rpcUrl, Map<String, dynamic> data) async {
  if (chainId == null || rpcUrl == null) {
    throw const NotFoundChainException();
  }
  /// 1. Unauthorized
  /// The requested account and/or method has not been authorized by the user.
  /// throw const UnauthorizedException();
  /// 
  ///  2. User Reject
  /// throw const RpcException(4001, 'User rejected the request.');
  /// 
  JsTransaction transaction = JsTransaction.fromJson(data);
  try {
    final tx = Transaction(
      from: EthereumAddress.fromHex(transaction.from),
      data: hexToBytes(transaction.data),
      value: EtherAmount.fromBase10String(EtherUnit.wei, transaction.value ?? '0'),
      to: EthereumAddress.fromHex(transaction.to),
      // maxGas: 61931, /// mock or transaction.gas
    );
    final client = Web3Client(rpcUrl, http.Client());
    final hash = await client.sendTransaction(
      EthPrivateKey.fromHex(_privateKey),
      tx,
      chainId: chainId,
    );
    return hash;
  } on RPCError catch(e) {
    throw RpcException(e.errorCode, e.message);
  } catch(e) {
    rethrow;
  }
}

Errors

To send an error message, you need to throw an exception. I have provided some specific exceptions such as:

  • throw UserRejectException();
  • throw NotFoundChainException();
  • throw InvalidInputException();
  • throw UnSupportMethodException();
  • throw UnauthorizedException();

Additionally, you can edit it to have an exception with specific errorCode and message:

throw RpcException(4001, 'User rejected the request.');

/// errorCode: 4001
/// message: User rejected the request.

Provider Errors

Code Message
4001 User rejected the request.
4100 The requested account and/or method has not been authorized by the user.
4200 The requested method is not supported by this Ethereum provider.
4900 The provider is disconnected from all chains.
4901 The provider is disconnected from the specified chain.

RPC Errors

Code Message
-32000 Invalid input.
-32001 Resource not found.
-32002 Resource unavailable.
-32003 Transaction rejected
-32004 Method not supported.
-32005 Request limit exceeded.
-32700 Invalid JSON was received by the server. An error occurred on the server while parsing the JSON text.
-32600 The JSON sent is not a valid Request object.
-32601 The method does not exist / is not available.
-32602 Invalid method parameter(s).
-32603 Internal JSON-RPC error.
4902 Unrecognized chain ID. Try adding the chain using wallet_addEthereumChain first.