convert method

  1. @override
Bech32 convert(
  1. String input, [
  2. int maxLength = Bech32Validations.maxInputLength
])
override

Converts input and returns the result of the conversion.

Implementation

@override
Bech32 convert(String input,
    [int maxLength = Bech32Validations.maxInputLength]) {
  if (input.length > maxLength) {
    throw TooLong(input.length);
  }

  if (isMixedCase(input)) {
    throw MixedCase(input);
  }

  if (hasInvalidSeparator(input)) {
    throw InvalidSeparator(input.lastIndexOf(separator));
  }

  var separatorPosition = input.lastIndexOf(separator);

  if (isChecksumTooShort(separatorPosition, input)) {
    throw TooShortChecksum();
  }

  if (isHrpTooShort(separatorPosition)) {
    throw TooShortHrp();
  }

  input = input.toLowerCase();

  var hrp = input.substring(0, separatorPosition);
  var data = input.substring(
      separatorPosition + 1, input.length - Bech32Validations.checksumLength);
  var checksum =
      input.substring(input.length - Bech32Validations.checksumLength);

  if (hasOutOfRangeHrpCharacters(hrp)) {
    throw OutOfRangeHrpCharacters(hrp);
  }

  var dataBytes = data.split('').map((c) {
    return charset.indexOf(c);
  }).toList();

  if (hasOutOfBoundsChars(dataBytes)) {
    throw OutOfBoundChars(data[dataBytes.indexOf(-1)]);
  }

  var 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 InvalidChecksum();
  }

  return Bech32(hrp, dataBytes);
}