decode static method

(int, List<int>) decode(
  1. String ss58Str
)

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

Parameters:

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

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

Implementation

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

  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.invalidOperationArguments(
      "decode",
      name: "ss58Str",
      reason: "Invalid SS58 format.",
    );
  }
  final int checkSumLength = _Ss58Const.checkBytesLen(
    decBytes.length - ss58FormatLen,
  );
  final dataBytes = 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.invalidChecksum;
  }

  return (ss58Format, dataBytes);
}