encode function

Uint8List encode(
  1. List<int> encodedInput
)

Encodes the given bytes as a base58 string (no checksum is appended).

Implementation

Uint8List encode(List<int> encodedInput){
    var uintAlphabet = utf8.encode(ALPHABET);
    var ENCODED_ZERO = uintAlphabet[0];

//    var encodedInput = utf8.encode(input);

    if (encodedInput.isEmpty) {
        return <int>[] as Uint8List;
    }

    // Count leading zeros.
    int zeros = 0;
    while (zeros < encodedInput.length && encodedInput[zeros] == 0) {
        ++zeros;
    }

    // Convert base-256 digits to base-58 digits (plus conversion to ASCII characters)
    //input = Arrays.copyOf(input, input.length); // since we modify it in-place
    Uint8List encoded = Uint8List(encodedInput.length * 2); // upper bound <----- ???
    int outputStart = encoded.length;
    for (int inputStart = zeros; inputStart < encodedInput.length; ) {
        encoded[--outputStart] = uintAlphabet[divmod(encodedInput, inputStart, 256, 58)];
        if (encodedInput[inputStart] == 0) {
            ++inputStart; // optimization - skip leading zeros
        }
    }
    // Preserve exactly as many leading encoded zeros in output as there were leading zeros in input.
    while (outputStart < encoded.length && encoded[outputStart] == ENCODED_ZERO) {
        ++outputStart;
    }
    while (--zeros >= 0) {
        encoded[--outputStart] = ENCODED_ZERO;
    }
    // Return encoded string (including encoded leading zeros).
    return encoded.sublist(outputStart, encoded.length );
}