reEncodedFromForm function

Uint8List reEncodedFromForm(
  1. Uint8List p,
  2. bool compressed
)

This function re-encodes a given Uint8List point representation based on a compression flag. If 'compressed' is true, it ensures that the point representation is compressed. If 'compressed' is false, it ensures that the point representation is uncompressed.

Implementation

Uint8List reEncodedFromForm(Uint8List p, bool compressed) {
  /// Decode the input Uint8List 'p' to obtain a point representation.
  final decode = _decodeFrom(p);

  /// Check if decoding was successful. If not, raise an error.
  if (decode == null) {
    throw ArgumentError("Bad point");
  }

  /// Get the encoded representation of the point with the desired compression status.
  final encode = decode.getEncoded(compressed);

  /// Check if the resulting 'encode' is not compressed.
  if (!_isPointCompressed(encode)) {
    /// If it's not compressed, remove the compression flag (first byte).
    return encode.sublist(1, encode.length);
  }

  /// If it is compressed or remains compressed, return it as is.
  return encode;
}