convert method

  1. @override
Bech32 convert(
  1. String input, {
  2. int maxLength = Bech32Validations.maxInputLength,
  3. String encoding = 'bech32',
})
override

Converts input and returns the result of the conversion.

Implementation

@override
Bech32 convert(String input, {int maxLength = Bech32Validations.maxInputLength, String encoding = 'bech32'}) {
  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) => charset.indexOf(c)).toList();

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

  var checksumBytes = checksum.split('').map((c) => charset.indexOf(c)).toList();

  if (hasOutOfBoundsChars(checksumBytes)) throw OutOfBoundChars(checksum[checksumBytes.indexOf(-1)]);
  if (isInvalidChecksum(hrp, dataBytes, checksumBytes, encoding: encoding)) throw InvalidChecksum();

  return Bech32(hrp, dataBytes);
}