minimallyEncode function

List<int> minimallyEncode(
  1. List<int> buf
)

Implementation

List<int> minimallyEncode(List<int> buf) {
  if (buf.isEmpty) {
    return buf;
  }

  // If the last byte is not 0x00 or 0x80, we are minimally encoded.
  var last = buf[buf.length - 1];
  if (last & 0x7f != 0) {
    return buf;
  }

  // If the script is one byte long, then we have a zero, which encodes as an
  // empty array.
  if (buf.length == 1) {
    return <int>[];
  }

  // If the next byte has it sign bit set, then we are minimaly encoded.
  if (buf[buf.length - 2] & 0x80 != 0) {
    return buf;
  }

  // We are not minimally encoded, we need to figure out how much to trim.
  for (var i = buf.length - 1; i > 0; i--) {
    // We found a non zero byte, time to encode.
    if (buf[i - 1] != 0) {
      if (buf[i - 1] & 0x80 != 0) {
        // We found a byte with it sign bit set so we need one more
        // byte.
        buf[i++] = last;
      } else {
        // the sign bit is clear, we can use it.
        buf[i - 1] |= last;
      }

      return buf.sublist(0, i);
    }
  }

  // If we found the whole thing is zeros, then we have a zero.
  return <int>[];
}