convert method

  1. @override
Bech32 convert(
  1. String input
)
override

Converts input and returns the result of the conversion.

Implementation

@override
Bech32 convert(String input) {
  if (input.length > Bech32ValidationsMixin.maxInputLength) {
    throw TooLong(input.length);
  }
  if (isMixedCase(input)) {
    throw MixedCase(input);
  }
  if (hasInvalidSeparator(input)) {
    throw InvalidSeparator(input.lastIndexOf(_separator));
  }
  final separatorPosition = input.lastIndexOf(_separator);
  if (isChecksumTooShort(separatorPosition, input)) {
    throw const TooShortChecksum();
  }
  if (isHrpTooShort(separatorPosition)) {
    throw const TooShortHrp();
  }
  input = input.toLowerCase();
  final hrp = input.substring(0, separatorPosition);
  final data = input.substring(
    separatorPosition + 1,
    input.length - Bech32ValidationsMixin.checksumLength,
  );
  final checksum = input.substring(
    input.length - Bech32ValidationsMixin.checksumLength,
  );
  if (hasOutOfRangeHrpCharacters(hrp)) {
    throw OutOfRangeHrpCharacters(hrp);
  }
  final List<int> dataBytes = data.split('').map((c) {
    return charset.indexOf(c);
  }).toList();
  if (hasOutOfBoundsChars(dataBytes)) {
    throw OutOfBoundChars(data[dataBytes.indexOf(-1)]);
  }
  final List<int> checksumBytes = checksum.split('').map((c) {
    return charset.indexOf(c);
  }).toList();
  if (hasOutOfBoundsChars(checksumBytes)) {
    throw OutOfBoundChars(checksum[checksumBytes.indexOf(-1)]);
  }
  if (isInvalidChecksum(hrp, dataBytes, checksumBytes)) {
    throw const InvalidChecksum();
  }
  return Bech32(hrp, dataBytes);
}