RawEVMTransactionType2.fromUnsignedHex constructor

RawEVMTransactionType2.fromUnsignedHex(
  1. String rawTxHex
)

Implementation

factory RawEVMTransactionType2.fromUnsignedHex(String rawTxHex) {
  rawTxHex = rawTxHex.replaceFirst("0x", ""); // Remove the 0x prefix
  assert(rawTxHex.startsWith("02"), "Invalid Type 1 Transaction");
  rawTxHex = rawTxHex.substring(2); // Remove the type prefix

  final rlpDecoded = decodeRLP(rawTxHex.hexToBytes, 0).result;

  if (rlpDecoded is! List) {
    throw Exception("Error RLP decoding transaction: $rlpDecoded");
  }

  if (rlpDecoded.length < 9) {
    throw Exception("Invalid transaction, missing fields: $rlpDecoded");
  }

  final chainId = parseAsHexInt(rlpDecoded[0]);
  final nonce = parseAsHexBigInt(rlpDecoded[1]);
  final maxPriorityFeePerGas = parseAsHexBigInt(rlpDecoded[2]);
  final maxFeePerGas = parseAsHexBigInt(rlpDecoded[3]);
  final gasLimit = parseAsHexBigInt(rlpDecoded[4]);
  final to = "0x" + rlpDecoded[5];
  final value = parseAsHexBigInt(rlpDecoded[6]);
  final data = (rlpDecoded[7] as String).hexToBytes;
  final accessList = [
    for (final item in rlpDecoded[8])
      (
        address: "0x" + item[0],
        storageKeys: (item[1] as List<dynamic>).whereType<String>().toList(),
      )
  ];

  return RawEVMTransactionType2.unsigned(
    nonce: nonce,
    gasLimit: gasLimit,
    to: to,
    value: value,
    data: data,
    chainId: chainId,
    maxFeePerGas: maxFeePerGas,
    maxPriorityFeePerGas: maxPriorityFeePerGas,
    accessList: accessList,
  );
}