RawEVMTransactionType1.fromUnsignedHex constructor

RawEVMTransactionType1.fromUnsignedHex(
  1. String rawTxHex
)

Implementation

factory RawEVMTransactionType1.fromUnsignedHex(String rawTxHex) {
  rawTxHex = rawTxHex.replaceFirst("0x", ""); // Remove the 0x prefix

  if (rawTxHex.startsWith("01") == false) {
    throw Exception("Invalid Type 1 Transaction");
  }

  rawTxHex = rawTxHex.substring(2); // Remove the type prefix

  final rlpDecoded = decodeRLP(rawTxHex.hexToBytes).$1;

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

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

  return RawEVMTransactionType1.unsigned(
    chainId: rlpDecoded[0].buffer.toUInt,
    nonce: rlpDecoded[1].buffer.toUBigInt,
    gasPrice: rlpDecoded[2].buffer.toUBigInt,
    gasLimit: rlpDecoded[3].buffer.toUBigInt,
    to: "0x" + rlpDecoded[4].buffer.toHex,
    value: rlpDecoded[5].buffer.toUBigInt,
    data: rlpDecoded[6].buffer,
    accessList: switch (rlpDecoded[7]) {
      RLPList list => list.value
          .whereType<RLPList>()
          .map((item) {
            final subList = item[1];
            if (subList is RLPList)
              return (
                address: "0x" + item[0].buffer.toHex,
                storageKeys:
                    subList.value.map((key) => key.buffer.toHex).toList(),
              );
            return null;
          })
          .nonNulls
          .toList(),
      _ => [],
    },
  );
}