decodeGeneric static method
Decodes a generic Bech32 address. Returns (hrp, data).
Implementation
static ({String hrp, Uint8List data}) decodeGeneric(String address) {
final lower = address.toLowerCase();
final upper = address.toUpperCase();
if (address != lower && address != upper) {
throw FormatException('Mixed case in Bech32 address');
}
final pos = lower.lastIndexOf('1');
if (pos < 1 || pos + 7 > lower.length || lower.length > 90) {
throw FormatException('Invalid Bech32 format');
}
final hrp = lower.substring(0, pos);
final dataStr = lower.substring(pos + 1);
final data = <int>[];
for (final c in dataStr.split('')) {
final index = _charset.indexOf(c);
if (index < 0) {
throw FormatException('Invalid Bech32 character: $c');
}
data.add(index);
}
// Use constant 1 (Bech32)
if (!_verifyChecksum(hrp, data, false)) {
throw FormatException('Invalid Bech32 checksum');
}
final values = data.sublist(0, data.length - 6);
if (values.isEmpty) {
// Empty data allowed? Maybe. But usually not.
}
final program = _convertBits(values, 5, 8, false);
return (hrp: hrp, data: Uint8List.fromList(program));
}