toText method

String toText()

Implementation

String toText() {
  final checksum = _getChecksum(_principal.buffer);
  final bytes = Uint8List.fromList(_principal);
  final array = Uint8List.fromList([...checksum, ...bytes]);
  final result = base32Encode(array);
  final reg = RegExp(r'.{1,5}');
  final matches = reg.allMatches(result);
  if (matches.isEmpty) {
    // This should only happen if there's no character, which is unreachable.
    throw StateError('No characters found.');
  }
  final buffer = StringBuffer(matches.map((e) => e.group(0)).join('-'));
  if (_subAccount case final subAccount?
      when !subAccount.eq(_emptySubAccount)) {
    final subAccountHex = subAccount.toHex();
    int nonZeroStart = 0;
    while (nonZeroStart < subAccountHex.length) {
      if (subAccountHex[nonZeroStart] != '0') {
        break;
      }
      nonZeroStart++;
    }
    if (nonZeroStart != subAccountHex.length) {
      final checksum = base32Encode(
        _getChecksum(Uint8List.fromList(_principal + subAccount).buffer),
      );
      buffer.write('-$checksum');
      buffer.write('.');
      buffer.write(subAccountHex.replaceRange(0, nonZeroStart, ''));
    }
  }
  return buffer.toString();
}