decode static method

Tuple<int, List<int>> decode(
  1. String dataStr
)

Decodes the provided Base58-encoded SS58 address string into its SS58 format and data bytes.

Parameters:

  • dataStr: The Base58-encoded SS58 address to be decoded.

Returns: A tuple containing the SS58 format (address type) and the data bytes of the SS58 address.

Throws an ArgumentException or SS58ChecksumError if the input string is invalid, contains an invalid format, or fails the checksum verification.

Implementation

static Tuple<int, List<int>> decode(String dataStr) {
  final decBytes = Base58Decoder.decode(dataStr);

  int ss58Format;
  int ss58FormatLen;

  if ((decBytes[0] & 0x40) != 0) {
    ss58FormatLen = 2;
    ss58Format = ((decBytes[0] & 0x3F) << 2) |
        (decBytes[1] >> 6) |
        ((decBytes[1] & 0x3F) << 8);
  } else {
    ss58FormatLen = 1;
    ss58Format = decBytes[0];
  }

  if (_Ss58Const.reservedFormats.contains(ss58Format)) {
    throw ArgumentException('Invalid SS58 format ($ss58Format)');
  }
  final int checkSumLength =
      _Ss58Const.checkBytesLen(decBytes.length - ss58FormatLen);
  final dataBytes = List<int>.from(
      decBytes.sublist(ss58FormatLen, decBytes.length - checkSumLength));
  final checksumBytes = List<int>.unmodifiable(
      decBytes.sublist(decBytes.length - checkSumLength));

  final checksumBytesGot = _Ss58Utils.computeChecksum(
      decBytes.sublist(0, decBytes.length - checkSumLength));

  if (!BytesUtils.bytesEqual(checksumBytesGot, checksumBytes)) {
    throw SS58ChecksumError(
        'Invalid checksum (expected ${BytesUtils.toHexString(checksumBytesGot)}, '
        'got ${BytesUtils.toHexString(checksumBytes)})');
  }

  return Tuple(ss58Format, dataBytes);
}