decode static method

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

Decode the provided Base58 encoded data into a List

Parameters:

  • data: The Base58 encoded string to be decoded.
  • base58alphabets: Optional Base58Alphabets enum to choose the alphabet (default is Base58Alphabets.bitcoin).

Returns: A List

Implementation

static List<int> decode(String data,
    [Base58Alphabets base58alphabets = Base58Alphabets.bitcoin]) {
  final alphabet = Base58Const.alphabets[base58alphabets]!;
  var val = BigInt.zero;

  for (int i = 0; i < data.length; i++) {
    final c = data[data.length - 1 - i];
    final charIndex = alphabet.indexOf(c);
    if (charIndex == -1) {
      throw const MessageException("Invalid character in Base58 string");
    }
    val += BigInt.from(charIndex) * BigInt.from(Base58Const.radix).pow(i);
  }

  final bytes =
      BigintUtils.toBytes(val, length: BigintUtils.bitlengthInBytes(val));

  // Remove leading zeros from bytes
  var padLen = 0;
  for (var i = 0; i < data.length; i++) {
    if (data[i] == alphabet[0]) {
      padLen++;
    } else {
      break;
    }
  }

  return List<int>.from([...List<int>.filled(padLen, 0), ...bytes]);
}