encode static method

String encode(
  1. List<int> dataBytes, [
  2. Base58Alphabets base58alphabets = Base58Alphabets.bitcoin
])

Encodes the provided List

Parameters:

  • dataBytes: The List
  • base58alphabets: Optional Base58Alphabets enum to choose the alphabet (default is Base58Alphabets.bitcoin).

Returns: A Base58 encoded string of the input dataBytes.

Implementation

static String encode(List<int> dataBytes,
    [Base58Alphabets base58alphabets = Base58Alphabets.bitcoin]) {
  final alphabet = Base58Const.alphabets[base58alphabets]!;

  /// Convert the dataBytes into a BigInteger for encoding.
  BigInt val = BigintUtils.fromBytes(dataBytes);
  String enc = "";
  while (val > BigInt.zero) {
    /// Perform division by Base58 radix and get remainder for encoding.
    final result = BigintUtils.divmod(val, Base58Const.radix);
    val = result.item1;
    final mod = result.item2;
    enc = alphabet[mod.toInt()] + enc;
  }

  /// Count leading zero bytes in the dataBytes for leading zero characters in the encoded string.
  int zero = 0;
  for (int byte in dataBytes) {
    if (byte == 0) {
      zero++;
    } else {
      break;
    }
  }
  final int leadingZeros = dataBytes.length - (dataBytes.length - zero);

  /// Append leading zero characters to the encoded string.
  return (alphabet[0] * leadingZeros) + enc;
}