decode static method

Uint8List decode(
  1. String input
)

Decodes a Base58 string to bytes.

Implementation

static Uint8List decode(String input) {
  if (input.isEmpty) return Uint8List(0);

  // Count leading '1's
  var leadingOnes = 0;
  for (final c in input.split('')) {
    if (c == '1') {
      leadingOnes++;
    } else {
      break;
    }
  }

  // Convert from base58
  var value = BigInt.zero;
  for (final c in input.split('')) {
    final index = _alphabet.indexOf(c);
    if (index < 0) {
      throw FormatException('Invalid Base58 character: $c');
    }
    value = value * BigInt.from(58) + BigInt.from(index);
  }

  // Convert to bytes
  final bytes = <int>[];
  while (value > BigInt.zero) {
    bytes.insert(0, (value & BigInt.from(0xff)).toInt());
    value >>= 8;
  }

  // Add leading zeros
  for (var i = 0; i < leadingOnes; i++) {
    bytes.insert(0, 0);
  }

  return Uint8List.fromList(bytes);
}