EthereumAddress.fromHex constructor

EthereumAddress.fromHex(
  1. String hex
)

Parses an Ethereum address from the hexadecimal representation. The representation must have a length of 20 bytes (or 40 hexadecimal chars), and can optionally be prefixed with "0x".

Implementation

factory EthereumAddress.fromHex(String hex) {
  if (!_basicAddress.hasMatch(hex)) {
    throw ArgumentError.value(
      hex,
      'address',
      'Must be a hex string with a length of 40, optionally prefixed with "0x"',
    );
  }

  final startsWith0x = hex.startsWith('0x') || hex.startsWith('0X');

  final hexNo0x = startsWith0x ? hex.substring(2) : hex;
  final hexWith0x = startsWith0x ? hex : '0x$hex';

  final hexEip55 = toChecksumAddress(hexNo0x);
  final bytes = convert.hex.decode(hexNo0x);

  return EthereumAddress._(
    value: Uint8List.fromList(bytes),
    without0x: hexNo0x.toLowerCase(),
    with0x: hexWith0x.toLowerCase(),
    eip55: hexEip55,
  );
}