decode function

Uint8List decode(
  1. String string, {
  2. String alphabet = bitcoin,
})

Decode a string into Uint8List data using the specified alphabet.

Implementation

Uint8List decode(String string, {String alphabet = bitcoin}) {
  if (string.isEmpty) {
    throw ArgumentError('invalid base58 characters');
  }
  final length = alphabet.length;
  List<int> bytes = [0];
  for (var i = 0; i < string.length; i++) {
    var value = alphabet.indexOf(string[i]);
    if (value < 0) {
      throw ArgumentError('invalid base58 character');
    }
    var carry = value;
    for (var j = 0; j < bytes.length; ++j) {
      carry += bytes[j] * length;
      bytes[j] = carry & 0xff;
      carry >>= 8;
    }
    while (carry > 0) {
      bytes.add(carry & 0xff);
      carry >>= 8;
    }
  }

  /// Deal with leading zeros
  for (var k = 0; string[k] == alphabet[0] && k < string.length - 1; ++k) {
    bytes.add(0);
  }
  return Uint8List.fromList(bytes.reversed.toList());
}