encode method

  1. @override
Uint8List encode(
  1. dynamic value
)
override

Encodes a value of this type.

Implementation

@override
Uint8List encode(dynamic value) {
  Uint8List bytes;
  if (value is Uint8List) {
    bytes = value;
  } else if (value is String) {
    final hex = value.startsWith('0x') || value.startsWith('0X')
        ? value.substring(2)
        : value;
    if (hex.length != length * 2) {
      throw ArgumentError(
          'Expected $length bytes ($name), got ${hex.length ~/ 2}');
    }
    bytes = Uint8List(length);
    for (var i = 0; i < length; i++) {
      bytes[i] = int.parse(hex.substring(i * 2, i * 2 + 2), radix: 16);
    }
  } else {
    throw ArgumentError('Unsupported type for $name: ${value.runtimeType}');
  }

  if (bytes.length != length) {
    throw ArgumentError('Expected $length bytes, got ${bytes.length}');
  }

  final result = Uint8List(32);
  result.setRange(0, length, bytes);
  return result;
}