toBase58 method

String toBase58()

Implementation

String toBase58() {
  const String base58Alphabet = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz";
  final Uint8List input = utf8.encode(this);
  BigInt intData = BigInt.from(0);

  for (int byte in input) {
    intData = intData * BigInt.from(256) + BigInt.from(byte);
  }

  final StringBuffer result = StringBuffer();
  while (intData > BigInt.zero) {
    final BigInt remainder = intData % BigInt.from(58);
    intData = intData ~/ BigInt.from(58);
    result.write(base58Alphabet[remainder.toInt()]);
  }

  for (int byte in input) {
    if (byte == 0) {
      result.write(base58Alphabet[0]);
    } else {
      break;
    }
  }

  return result.toString().split("").reversed.join();
}