encode static method

String encode(
  1. List<int> bytes
)

Encodes a byte-array to a base58 string

Implementation

static String encode(List<int> bytes) {
  if (bytes.isEmpty) {
    return '';
  }

  // copy bytes because we are going to change it
  bytes = Uint8List.fromList(bytes);

  // count number of leading zeros
  var leadingZeroes = 0;
  while (leadingZeroes < bytes.length && bytes[leadingZeroes] == 0) {
    leadingZeroes++;
  }

  var output = '';
  var startAt = leadingZeroes;
  while (startAt < bytes.length) {
    var mod = _divmod58(bytes, startAt);
    if (bytes[startAt] == 0) {
      startAt++;
    }
    output = ALPHABET[mod] + output;
  }

  if (output.isNotEmpty) {
    while (output[0] == ALPHABET[0]) {
      output = output.substring(1, output.length);
    }
  }
  while (leadingZeroes-- > 0) {
    output = ALPHABET[0] + output;
  }

  return output;
}