decode static method

(int, BuBytes) decode(
  1. String ss58Str
)

Decodes ss58Str into (ss58Format, dataBytes).

Throws SS58FormatError if the decoded format is reserved, the input is empty, or a two-byte format marker is cut off - or SS58ChecksumError if the checksum doesn't match (the address was mistyped, truncated, or corrupted) - or genuinely can't be resolved, see the comment on the checksum-length loop below. Base58FormatError propagates from the inner Base58 decode when ss58Str contains a character outside the Bitcoin alphabet.

Implementation

static (int, BuBytes) decode(String ss58Str) {
  final decBytes = Base58Decoder.decode(ss58Str);

  if (decBytes.isEmpty) {
    throw BuError(
      DefaultErrorType.badSs58,
      'Invalid SS58 address.',
      details: {'reason': 'decoded address is empty'},
    );
  }
  int ss58Format;
  int ss58FormatLen;
  if ((decBytes[0] & 0x40) != 0) {
    if (decBytes.length < 2) {
      throw BuError(
        DefaultErrorType.badSs58,
        'Invalid SS58 address.',
        details: {'reason': 'truncated two-byte format'},
      );
    }
    // Top two bits `01` set (see SS58Encoder.encode) - this is the
    // two-byte full-format encoding; undo the same bit spread.
    ss58FormatLen = 2;
    ss58Format =
        ((decBytes[0] & 0x3F) << 2) |
        (decBytes[1] >> 6) |
        ((decBytes[1] & 0x3F) << 8);
  } else {
    // Single format byte, value 0..63.
    ss58FormatLen = 1;
    ss58Format = decBytes[0];
  }

  if (_Ss58Const.reservedFormats.contains(ss58Format)) {
    throw BuError(
      DefaultErrorType.badSs58,
      'Invalid SS58 format.',
      details: {'reason': 'decoded ss58Format $ss58Format is reserved'},
    );
  }

  // The checksum is 1 or 2 bytes, per _Ss58Const.checkBytesLen - but
  // that decision is keyed on the *payload* length (format + data,
  // excluding the checksum itself), which isn't directly recoverable
  // from decBytes.length alone: the same total length can arise from
  // two different (payload, checksum) splits (e.g. a 34-byte payload
  // with a 2-byte checksum, and a 35-byte payload with a 1-byte
  // checksum, both total 36 bytes). Rather than guess via a formula
  // that gets this wrong for some lengths, try both candidate checksum
  // lengths and accept whichever one's checksum actually verifies - a
  // checksum collision on the wrong split is cryptographically
  // negligible, so this is unambiguous in practice. The common case (1
  // byte) is tried first.
  for (final checkSumLength in const [1, 2]) {
    if (decBytes.length <= ss58FormatLen + checkSumLength) continue;

    final splitAt = decBytes.length - checkSumLength;
    final payload = decBytes.sublist(0, splitAt);
    final checksumBytes = decBytes.sublist(splitAt);
    final expectedChecksum = _Ss58Utils.computeChecksum(payload);

    // Self-consistency: this split is only a real candidate if
    // checkBytesLen() would independently choose this same checksum
    // length for a payload of this length - otherwise the byte
    // comparison below is meaningless (comparing against a checksum of
    // the wrong length can never match).
    if (expectedChecksum.length != checkSumLength) continue;

    if (BytesUtils.bytesEqual(expectedChecksum, checksumBytes)) {
      return (ss58Format, decBytes.sublist(ss58FormatLen, splitAt));
    }
  }

  throw CoreErrors.ss58InvalidChecksum;
}