decode static method

BuBytes decode(
  1. String data
)

Decodes a Monero-style Base58 string back to raw bytes.

Throws Base58FormatError for characters outside the Bitcoin alphabet or for a total length whose final-block residue isn't a valid Monero block width (valid residues: 0, 2, 3, 5, 6, 7, 9, 10 - so 1, 4 and 8 can never occur in a well-formed string).

Implementation

static BuBytes decode(String data) {
  const blockCharLen = Base58XmrConst.blockEncMaxByteLen;
  const blockByteLen = Base58XmrConst.blockDecMaxByteLen;

  final fullBlockCount = data.length ~/ blockCharLen;
  final finalBlockCharLen = data.length % blockCharLen;
  final finalBlockByteLen = Base58XmrConst.blockEncByteLens.indexOf(
    finalBlockCharLen,
  );
  if (finalBlockByteLen < 0) {
    throw BuError(
      DefaultErrorType.invalidLength,
      "Invalid Monero Base58 string.",
      details: {
        "reason": 'Invalid Monero Base58 length residue $finalBlockCharLen.',
      },
    );
  }

  final out = <int>[];
  for (var i = 0; i < fullBlockCount; i++) {
    final chunk = data.substring(i * blockCharLen, (i + 1) * blockCharLen);
    out.addAll(_stripPadding(Base58Decoder.decode(chunk), blockByteLen));
  }

  if (finalBlockCharLen > 0) {
    final chunk = data.substring(fullBlockCount * blockCharLen, data.length);
    out.addAll(_stripPadding(Base58Decoder.decode(chunk), finalBlockByteLen));
  }

  return BuBytes(out);
}