encode static method

String encode(
  1. List<int> dataBytes,
  2. int ss58Format
)

Encodes SS58 address from the provided data bytes and SS58 format.

Parameters:

  • dataBytes: The data bytes to be encoded into an SS58 address. It should have a length of Ss58Const.dataByteLen.
  • ss58Format: The SS58 format specifying the address type.

Returns: A Base58-encoded SS58 address string.

Throws an ArgumentException if the input parameters are invalid, such as incorrect data length, out-of-range SS58 format, or using reserved formats.

Implementation

static String encode(List<int> dataBytes, int ss58Format) {
  // Check parameters
  // if (dataBytes.length != _Ss58Const.dataByteLen) {
  //   throw ArgumentException('Invalid data length (${dataBytes.length})');
  // }
  if (ss58Format < 0 || ss58Format > _Ss58Const.formatMaxVal) {
    throw ArgumentException('Invalid SS58 format ($ss58Format)');
  }
  if (_Ss58Const.reservedFormats.contains(ss58Format)) {
    throw ArgumentException('Invalid SS58 format ($ss58Format)');
  }

  List<int> ss58FormatBytes;

  if (ss58Format <= _Ss58Const.simpleAccountFormatMaxVal) {
    ss58FormatBytes = IntUtils.toBytes(ss58Format,
        length: IntUtils.bitlengthInBytes(ss58Format),
        byteOrder: Endian.little);
  } else {
    ss58FormatBytes = List<int>.from([
      ((ss58Format & 0x00FC) >> 2) | 0x0040,
      (ss58Format >> 8) | ((ss58Format & 0x0003) << 6)
    ]);
  }

  final payload = List<int>.from([...ss58FormatBytes, ...dataBytes]);

  final checksum = _Ss58Utils.computeChecksum(payload);

  return Base58Encoder.encode(List<int>.from([...payload, ...checksum]));
}