bnToU8a function

Uint8List bnToU8a(
  1. BigInt? value, {
  2. int bitLength = -1,
  3. Endian endian = Endian.little,
  4. bool isNegative = false,
})

Implementation

Uint8List bnToU8a(BigInt? value,
    {int bitLength = -1,
    Endian endian = Endian.little,
    bool isNegative = false}) {
  BigInt valueBn = bnToBn(value);
  int byteLength;
  if (bitLength == -1) {
    byteLength = (valueBn.bitLength / 8).ceil();
  } else {
    byteLength = ((bitLength) / 8).ceil();
  }

  if (value == null) {
    if (bitLength == -1) {
      return Uint8List.fromList([0]);
    } else {
      return Uint8List(byteLength);
    }
  }

  // print((0x80 & valueBn.toInt()) > 0);

  var newU8a = encodeBigInt(
      isNegative
          ? (0x80 & valueBn.toInt()) > 0
              ? bitnot(valueBn, bitLength: byteLength * 8)
              : valueBn
          : valueBn,
      endian: endian,
      bitLength: byteLength * 8);

  var ret = Uint8List(byteLength);

  ret.setAll(0, newU8a);
  return ret;
}