encodeVarint static method

List<int> encodeVarint(
  1. int value
)

Encodes an integer into a variable-length byte array according to Bitcoin's variable-length integer encoding scheme.

Implementation

static List<int> encodeVarint(int value) {
  if (value < 253) {
    return [value];
  } else if (value < 0x10000) {
    final bytes = List<int>.filled(3, 0);
    bytes[0] = 0xfd;
    BinaryOps.writeUint16LE(value, bytes, 1);
    return bytes;
  } else if (value < 0x100000000) {
    final bytes = List<int>.filled(5, 0);
    bytes[0] = 0xfe;
    BinaryOps.writeUint32LE(value, bytes, 1);
    return bytes;
  } else {
    throw ArgumentException.invalidOperationArguments(
      "encodeVarint",
      name: "byteint",
      reason: "Value is to large.",
    );
  }
}